Skip to content

Commit 015b002

Browse files
committed
test(metrics): unit test for incrementRequests
1 parent 87be1b4 commit 015b002

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

agent-inject/metrics_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package agent_inject
55

66
import (
7+
"errors"
78
"testing"
89

910
"github.com/prometheus/client_golang/prometheus"
@@ -12,7 +13,8 @@ import (
1213
)
1314

1415
func Test_incrementInjections(t *testing.T) {
15-
MustRegisterInjectorMetrics(prometheus.DefaultRegisterer)
16+
reg := prometheus.NewRegistry()
17+
MustRegisterInjectorMetrics(reg)
1618

1719
tests := map[string]struct {
1820
namespace string
@@ -65,3 +67,33 @@ func Test_incrementInjections(t *testing.T) {
6567
})
6668
}
6769
}
70+
71+
func Test_incrementRequests(t *testing.T) {
72+
one := 1.0
73+
reg := prometheus.NewRegistry()
74+
MustRegisterInjectorMetrics(reg)
75+
76+
tests := map[string]struct {
77+
err error
78+
}{
79+
"valid_request": {err: nil},
80+
"invalid_content_type": {err: errors.New("Invalid content-type: ")},
81+
"error_reading_body": {err: errors.New("error reading request body: ")},
82+
}
83+
for name, test := range tests {
84+
t.Run(name, func(t *testing.T) {
85+
// Unlike CounterVec, Counter does not have a Reset() method. As a workaround, we can
86+
// collect the before and after counts and assert that the difference is 0 or 1, as
87+
// applicable.
88+
reqsExpected := testutil.ToFloat64(requestsReceived) + one
89+
errsExpected := testutil.ToFloat64(requestsErrored)
90+
if test.err != nil {
91+
errsExpected += one
92+
}
93+
94+
incrementRequests(test.err)
95+
assert.Equal(t, reqsExpected, testutil.ToFloat64(requestsReceived))
96+
assert.Equal(t, errsExpected, testutil.ToFloat64(requestsErrored))
97+
})
98+
}
99+
}

0 commit comments

Comments
 (0)