priority_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. Copyright 2016 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 meta
  14. import (
  15. "errors"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. )
  21. func TestPriorityRESTMapperResourceForErrorHandling(t *testing.T) {
  22. tcs := []struct {
  23. name string
  24. delegate RESTMapper
  25. resourcePatterns []unversioned.GroupVersionResource
  26. result unversioned.GroupVersionResource
  27. err string
  28. }{
  29. {
  30. name: "single hit",
  31. delegate: fixedRESTMapper{resourcesFor: []unversioned.GroupVersionResource{{Resource: "single-hit"}}},
  32. result: unversioned.GroupVersionResource{Resource: "single-hit"},
  33. },
  34. {
  35. name: "ambiguous match",
  36. delegate: fixedRESTMapper{resourcesFor: []unversioned.GroupVersionResource{
  37. {Group: "one", Version: "a", Resource: "first"},
  38. {Group: "two", Version: "b", Resource: "second"},
  39. }},
  40. err: "matches multiple resources",
  41. },
  42. {
  43. name: "group selection",
  44. delegate: fixedRESTMapper{resourcesFor: []unversioned.GroupVersionResource{
  45. {Group: "one", Version: "a", Resource: "first"},
  46. {Group: "two", Version: "b", Resource: "second"},
  47. }},
  48. resourcePatterns: []unversioned.GroupVersionResource{
  49. {Group: "one", Version: AnyVersion, Resource: AnyResource},
  50. },
  51. result: unversioned.GroupVersionResource{Group: "one", Version: "a", Resource: "first"},
  52. },
  53. {
  54. name: "empty match continues",
  55. delegate: fixedRESTMapper{resourcesFor: []unversioned.GroupVersionResource{
  56. {Group: "one", Version: "a", Resource: "first"},
  57. {Group: "two", Version: "b", Resource: "second"},
  58. }},
  59. resourcePatterns: []unversioned.GroupVersionResource{
  60. {Group: "fail", Version: AnyVersion, Resource: AnyResource},
  61. {Group: "one", Version: AnyVersion, Resource: AnyResource},
  62. },
  63. result: unversioned.GroupVersionResource{Group: "one", Version: "a", Resource: "first"},
  64. },
  65. {
  66. name: "group followed by version selection",
  67. delegate: fixedRESTMapper{resourcesFor: []unversioned.GroupVersionResource{
  68. {Group: "one", Version: "a", Resource: "first"},
  69. {Group: "two", Version: "b", Resource: "second"},
  70. {Group: "one", Version: "c", Resource: "third"},
  71. }},
  72. resourcePatterns: []unversioned.GroupVersionResource{
  73. {Group: "one", Version: AnyVersion, Resource: AnyResource},
  74. {Group: AnyGroup, Version: "a", Resource: AnyResource},
  75. },
  76. result: unversioned.GroupVersionResource{Group: "one", Version: "a", Resource: "first"},
  77. },
  78. {
  79. name: "resource selection",
  80. delegate: fixedRESTMapper{resourcesFor: []unversioned.GroupVersionResource{
  81. {Group: "one", Version: "a", Resource: "first"},
  82. {Group: "one", Version: "a", Resource: "second"},
  83. }},
  84. resourcePatterns: []unversioned.GroupVersionResource{
  85. {Group: AnyGroup, Version: AnyVersion, Resource: "second"},
  86. },
  87. result: unversioned.GroupVersionResource{Group: "one", Version: "a", Resource: "second"},
  88. },
  89. }
  90. for _, tc := range tcs {
  91. mapper := PriorityRESTMapper{Delegate: tc.delegate, ResourcePriority: tc.resourcePatterns}
  92. actualResult, actualErr := mapper.ResourceFor(unversioned.GroupVersionResource{})
  93. if e, a := tc.result, actualResult; e != a {
  94. t.Errorf("%s: expected %v, got %v", tc.name, e, a)
  95. }
  96. if len(tc.err) == 0 && actualErr == nil {
  97. continue
  98. }
  99. if len(tc.err) > 0 && actualErr == nil {
  100. t.Errorf("%s: missing expected err: %v", tc.name, tc.err)
  101. continue
  102. }
  103. if !strings.Contains(actualErr.Error(), tc.err) {
  104. t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
  105. }
  106. }
  107. }
  108. func TestPriorityRESTMapperKindForErrorHandling(t *testing.T) {
  109. tcs := []struct {
  110. name string
  111. delegate RESTMapper
  112. kindPatterns []unversioned.GroupVersionKind
  113. result unversioned.GroupVersionKind
  114. err string
  115. }{
  116. {
  117. name: "single hit",
  118. delegate: fixedRESTMapper{kindsFor: []unversioned.GroupVersionKind{{Kind: "single-hit"}}},
  119. result: unversioned.GroupVersionKind{Kind: "single-hit"},
  120. },
  121. {
  122. name: "ambiguous match",
  123. delegate: fixedRESTMapper{kindsFor: []unversioned.GroupVersionKind{
  124. {Group: "one", Version: "a", Kind: "first"},
  125. {Group: "two", Version: "b", Kind: "second"},
  126. }},
  127. err: "matches multiple kinds",
  128. },
  129. {
  130. name: "group selection",
  131. delegate: fixedRESTMapper{kindsFor: []unversioned.GroupVersionKind{
  132. {Group: "one", Version: "a", Kind: "first"},
  133. {Group: "two", Version: "b", Kind: "second"},
  134. }},
  135. kindPatterns: []unversioned.GroupVersionKind{
  136. {Group: "one", Version: AnyVersion, Kind: AnyKind},
  137. },
  138. result: unversioned.GroupVersionKind{Group: "one", Version: "a", Kind: "first"},
  139. },
  140. {
  141. name: "empty match continues",
  142. delegate: fixedRESTMapper{kindsFor: []unversioned.GroupVersionKind{
  143. {Group: "one", Version: "a", Kind: "first"},
  144. {Group: "two", Version: "b", Kind: "second"},
  145. }},
  146. kindPatterns: []unversioned.GroupVersionKind{
  147. {Group: "fail", Version: AnyVersion, Kind: AnyKind},
  148. {Group: "one", Version: AnyVersion, Kind: AnyKind},
  149. },
  150. result: unversioned.GroupVersionKind{Group: "one", Version: "a", Kind: "first"},
  151. },
  152. {
  153. name: "group followed by version selection",
  154. delegate: fixedRESTMapper{kindsFor: []unversioned.GroupVersionKind{
  155. {Group: "one", Version: "a", Kind: "first"},
  156. {Group: "two", Version: "b", Kind: "second"},
  157. {Group: "one", Version: "c", Kind: "third"},
  158. }},
  159. kindPatterns: []unversioned.GroupVersionKind{
  160. {Group: "one", Version: AnyVersion, Kind: AnyKind},
  161. {Group: AnyGroup, Version: "a", Kind: AnyKind},
  162. },
  163. result: unversioned.GroupVersionKind{Group: "one", Version: "a", Kind: "first"},
  164. },
  165. {
  166. name: "kind selection",
  167. delegate: fixedRESTMapper{kindsFor: []unversioned.GroupVersionKind{
  168. {Group: "one", Version: "a", Kind: "first"},
  169. {Group: "one", Version: "a", Kind: "second"},
  170. }},
  171. kindPatterns: []unversioned.GroupVersionKind{
  172. {Group: AnyGroup, Version: AnyVersion, Kind: "second"},
  173. },
  174. result: unversioned.GroupVersionKind{Group: "one", Version: "a", Kind: "second"},
  175. },
  176. }
  177. for _, tc := range tcs {
  178. mapper := PriorityRESTMapper{Delegate: tc.delegate, KindPriority: tc.kindPatterns}
  179. actualResult, actualErr := mapper.KindFor(unversioned.GroupVersionResource{})
  180. if e, a := tc.result, actualResult; e != a {
  181. t.Errorf("%s: expected %v, got %v", tc.name, e, a)
  182. }
  183. if len(tc.err) == 0 && actualErr == nil {
  184. continue
  185. }
  186. if len(tc.err) > 0 && actualErr == nil {
  187. t.Errorf("%s: missing expected err: %v", tc.name, tc.err)
  188. continue
  189. }
  190. if !strings.Contains(actualErr.Error(), tc.err) {
  191. t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
  192. }
  193. }
  194. }
  195. func TestPriorityRESTMapperRESTMapping(t *testing.T) {
  196. mapping1 := &RESTMapping{
  197. GroupVersionKind: unversioned.GroupVersionKind{Kind: "Foo", Version: "v1alpha1"},
  198. }
  199. mapping2 := &RESTMapping{
  200. GroupVersionKind: unversioned.GroupVersionKind{Kind: "Foo", Version: "v1"},
  201. }
  202. mapping3 := &RESTMapping{
  203. GroupVersionKind: unversioned.GroupVersionKind{Group: "other", Kind: "Foo", Version: "v1"},
  204. }
  205. allMappers := MultiRESTMapper{
  206. fixedRESTMapper{mappings: []*RESTMapping{mapping1}},
  207. fixedRESTMapper{mappings: []*RESTMapping{mapping2}},
  208. fixedRESTMapper{mappings: []*RESTMapping{mapping3}},
  209. }
  210. tcs := []struct {
  211. name string
  212. mapper PriorityRESTMapper
  213. input unversioned.GroupKind
  214. result *RESTMapping
  215. err error
  216. }{
  217. {
  218. name: "empty",
  219. mapper: PriorityRESTMapper{Delegate: MultiRESTMapper{}},
  220. input: unversioned.GroupKind{Kind: "Foo"},
  221. err: &NoKindMatchError{PartialKind: unversioned.GroupVersionKind{Kind: "Foo"}},
  222. },
  223. {
  224. name: "ignore not found",
  225. mapper: PriorityRESTMapper{Delegate: MultiRESTMapper{fixedRESTMapper{err: &NoKindMatchError{PartialKind: unversioned.GroupVersionKind{Kind: "IGNORE_THIS"}}}}},
  226. input: unversioned.GroupKind{Kind: "Foo"},
  227. err: &NoKindMatchError{PartialKind: unversioned.GroupVersionKind{Kind: "Foo"}},
  228. },
  229. {
  230. name: "accept first failure",
  231. mapper: PriorityRESTMapper{Delegate: MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{mappings: []*RESTMapping{mapping1}}}},
  232. input: unversioned.GroupKind{Kind: "Foo"},
  233. err: errors.New("fail on this"),
  234. },
  235. {
  236. name: "return error for ambiguous",
  237. mapper: PriorityRESTMapper{
  238. Delegate: allMappers,
  239. },
  240. input: unversioned.GroupKind{Kind: "Foo"},
  241. err: &AmbiguousKindError{
  242. PartialKind: unversioned.GroupVersionKind{Kind: "Foo"},
  243. MatchingKinds: []unversioned.GroupVersionKind{
  244. {Kind: "Foo", Version: "v1alpha1"},
  245. {Kind: "Foo", Version: "v1"},
  246. {Group: "other", Kind: "Foo", Version: "v1"},
  247. },
  248. },
  249. },
  250. {
  251. name: "accept only item",
  252. mapper: PriorityRESTMapper{
  253. Delegate: fixedRESTMapper{mappings: []*RESTMapping{mapping1}},
  254. },
  255. input: unversioned.GroupKind{Kind: "Foo"},
  256. result: mapping1,
  257. },
  258. {
  259. name: "return single priority",
  260. mapper: PriorityRESTMapper{
  261. Delegate: allMappers,
  262. KindPriority: []unversioned.GroupVersionKind{{Version: "v1", Kind: AnyKind}, {Version: "v1alpha1", Kind: AnyKind}},
  263. },
  264. input: unversioned.GroupKind{Kind: "Foo"},
  265. result: mapping2,
  266. },
  267. {
  268. name: "return out of group match",
  269. mapper: PriorityRESTMapper{
  270. Delegate: allMappers,
  271. KindPriority: []unversioned.GroupVersionKind{{Group: AnyGroup, Version: "v1", Kind: AnyKind}, {Group: "other", Version: AnyVersion, Kind: AnyKind}},
  272. },
  273. input: unversioned.GroupKind{Kind: "Foo"},
  274. result: mapping3,
  275. },
  276. }
  277. for _, tc := range tcs {
  278. actualResult, actualErr := tc.mapper.RESTMapping(tc.input)
  279. if e, a := tc.result, actualResult; !reflect.DeepEqual(e, a) {
  280. t.Errorf("%s: expected %v, got %v", tc.name, e, a)
  281. }
  282. switch {
  283. case tc.err == nil && actualErr == nil:
  284. case tc.err == nil:
  285. t.Errorf("%s: unexpected error: %v", tc.name, actualErr)
  286. case actualErr == nil:
  287. t.Errorf("%s: expected error: %v got nil", tc.name, tc.err)
  288. case tc.err.Error() != actualErr.Error():
  289. t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
  290. }
  291. }
  292. }