Skip to content

Commit

Permalink
add wildcard support for label key and values
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBugwadia committed Dec 2, 2020
1 parent 50e5e7e commit 8aa0010
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/engine/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,49 @@ func TestResourceDescriptionExclude_Label_Expression_Match(t *testing.T) {
t.Errorf("Testcase has failed due to the following:\n Function has returned no error, even though it was supposed to fail")
}
}

func TestWildCardLabels(t *testing.T) {

testSelector(t, &metav1.LabelSelector{}, map[string]string{}, true)

testSelector(t, &metav1.LabelSelector{}, map[string]string{"foo": "bar"}, true)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"test.io/*": "bar"}},
map[string]string{"foo": "bar"}, false)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"scale.test.io/*": "bar"}},
map[string]string{"foo": "bar"}, false)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"test.io/*": "bar"}},
map[string]string{"test.io/scale": "foo", "test.io/functional": "bar"}, true)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"test.io/*": "*"}},
map[string]string{"test.io/scale": "foo", "test.io/functional": "bar"}, true)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"test.io/*": "a*"}},
map[string]string{"test.io/scale": "foo", "test.io/functional": "bar"}, false)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"test.io/scale": "f??"}},
map[string]string{"test.io/scale": "foo", "test.io/functional": "bar"}, true)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"*": "*"}},
map[string]string{"test.io/scale": "foo", "test.io/functional": "bar"}, true)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"test.io/functional": "foo"}},
map[string]string{"test.io/scale": "foo", "test.io/functional": "bar"}, false)

testSelector(t, &metav1.LabelSelector{MatchLabels: map[string]string{"*": "*"}},
map[string]string{}, false)
}

func testSelector(t *testing.T, s *metav1.LabelSelector, l map[string]string, match bool) {
res, err := checkSelector(s, l)
if err != nil {
t.Errorf("selector %v failed to select labels %v: %v", s.MatchLabels, l, err)
return
}

if res != match {
t.Errorf("select %v -> labels %v: expected %v received %v", s.MatchLabels, l, match, res)
}
}

0 comments on commit 8aa0010

Please sign in to comment.