Skip to content

Commit

Permalink
Make selector parsing deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Jun 20, 2014
1 parent 671a7f1 commit b3fbc47
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package labels

import (
"fmt"
"sort"
"strings"
)

Expand Down Expand Up @@ -101,6 +102,7 @@ func SelectorFromSet(ls Set) Selector {
// Takes a string repsenting a selector and returns an object suitable for matching, or an error.
func ParseSelector(selector string) (Selector, error) {
parts := strings.Split(selector, ",")
sort.StringSlice(parts).Sort()
var items []Selector
for _, part := range parts {
if part == "" {
Expand Down
11 changes: 11 additions & 0 deletions pkg/labels/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func TestSelectorParse(t *testing.T) {
}
}

func TestDeterministicParse(t *testing.T) {
s1, err := ParseSelector("x=a,a=x")
s2, err2 := ParseSelector("a=x,x=a")
if err != nil || err2 != nil {
t.Errorf("Unexpected parse error")
}
if s1.String() != s2.String() {
t.Errorf("Non-deterministic parse")
}
}

func expectMatch(t *testing.T, selector string, ls Set) {
lq, err := ParseSelector(selector)
if err != nil {
Expand Down

0 comments on commit b3fbc47

Please sign in to comment.