Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage/etcd3/metrics: add unit test for apiserver_storage_objects #120827

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,48 @@ func TestStorageSizeCollector(t *testing.T) {

}

func TestUpdateObjectCount(t *testing.T) {
registry := metrics.NewKubeRegistry()
registry.Register(objectCounts)
testedMetrics := "apiserver_storage_objects"

testCases := []struct {
desc string
resource string
count int64
want string
}{
{
desc: "successful fetch",
resource: "foo",
count: 10,
want: `# HELP apiserver_storage_objects [STABLE] Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.
# TYPE apiserver_storage_objects gauge
apiserver_storage_objects{resource="foo"} 10
`,
},
{
desc: "failed fetch",
resource: "bar",
count: -1,
want: `# HELP apiserver_storage_objects [STABLE] Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.
# TYPE apiserver_storage_objects gauge
apiserver_storage_objects{resource="bar"} -1
`,
},
}

for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer registry.Reset()
UpdateObjectCount(test.resource, test.count)
if err := testutil.GatherAndCompare(registry, strings.NewReader(test.want), testedMetrics); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the GatherAndCompare check error-prone as it always passes if the metric isn't present, I don't know what you think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgrisonnet - seems like something we want to fix there ^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's definitely something that should be fixed. Great find @machine424 👍

The testutil function we have is just a wrapper around the function in client_golang, so we would have to fix it there: https://github.com/prometheus/client_golang/blob/1bae6c1e6314f6a20be183a7277059630780232a/prometheus/testutil/testutil.go#L201-L203

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, now that we agree that this can be improved, I can check with client_golang folks (I think this was introduced here https://github.com/prometheus/client_golang/pull/1143/files), independently of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said the issue prometheus/client_golang#1351 is not a prerequisite, we can try to merge this without.

t.Fatal(err)
}
})
}
}

type fakeEtcdMonitor struct {
storageSize int64
}
Expand Down