get.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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_kubectl_get_tests() {
  19. set -o nounset
  20. set -o errexit
  21. create_and_use_new_namespace
  22. kube::log::status "Testing kubectl get"
  23. ### Test retrieval of non-existing pods
  24. # Pre-condition: no POD exists
  25. kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
  26. # Command
  27. output_message=$(! kubectl get pods abc 2>&1 "${kube_flags[@]:?}")
  28. # Post-condition: POD abc should error since it doesn't exist
  29. kube::test::if_has_string "${output_message}" 'pods "abc" not found'
  30. ### Test retrieval of non-existing POD with output flag specified
  31. # Pre-condition: no POD exists
  32. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  33. # Command
  34. output_message=$(! kubectl get pods abc 2>&1 "${kube_flags[@]}" -o name)
  35. # Post-condition: POD abc should error since it doesn't exist
  36. kube::test::if_has_string "${output_message}" 'pods "abc" not found'
  37. ### Test retrieval of pods when none exist with non-human readable output format flag specified
  38. # Pre-condition: no pods exist
  39. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  40. # Command
  41. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o json)
  42. # Post-condition: The text "No resources found" should not be part of the output
  43. kube::test::if_has_not_string "${output_message}" 'No resources found'
  44. # Command
  45. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o yaml)
  46. # Post-condition: The text "No resources found" should not be part of the output
  47. kube::test::if_has_not_string "${output_message}" 'No resources found'
  48. # Command
  49. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o name)
  50. # Post-condition: The text "No resources found" should not be part of the output
  51. kube::test::if_has_not_string "${output_message}" 'No resources found'
  52. # Command
  53. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o jsonpath='{.items}')
  54. # Post-condition: The text "No resources found" should not be part of the output
  55. kube::test::if_has_not_string "${output_message}" 'No resources found'
  56. # Command
  57. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o go-template='{{.items}}')
  58. # Post-condition: The text "No resources found" should not be part of the output
  59. kube::test::if_has_not_string "${output_message}" 'No resources found'
  60. # Command
  61. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o custom-columns=NAME:.metadata.name)
  62. # Post-condition: The text "No resources found" should not be part of the output
  63. kube::test::if_has_not_string "${output_message}" 'No resources found'
  64. ### Test retrieval of pods when none exist, with human-readable output format flag specified
  65. # Pre-condition: no pods exist
  66. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  67. # Command
  68. output_message=$(! kubectl get foobar 2>&1 "${kube_flags[@]}")
  69. # Post-condition: The text "No resources found" should not be part of the output when an error occurs
  70. kube::test::if_has_not_string "${output_message}" 'No resources found'
  71. # Command
  72. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}")
  73. # Post-condition: The text "No resources found" should be part of the output
  74. kube::test::if_has_string "${output_message}" 'No resources found'
  75. # Command
  76. output_message=$(kubectl get pods --ignore-not-found 2>&1 "${kube_flags[@]}")
  77. # Post-condition: The text "No resources found" should not be part of the output
  78. kube::test::if_has_not_string "${output_message}" 'No resources found'
  79. # Command
  80. output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o wide)
  81. # Post-condition: The text "No resources found" should be part of the output
  82. kube::test::if_has_string "${output_message}" 'No resources found'
  83. ### Test retrieval of non-existing POD with json output flag specified
  84. # Pre-condition: no POD exists
  85. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  86. # Command
  87. output_message=$(! kubectl get pods abc 2>&1 "${kube_flags[@]}" -o json)
  88. # Post-condition: POD abc should error since it doesn't exist
  89. kube::test::if_has_string "${output_message}" 'pods "abc" not found'
  90. # Post-condition: make sure we don't display an empty List
  91. kube::test::if_has_not_string "${output_message}" 'List'
  92. ### Test kubectl get all
  93. output_message=$(kubectl --v=6 --namespace default get all --chunk-size=0 2>&1 "${kube_flags[@]}")
  94. # Post-condition: Check if we get 200 OK from all the url(s)
  95. kube::test::if_has_string "${output_message}" "/api/v1/namespaces/default/pods 200 OK"
  96. kube::test::if_has_string "${output_message}" "/api/v1/namespaces/default/replicationcontrollers 200 OK"
  97. kube::test::if_has_string "${output_message}" "/api/v1/namespaces/default/services 200 OK"
  98. kube::test::if_has_string "${output_message}" "/apis/apps/v1/namespaces/default/daemonsets 200 OK"
  99. kube::test::if_has_string "${output_message}" "/apis/apps/v1/namespaces/default/deployments 200 OK"
  100. kube::test::if_has_string "${output_message}" "/apis/apps/v1/namespaces/default/replicasets 200 OK"
  101. kube::test::if_has_string "${output_message}" "/apis/apps/v1/namespaces/default/statefulsets 200 OK"
  102. kube::test::if_has_string "${output_message}" "/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers 200"
  103. kube::test::if_has_string "${output_message}" "/apis/batch/v1/namespaces/default/jobs 200 OK"
  104. kube::test::if_has_not_string "${output_message}" "/apis/extensions/v1beta1/namespaces/default/daemonsets 200 OK"
  105. kube::test::if_has_not_string "${output_message}" "/apis/extensions/v1beta1/namespaces/default/deployments 200 OK"
  106. kube::test::if_has_not_string "${output_message}" "/apis/extensions/v1beta1/namespaces/default/replicasets 200 OK"
  107. ### Test kubectl get chunk size
  108. output_message=$(kubectl --v=6 get clusterrole --chunk-size=10 2>&1 "${kube_flags[@]}")
  109. # Post-condition: Check if we get a limit and continue
  110. kube::test::if_has_string "${output_message}" "/clusterroles?limit=10 200 OK"
  111. kube::test::if_has_string "${output_message}" "/v1/clusterroles?continue="
  112. ### Test kubectl get chunk size defaults to 500
  113. output_message=$(kubectl --v=6 get clusterrole 2>&1 "${kube_flags[@]}")
  114. # Post-condition: Check if we get a limit and continue
  115. kube::test::if_has_string "${output_message}" "/clusterroles?limit=500 200 OK"
  116. ### Test kubectl get accumulates pages
  117. output_message=$(kubectl get namespaces --chunk-size=1 --no-headers "${kube_flags[@]}")
  118. # Post-condition: Check we got multiple pages worth of namespaces
  119. kube::test::if_has_string "${output_message}" "default"
  120. kube::test::if_has_string "${output_message}" "kube-public"
  121. kube::test::if_has_string "${output_message}" "kube-system"
  122. ### Test kubectl get chunk size does not result in a --watch error when resource list is served in multiple chunks
  123. # Pre-condition: ConfigMap one two tree does not exist
  124. kube::test::get_object_assert 'configmaps' "{{range.items}}{{ if eq $id_field \\\"one\\\" }}found{{end}}{{end}}:" ':'
  125. kube::test::get_object_assert 'configmaps' "{{range.items}}{{ if eq $id_field \\\"two\\\" }}found{{end}}{{end}}:" ':'
  126. kube::test::get_object_assert 'configmaps' "{{range.items}}{{ if eq $id_field \\\"three\\\" }}found{{end}}{{end}}:" ':'
  127. # Post-condition: Create three configmaps and ensure that we can --watch them with a --chunk-size of 1
  128. kubectl create cm one "${kube_flags[@]}"
  129. kubectl create cm two "${kube_flags[@]}"
  130. kubectl create cm three "${kube_flags[@]}"
  131. output_message=$(kubectl get configmap --chunk-size=1 --watch --request-timeout=1s 2>&1 "${kube_flags[@]}")
  132. kube::test::if_has_not_string "${output_message}" "watch is only supported on individual resources"
  133. output_message=$(kubectl get configmap --chunk-size=1 --watch-only --request-timeout=1s 2>&1 "${kube_flags[@]}")
  134. kube::test::if_has_not_string "${output_message}" "watch is only supported on individual resources"
  135. ### Test --allow-missing-template-keys
  136. # Pre-condition: no POD exists
  137. create_and_use_new_namespace
  138. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  139. # Command
  140. kubectl create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml "${kube_flags[@]}"
  141. # Post-condition: valid-pod POD is created
  142. kubectl get "${kube_flags[@]}" pods -o json
  143. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
  144. ## check --allow-missing-template-keys defaults to true for jsonpath templates
  145. kubectl get "${kube_flags[@]}" pod valid-pod -o jsonpath='{.missing}'
  146. ## check --allow-missing-template-keys defaults to true for go templates
  147. kubectl get "${kube_flags[@]}" pod valid-pod -o go-template='{{.missing}}'
  148. ## check --template flag causes go-template to be printed, even when no --output value is provided
  149. output_message=$(kubectl get "${kube_flags[@]}" pod valid-pod --template="{{$id_field}}:")
  150. kube::test::if_has_string "${output_message}" 'valid-pod:'
  151. ## check --allow-missing-template-keys=false results in an error for a missing key with jsonpath
  152. output_message=$(! kubectl get pod valid-pod --allow-missing-template-keys=false -o jsonpath='{.missing}' 2>&1 "${kube_flags[@]}")
  153. kube::test::if_has_string "${output_message}" 'missing is not found'
  154. ## check --allow-missing-template-keys=false results in an error for a missing key with go
  155. output_message=$(! kubectl get pod valid-pod --allow-missing-template-keys=false -o go-template='{{.missing}}' "${kube_flags[@]}")
  156. kube::test::if_has_string "${output_message}" 'map has no entry for key "missing"'
  157. ### Test kubectl get watch
  158. output_message=$(kubectl get pods -w --request-timeout=1 "${kube_flags[@]}")
  159. kube::test::if_has_string "${output_message}" 'STATUS' # headers
  160. kube::test::if_has_string "${output_message}" 'valid-pod' # pod details
  161. output_message=$(kubectl get pods/valid-pod -o name -w --request-timeout=1 "${kube_flags[@]}")
  162. kube::test::if_has_not_string "${output_message}" 'STATUS' # no headers
  163. kube::test::if_has_string "${output_message}" 'pod/valid-pod' # resource name
  164. output_message=$(kubectl get pods/valid-pod -o yaml -w --request-timeout=1 "${kube_flags[@]}")
  165. kube::test::if_has_not_string "${output_message}" 'STATUS' # no headers
  166. kube::test::if_has_string "${output_message}" 'name: valid-pod' # yaml
  167. output_message=$(! kubectl get pods/invalid-pod -w --request-timeout=1 "${kube_flags[@]}" 2>&1)
  168. kube::test::if_has_string "${output_message}" '"invalid-pod" not found'
  169. # cleanup
  170. kubectl delete pods valid-pod "${kube_flags[@]}"
  171. ### Test 'kubectl get -f <file> -o <non default printer>' prints all the items in the file's list
  172. # Pre-condition: no POD exists
  173. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  174. # Command
  175. kubectl create -f test/fixtures/doc-yaml/user-guide/multi-pod.yaml "${kube_flags[@]}"
  176. # Post-condition: PODs redis-master and valid-pod exist
  177. # Check that all items in the list are printed
  178. output_message=$(kubectl get -f test/fixtures/doc-yaml/user-guide/multi-pod.yaml -o jsonpath="{..metadata.name}" "${kube_flags[@]}")
  179. kube::test::if_has_string "${output_message}" "redis-master valid-pod"
  180. # cleanup
  181. kubectl delete pods redis-master valid-pod "${kube_flags[@]}"
  182. ### Test 'kubectl get -k <dir>' prints all the items built from a kustomization directory
  183. # Pre-condition: no ConfigMap, Deployment, Service exist
  184. kube::test::get_object_assert configmaps "{{range.items}}{{$id_field}}:{{end}}" ''
  185. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
  186. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
  187. # Command
  188. kubectl apply -k hack/testdata/kustomize
  189. # Post-condition: test-the-map, test-the-deployment, test-the-service exist
  190. # Check that all items in the list are printed
  191. output_message=$(kubectl get -k hack/testdata/kustomize -o jsonpath="{..metadata.name}" "${kube_flags[@]}")
  192. kube::test::if_has_string "${output_message}" "test-the-map"
  193. kube::test::if_has_string "${output_message}" "test-the-deployment"
  194. kube::test::if_has_string "${output_message}" "test-the-service"
  195. # cleanup
  196. kubectl delete -k hack/testdata/kustomize
  197. # Check that all items in the list are deleted
  198. kube::test::get_object_assert configmaps "{{range.items}}{{$id_field}}:{{end}}" ''
  199. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
  200. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
  201. set +o nounset
  202. set +o errexit
  203. }
  204. run_retrieve_multiple_tests() {
  205. set -o nounset
  206. set -o errexit
  207. # switch back to the default namespace
  208. kubectl config set-context "${CONTEXT}" --namespace=""
  209. kube::log::status "Testing kubectl(v1:multiget)"
  210. kube::test::get_object_assert 'nodes/127.0.0.1 service/kubernetes' "{{range.items}}{{$id_field}}:{{end}}" '127.0.0.1:kubernetes:'
  211. set +o nounset
  212. set +o errexit
  213. }
  214. run_kubectl_sort_by_tests() {
  215. set -o nounset
  216. set -o errexit
  217. kube::log::status "Testing kubectl --sort-by"
  218. ### sort-by should not panic if no pod exists
  219. # Pre-condition: no POD exists
  220. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  221. # Command
  222. kubectl get pods --sort-by="{metadata.name}"
  223. kubectl get pods --sort-by="{metadata.creationTimestamp}"
  224. ### sort-by should works if pod exists
  225. # Create POD
  226. # Pre-condition: no POD exists
  227. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  228. # Command
  229. kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml
  230. # Post-condition: valid-pod is created
  231. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
  232. # Check output of sort-by
  233. output_message=$(kubectl get pods --sort-by="{metadata.name}")
  234. kube::test::if_has_string "${output_message}" "valid-pod"
  235. # ensure sort-by receivers objects as Table
  236. output_message=$(kubectl get pods --v=8 --sort-by="{metadata.name}" 2>&1)
  237. kube::test::if_has_string "${output_message}" "as=Table"
  238. # ensure sort-by requests the full object
  239. kube::test::if_has_string "${output_message}" "includeObject=Object"
  240. ### Clean up
  241. # Pre-condition: valid-pod exists
  242. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
  243. # Command
  244. kubectl delete "${kube_flags[@]}" pod valid-pod --grace-period=0 --force
  245. # Post-condition: valid-pod doesn't exist
  246. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  247. ### sort-by should works by sorting by name
  248. # Create three PODs
  249. # Pre-condition: no POD exists
  250. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  251. # Command
  252. kubectl create "${kube_flags[@]}" -f hack/testdata/sorted-pods/sorted-pod1.yaml
  253. # Post-condition: sorted-pod1 is created
  254. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:'
  255. # Command
  256. kubectl create "${kube_flags[@]}" -f hack/testdata/sorted-pods/sorted-pod2.yaml
  257. # Post-condition: sorted-pod1 is created
  258. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:sorted-pod2:'
  259. # Command
  260. kubectl create "${kube_flags[@]}" -f hack/testdata/sorted-pods/sorted-pod3.yaml
  261. # Post-condition: sorted-pod1 is created
  262. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:sorted-pod2:sorted-pod3:'
  263. # Check output of sort-by '{metadata.name}'
  264. output_message=$(kubectl get pods --sort-by="{metadata.name}")
  265. kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod1:sorted-pod2:sorted-pod3:"
  266. # Check output of sort-by '{metadata.labels.name}'
  267. output_message=$(kubectl get pods --sort-by="{metadata.labels.name}")
  268. kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod3:sorted-pod2:sorted-pod1:"
  269. # if sorting, we should be able to use any field in our objects
  270. output_message=$(kubectl get pods --sort-by="{spec.containers[0].name}")
  271. kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod2:sorted-pod1:sorted-pod3:"
  272. # ensure sorting by creation timestamps works
  273. output_message=$(kubectl get pods --sort-by="{metadata.creationTimestamp}")
  274. kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod1:sorted-pod2:sorted-pod3:"
  275. # ensure sorting using fallback codepath still works
  276. output_message=$(kubectl get pods --sort-by="{spec.containers[0].name}" --server-print=false --v=8 2>&1)
  277. kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod2:sorted-pod1:sorted-pod3:"
  278. kube::test::if_has_not_string "${output_message}" "Table"
  279. ### Clean up
  280. # Pre-condition: valid-pod exists
  281. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:sorted-pod2:sorted-pod3:'
  282. # Command
  283. kubectl delete "${kube_flags[@]}" pod --grace-period=0 --force --all
  284. # Post-condition: valid-pod doesn't exist
  285. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  286. set +o nounset
  287. set +o errexit
  288. }
  289. run_kubectl_all_namespace_tests() {
  290. set -o nounset
  291. set -o errexit
  292. kube::log::status "Testing kubectl --all-namespace"
  293. # Pre-condition: the "default" namespace exists
  294. kube::test::get_object_assert namespaces "{{range.items}}{{if eq $id_field \\\"default\\\"}}{{$id_field}}:{{end}}{{end}}" 'default:'
  295. ### Create POD
  296. # Pre-condition: no POD exists
  297. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  298. # Command
  299. kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml
  300. # Post-condition: valid-pod is created
  301. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
  302. ### Verify a specific namespace is ignored when all-namespaces is provided
  303. # Command
  304. kubectl get pods --all-namespaces --namespace=default
  305. ### Check --all-namespaces option shows namespaces
  306. # Create objects in multiple namespaces
  307. kubectl create "${kube_flags[@]}" namespace all-ns-test-1
  308. kubectl create "${kube_flags[@]}" serviceaccount test -n all-ns-test-1
  309. kubectl create "${kube_flags[@]}" namespace all-ns-test-2
  310. kubectl create "${kube_flags[@]}" serviceaccount test -n all-ns-test-2
  311. # Ensure listing across namespaces displays the namespace (--all-namespaces)
  312. output_message=$(kubectl get serviceaccounts --all-namespaces "${kube_flags[@]}")
  313. kube::test::if_has_string "${output_message}" "all-ns-test-1"
  314. kube::test::if_has_string "${output_message}" "all-ns-test-2"
  315. # Ensure listing across namespaces displays the namespace (-A)
  316. output_message=$(kubectl get serviceaccounts -A "${kube_flags[@]}")
  317. kube::test::if_has_string "${output_message}" "all-ns-test-1"
  318. kube::test::if_has_string "${output_message}" "all-ns-test-2"
  319. # Clean up
  320. kubectl delete "${kube_flags[@]}" namespace all-ns-test-1
  321. kubectl delete "${kube_flags[@]}" namespace all-ns-test-2
  322. ### Clean up
  323. # Pre-condition: valid-pod exists
  324. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
  325. # Command
  326. kubectl delete "${kube_flags[@]}" pod valid-pod --grace-period=0 --force
  327. # Post-condition: valid-pod doesn't exist
  328. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  329. ### Verify flag all-namespaces is ignored for rootScoped resources
  330. # Pre-condition: node exists
  331. kube::test::get_object_assert nodes "{{range.items}}{{$id_field}}:{{end}}" '127.0.0.1:'
  332. # Command
  333. output_message=$(kubectl get nodes --all-namespaces 2>&1)
  334. # Post-condition: output with no NAMESPACE field
  335. kube::test::if_has_not_string "${output_message}" "NAMESPACE"
  336. set +o nounset
  337. set +o errexit
  338. }