From dee6ac274e7723cc96774384809e0e8add62955a Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Tue, 13 Oct 2020 09:57:53 +0200 Subject: [PATCH] labels: Extra tests and comment for WithoutLabels method. (#8024) Signed-off-by: Bartlomiej Plotka --- pkg/labels/labels.go | 1 + pkg/labels/labels_test.go | 122 +++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 0b7f6f7f3..b6b859fa7 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -275,6 +275,7 @@ func (ls Labels) WithoutEmpty() Labels { if v.Value != "" { continue } + // Do not copy the slice until it's necessary. els := make(Labels, 0, len(ls)-1) for _, v := range ls { if v.Value != "" { diff --git a/pkg/labels/labels_test.go b/pkg/labels/labels_test.go index b486677d2..e40bb1d74 100644 --- a/pkg/labels/labels_test.go +++ b/pkg/labels/labels_test.go @@ -210,81 +210,83 @@ func TestLabels_HasDuplicateLabelNames(t *testing.T) { } func TestLabels_WithoutEmpty(t *testing.T) { - tests := []struct { + for _, test := range []struct { input Labels expected Labels }{ { input: Labels{ - { - Name: "__name__", - Value: "test", - }, - { - Name: "foo", - }, - { - Name: "hostname", - Value: "localhost", - }, - { - Name: "bar", - }, - { - Name: "job", - Value: "check", - }, + {Name: "foo"}, + {Name: "bar"}, + }, + expected: Labels{}, + }, + { + input: Labels{ + {Name: "foo"}, + {Name: "bar"}, + {Name: "baz"}, + }, + expected: Labels{}, + }, + { + input: Labels{ + {Name: "__name__", Value: "test"}, + {Name: "hostname", Value: "localhost"}, + {Name: "job", Value: "check"}, }, expected: Labels{ - { - Name: "__name__", - Value: "test", - }, - { - Name: "hostname", - Value: "localhost", - }, - { - Name: "job", - Value: "check", - }, + {Name: "__name__", Value: "test"}, + {Name: "hostname", Value: "localhost"}, + {Name: "job", Value: "check"}, }, }, { input: Labels{ - { - Name: "__name__", - Value: "test", - }, - { - Name: "hostname", - Value: "localhost", - }, - { - Name: "job", - Value: "check", - }, + {Name: "__name__", Value: "test"}, + {Name: "hostname", Value: "localhost"}, + {Name: "bar"}, + {Name: "job", Value: "check"}, }, expected: Labels{ - { - Name: "__name__", - Value: "test", - }, - { - Name: "hostname", - Value: "localhost", - }, - { - Name: "job", - Value: "check", - }, + {Name: "__name__", Value: "test"}, + {Name: "hostname", Value: "localhost"}, + {Name: "job", Value: "check"}, }, }, - } - - for i, test := range tests { - got := test.input.WithoutEmpty() - testutil.Equals(t, test.expected, got, "unexpected labelset for test case %d", i) + { + input: Labels{ + {Name: "__name__", Value: "test"}, + {Name: "foo"}, + {Name: "hostname", Value: "localhost"}, + {Name: "bar"}, + {Name: "job", Value: "check"}, + }, + expected: Labels{ + {Name: "__name__", Value: "test"}, + {Name: "hostname", Value: "localhost"}, + {Name: "job", Value: "check"}, + }, + }, + { + input: Labels{ + {Name: "__name__", Value: "test"}, + {Name: "foo"}, + {Name: "baz"}, + {Name: "hostname", Value: "localhost"}, + {Name: "bar"}, + {Name: "job", Value: "check"}, + }, + expected: Labels{ + {Name: "__name__", Value: "test"}, + {Name: "hostname", Value: "localhost"}, + {Name: "job", Value: "check"}, + }, + }, + } { + t.Run("", func(t *testing.T) { + testutil.Equals(t, test.expected, test.input.WithoutEmpty()) + }) } }