Skip to content

Commit

Permalink
labels: Extra tests and comment for WithoutLabels method. (prometheus…
Browse files Browse the repository at this point in the history
…#8024)

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka authored Oct 13, 2020
1 parent 71577e4 commit dee6ac2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 60 deletions.
1 change: 1 addition & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
122 changes: 62 additions & 60 deletions pkg/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
}

Expand Down

0 comments on commit dee6ac2

Please sign in to comment.