From 507227781b01e8e55fca9f1d5d4578ee0aa722c9 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 1 Jul 2025 11:29:00 +0100 Subject: [PATCH 1/3] [REFACTOR] Labels: Extract test case data from TestLabels_String So we can use them in other tests. Signed-off-by: Bryan Boreham --- model/labels/labels_test.go | 52 ++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/model/labels/labels_test.go b/model/labels/labels_test.go index d9368ccfc3..1a4e2b29d7 100644 --- a/model/labels/labels_test.go +++ b/model/labels/labels_test.go @@ -26,37 +26,31 @@ import ( "gopkg.in/yaml.v2" ) +var ( + s254 = strings.Repeat("x", 254) // Edge cases for stringlabels encoding. + s255 = strings.Repeat("x", 255) +) + +var testCaseLabels = []Labels{ + FromStrings("t1", "t1", "t2", "t2"), + {}, + FromStrings("service.name", "t1", "whatever\\whatever", "t2"), + FromStrings("aaa", "111", "xx", s254), + FromStrings("aaa", "111", "xx", s255), +} + func TestLabels_String(t *testing.T) { - s254 := strings.Repeat("x", 254) // Edge cases for stringlabels encoding. - s255 := strings.Repeat("x", 255) - cases := []struct { - labels Labels - expected string - }{ - { - labels: FromStrings("t1", "t1", "t2", "t2"), - expected: "{t1=\"t1\", t2=\"t2\"}", - }, - { - labels: Labels{}, - expected: "{}", - }, - { - labels: FromStrings("service.name", "t1", "whatever\\whatever", "t2"), - expected: `{"service.name"="t1", "whatever\\whatever"="t2"}`, - }, - { - labels: FromStrings("aaa", "111", "xx", s254), - expected: `{aaa="111", xx="` + s254 + `"}`, - }, - { - labels: FromStrings("aaa", "111", "xx", s255), - expected: `{aaa="111", xx="` + s255 + `"}`, - }, + expected := []string{ // Values must line up with testCaseLabels. + "{t1=\"t1\", t2=\"t2\"}", + "{}", + `{"service.name"="t1", "whatever\\whatever"="t2"}`, + `{aaa="111", xx="` + s254 + `"}`, + `{aaa="111", xx="` + s255 + `"}`, } - for _, c := range cases { - str := c.labels.String() - require.Equal(t, c.expected, str) + require.Equal(t, len(testCaseLabels), len(expected)) + for i, c := range expected { + str := testCaseLabels[i].String() + require.Equal(t, c, str) } } From e7ac3f440d918f0c3a4d6c4297be96e9c65f2347 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 1 Jul 2025 11:44:37 +0100 Subject: [PATCH 2/3] [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 --- model/labels/labels_dedupelabels_test.go | 9 +++++++++ model/labels/labels_slicelabels_test.go | 25 ++++++++++++++++++++++++ model/labels/labels_stringlabels_test.go | 25 ++++++++++++++++++++++++ model/labels/labels_test.go | 13 ++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 model/labels/labels_slicelabels_test.go create mode 100644 model/labels/labels_stringlabels_test.go diff --git a/model/labels/labels_dedupelabels_test.go b/model/labels/labels_dedupelabels_test.go index 5ef9255c21..baf0423db2 100644 --- a/model/labels/labels_dedupelabels_test.go +++ b/model/labels/labels_dedupelabels_test.go @@ -21,6 +21,15 @@ import ( "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) { cases := []struct { v int diff --git a/model/labels/labels_slicelabels_test.go b/model/labels/labels_slicelabels_test.go new file mode 100644 index 0000000000..e9edc9152d --- /dev/null +++ b/model/labels/labels_slicelabels_test.go @@ -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, +} diff --git a/model/labels/labels_stringlabels_test.go b/model/labels/labels_stringlabels_test.go new file mode 100644 index 0000000000..aaa8b52415 --- /dev/null +++ b/model/labels/labels_stringlabels_test.go @@ -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, +} diff --git a/model/labels/labels_test.go b/model/labels/labels_test.go index 1a4e2b29d7..feee476dcc 100644 --- a/model/labels/labels_test.go +++ b/model/labels/labels_test.go @@ -37,6 +37,7 @@ var testCaseLabels = []Labels{ FromStrings("service.name", "t1", "whatever\\whatever", "t2"), FromStrings("aaa", "111", "xx", s254), 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) { @@ -46,6 +47,7 @@ func TestLabels_String(t *testing.T) { `{"service.name"="t1", "whatever\\whatever"="t2"}`, `{aaa="111", xx="` + s254 + `"}`, `{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)) 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) { labels := FromStrings( "__name__", "ALERTS", From 4eafbcae93d4848bcf064a4dcbf2a3ca7157ed43 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 2 Jul 2025 09:42:32 +0100 Subject: [PATCH 3/3] lint Signed-off-by: Bryan Boreham --- model/labels/labels_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/labels/labels_test.go b/model/labels/labels_test.go index feee476dcc..3b4f802160 100644 --- a/model/labels/labels_test.go +++ b/model/labels/labels_test.go @@ -49,7 +49,7 @@ func TestLabels_String(t *testing.T) { `{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.Len(t, expected, len(testCaseLabels)) for i, c := range expected { str := testCaseLabels[i].String() require.Equal(t, c, str) @@ -64,7 +64,7 @@ func BenchmarkString(b *testing.B) { } func TestSizeOfLabels(t *testing.T) { - require.Equal(t, len(testCaseLabels), len(expectedSizeOfLabels)) + require.Len(t, expectedSizeOfLabels, len(testCaseLabels)) 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) {