selector_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fields
  14. import (
  15. "testing"
  16. )
  17. func TestSelectorParse(t *testing.T) {
  18. testGoodStrings := []string{
  19. "x=a,y=b,z=c",
  20. "",
  21. "x!=a,y=b",
  22. }
  23. testBadStrings := []string{
  24. "x=a||y=b",
  25. "x==a==b",
  26. }
  27. for _, test := range testGoodStrings {
  28. lq, err := ParseSelector(test)
  29. if err != nil {
  30. t.Errorf("%v: error %v (%#v)\n", test, err, err)
  31. }
  32. if test != lq.String() {
  33. t.Errorf("%v restring gave: %v\n", test, lq.String())
  34. }
  35. }
  36. for _, test := range testBadStrings {
  37. _, err := ParseSelector(test)
  38. if err == nil {
  39. t.Errorf("%v: did not get expected error\n", test)
  40. }
  41. }
  42. }
  43. func TestDeterministicParse(t *testing.T) {
  44. s1, err := ParseSelector("x=a,a=x")
  45. s2, err2 := ParseSelector("a=x,x=a")
  46. if err != nil || err2 != nil {
  47. t.Errorf("Unexpected parse error")
  48. }
  49. if s1.String() != s2.String() {
  50. t.Errorf("Non-deterministic parse")
  51. }
  52. }
  53. func expectMatch(t *testing.T, selector string, ls Set) {
  54. lq, err := ParseSelector(selector)
  55. if err != nil {
  56. t.Errorf("Unable to parse %v as a selector\n", selector)
  57. return
  58. }
  59. if !lq.Matches(ls) {
  60. t.Errorf("Wanted %s to match '%s', but it did not.\n", selector, ls)
  61. }
  62. }
  63. func expectNoMatch(t *testing.T, selector string, ls Set) {
  64. lq, err := ParseSelector(selector)
  65. if err != nil {
  66. t.Errorf("Unable to parse %v as a selector\n", selector)
  67. return
  68. }
  69. if lq.Matches(ls) {
  70. t.Errorf("Wanted '%s' to not match '%s', but it did.", selector, ls)
  71. }
  72. }
  73. func TestEverything(t *testing.T) {
  74. if !Everything().Matches(Set{"x": "y"}) {
  75. t.Errorf("Nil selector didn't match")
  76. }
  77. if !Everything().Empty() {
  78. t.Errorf("Everything was not empty")
  79. }
  80. }
  81. func TestSelectorMatches(t *testing.T) {
  82. expectMatch(t, "", Set{"x": "y"})
  83. expectMatch(t, "x=y", Set{"x": "y"})
  84. expectMatch(t, "x=y,z=w", Set{"x": "y", "z": "w"})
  85. expectMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "a"})
  86. expectMatch(t, "notin=in", Set{"notin": "in"}) // in and notin in exactMatch
  87. expectNoMatch(t, "x=y", Set{"x": "z"})
  88. expectNoMatch(t, "x=y,z=w", Set{"x": "w", "z": "w"})
  89. expectNoMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "w"})
  90. labelset := Set{
  91. "foo": "bar",
  92. "baz": "blah",
  93. }
  94. expectMatch(t, "foo=bar", labelset)
  95. expectMatch(t, "baz=blah", labelset)
  96. expectMatch(t, "foo=bar,baz=blah", labelset)
  97. expectNoMatch(t, "foo=blah", labelset)
  98. expectNoMatch(t, "baz=bar", labelset)
  99. expectNoMatch(t, "foo=bar,foobar=bar,baz=blah", labelset)
  100. }
  101. func TestOneTermEqualSelector(t *testing.T) {
  102. if !OneTermEqualSelector("x", "y").Matches(Set{"x": "y"}) {
  103. t.Errorf("No match when match expected.")
  104. }
  105. if OneTermEqualSelector("x", "y").Matches(Set{"x": "z"}) {
  106. t.Errorf("Match when none expected.")
  107. }
  108. }
  109. func expectMatchDirect(t *testing.T, selector, ls Set) {
  110. if !SelectorFromSet(selector).Matches(ls) {
  111. t.Errorf("Wanted %s to match '%s', but it did not.\n", selector, ls)
  112. }
  113. }
  114. func expectNoMatchDirect(t *testing.T, selector, ls Set) {
  115. if SelectorFromSet(selector).Matches(ls) {
  116. t.Errorf("Wanted '%s' to not match '%s', but it did.", selector, ls)
  117. }
  118. }
  119. func TestSetMatches(t *testing.T) {
  120. labelset := Set{
  121. "foo": "bar",
  122. "baz": "blah",
  123. }
  124. expectMatchDirect(t, Set{}, labelset)
  125. expectMatchDirect(t, Set{"foo": "bar"}, labelset)
  126. expectMatchDirect(t, Set{"baz": "blah"}, labelset)
  127. expectMatchDirect(t, Set{"foo": "bar", "baz": "blah"}, labelset)
  128. expectNoMatchDirect(t, Set{"foo": "=blah"}, labelset)
  129. expectNoMatchDirect(t, Set{"baz": "=bar"}, labelset)
  130. expectNoMatchDirect(t, Set{"foo": "=bar", "foobar": "bar", "baz": "blah"}, labelset)
  131. }
  132. func TestNilMapIsValid(t *testing.T) {
  133. selector := Set(nil).AsSelector()
  134. if selector == nil {
  135. t.Errorf("Selector for nil set should be Everything")
  136. }
  137. if !selector.Empty() {
  138. t.Errorf("Selector for nil set should be Empty")
  139. }
  140. }
  141. func TestSetIsEmpty(t *testing.T) {
  142. if !(Set{}).AsSelector().Empty() {
  143. t.Errorf("Empty set should be empty")
  144. }
  145. if !(andTerm(nil)).Empty() {
  146. t.Errorf("Nil andTerm should be empty")
  147. }
  148. if (&hasTerm{}).Empty() {
  149. t.Errorf("hasTerm should not be empty")
  150. }
  151. if (&notHasTerm{}).Empty() {
  152. t.Errorf("notHasTerm should not be empty")
  153. }
  154. if !(andTerm{andTerm{}}).Empty() {
  155. t.Errorf("Nested andTerm should be empty")
  156. }
  157. if (andTerm{&hasTerm{"a", "b"}}).Empty() {
  158. t.Errorf("Nested andTerm should not be empty")
  159. }
  160. }
  161. func TestRequiresExactMatch(t *testing.T) {
  162. testCases := map[string]struct {
  163. S Selector
  164. Label string
  165. Value string
  166. Found bool
  167. }{
  168. "empty set": {Set{}.AsSelector(), "test", "", false},
  169. "empty hasTerm": {&hasTerm{}, "test", "", false},
  170. "skipped hasTerm": {&hasTerm{"a", "b"}, "test", "", false},
  171. "valid hasTerm": {&hasTerm{"test", "b"}, "test", "b", true},
  172. "valid hasTerm no value": {&hasTerm{"test", ""}, "test", "", true},
  173. "valid notHasTerm": {&notHasTerm{"test", "b"}, "test", "", false},
  174. "valid notHasTerm no value": {&notHasTerm{"test", ""}, "test", "", false},
  175. "nil andTerm": {andTerm(nil), "test", "", false},
  176. "empty andTerm": {andTerm{}, "test", "", false},
  177. "nested andTerm": {andTerm{andTerm{}}, "test", "", false},
  178. "nested andTerm matches": {andTerm{&hasTerm{"test", "b"}}, "test", "b", true},
  179. "andTerm with non-match": {andTerm{&hasTerm{}, &hasTerm{"test", "b"}}, "test", "b", true},
  180. }
  181. for k, v := range testCases {
  182. value, found := v.S.RequiresExactMatch(v.Label)
  183. if value != v.Value {
  184. t.Errorf("%s: expected value %s, got %s", k, v.Value, value)
  185. }
  186. if found != v.Found {
  187. t.Errorf("%s: expected found %t, got %t", k, v.Found, found)
  188. }
  189. }
  190. }