diff --git a/go.mod b/go.mod index 0201b99b3f..9b4d4f648f 100644 --- a/go.mod +++ b/go.mod @@ -104,6 +104,7 @@ require ( github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.9 // indirect + github.com/kylelemons/godebug v1.1.0 // indirect github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect github.com/lib/pq v1.10.9 // indirect diff --git a/pkg/ipamd/rpc_handler_test.go b/pkg/ipamd/rpc_handler_test.go index 4ddb74c93f..53389ba9ca 100644 --- a/pkg/ipamd/rpc_handler_test.go +++ b/pkg/ipamd/rpc_handler_test.go @@ -22,6 +22,9 @@ import ( pb "github.com/aws/amazon-vpc-cni-k8s/rpc" + "github.com/aws/amazon-vpc-cni-k8s/utils/prometheusmetrics" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/assert" ) @@ -238,6 +241,12 @@ func TestServer_AddNetwork(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + // Reset the counter for each test case + prometheusmetrics.AddIPCnt = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "awscni_add_ip_req_count", + Help: "Number of add IP address requests", + }) + m := setup(t) defer m.ctrl.Finish() @@ -302,6 +311,10 @@ func TestServer_AddNetwork(t *testing.T) { assert.NoError(t, err) assert.Equal(t, tt.want, resp) } + + // Add more detailed assertion messages + assert.Equal(t, float64(1), testutil.ToFloat64(prometheusmetrics.AddIPCnt), + "AddIPCnt should be incremented exactly once for test case: %s", tt.name) }) } } diff --git a/utils/prometheusmetrics/prometheusmetrics.go b/utils/prometheusmetrics/prometheusmetrics.go index fadda0a094..1fc019ed51 100644 --- a/utils/prometheusmetrics/prometheusmetrics.go +++ b/utils/prometheusmetrics/prometheusmetrics.go @@ -61,8 +61,8 @@ var ( }, []string{"fn"}, ) - AddIPCnt = prometheus.NewGauge( - prometheus.GaugeOpts{ + AddIPCnt = prometheus.NewCounter( + prometheus.CounterOpts{ Name: "awscni_add_ip_req_count", Help: "The number of add IP address requests", },