utils_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 taint
  14. import (
  15. "reflect"
  16. "testing"
  17. corev1 "k8s.io/api/core/v1"
  18. )
  19. func TestDeleteTaint(t *testing.T) {
  20. cases := []struct {
  21. name string
  22. taints []corev1.Taint
  23. taintToDelete *corev1.Taint
  24. expectedTaints []corev1.Taint
  25. expectedResult bool
  26. }{
  27. {
  28. name: "delete taint with different name",
  29. taints: []corev1.Taint{
  30. {
  31. Key: "foo",
  32. Effect: corev1.TaintEffectNoSchedule,
  33. },
  34. },
  35. taintToDelete: &corev1.Taint{Key: "foo_1", Effect: corev1.TaintEffectNoSchedule},
  36. expectedTaints: []corev1.Taint{
  37. {
  38. Key: "foo",
  39. Effect: corev1.TaintEffectNoSchedule,
  40. },
  41. },
  42. expectedResult: false,
  43. },
  44. {
  45. name: "delete taint with different effect",
  46. taints: []corev1.Taint{
  47. {
  48. Key: "foo",
  49. Effect: corev1.TaintEffectNoSchedule,
  50. },
  51. },
  52. taintToDelete: &corev1.Taint{Key: "foo", Effect: corev1.TaintEffectNoExecute},
  53. expectedTaints: []corev1.Taint{
  54. {
  55. Key: "foo",
  56. Effect: corev1.TaintEffectNoSchedule,
  57. },
  58. },
  59. expectedResult: false,
  60. },
  61. {
  62. name: "delete taint successfully",
  63. taints: []corev1.Taint{
  64. {
  65. Key: "foo",
  66. Effect: corev1.TaintEffectNoSchedule,
  67. },
  68. },
  69. taintToDelete: &corev1.Taint{Key: "foo", Effect: corev1.TaintEffectNoSchedule},
  70. expectedTaints: []corev1.Taint{},
  71. expectedResult: true,
  72. },
  73. {
  74. name: "delete taint from empty taint array",
  75. taints: []corev1.Taint{},
  76. taintToDelete: &corev1.Taint{Key: "foo", Effect: corev1.TaintEffectNoSchedule},
  77. expectedTaints: []corev1.Taint{},
  78. expectedResult: false,
  79. },
  80. }
  81. for _, c := range cases {
  82. taints, result := deleteTaint(c.taints, c.taintToDelete)
  83. if result != c.expectedResult {
  84. t.Errorf("[%s] should return %t, but got: %t", c.name, c.expectedResult, result)
  85. }
  86. if !reflect.DeepEqual(taints, c.expectedTaints) {
  87. t.Errorf("[%s] the result taints should be %v, but got: %v", c.name, c.expectedTaints, taints)
  88. }
  89. }
  90. }
  91. func TestDeleteTaintByKey(t *testing.T) {
  92. cases := []struct {
  93. name string
  94. taints []corev1.Taint
  95. taintKey string
  96. expectedTaints []corev1.Taint
  97. expectedResult bool
  98. }{
  99. {
  100. name: "delete taint unsuccessfully",
  101. taints: []corev1.Taint{
  102. {
  103. Key: "foo",
  104. Value: "bar",
  105. Effect: corev1.TaintEffectNoSchedule,
  106. },
  107. },
  108. taintKey: "foo_1",
  109. expectedTaints: []corev1.Taint{
  110. {
  111. Key: "foo",
  112. Value: "bar",
  113. Effect: corev1.TaintEffectNoSchedule,
  114. },
  115. },
  116. expectedResult: false,
  117. },
  118. {
  119. name: "delete taint successfully",
  120. taints: []corev1.Taint{
  121. {
  122. Key: "foo",
  123. Value: "bar",
  124. Effect: corev1.TaintEffectNoSchedule,
  125. },
  126. },
  127. taintKey: "foo",
  128. expectedTaints: []corev1.Taint{},
  129. expectedResult: true,
  130. },
  131. {
  132. name: "delete taint from empty taint array",
  133. taints: []corev1.Taint{},
  134. taintKey: "foo",
  135. expectedTaints: []corev1.Taint{},
  136. expectedResult: false,
  137. },
  138. }
  139. for _, c := range cases {
  140. taints, result := deleteTaintsByKey(c.taints, c.taintKey)
  141. if result != c.expectedResult {
  142. t.Errorf("[%s] should return %t, but got: %t", c.name, c.expectedResult, result)
  143. }
  144. if !reflect.DeepEqual(c.expectedTaints, taints) {
  145. t.Errorf("[%s] the result taints should be %v, but got: %v", c.name, c.expectedTaints, taints)
  146. }
  147. }
  148. }
  149. func TestCheckIfTaintsAlreadyExists(t *testing.T) {
  150. oldTaints := []corev1.Taint{
  151. {
  152. Key: "foo_1",
  153. Value: "bar",
  154. Effect: corev1.TaintEffectNoSchedule,
  155. },
  156. {
  157. Key: "foo_2",
  158. Value: "bar",
  159. Effect: corev1.TaintEffectNoSchedule,
  160. },
  161. {
  162. Key: "foo_3",
  163. Value: "bar",
  164. Effect: corev1.TaintEffectNoSchedule,
  165. },
  166. }
  167. cases := []struct {
  168. name string
  169. taintsToCheck []corev1.Taint
  170. expectedResult string
  171. }{
  172. {
  173. name: "empty array",
  174. taintsToCheck: []corev1.Taint{},
  175. expectedResult: "",
  176. },
  177. {
  178. name: "no match",
  179. taintsToCheck: []corev1.Taint{
  180. {
  181. Key: "foo_1",
  182. Effect: corev1.TaintEffectNoExecute,
  183. },
  184. },
  185. expectedResult: "",
  186. },
  187. {
  188. name: "match one taint",
  189. taintsToCheck: []corev1.Taint{
  190. {
  191. Key: "foo_2",
  192. Effect: corev1.TaintEffectNoSchedule,
  193. },
  194. },
  195. expectedResult: "foo_2",
  196. },
  197. {
  198. name: "match two taints",
  199. taintsToCheck: []corev1.Taint{
  200. {
  201. Key: "foo_2",
  202. Effect: corev1.TaintEffectNoSchedule,
  203. },
  204. {
  205. Key: "foo_3",
  206. Effect: corev1.TaintEffectNoSchedule,
  207. },
  208. },
  209. expectedResult: "foo_2,foo_3",
  210. },
  211. }
  212. for _, c := range cases {
  213. result := checkIfTaintsAlreadyExists(oldTaints, c.taintsToCheck)
  214. if result != c.expectedResult {
  215. t.Errorf("[%s] should return '%s', but got: '%s'", c.name, c.expectedResult, result)
  216. }
  217. }
  218. }
  219. func TestReorganizeTaints(t *testing.T) {
  220. node := &corev1.Node{
  221. Spec: corev1.NodeSpec{
  222. Taints: []corev1.Taint{
  223. {
  224. Key: "foo",
  225. Value: "bar",
  226. Effect: corev1.TaintEffectNoSchedule,
  227. },
  228. },
  229. },
  230. }
  231. cases := []struct {
  232. name string
  233. overwrite bool
  234. taintsToAdd []corev1.Taint
  235. taintsToDelete []corev1.Taint
  236. expectedTaints []corev1.Taint
  237. expectedOperation string
  238. expectedErr bool
  239. }{
  240. {
  241. name: "no changes with overwrite is true",
  242. overwrite: true,
  243. taintsToAdd: []corev1.Taint{},
  244. taintsToDelete: []corev1.Taint{},
  245. expectedTaints: node.Spec.Taints,
  246. expectedOperation: MODIFIED,
  247. expectedErr: false,
  248. },
  249. {
  250. name: "no changes with overwrite is false",
  251. overwrite: false,
  252. taintsToAdd: []corev1.Taint{},
  253. taintsToDelete: []corev1.Taint{},
  254. expectedTaints: node.Spec.Taints,
  255. expectedOperation: UNTAINTED,
  256. expectedErr: false,
  257. },
  258. {
  259. name: "add new taint",
  260. overwrite: false,
  261. taintsToAdd: []corev1.Taint{
  262. {
  263. Key: "foo_1",
  264. Effect: corev1.TaintEffectNoExecute,
  265. },
  266. },
  267. taintsToDelete: []corev1.Taint{},
  268. expectedTaints: append([]corev1.Taint{{Key: "foo_1", Effect: corev1.TaintEffectNoExecute}}, node.Spec.Taints...),
  269. expectedOperation: TAINTED,
  270. expectedErr: false,
  271. },
  272. {
  273. name: "delete taint with effect",
  274. overwrite: false,
  275. taintsToAdd: []corev1.Taint{},
  276. taintsToDelete: []corev1.Taint{
  277. {
  278. Key: "foo",
  279. Effect: corev1.TaintEffectNoSchedule,
  280. },
  281. },
  282. expectedTaints: []corev1.Taint{},
  283. expectedOperation: UNTAINTED,
  284. expectedErr: false,
  285. },
  286. {
  287. name: "delete taint with no effect",
  288. overwrite: false,
  289. taintsToAdd: []corev1.Taint{},
  290. taintsToDelete: []corev1.Taint{
  291. {
  292. Key: "foo",
  293. },
  294. },
  295. expectedTaints: []corev1.Taint{},
  296. expectedOperation: UNTAINTED,
  297. expectedErr: false,
  298. },
  299. {
  300. name: "delete non-exist taint",
  301. overwrite: false,
  302. taintsToAdd: []corev1.Taint{},
  303. taintsToDelete: []corev1.Taint{
  304. {
  305. Key: "foo_1",
  306. Effect: corev1.TaintEffectNoSchedule,
  307. },
  308. },
  309. expectedTaints: node.Spec.Taints,
  310. expectedOperation: UNTAINTED,
  311. expectedErr: true,
  312. },
  313. {
  314. name: "add new taint and delete old one",
  315. overwrite: false,
  316. taintsToAdd: []corev1.Taint{
  317. {
  318. Key: "foo_1",
  319. Effect: corev1.TaintEffectNoSchedule,
  320. },
  321. },
  322. taintsToDelete: []corev1.Taint{
  323. {
  324. Key: "foo",
  325. Effect: corev1.TaintEffectNoSchedule,
  326. },
  327. },
  328. expectedTaints: []corev1.Taint{
  329. {
  330. Key: "foo_1",
  331. Effect: corev1.TaintEffectNoSchedule,
  332. },
  333. },
  334. expectedOperation: MODIFIED,
  335. expectedErr: false,
  336. },
  337. }
  338. for _, c := range cases {
  339. operation, taints, err := reorganizeTaints(node, c.overwrite, c.taintsToAdd, c.taintsToDelete)
  340. if c.expectedErr && err == nil {
  341. t.Errorf("[%s] expect to see an error, but did not get one", c.name)
  342. } else if !c.expectedErr && err != nil {
  343. t.Errorf("[%s] expect not to see an error, but got one: %v", c.name, err)
  344. }
  345. if !reflect.DeepEqual(c.expectedTaints, taints) {
  346. t.Errorf("[%s] expect to see taint list %#v, but got: %#v", c.name, c.expectedTaints, taints)
  347. }
  348. if c.expectedOperation != operation {
  349. t.Errorf("[%s] expect to see operation %s, but got: %s", c.name, c.expectedOperation, operation)
  350. }
  351. }
  352. }
  353. func TestParseTaints(t *testing.T) {
  354. cases := []struct {
  355. name string
  356. spec []string
  357. expectedTaints []corev1.Taint
  358. expectedTaintsToRemove []corev1.Taint
  359. expectedErr bool
  360. }{
  361. {
  362. name: "invalid spec format",
  363. spec: []string{""},
  364. expectedErr: true,
  365. },
  366. {
  367. name: "invalid spec format",
  368. spec: []string{"foo=abc"},
  369. expectedErr: true,
  370. },
  371. {
  372. name: "invalid spec format",
  373. spec: []string{"foo=abc=xyz:NoSchedule"},
  374. expectedErr: true,
  375. },
  376. {
  377. name: "invalid spec format",
  378. spec: []string{"foo=abc:xyz:NoSchedule"},
  379. expectedErr: true,
  380. },
  381. {
  382. name: "invalid spec format for adding taint",
  383. spec: []string{"foo"},
  384. expectedErr: true,
  385. },
  386. {
  387. name: "invalid spec effect for adding taint",
  388. spec: []string{"foo=abc:invalid_effect"},
  389. expectedErr: true,
  390. },
  391. {
  392. name: "invalid spec effect for deleting taint",
  393. spec: []string{"foo:invalid_effect-"},
  394. expectedErr: true,
  395. },
  396. {
  397. name: "add new taints",
  398. spec: []string{"foo=abc:NoSchedule", "bar=abc:NoSchedule", "baz:NoSchedule", "qux:NoSchedule", "foobar=:NoSchedule"},
  399. expectedTaints: []corev1.Taint{
  400. {
  401. Key: "foo",
  402. Value: "abc",
  403. Effect: corev1.TaintEffectNoSchedule,
  404. },
  405. {
  406. Key: "bar",
  407. Value: "abc",
  408. Effect: corev1.TaintEffectNoSchedule,
  409. },
  410. {
  411. Key: "baz",
  412. Value: "",
  413. Effect: corev1.TaintEffectNoSchedule,
  414. },
  415. {
  416. Key: "qux",
  417. Value: "",
  418. Effect: corev1.TaintEffectNoSchedule,
  419. },
  420. {
  421. Key: "foobar",
  422. Value: "",
  423. Effect: corev1.TaintEffectNoSchedule,
  424. },
  425. },
  426. expectedErr: false,
  427. },
  428. {
  429. name: "delete taints",
  430. spec: []string{"foo:NoSchedule-", "bar:NoSchedule-", "qux=:NoSchedule-", "dedicated-"},
  431. expectedTaintsToRemove: []corev1.Taint{
  432. {
  433. Key: "foo",
  434. Effect: corev1.TaintEffectNoSchedule,
  435. },
  436. {
  437. Key: "bar",
  438. Effect: corev1.TaintEffectNoSchedule,
  439. },
  440. {
  441. Key: "qux",
  442. Effect: corev1.TaintEffectNoSchedule,
  443. },
  444. {
  445. Key: "dedicated",
  446. },
  447. },
  448. expectedErr: false,
  449. },
  450. {
  451. name: "add taints and delete taints",
  452. spec: []string{"foo=abc:NoSchedule", "bar=abc:NoSchedule", "baz:NoSchedule", "qux:NoSchedule", "foobar=:NoSchedule", "foo:NoSchedule-", "bar:NoSchedule-", "baz=:NoSchedule-"},
  453. expectedTaints: []corev1.Taint{
  454. {
  455. Key: "foo",
  456. Value: "abc",
  457. Effect: corev1.TaintEffectNoSchedule,
  458. },
  459. {
  460. Key: "bar",
  461. Value: "abc",
  462. Effect: corev1.TaintEffectNoSchedule,
  463. },
  464. {
  465. Key: "baz",
  466. Value: "",
  467. Effect: corev1.TaintEffectNoSchedule,
  468. },
  469. {
  470. Key: "qux",
  471. Value: "",
  472. Effect: corev1.TaintEffectNoSchedule,
  473. },
  474. {
  475. Key: "foobar",
  476. Value: "",
  477. Effect: corev1.TaintEffectNoSchedule,
  478. },
  479. },
  480. expectedTaintsToRemove: []corev1.Taint{
  481. {
  482. Key: "foo",
  483. Effect: corev1.TaintEffectNoSchedule,
  484. },
  485. {
  486. Key: "bar",
  487. Effect: corev1.TaintEffectNoSchedule,
  488. },
  489. {
  490. Key: "baz",
  491. Value: "",
  492. Effect: corev1.TaintEffectNoSchedule,
  493. },
  494. },
  495. expectedErr: false,
  496. },
  497. }
  498. for _, c := range cases {
  499. taints, taintsToRemove, err := parseTaints(c.spec)
  500. if c.expectedErr && err == nil {
  501. t.Errorf("[%s] expected error for spec %s, but got nothing", c.name, c.spec)
  502. }
  503. if !c.expectedErr && err != nil {
  504. t.Errorf("[%s] expected no error for spec %s, but got: %v", c.name, c.spec, err)
  505. }
  506. if !reflect.DeepEqual(c.expectedTaints, taints) {
  507. t.Errorf("[%s] expected returen taints as %v, but got: %v", c.name, c.expectedTaints, taints)
  508. }
  509. if !reflect.DeepEqual(c.expectedTaintsToRemove, taintsToRemove) {
  510. t.Errorf("[%s] expected return taints to be removed as %v, but got: %v", c.name, c.expectedTaintsToRemove, taintsToRemove)
  511. }
  512. }
  513. }