helpers_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 api
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/api/resource"
  19. "k8s.io/kubernetes/pkg/labels"
  20. )
  21. func TestConversionError(t *testing.T) {
  22. var i int
  23. var s string
  24. i = 3
  25. s = "foo"
  26. c := ConversionError{
  27. In: &i, Out: &s,
  28. Message: "Can't make x into y, silly",
  29. }
  30. var e error
  31. e = &c // ensure it implements error
  32. msg := e.Error()
  33. t.Logf("Message is %v", msg)
  34. for _, part := range []string{"3", "int", "string", "Can't"} {
  35. if !strings.Contains(msg, part) {
  36. t.Errorf("didn't find %v", part)
  37. }
  38. }
  39. }
  40. func TestSemantic(t *testing.T) {
  41. table := []struct {
  42. a, b interface{}
  43. shouldEqual bool
  44. }{
  45. {resource.MustParse("0"), resource.Quantity{}, true},
  46. {resource.Quantity{}, resource.MustParse("0"), true},
  47. {resource.Quantity{}, resource.MustParse("1m"), false},
  48. {
  49. resource.NewQuantity(5, resource.BinarySI),
  50. resource.NewQuantity(5, resource.DecimalSI),
  51. true,
  52. },
  53. {resource.MustParse("2m"), resource.MustParse("1m"), false},
  54. }
  55. for index, item := range table {
  56. if e, a := item.shouldEqual, Semantic.DeepEqual(item.a, item.b); e != a {
  57. t.Errorf("case[%d], expected %v, got %v.", index, e, a)
  58. }
  59. }
  60. }
  61. func TestIsStandardResource(t *testing.T) {
  62. testCases := []struct {
  63. input string
  64. output bool
  65. }{
  66. {"cpu", true},
  67. {"memory", true},
  68. {"disk", false},
  69. {"blah", false},
  70. {"x.y.z", false},
  71. }
  72. for i, tc := range testCases {
  73. if IsStandardResourceName(tc.input) != tc.output {
  74. t.Errorf("case[%d], expected: %t, got: %t", i, tc.output, !tc.output)
  75. }
  76. }
  77. }
  78. func TestAddToNodeAddresses(t *testing.T) {
  79. testCases := []struct {
  80. existing []NodeAddress
  81. toAdd []NodeAddress
  82. expected []NodeAddress
  83. }{
  84. {
  85. existing: []NodeAddress{},
  86. toAdd: []NodeAddress{},
  87. expected: []NodeAddress{},
  88. },
  89. {
  90. existing: []NodeAddress{},
  91. toAdd: []NodeAddress{
  92. {Type: NodeExternalIP, Address: "1.1.1.1"},
  93. {Type: NodeHostName, Address: "localhost"},
  94. },
  95. expected: []NodeAddress{
  96. {Type: NodeExternalIP, Address: "1.1.1.1"},
  97. {Type: NodeHostName, Address: "localhost"},
  98. },
  99. },
  100. {
  101. existing: []NodeAddress{},
  102. toAdd: []NodeAddress{
  103. {Type: NodeExternalIP, Address: "1.1.1.1"},
  104. {Type: NodeExternalIP, Address: "1.1.1.1"},
  105. },
  106. expected: []NodeAddress{
  107. {Type: NodeExternalIP, Address: "1.1.1.1"},
  108. },
  109. },
  110. {
  111. existing: []NodeAddress{
  112. {Type: NodeExternalIP, Address: "1.1.1.1"},
  113. {Type: NodeInternalIP, Address: "10.1.1.1"},
  114. },
  115. toAdd: []NodeAddress{
  116. {Type: NodeExternalIP, Address: "1.1.1.1"},
  117. {Type: NodeHostName, Address: "localhost"},
  118. },
  119. expected: []NodeAddress{
  120. {Type: NodeExternalIP, Address: "1.1.1.1"},
  121. {Type: NodeInternalIP, Address: "10.1.1.1"},
  122. {Type: NodeHostName, Address: "localhost"},
  123. },
  124. },
  125. }
  126. for i, tc := range testCases {
  127. AddToNodeAddresses(&tc.existing, tc.toAdd...)
  128. if !Semantic.DeepEqual(tc.expected, tc.existing) {
  129. t.Errorf("case[%d], expected: %v, got: %v", i, tc.expected, tc.existing)
  130. }
  131. }
  132. }
  133. func TestGetAccessModesFromString(t *testing.T) {
  134. modes := GetAccessModesFromString("ROX")
  135. if !containsAccessMode(modes, ReadOnlyMany) {
  136. t.Errorf("Expected mode %s, but got %+v", ReadOnlyMany, modes)
  137. }
  138. modes = GetAccessModesFromString("ROX,RWX")
  139. if !containsAccessMode(modes, ReadOnlyMany) {
  140. t.Errorf("Expected mode %s, but got %+v", ReadOnlyMany, modes)
  141. }
  142. if !containsAccessMode(modes, ReadWriteMany) {
  143. t.Errorf("Expected mode %s, but got %+v", ReadWriteMany, modes)
  144. }
  145. modes = GetAccessModesFromString("RWO,ROX,RWX")
  146. if !containsAccessMode(modes, ReadOnlyMany) {
  147. t.Errorf("Expected mode %s, but got %+v", ReadOnlyMany, modes)
  148. }
  149. if !containsAccessMode(modes, ReadWriteMany) {
  150. t.Errorf("Expected mode %s, but got %+v", ReadWriteMany, modes)
  151. }
  152. }
  153. func TestRemoveDuplicateAccessModes(t *testing.T) {
  154. modes := []PersistentVolumeAccessMode{
  155. ReadWriteOnce, ReadOnlyMany, ReadOnlyMany, ReadOnlyMany,
  156. }
  157. modes = removeDuplicateAccessModes(modes)
  158. if len(modes) != 2 {
  159. t.Errorf("Expected 2 distinct modes in set but found %v", len(modes))
  160. }
  161. }
  162. func TestNodeSelectorRequirementsAsSelector(t *testing.T) {
  163. matchExpressions := []NodeSelectorRequirement{{
  164. Key: "foo",
  165. Operator: NodeSelectorOpIn,
  166. Values: []string{"bar", "baz"},
  167. }}
  168. mustParse := func(s string) labels.Selector {
  169. out, e := labels.Parse(s)
  170. if e != nil {
  171. panic(e)
  172. }
  173. return out
  174. }
  175. tc := []struct {
  176. in []NodeSelectorRequirement
  177. out labels.Selector
  178. expectErr bool
  179. }{
  180. {in: nil, out: labels.Nothing()},
  181. {in: []NodeSelectorRequirement{}, out: labels.Nothing()},
  182. {
  183. in: matchExpressions,
  184. out: mustParse("foo in (baz,bar)"),
  185. },
  186. {
  187. in: []NodeSelectorRequirement{{
  188. Key: "foo",
  189. Operator: NodeSelectorOpExists,
  190. Values: []string{"bar", "baz"},
  191. }},
  192. expectErr: true,
  193. },
  194. {
  195. in: []NodeSelectorRequirement{{
  196. Key: "foo",
  197. Operator: NodeSelectorOpGt,
  198. Values: []string{"1"},
  199. }},
  200. out: mustParse("foo>1"),
  201. },
  202. {
  203. in: []NodeSelectorRequirement{{
  204. Key: "bar",
  205. Operator: NodeSelectorOpLt,
  206. Values: []string{"7"},
  207. }},
  208. out: mustParse("bar<7"),
  209. },
  210. }
  211. for i, tc := range tc {
  212. out, err := NodeSelectorRequirementsAsSelector(tc.in)
  213. if err == nil && tc.expectErr {
  214. t.Errorf("[%v]expected error but got none.", i)
  215. }
  216. if err != nil && !tc.expectErr {
  217. t.Errorf("[%v]did not expect error but got: %v", i, err)
  218. }
  219. if !reflect.DeepEqual(out, tc.out) {
  220. t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)
  221. }
  222. }
  223. }
  224. func TestGetAffinityFromPod(t *testing.T) {
  225. testCases := []struct {
  226. pod *Pod
  227. expectErr bool
  228. }{
  229. {
  230. pod: &Pod{},
  231. expectErr: false,
  232. },
  233. {
  234. pod: &Pod{
  235. ObjectMeta: ObjectMeta{
  236. Annotations: map[string]string{
  237. AffinityAnnotationKey: `
  238. {"nodeAffinity": { "requiredDuringSchedulingIgnoredDuringExecution": {
  239. "nodeSelectorTerms": [{
  240. "matchExpressions": [{
  241. "key": "foo",
  242. "operator": "In",
  243. "values": ["value1", "value2"]
  244. }]
  245. }]
  246. }}}`,
  247. },
  248. },
  249. },
  250. expectErr: false,
  251. },
  252. {
  253. pod: &Pod{
  254. ObjectMeta: ObjectMeta{
  255. Annotations: map[string]string{
  256. AffinityAnnotationKey: `
  257. {"nodeAffinity": { "requiredDuringSchedulingIgnoredDuringExecution": {
  258. "nodeSelectorTerms": [{
  259. "matchExpressions": [{
  260. "key": "foo",
  261. `,
  262. },
  263. },
  264. },
  265. expectErr: true,
  266. },
  267. }
  268. for i, tc := range testCases {
  269. _, err := GetAffinityFromPodAnnotations(tc.pod.Annotations)
  270. if err == nil && tc.expectErr {
  271. t.Errorf("[%v]expected error but got none.", i)
  272. }
  273. if err != nil && !tc.expectErr {
  274. t.Errorf("[%v]did not expect error but got: %v", i, err)
  275. }
  276. }
  277. }
  278. func TestGetAvoidPodsFromNode(t *testing.T) {
  279. controllerFlag := true
  280. testCases := []struct {
  281. node *Node
  282. expectValue AvoidPods
  283. expectErr bool
  284. }{
  285. {
  286. node: &Node{},
  287. expectValue: AvoidPods{},
  288. expectErr: false,
  289. },
  290. {
  291. node: &Node{
  292. ObjectMeta: ObjectMeta{
  293. Annotations: map[string]string{
  294. PreferAvoidPodsAnnotationKey: `
  295. {
  296. "preferAvoidPods": [
  297. {
  298. "podSignature": {
  299. "podController": {
  300. "apiVersion": "v1",
  301. "kind": "ReplicationController",
  302. "name": "foo",
  303. "uid": "abcdef123456",
  304. "controller": true
  305. }
  306. },
  307. "reason": "some reason",
  308. "message": "some message"
  309. }
  310. ]
  311. }`,
  312. },
  313. },
  314. },
  315. expectValue: AvoidPods{
  316. PreferAvoidPods: []PreferAvoidPodsEntry{
  317. {
  318. PodSignature: PodSignature{
  319. PodController: &OwnerReference{
  320. APIVersion: "v1",
  321. Kind: "ReplicationController",
  322. Name: "foo",
  323. UID: "abcdef123456",
  324. Controller: &controllerFlag,
  325. },
  326. },
  327. Reason: "some reason",
  328. Message: "some message",
  329. },
  330. },
  331. },
  332. expectErr: false,
  333. },
  334. {
  335. node: &Node{
  336. // Missing end symbol of "podController" and "podSignature"
  337. ObjectMeta: ObjectMeta{
  338. Annotations: map[string]string{
  339. PreferAvoidPodsAnnotationKey: `
  340. {
  341. "preferAvoidPods": [
  342. {
  343. "podSignature": {
  344. "podController": {
  345. "kind": "ReplicationController",
  346. "apiVersion": "v1"
  347. "reason": "some reason",
  348. "message": "some message"
  349. }
  350. ]
  351. }`,
  352. },
  353. },
  354. },
  355. expectValue: AvoidPods{},
  356. expectErr: true,
  357. },
  358. }
  359. for i, tc := range testCases {
  360. v, err := GetAvoidPodsFromNodeAnnotations(tc.node.Annotations)
  361. if err == nil && tc.expectErr {
  362. t.Errorf("[%v]expected error but got none.", i)
  363. }
  364. if err != nil && !tc.expectErr {
  365. t.Errorf("[%v]did not expect error but got: %v", i, err)
  366. }
  367. if !reflect.DeepEqual(tc.expectValue, v) {
  368. t.Errorf("[%v]expect value %v but got %v with %v", i, tc.expectValue, v, v.PreferAvoidPods[0].PodSignature.PodController.Controller)
  369. }
  370. }
  371. }
  372. func TestSysctlsFromPodAnnotation(t *testing.T) {
  373. type Test struct {
  374. annotation string
  375. expectValue []Sysctl
  376. expectErr bool
  377. }
  378. for i, test := range []Test{
  379. {
  380. annotation: "",
  381. expectValue: nil,
  382. },
  383. {
  384. annotation: "foo.bar",
  385. expectErr: true,
  386. },
  387. {
  388. annotation: "foo.bar=42",
  389. expectValue: []Sysctl{{Name: "foo.bar", Value: "42"}},
  390. },
  391. {
  392. annotation: "foo.bar=42,",
  393. expectErr: true,
  394. },
  395. {
  396. annotation: "foo.bar=42,abc.def=1",
  397. expectValue: []Sysctl{{Name: "foo.bar", Value: "42"}, {Name: "abc.def", Value: "1"}},
  398. },
  399. } {
  400. sysctls, err := SysctlsFromPodAnnotation(test.annotation)
  401. if test.expectErr && err == nil {
  402. t.Errorf("[%v]expected error but got none", i)
  403. } else if !test.expectErr && err != nil {
  404. t.Errorf("[%v]did not expect error but got: %v", i, err)
  405. } else if !reflect.DeepEqual(sysctls, test.expectValue) {
  406. t.Errorf("[%v]expect value %v but got %v", i, test.expectValue, sysctls)
  407. }
  408. }
  409. }