4
4
package agent_inject
5
5
6
6
import (
7
+ "errors"
7
8
"testing"
8
9
9
10
"github.com/prometheus/client_golang/prometheus"
@@ -12,7 +13,8 @@ import (
12
13
)
13
14
14
15
func Test_incrementInjections (t * testing.T ) {
15
- MustRegisterInjectorMetrics (prometheus .DefaultRegisterer )
16
+ reg := prometheus .NewRegistry ()
17
+ MustRegisterInjectorMetrics (reg )
16
18
17
19
tests := map [string ]struct {
18
20
namespace string
@@ -65,3 +67,33 @@ func Test_incrementInjections(t *testing.T) {
65
67
})
66
68
}
67
69
}
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