generic-resources.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #!/usr/bin/env bash
  2. # Copyright 2018 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. run_multi_resources_tests() {
  19. set -o nounset
  20. set -o errexit
  21. create_and_use_new_namespace
  22. kube::log::status "Testing kubectl(v1:multiple resources)"
  23. FILES="hack/testdata/multi-resource-yaml
  24. hack/testdata/multi-resource-list
  25. hack/testdata/multi-resource-json
  26. hack/testdata/multi-resource-rclist
  27. hack/testdata/multi-resource-svclist"
  28. YAML=".yaml"
  29. JSON=".json"
  30. for file in $FILES; do
  31. if [ -f "${file}${YAML}" ]
  32. then
  33. file=${file}${YAML}
  34. replace_file="${file%.yaml}-modify.yaml"
  35. else
  36. file=${file}${JSON}
  37. replace_file="${file%.json}-modify.json"
  38. fi
  39. has_svc=true
  40. has_rc=true
  41. two_rcs=false
  42. two_svcs=false
  43. if [[ "${file}" == *rclist* ]]; then
  44. has_svc=false
  45. two_rcs=true
  46. fi
  47. if [[ "${file}" == *svclist* ]]; then
  48. has_rc=false
  49. two_svcs=true
  50. fi
  51. ### Create, get, describe, replace, label, annotate, and then delete service nginxsvc and replication controller my-nginx from 5 types of files:
  52. ### 1) YAML, separated by ---; 2) JSON, with a List type; 3) JSON, with JSON object concatenation
  53. ### 4) JSON, with a ReplicationControllerList type; 5) JSON, with a ServiceList type
  54. echo "Testing with file ${file} and replace with file ${replace_file}"
  55. # Pre-condition: no service (other than default kubernetes services) or replication controller exists
  56. kube::test::get_object_assert services "{{range.items}}{{${id_field:?}}}:{{end}}" ''
  57. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  58. # Command
  59. kubectl create -f "${file}" "${kube_flags[@]:?}"
  60. # Post-condition: mock service (and mock2) exists
  61. if [ "$has_svc" = true ]; then
  62. if [ "$two_svcs" = true ]; then
  63. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" 'mock:mock2:'
  64. else
  65. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" 'mock:'
  66. fi
  67. fi
  68. # Post-condition: mock rc (and mock2) exists
  69. if [ "$has_rc" = true ]; then
  70. if [ "$two_rcs" = true ]; then
  71. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'mock:mock2:'
  72. else
  73. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'mock:'
  74. fi
  75. fi
  76. # Command
  77. kubectl get -f "${file}" "${kube_flags[@]}"
  78. # Command: watching multiple resources should return "not supported" error
  79. WATCH_ERROR_FILE="${KUBE_TEMP}/kubectl-watch-error"
  80. kubectl get -f "${file}" "${kube_flags[@]}" "--watch" 2> "${WATCH_ERROR_FILE}" || true
  81. if ! grep -q "watch is only supported on individual resources and resource collections" "${WATCH_ERROR_FILE}"; then
  82. kube::log::error_exit "kubectl watch multiple resource returns unexpected error or non-error: $(cat "${WATCH_ERROR_FILE}")" "1"
  83. fi
  84. kubectl describe -f "${file}" "${kube_flags[@]}"
  85. # Command
  86. kubectl replace -f "${replace_file}" --force --cascade "${kube_flags[@]}"
  87. # Post-condition: mock service (and mock2) and mock rc (and mock2) are replaced
  88. if [ "$has_svc" = true ]; then
  89. kube::test::get_object_assert 'services mock' "{{${labels_field:?}.status}}" 'replaced'
  90. if [ "$two_svcs" = true ]; then
  91. kube::test::get_object_assert 'services mock2' "{{${labels_field}.status}}" 'replaced'
  92. fi
  93. fi
  94. if [ "$has_rc" = true ]; then
  95. kube::test::get_object_assert 'rc mock' "{{${labels_field}.status}}" 'replaced'
  96. if [ "$two_rcs" = true ]; then
  97. kube::test::get_object_assert 'rc mock2' "{{${labels_field}.status}}" 'replaced'
  98. fi
  99. fi
  100. # Command: kubectl edit multiple resources
  101. temp_editor="${KUBE_TEMP}/tmp-editor.sh"
  102. echo -e "#!/usr/bin/env bash\n${SED} -i \"s/status\:\ replaced/status\:\ edited/g\" \$@" > "${temp_editor}"
  103. chmod +x "${temp_editor}"
  104. EDITOR="${temp_editor}" kubectl edit "${kube_flags[@]}" -f "${file}"
  105. # Post-condition: mock service (and mock2) and mock rc (and mock2) are edited
  106. if [ "$has_svc" = true ]; then
  107. kube::test::get_object_assert 'services mock' "{{${labels_field}.status}}" 'edited'
  108. if [ "$two_svcs" = true ]; then
  109. kube::test::get_object_assert 'services mock2' "{{${labels_field}.status}}" 'edited'
  110. fi
  111. fi
  112. if [ "$has_rc" = true ]; then
  113. kube::test::get_object_assert 'rc mock' "{{${labels_field}.status}}" 'edited'
  114. if [ "$two_rcs" = true ]; then
  115. kube::test::get_object_assert 'rc mock2' "{{${labels_field}.status}}" 'edited'
  116. fi
  117. fi
  118. # cleaning
  119. rm "${temp_editor}"
  120. # Command
  121. # We need to set --overwrite, because otherwise, if the first attempt to run "kubectl label"
  122. # fails on some, but not all, of the resources, retries will fail because it tries to modify
  123. # existing labels.
  124. kubectl-with-retry label -f "${file}" labeled=true --overwrite "${kube_flags[@]}"
  125. # Post-condition: mock service and mock rc (and mock2) are labeled
  126. if [ "$has_svc" = true ]; then
  127. kube::test::get_object_assert 'services mock' "{{${labels_field}.labeled}}" 'true'
  128. if [ "$two_svcs" = true ]; then
  129. kube::test::get_object_assert 'services mock2' "{{${labels_field}.labeled}}" 'true'
  130. fi
  131. fi
  132. if [ "$has_rc" = true ]; then
  133. kube::test::get_object_assert 'rc mock' "{{${labels_field}.labeled}}" 'true'
  134. if [ "$two_rcs" = true ]; then
  135. kube::test::get_object_assert 'rc mock2' "{{${labels_field}.labeled}}" 'true'
  136. fi
  137. fi
  138. # Command
  139. # Command
  140. # We need to set --overwrite, because otherwise, if the first attempt to run "kubectl annotate"
  141. # fails on some, but not all, of the resources, retries will fail because it tries to modify
  142. # existing annotations.
  143. kubectl-with-retry annotate -f "${file}" annotated=true --overwrite "${kube_flags[@]}"
  144. # Post-condition: mock service (and mock2) and mock rc (and mock2) are annotated
  145. if [ "$has_svc" = true ]; then
  146. kube::test::get_object_assert 'services mock' "{{${annotations_field:?}.annotated}}" 'true'
  147. if [ "$two_svcs" = true ]; then
  148. kube::test::get_object_assert 'services mock2' "{{${annotations_field}.annotated}}" 'true'
  149. fi
  150. fi
  151. if [ "$has_rc" = true ]; then
  152. kube::test::get_object_assert 'rc mock' "{{${annotations_field}.annotated}}" 'true'
  153. if [ "$two_rcs" = true ]; then
  154. kube::test::get_object_assert 'rc mock2' "{{${annotations_field}.annotated}}" 'true'
  155. fi
  156. fi
  157. # Cleanup resources created
  158. kubectl delete -f "${file}" "${kube_flags[@]}"
  159. done
  160. #############################
  161. # Multiple Resources via URL#
  162. #############################
  163. # Pre-condition: no service (other than default kubernetes services) or replication controller exists
  164. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
  165. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  166. # Command
  167. kubectl create -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/hack/testdata/multi-resource-yaml.yaml "${kube_flags[@]}"
  168. # Post-condition: service(mock) and rc(mock) exist
  169. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" 'mock:'
  170. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'mock:'
  171. # Clean up
  172. kubectl delete -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/hack/testdata/multi-resource-yaml.yaml "${kube_flags[@]}"
  173. # Post-condition: no service (other than default kubernetes services) or replication controller exists
  174. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
  175. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  176. set +o nounset
  177. set +o errexit
  178. }
  179. run_recursive_resources_tests() {
  180. set -o nounset
  181. set -o errexit
  182. kube::log::status "Testing recursive resources"
  183. ### Create multiple busybox PODs recursively from directory of YAML files
  184. # Pre-condition: no POD exists
  185. create_and_use_new_namespace
  186. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  187. # Command
  188. output_message=$(! kubectl create -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}")
  189. # Post-condition: busybox0 & busybox1 PODs are created, and since busybox2 is malformed, it should error
  190. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  191. kube::test::if_has_string "${output_message}" 'error validating data: kind not set'
  192. ## Edit multiple busybox PODs by updating the image field of multiple PODs recursively from a directory. tmp-editor.sh is a fake editor
  193. # Pre-condition: busybox0 & busybox1 PODs exist
  194. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  195. # Command
  196. # shellcheck disable=SC2016 # $1 here is not a Expressions
  197. echo -e '#!/usr/bin/env bash\nsed -i "s/image: busybox/image: prom\/busybox/g" $1' > /tmp/tmp-editor.sh
  198. chmod +x /tmp/tmp-editor.sh
  199. output_message=$(! EDITOR=/tmp/tmp-editor.sh kubectl edit -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}")
  200. # Post-condition: busybox0 & busybox1 PODs are not edited, and since busybox2 is malformed, it should error
  201. # The reason why busybox0 & busybox1 PODs are not edited is because the editor tries to load all objects in
  202. # a list but since it contains invalid objects, it will never open.
  203. kube::test::get_object_assert pods "{{range.items}}{{${image_field:?}}}:{{end}}" 'busybox:busybox:'
  204. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  205. # cleaning
  206. rm /tmp/tmp-editor.sh
  207. ## Replace multiple busybox PODs recursively from directory of YAML files
  208. # Pre-condition: busybox0 & busybox1 PODs exist
  209. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  210. # Command
  211. output_message=$(! kubectl replace -f hack/testdata/recursive/pod-modify --recursive 2>&1 "${kube_flags[@]}")
  212. # Post-condition: busybox0 & busybox1 PODs are replaced, and since busybox2 is malformed, it should error
  213. kube::test::get_object_assert pods "{{range.items}}{{${labels_field}.status}}:{{end}}" 'replaced:replaced:'
  214. kube::test::if_has_string "${output_message}" 'error validating data: kind not set'
  215. ## Describe multiple busybox PODs recursively from directory of YAML files
  216. # Pre-condition: busybox0 & busybox1 PODs exist
  217. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  218. # Command
  219. output_message=$(! kubectl describe -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}")
  220. # Post-condition: busybox0 & busybox1 PODs are described, and since busybox2 is malformed, it should error
  221. kube::test::if_has_string "${output_message}" "app=busybox0"
  222. kube::test::if_has_string "${output_message}" "app=busybox1"
  223. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  224. ## Annotate multiple busybox PODs recursively from directory of YAML files
  225. # Pre-condition: busybox0 & busybox1 PODs exist
  226. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  227. # Command
  228. output_message=$(! kubectl annotate -f hack/testdata/recursive/pod annotatekey='annotatevalue' --recursive 2>&1 "${kube_flags[@]}")
  229. # Post-condition: busybox0 & busybox1 PODs are annotated, and since busybox2 is malformed, it should error
  230. kube::test::get_object_assert pods "{{range.items}}{{${annotations_field}.annotatekey}}:{{end}}" 'annotatevalue:annotatevalue:'
  231. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  232. ## Apply multiple busybox PODs recursively from directory of YAML files
  233. # Pre-condition: busybox0 & busybox1 PODs exist
  234. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  235. # Command
  236. output_message=$(! kubectl apply -f hack/testdata/recursive/pod-modify --recursive 2>&1 "${kube_flags[@]}")
  237. # Post-condition: busybox0 & busybox1 PODs are updated, and since busybox2 is malformed, it should error
  238. kube::test::get_object_assert pods "{{range.items}}{{${labels_field}.status}}:{{end}}" 'replaced:replaced:'
  239. kube::test::if_has_string "${output_message}" 'error validating data: kind not set'
  240. ### Convert deployment YAML file locally without affecting the live deployment.
  241. # Pre-condition: no deployments exist
  242. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
  243. # Command
  244. # Create a deployment (revision 1)
  245. kubectl create -f hack/testdata/deployment-revision1.yaml "${kube_flags[@]}"
  246. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" 'nginx:'
  247. kube::test::get_object_assert deployment "{{range.items}}{{${image_field0:?}}}:{{end}}" "${IMAGE_DEPLOYMENT_R1}:"
  248. # Command
  249. output_message=$(kubectl convert --local -f hack/testdata/deployment-revision1.yaml --output-version=extensions/v1beta1 -o yaml "${kube_flags[@]}")
  250. # Post-condition: apiVersion is still apps/v1 in the live deployment, but command output is the new value
  251. kube::test::get_object_assert 'deployment nginx' "{{ .apiVersion }}" 'apps/v1'
  252. kube::test::if_has_string "${output_message}" "extensions/v1beta1"
  253. # Clean up
  254. kubectl delete deployment nginx "${kube_flags[@]}"
  255. ## Convert multiple busybox PODs recursively from directory of YAML files
  256. # Pre-condition: only busybox0 & busybox1 PODs exist
  257. kube::test::wait_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  258. # Command
  259. output_message=$(! kubectl convert -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}")
  260. # Post-condition: busybox0 & busybox1 PODs are converted, and since busybox2 is malformed, it should error
  261. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  262. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  263. ## Get multiple busybox PODs recursively from directory of YAML files
  264. # Pre-condition: busybox0 & busybox1 PODs exist
  265. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  266. # Command
  267. output_message=$(! kubectl get -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}" -o go-template="{{range.items}}{{$id_field}}:{{end}}")
  268. # Post-condition: busybox0 & busybox1 PODs are retrieved, but because busybox2 is malformed, it should not show up
  269. kube::test::if_has_string "${output_message}" "busybox0:busybox1:"
  270. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  271. ## Label multiple busybox PODs recursively from directory of YAML files
  272. # Pre-condition: busybox0 & busybox1 PODs exist
  273. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  274. # Command
  275. output_message=$(! kubectl label -f hack/testdata/recursive/pod mylabel='myvalue' --recursive 2>&1 "${kube_flags[@]}")
  276. echo "${output_message}"
  277. # Post-condition: busybox0 & busybox1 PODs are labeled, but because busybox2 is malformed, it should not show up
  278. kube::test::get_object_assert pods "{{range.items}}{{${labels_field}.mylabel}}:{{end}}" 'myvalue:myvalue:'
  279. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  280. ## Patch multiple busybox PODs recursively from directory of YAML files
  281. # Pre-condition: busybox0 & busybox1 PODs exist
  282. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  283. # Command
  284. output_message=$(! kubectl patch -f hack/testdata/recursive/pod -p='{"spec":{"containers":[{"name":"busybox","image":"prom/busybox"}]}}' --recursive 2>&1 "${kube_flags[@]}")
  285. echo "${output_message}"
  286. # Post-condition: busybox0 & busybox1 PODs are patched, but because busybox2 is malformed, it should not show up
  287. kube::test::get_object_assert pods "{{range.items}}{{$image_field}}:{{end}}" 'prom/busybox:prom/busybox:'
  288. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  289. ### Delete multiple busybox PODs recursively from directory of YAML files
  290. # Pre-condition: busybox0 & busybox1 PODs exist
  291. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  292. # Command
  293. output_message=$(! kubectl delete -f hack/testdata/recursive/pod --recursive --grace-period=0 --force 2>&1 "${kube_flags[@]}")
  294. # Post-condition: busybox0 & busybox1 PODs are deleted, and since busybox2 is malformed, it should error
  295. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  296. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  297. ### Create replication controller recursively from directory of YAML files
  298. # Pre-condition: no replication controller exists
  299. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  300. # Command
  301. ! kubectl create -f hack/testdata/recursive/rc --recursive "${kube_flags[@]}" || exit 1
  302. # Post-condition: frontend replication controller is created
  303. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  304. ### Autoscale multiple replication controllers recursively from directory of YAML files
  305. # Pre-condition: busybox0 & busybox1 replication controllers exist & 1
  306. # replica each
  307. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  308. kube::test::get_object_assert 'rc busybox0' "{{${rc_replicas_field:?}}}" '1'
  309. kube::test::get_object_assert 'rc busybox1' "{{$rc_replicas_field}}" '1'
  310. # Command
  311. output_message=$(! kubectl autoscale --min=1 --max=2 -f hack/testdata/recursive/rc --recursive 2>&1 "${kube_flags[@]}")
  312. # Post-condition: busybox0 & busybox replication controllers are autoscaled
  313. # with min. of 1 replica & max of 2 replicas, and since busybox2 is malformed, it should error
  314. kube::test::get_object_assert 'hpa busybox0' "{{${hpa_min_field:?}}} {{${hpa_max_field:?}}} {{${hpa_cpu_field:?}}}" '1 2 80'
  315. kube::test::get_object_assert 'hpa busybox1' "{{$hpa_min_field}} {{$hpa_max_field}} {{$hpa_cpu_field}}" '1 2 80'
  316. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  317. kubectl delete hpa busybox0 "${kube_flags[@]}"
  318. kubectl delete hpa busybox1 "${kube_flags[@]}"
  319. ### Expose multiple replication controllers as service recursively from directory of YAML files
  320. # Pre-condition: busybox0 & busybox1 replication controllers exist & 1
  321. # replica each
  322. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  323. kube::test::get_object_assert 'rc busybox0' "{{$rc_replicas_field}}" '1'
  324. kube::test::get_object_assert 'rc busybox1' "{{$rc_replicas_field}}" '1'
  325. # Command
  326. output_message=$(! kubectl expose -f hack/testdata/recursive/rc --recursive --port=80 2>&1 "${kube_flags[@]}")
  327. # Post-condition: service exists and the port is unnamed
  328. kube::test::get_object_assert 'service busybox0' "{{${port_name:?}}} {{${port_field:?}}}" '<no value> 80'
  329. kube::test::get_object_assert 'service busybox1' "{{$port_name}} {{$port_field}}" '<no value> 80'
  330. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  331. ### Scale multiple replication controllers recursively from directory of YAML files
  332. # Pre-condition: busybox0 & busybox1 replication controllers exist & 1
  333. # replica each
  334. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  335. kube::test::get_object_assert 'rc busybox0' "{{$rc_replicas_field}}" '1'
  336. kube::test::get_object_assert 'rc busybox1' "{{$rc_replicas_field}}" '1'
  337. # Command
  338. output_message=$(! kubectl scale --current-replicas=1 --replicas=2 -f hack/testdata/recursive/rc --recursive 2>&1 "${kube_flags[@]}")
  339. # Post-condition: busybox0 & busybox1 replication controllers are scaled to 2 replicas, and since busybox2 is malformed, it should error
  340. kube::test::get_object_assert 'rc busybox0' "{{$rc_replicas_field}}" '2'
  341. kube::test::get_object_assert 'rc busybox1' "{{$rc_replicas_field}}" '2'
  342. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  343. ### Delete multiple busybox replication controllers recursively from directory of YAML files
  344. # Pre-condition: busybox0 & busybox1 PODs exist
  345. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  346. # Command
  347. output_message=$(! kubectl delete -f hack/testdata/recursive/rc --recursive --grace-period=0 --force 2>&1 "${kube_flags[@]}")
  348. # Post-condition: busybox0 & busybox1 replication controllers are deleted, and since busybox2 is malformed, it should error
  349. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  350. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  351. ### Rollout on multiple deployments recursively
  352. # Pre-condition: no deployments exist
  353. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
  354. # Command
  355. # Create deployments (revision 1) recursively from directory of YAML files
  356. ! kubectl create -f hack/testdata/recursive/deployment --recursive "${kube_flags[@]}" || exit 1
  357. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" 'nginx0-deployment:nginx1-deployment:'
  358. kube::test::get_object_assert deployment "{{range.items}}{{$image_field0}}:{{end}}" "${IMAGE_NGINX}:${IMAGE_NGINX}:"
  359. ## Rollback the deployments to revision 1 recursively
  360. output_message=$(! kubectl rollout undo -f hack/testdata/recursive/deployment --recursive --to-revision=1 2>&1 "${kube_flags[@]}")
  361. # Post-condition: nginx0 & nginx1 should be a no-op, and since nginx2 is malformed, it should error
  362. kube::test::get_object_assert deployment "{{range.items}}{{$image_field0}}:{{end}}" "${IMAGE_NGINX}:${IMAGE_NGINX}:"
  363. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  364. ## Pause the deployments recursively
  365. # shellcheck disable=SC2034 # PRESERVE_ERR_FILE is used in kubectl-with-retry
  366. PRESERVE_ERR_FILE=true
  367. kubectl-with-retry rollout pause -f hack/testdata/recursive/deployment --recursive "${kube_flags[@]}"
  368. output_message=$(cat "${ERROR_FILE}")
  369. # Post-condition: nginx0 & nginx1 should both have paused set to true, and since nginx2 is malformed, it should error
  370. kube::test::get_object_assert deployment "{{range.items}}{{.spec.paused}}:{{end}}" "true:true:"
  371. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  372. ## Resume the deployments recursively
  373. kubectl-with-retry rollout resume -f hack/testdata/recursive/deployment --recursive "${kube_flags[@]}"
  374. output_message=$(cat "${ERROR_FILE}")
  375. # Post-condition: nginx0 & nginx1 should both have paused set to nothing, and since nginx2 is malformed, it should error
  376. kube::test::get_object_assert deployment "{{range.items}}{{.spec.paused}}:{{end}}" "<no value>:<no value>:"
  377. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  378. ## Retrieve the rollout history of the deployments recursively
  379. output_message=$(! kubectl rollout history -f hack/testdata/recursive/deployment --recursive 2>&1 "${kube_flags[@]}")
  380. # Post-condition: nginx0 & nginx1 should both have a history, and since nginx2 is malformed, it should error
  381. kube::test::if_has_string "${output_message}" "nginx0-deployment"
  382. kube::test::if_has_string "${output_message}" "nginx1-deployment"
  383. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  384. # Clean up
  385. unset PRESERVE_ERR_FILE
  386. rm "${ERROR_FILE}"
  387. ! kubectl delete -f hack/testdata/recursive/deployment --recursive "${kube_flags[@]}" --grace-period=0 --force || exit 1
  388. sleep 1
  389. ### Rollout on multiple replication controllers recursively - these tests ensure that rollouts cannot be performed on resources that don't support it
  390. # Pre-condition: no replication controller exists
  391. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  392. # Command
  393. # Create replication controllers recursively from directory of YAML files
  394. ! kubectl create -f hack/testdata/recursive/rc --recursive "${kube_flags[@]}" || exit 1
  395. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
  396. # Command
  397. ## Attempt to rollback the replication controllers to revision 1 recursively
  398. output_message=$(! kubectl rollout undo -f hack/testdata/recursive/rc --recursive --to-revision=1 2>&1 "${kube_flags[@]}")
  399. # Post-condition: busybox0 & busybox1 should error as they are RC's, and since busybox2 is malformed, it should error
  400. kube::test::if_has_string "${output_message}" 'no rollbacker has been implemented for "ReplicationController"'
  401. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  402. ## Attempt to pause the replication controllers recursively
  403. output_message=$(! kubectl rollout pause -f hack/testdata/recursive/rc --recursive 2>&1 "${kube_flags[@]}")
  404. # Post-condition: busybox0 & busybox1 should error as they are RC's, and since busybox2 is malformed, it should error
  405. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  406. kube::test::if_has_string "${output_message}" 'replicationcontrollers "busybox0" pausing is not supported'
  407. kube::test::if_has_string "${output_message}" 'replicationcontrollers "busybox1" pausing is not supported'
  408. ## Attempt to resume the replication controllers recursively
  409. output_message=$(! kubectl rollout resume -f hack/testdata/recursive/rc --recursive 2>&1 "${kube_flags[@]}")
  410. # Post-condition: busybox0 & busybox1 should error as they are RC's, and since busybox2 is malformed, it should error
  411. kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
  412. kube::test::if_has_string "${output_message}" 'replicationcontrollers "busybox0" resuming is not supported'
  413. kube::test::if_has_string "${output_message}" 'replicationcontrollers "busybox1" resuming is not supported'
  414. # Clean up
  415. ! kubectl delete -f hack/testdata/recursive/rc --recursive "${kube_flags[@]}" --grace-period=0 --force || exit 1
  416. sleep 1
  417. set +o nounset
  418. set +o errexit
  419. }
  420. run_lists_tests() {
  421. set -o nounset
  422. set -o errexit
  423. create_and_use_new_namespace
  424. kube::log::status "Testing kubectl(v1:lists)"
  425. ### Create a List with objects from multiple versions
  426. # Command
  427. kubectl create -f hack/testdata/list.yaml "${kube_flags[@]}"
  428. ### Delete the List with objects from multiple versions
  429. # Command
  430. kubectl delete service/list-service-test deployment/list-deployment-test
  431. set +o nounset
  432. set +o errexit
  433. }