get.sh 20 KB

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