[TESTS] Labels: Add a test for SizeOfLabels

This requires a bit of repetition to cover all the different builds, but
it seems worth checking that the function does what is expected.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2025-07-01 11:44:37 +01:00
parent 507227781b
commit e7ac3f440d
4 changed files with 72 additions and 0 deletions

View File

@ -21,6 +21,15 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var expectedSizeOfLabels = []uint64{ // Values must line up with testCaseLabels.
16,
0,
41,
270,
271,
325,
}
func TestVarint(t *testing.T) { func TestVarint(t *testing.T) {
cases := []struct { cases := []struct {
v int v int

View File

@ -0,0 +1,25 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build slicelabels
package labels
var expectedSizeOfLabels = []uint64{ // Values must line up with testCaseLabels.
72,
0,
97,
326,
327,
549,
}

View File

@ -0,0 +1,25 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !slicelabels && !dedupelabels
package labels
var expectedSizeOfLabels = []uint64{ // Values must line up with testCaseLabels.
12,
0,
37,
266,
270,
309,
}

View File

@ -37,6 +37,7 @@ var testCaseLabels = []Labels{
FromStrings("service.name", "t1", "whatever\\whatever", "t2"), FromStrings("service.name", "t1", "whatever\\whatever", "t2"),
FromStrings("aaa", "111", "xx", s254), FromStrings("aaa", "111", "xx", s254),
FromStrings("aaa", "111", "xx", s255), FromStrings("aaa", "111", "xx", s255),
FromStrings("__name__", "kube_pod_container_status_last_terminated_exitcode", "cluster", "prod-af-north-0", " container", "prometheus", "instance", "kube-state-metrics-0:kube-state-metrics:ksm", "job", "kube-state-metrics/kube-state-metrics", " namespace", "observability-prometheus", "pod", "observability-prometheus-0", "uid", "d3ec90b2-4975-4607-b45d-b9ad64bb417e"),
} }
func TestLabels_String(t *testing.T) { func TestLabels_String(t *testing.T) {
@ -46,6 +47,7 @@ func TestLabels_String(t *testing.T) {
`{"service.name"="t1", "whatever\\whatever"="t2"}`, `{"service.name"="t1", "whatever\\whatever"="t2"}`,
`{aaa="111", xx="` + s254 + `"}`, `{aaa="111", xx="` + s254 + `"}`,
`{aaa="111", xx="` + s255 + `"}`, `{aaa="111", xx="` + s255 + `"}`,
`{" container"="prometheus", " namespace"="observability-prometheus", __name__="kube_pod_container_status_last_terminated_exitcode", cluster="prod-af-north-0", instance="kube-state-metrics-0:kube-state-metrics:ksm", job="kube-state-metrics/kube-state-metrics", pod="observability-prometheus-0", uid="d3ec90b2-4975-4607-b45d-b9ad64bb417e"}`,
} }
require.Equal(t, len(testCaseLabels), len(expected)) require.Equal(t, len(testCaseLabels), len(expected))
for i, c := range expected { for i, c := range expected {
@ -61,6 +63,17 @@ func BenchmarkString(b *testing.B) {
} }
} }
func TestSizeOfLabels(t *testing.T) {
require.Equal(t, len(testCaseLabels), len(expectedSizeOfLabels))
for i, c := range expectedSizeOfLabels { // Declared in build-tag-specific files, e.g. labels_slicelabels_test.go.
var total uint64
testCaseLabels[i].Range(func(l Label) {
total += SizeOfLabels(l.Name, l.Value, 1)
})
require.Equal(t, c, total)
}
}
func TestLabels_MatchLabels(t *testing.T) { func TestLabels_MatchLabels(t *testing.T) {
labels := FromStrings( labels := FromStrings(
"__name__", "ALERTS", "__name__", "ALERTS",