Skip to content

Commit

Permalink
Add metrics unit test (#709)
Browse files Browse the repository at this point in the history
* Add metrics unit test

* go mod tidy
  • Loading branch information
tvoran authored Dec 2, 2024
1 parent 5f212a6 commit e7653f2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions agent-inject/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package agent_inject

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
)

func Test_incrementInjections(t *testing.T) {
MustRegisterInjectorMetrics(prometheus.DefaultRegisterer)

tests := map[string]struct {
namespace string
mutateResponse MutateResponse
expectedLabels map[string]string
}{
"init_only": {
namespace: "init",
mutateResponse: MutateResponse{
InjectedInit: true,
InjectedSidecar: false,
},
expectedLabels: map[string]string{
metricsLabelNamespace: "init",
metricsLabelType: metricsLabelTypeInitOnly,
},
},
"sidecar_only": {
namespace: "sidecar",
mutateResponse: MutateResponse{
InjectedInit: false,
InjectedSidecar: true,
},
expectedLabels: map[string]string{
metricsLabelNamespace: "sidecar",
metricsLabelType: metricsLabelTypeSidecarOnly,
},
},
"init_and_sidecar": {
namespace: "both",
mutateResponse: MutateResponse{
InjectedInit: true,
InjectedSidecar: true,
},
expectedLabels: map[string]string{
metricsLabelNamespace: "both",
metricsLabelType: metricsLabelTypeBoth,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Cleanup(func() {
injectionsByNamespace.Reset()
})
incrementInjections(test.namespace, test.mutateResponse)
assert.Equal(t, 1, testutil.CollectAndCount(injectionsByNamespace))
check := injectionsByNamespace.With(prometheus.Labels(test.expectedLabels))
assert.Equal(t, float64(1), testutil.ToFloat64(check))
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down

0 comments on commit e7653f2

Please sign in to comment.