taint_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. Copyright 2014 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 cmd
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "io/ioutil"
  18. "net/http"
  19. "reflect"
  20. "testing"
  21. "time"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/api/unversioned"
  24. "k8s.io/kubernetes/pkg/client/unversioned/fake"
  25. "k8s.io/kubernetes/pkg/conversion"
  26. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  27. "k8s.io/kubernetes/pkg/runtime"
  28. )
  29. func generateNodeAndTaintedNode(oldTaints []api.Taint, newTaints []api.Taint) (*api.Node, *api.Node) {
  30. var taintedNode *api.Node
  31. oldTaintsData, _ := json.Marshal(oldTaints)
  32. // Create a node.
  33. node := &api.Node{
  34. ObjectMeta: api.ObjectMeta{
  35. Name: "node-name",
  36. CreationTimestamp: unversioned.Time{Time: time.Now()},
  37. Annotations: map[string]string{
  38. api.TaintsAnnotationKey: string(oldTaintsData),
  39. },
  40. },
  41. Spec: api.NodeSpec{
  42. ExternalID: "node-name",
  43. },
  44. Status: api.NodeStatus{},
  45. }
  46. clone, _ := conversion.NewCloner().DeepCopy(node)
  47. newTaintsData, _ := json.Marshal(newTaints)
  48. // A copy of the same node, but tainted.
  49. taintedNode = clone.(*api.Node)
  50. taintedNode.Annotations = map[string]string{
  51. api.TaintsAnnotationKey: string(newTaintsData),
  52. }
  53. return node, taintedNode
  54. }
  55. func AnnotationsHaveEqualTaints(annotationA map[string]string, annotationB map[string]string) bool {
  56. taintsA, err := api.GetTaintsFromNodeAnnotations(annotationA)
  57. if err != nil {
  58. return false
  59. }
  60. taintsB, err := api.GetTaintsFromNodeAnnotations(annotationB)
  61. if err != nil {
  62. return false
  63. }
  64. if len(taintsA) != len(taintsB) {
  65. return false
  66. }
  67. for _, taintA := range taintsA {
  68. found := false
  69. for _, taintB := range taintsB {
  70. if reflect.DeepEqual(taintA, taintB) {
  71. found = true
  72. break
  73. }
  74. }
  75. if !found {
  76. return false
  77. }
  78. }
  79. return true
  80. }
  81. func TestTaint(t *testing.T) {
  82. tests := []struct {
  83. description string
  84. oldTaints []api.Taint
  85. newTaints []api.Taint
  86. args []string
  87. expectFatal bool
  88. expectTaint bool
  89. }{
  90. // success cases
  91. {
  92. description: "taints a node with effect NoSchedule",
  93. newTaints: []api.Taint{{
  94. Key: "foo",
  95. Value: "bar",
  96. Effect: "NoSchedule",
  97. }},
  98. args: []string{"node", "node-name", "foo=bar:NoSchedule"},
  99. expectFatal: false,
  100. expectTaint: true,
  101. },
  102. {
  103. description: "taints a node with effect PreferNoSchedule",
  104. newTaints: []api.Taint{{
  105. Key: "foo",
  106. Value: "bar",
  107. Effect: "PreferNoSchedule",
  108. }},
  109. args: []string{"node", "node-name", "foo=bar:PreferNoSchedule"},
  110. expectFatal: false,
  111. expectTaint: true,
  112. },
  113. {
  114. description: "update an existing taint on the node, change the value from bar to barz",
  115. oldTaints: []api.Taint{{
  116. Key: "foo",
  117. Value: "bar",
  118. Effect: "NoSchedule",
  119. }},
  120. newTaints: []api.Taint{{
  121. Key: "foo",
  122. Value: "barz",
  123. Effect: "NoSchedule",
  124. }},
  125. args: []string{"node", "node-name", "foo=barz:NoSchedule", "--overwrite"},
  126. expectFatal: false,
  127. expectTaint: true,
  128. },
  129. {
  130. description: "taints a node with two taints",
  131. newTaints: []api.Taint{{
  132. Key: "dedicated",
  133. Value: "namespaceA",
  134. Effect: "NoSchedule",
  135. }, {
  136. Key: "foo",
  137. Value: "bar",
  138. Effect: "PreferNoSchedule",
  139. }},
  140. args: []string{"node", "node-name", "dedicated=namespaceA:NoSchedule", "foo=bar:PreferNoSchedule"},
  141. expectFatal: false,
  142. expectTaint: true,
  143. },
  144. {
  145. description: "node has two taints with the same key but different effect, remove one of them by indicating exact key and effect",
  146. oldTaints: []api.Taint{{
  147. Key: "dedicated",
  148. Value: "namespaceA",
  149. Effect: "NoSchedule",
  150. }, {
  151. Key: "dedicated",
  152. Value: "namespaceA",
  153. Effect: "PreferNoSchedule",
  154. }},
  155. newTaints: []api.Taint{{
  156. Key: "dedicated",
  157. Value: "namespaceA",
  158. Effect: "PreferNoSchedule",
  159. }},
  160. args: []string{"node", "node-name", "dedicated:NoSchedule-"},
  161. expectFatal: false,
  162. expectTaint: true,
  163. },
  164. {
  165. description: "node has two taints with the same key but different effect, remove all of them with wildcard",
  166. oldTaints: []api.Taint{{
  167. Key: "dedicated",
  168. Value: "namespaceA",
  169. Effect: "NoSchedule",
  170. }, {
  171. Key: "dedicated",
  172. Value: "namespaceA",
  173. Effect: "PreferNoSchedule",
  174. }},
  175. newTaints: []api.Taint{},
  176. args: []string{"node", "node-name", "dedicated-"},
  177. expectFatal: false,
  178. expectTaint: true,
  179. },
  180. {
  181. description: "node has two taints, update one of them and remove the other",
  182. oldTaints: []api.Taint{{
  183. Key: "dedicated",
  184. Value: "namespaceA",
  185. Effect: "NoSchedule",
  186. }, {
  187. Key: "foo",
  188. Value: "bar",
  189. Effect: "PreferNoSchedule",
  190. }},
  191. newTaints: []api.Taint{{
  192. Key: "foo",
  193. Value: "barz",
  194. Effect: "PreferNoSchedule",
  195. }},
  196. args: []string{"node", "node-name", "dedicated:NoSchedule-", "foo=barz:PreferNoSchedule", "--overwrite"},
  197. expectFatal: false,
  198. expectTaint: true,
  199. },
  200. // error cases
  201. {
  202. description: "invalid taint key",
  203. args: []string{"node", "node-name", "nospecialchars^@=banana:NoSchedule"},
  204. expectFatal: true,
  205. expectTaint: false,
  206. },
  207. {
  208. description: "invalid taint effect",
  209. args: []string{"node", "node-name", "foo=bar:NoExcute"},
  210. expectFatal: true,
  211. expectTaint: false,
  212. },
  213. {
  214. description: "duplicated taints with the same key and effect should be rejected",
  215. args: []string{"node", "node-name", "foo=bar:NoExcute", "foo=barz:NoExcute"},
  216. expectFatal: true,
  217. expectTaint: false,
  218. },
  219. {
  220. description: "can't update existing taint on the node, since 'overwrite' flag is not set",
  221. oldTaints: []api.Taint{{
  222. Key: "foo",
  223. Value: "bar",
  224. Effect: "NoSchedule",
  225. }},
  226. newTaints: []api.Taint{{
  227. Key: "foo",
  228. Value: "bar",
  229. Effect: "NoSchedule",
  230. }},
  231. args: []string{"node", "node-name", "foo=bar:NoSchedule"},
  232. expectFatal: true,
  233. expectTaint: false,
  234. },
  235. }
  236. for _, test := range tests {
  237. oldNode, expectNewNode := generateNodeAndTaintedNode(test.oldTaints, test.newTaints)
  238. new_node := &api.Node{}
  239. tainted := false
  240. f, tf, codec, ns := NewAPIFactory()
  241. tf.Client = &fake.RESTClient{
  242. NegotiatedSerializer: ns,
  243. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  244. m := &MyReq{req}
  245. switch {
  246. case m.isFor("GET", "/nodes/node-name"):
  247. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, oldNode)}, nil
  248. case m.isFor("PATCH", "/nodes/node-name"), m.isFor("PUT", "/nodes/node-name"):
  249. tainted = true
  250. data, err := ioutil.ReadAll(req.Body)
  251. if err != nil {
  252. t.Fatalf("%s: unexpected error: %v", test.description, err)
  253. }
  254. defer req.Body.Close()
  255. if err := runtime.DecodeInto(codec, data, new_node); err != nil {
  256. t.Fatalf("%s: unexpected error: %v", test.description, err)
  257. }
  258. if !AnnotationsHaveEqualTaints(expectNewNode.Annotations, new_node.Annotations) {
  259. t.Fatalf("%s: expected:\n%v\nsaw:\n%v\n", test.description, expectNewNode.Annotations, new_node.Annotations)
  260. }
  261. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, new_node)}, nil
  262. default:
  263. t.Fatalf("%s: unexpected request: %v %#v\n%#v", test.description, req.Method, req.URL, req)
  264. return nil, nil
  265. }
  266. }),
  267. }
  268. tf.ClientConfig = defaultClientConfig()
  269. buf := bytes.NewBuffer([]byte{})
  270. cmd := NewCmdTaint(f, buf)
  271. saw_fatal := false
  272. func() {
  273. defer func() {
  274. // Recover from the panic below.
  275. _ = recover()
  276. // Restore cmdutil behavior
  277. cmdutil.DefaultBehaviorOnFatal()
  278. }()
  279. cmdutil.BehaviorOnFatal(func(e string, code int) { saw_fatal = true; panic(e) })
  280. cmd.SetArgs(test.args)
  281. cmd.Execute()
  282. }()
  283. if test.expectFatal {
  284. if !saw_fatal {
  285. t.Fatalf("%s: unexpected non-error", test.description)
  286. }
  287. }
  288. if test.expectTaint {
  289. if !tainted {
  290. t.Fatalf("%s: node not tainted", test.description)
  291. }
  292. }
  293. if !test.expectTaint {
  294. if tainted {
  295. t.Fatalf("%s: unexpected taint", test.description)
  296. }
  297. }
  298. }
  299. }