Skip to content

Commit

Permalink
Add helper for making simple selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
erictune committed Nov 21, 2014
1 parent 9f5ebef commit 01434a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,9 @@ func ParseSelector(selector string) (Selector, error) {
}
return andTerm(items), nil
}

// OneTermEqualSelector returns an object that matches objects where one label/field equals one value.
// Cannot return an error.
func OneTermEqualSelector(k, v string) Selector {
return &hasTerm{label: k, value: v}
}
9 changes: 9 additions & 0 deletions pkg/labels/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ func TestSelectorMatches(t *testing.T) {
expectNoMatch(t, "foo=bar,foobar=bar,baz=blah", labelset)
}

func TestOneTermEqualSelector(t *testing.T) {
if !OneTermEqualSelector("x", "y").Matches(Set{"x": "y"}) {
t.Errorf("No match when match expected.")
}
if OneTermEqualSelector("x", "y").Matches(Set{"x": "z"}) {
t.Errorf("Match when none expected.")
}
}

func expectMatchDirect(t *testing.T, selector, ls Set) {
if !SelectorFromSet(selector).Matches(ls) {
t.Errorf("Wanted %s to match '%s', but it did not.\n", selector, ls)
Expand Down

0 comments on commit 01434a8

Please sign in to comment.