44package agent_inject
55
66import (
7+ "errors"
78 "testing"
89
910 "github.com/prometheus/client_golang/prometheus"
@@ -12,7 +13,8 @@ import (
1213)
1314
1415func 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