template-output.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_template_output_tests() {
  19. set -o nounset
  20. set -o errexit
  21. kube::log::status "Testing --template support on commands"
  22. ### Test global request timeout option
  23. # Pre-condition: no POD exists
  24. create_and_use_new_namespace
  25. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  26. # Command
  27. # check that create supports --template output
  28. kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml
  29. # Post-condition: valid-pod POD is created
  30. kubectl get "${kube_flags[@]}" pods -o json
  31. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
  32. # check that patch command supports --template output
  33. output_message=$(kubectl "${kube_flags[@]}" patch --dry-run pods/valid-pod -p '{"patched":"value3"}' --type=merge --template="{{ .metadata.name }}:")
  34. kube::test::if_has_string "${output_message}" 'valid-pod:'
  35. # check that label command supports --template output
  36. output_message=$(kubectl "${kube_flags[@]}" label --dry-run pods/valid-pod label=value --template="{{ .metadata.name }}:")
  37. kube::test::if_has_string "${output_message}" 'valid-pod:'
  38. # check that annotate command supports --template output
  39. output_message=$(kubectl "${kube_flags[@]}" annotate --dry-run pods/valid-pod annotation=value --template="{{ .metadata.name }}:")
  40. kube::test::if_has_string "${output_message}" 'valid-pod:'
  41. # check that apply command supports --template output
  42. output_message=$(kubectl "${kube_flags[@]}" apply --dry-run -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --template="{{ .metadata.name }}:")
  43. kube::test::if_has_string "${output_message}" 'valid-pod:'
  44. # check that create command supports --template output
  45. output_message=$(kubectl "${kube_flags[@]}" create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --dry-run --template="{{ .metadata.name }}:")
  46. kube::test::if_has_string "${output_message}" 'valid-pod:'
  47. # check that autoscale command supports --template output
  48. output_message=$(kubectl "${kube_flags[@]}" autoscale --max=2 -f hack/testdata/scale-deploy-1.yaml --dry-run --template="{{ .metadata.name }}:")
  49. kube::test::if_has_string "${output_message}" 'scale-1:'
  50. # check that expose command supports --template output
  51. output_message=$(kubectl "${kube_flags[@]}" expose -f hack/testdata/redis-slave-replicaset.yaml --save-config --port=80 --target-port=8000 --dry-run --template="{{ .metadata.name }}:")
  52. kube::test::if_has_string "${output_message}" 'redis-slave:'
  53. # check that convert command supports --template output
  54. output_message=$(kubectl "${kube_flags[@]}" convert -f hack/testdata/deployment-revision1.yaml --output-version=apps/v1beta1 --template="{{ .metadata.name }}:")
  55. kube::test::if_has_string "${output_message}" 'nginx:'
  56. # check that run command supports --template output
  57. output_message=$(kubectl "${kube_flags[@]}" run --dry-run --template="{{ .metadata.name }}:" pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)')
  58. kube::test::if_has_string "${output_message}" 'pi:'
  59. # check that taint command supports --template output
  60. output_message=$(kubectl "${kube_flags[@]}" taint node 127.0.0.1 dedicated=foo:PreferNoSchedule --template="{{ .metadata.name }}:")
  61. kube::test::if_has_string "${output_message}" '127.0.0.1:'
  62. # untaint node
  63. kubectl taint node 127.0.0.1 dedicated-
  64. # check that "apply set-last-applied" command supports --template output
  65. kubectl "${kube_flags[@]}" create -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml
  66. output_message=$(kubectl "${kube_flags[@]}" apply set-last-applied -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml --dry-run --create-annotation --template="{{ .metadata.name }}:")
  67. kube::test::if_has_string "${output_message}" 'cassandra:'
  68. # check that "auth reconcile" command supports --template output
  69. output_message=$(kubectl "${kube_flags[@]}" auth reconcile --dry-run -f test/fixtures/pkg/kubectl/cmd/auth/rbac-resource-plus.yaml --template="{{ .metadata.name }}:")
  70. kube::test::if_has_string "${output_message}" 'testing-CR:testing-CRB:testing-RB:testing-R:'
  71. # check that "create clusterrole" command supports --template output
  72. output_message=$(kubectl "${kube_flags[@]}" create clusterrole --template="{{ .metadata.name }}:" --verb get myclusterrole --non-resource-url /logs/ --resource pods)
  73. kube::test::if_has_string "${output_message}" 'myclusterrole:'
  74. # check that "create clusterrolebinding" command supports --template output
  75. output_message=$(kubectl "${kube_flags[@]}" create clusterrolebinding foo --clusterrole=myclusterrole --template="{{ .metadata.name }}:")
  76. kube::test::if_has_string "${output_message}" 'foo:'
  77. # check that "create configmap" command supports --template output
  78. output_message=$(kubectl "${kube_flags[@]}" create configmap cm --dry-run --template="{{ .metadata.name }}:")
  79. kube::test::if_has_string "${output_message}" 'cm:'
  80. # check that "create deployment" command supports --template output
  81. output_message=$(kubectl "${kube_flags[@]}" create deployment deploy --image=nginx --template="{{ .metadata.name }}:")
  82. kube::test::if_has_string "${output_message}" 'deploy:'
  83. # check that "create job" command supports --template output
  84. kubectl create "${kube_flags[@]}" -f - <<EOF
  85. apiVersion: batch/v1beta1
  86. kind: CronJob
  87. metadata:
  88. name: pi
  89. spec:
  90. schedule: "*/10 * * * *"
  91. jobTemplate:
  92. spec:
  93. template:
  94. metadata:
  95. labels:
  96. parent: "pi"
  97. spec:
  98. containers:
  99. - name: pi
  100. image: perl
  101. command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
  102. restartPolicy: OnFailure
  103. EOF
  104. output_message=$(kubectl "${kube_flags[@]}" create job foo --from=cronjob/pi --dry-run --template="{{ .metadata.name }}:")
  105. kube::test::if_has_string "${output_message}" 'foo:'
  106. # check that "create namespace" command supports --template output
  107. output_message=$(kubectl "${kube_flags[@]}" create ns bar --dry-run --template="{{ .metadata.name }}:")
  108. kube::test::if_has_string "${output_message}" 'bar:'
  109. # check that "create namespace" command supports --template output
  110. output_message=$(kubectl "${kube_flags[@]}" create rolebinding foo --clusterrole=myclusterrole --dry-run --template="{{ .metadata.name }}:")
  111. kube::test::if_has_string "${output_message}" 'foo:'
  112. # check that "create role" command supports --template output
  113. output_message=$(kubectl "${kube_flags[@]}" create role --dry-run --template="{{ .metadata.name }}:" --verb get myrole --resource pods)
  114. kube::test::if_has_string "${output_message}" 'myrole:'
  115. # check that "create quota" command supports --template output
  116. output_message=$(kubectl "${kube_flags[@]}" create quota foo --dry-run --template="{{ .metadata.name }}:")
  117. kube::test::if_has_string "${output_message}" 'foo:'
  118. # check that "create priorityclass" command supports --template output
  119. output_message=$(kubectl "${kube_flags[@]}" create priorityclass foo --dry-run --template="{{ .metadata.name }}:")
  120. kube::test::if_has_string "${output_message}" 'foo:'
  121. # check that "create poddisruptionbudget" command supports --template output
  122. output_message=$(kubectl "${kube_flags[@]}" create poddisruptionbudget foo --dry-run --selector=foo --min-available=1 --template="{{ .metadata.name }}:")
  123. kube::test::if_has_string "${output_message}" 'foo:'
  124. # check that "create serviceaccount" command supports --template output
  125. output_message=$(kubectl "${kube_flags[@]}" create serviceaccount foo --dry-run --template="{{ .metadata.name }}:")
  126. kube::test::if_has_string "${output_message}" 'foo:'
  127. # check that "set env" command supports --template output
  128. output_message=$(kubectl "${kube_flags[@]}" set env pod/valid-pod --dry-run A=B --template="{{ .metadata.name }}:")
  129. kube::test::if_has_string "${output_message}" 'valid-pod:'
  130. # check that "set image" command supports --template output
  131. output_message=$(kubectl "${kube_flags[@]}" set image pod/valid-pod --dry-run kubernetes-serve-hostname=nginx --template="{{ .metadata.name }}:")
  132. kube::test::if_has_string "${output_message}" 'valid-pod:'
  133. # check that "set resources" command supports --template output
  134. output_message=$(kubectl "${kube_flags[@]}" set resources pod/valid-pod --limits=memory=256Mi --dry-run --template="{{ .metadata.name }}:")
  135. kube::test::if_has_string "${output_message}" 'valid-pod:'
  136. # check that "set selector" command supports --template output
  137. output_message=$(kubectl "${kube_flags[@]}" set selector -f hack/testdata/kubernetes-service.yaml A=B --local --dry-run --template="{{ .metadata.name }}:")
  138. kube::test::if_has_string "${output_message}" 'kubernetes:'
  139. # check that "set serviceaccount" command supports --template output
  140. output_message=$(kubectl "${kube_flags[@]}" set serviceaccount pod/valid-pod deployer --dry-run --template="{{ .metadata.name }}:")
  141. kube::test::if_has_string "${output_message}" 'valid-pod:'
  142. # check that "set subject" command supports --template output
  143. output_message=$(kubectl "${kube_flags[@]}" set subject clusterrolebinding/foo --user=foo --dry-run --template="{{ .metadata.name }}:")
  144. kube::test::if_has_string "${output_message}" 'foo:'
  145. # check that "create secret docker-registry" command supports --template output
  146. output_message=$(kubectl "${kube_flags[@]}" create secret docker-registry foo --docker-username user --docker-password pass --docker-email foo@bar.baz --dry-run --template="{{ .metadata.name }}:")
  147. kube::test::if_has_string "${output_message}" 'foo:'
  148. # check that "create secret generic" command supports --template output
  149. output_message=$(kubectl "${kube_flags[@]}" create secret generic foo --from-literal=key1=value1 --dry-run --template="{{ .metadata.name }}:")
  150. kube::test::if_has_string "${output_message}" 'foo:'
  151. # check that "create secret tls" command supports --template output
  152. output_message=$(kubectl "${kube_flags[@]}" create secret tls --dry-run foo --key=hack/testdata/tls.key --cert=hack/testdata/tls.crt --template="{{ .metadata.name }}:")
  153. kube::test::if_has_string "${output_message}" 'foo:'
  154. # check that "create service clusterip" command supports --template output
  155. output_message=$(kubectl "${kube_flags[@]}" create service clusterip foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
  156. kube::test::if_has_string "${output_message}" 'foo:'
  157. # check that "create service externalname" command supports --template output
  158. output_message=$(kubectl "${kube_flags[@]}" create service externalname foo --dry-run --external-name=bar --template="{{ .metadata.name }}:")
  159. kube::test::if_has_string "${output_message}" 'foo:'
  160. # check that "create service loadbalancer" command supports --template output
  161. output_message=$(kubectl "${kube_flags[@]}" create service loadbalancer foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
  162. kube::test::if_has_string "${output_message}" 'foo:'
  163. # check that "create service nodeport" command supports --template output
  164. output_message=$(kubectl "${kube_flags[@]}" create service nodeport foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
  165. kube::test::if_has_string "${output_message}" 'foo:'
  166. # check that "config view" ouputs "yaml" as its default output format
  167. output_message=$(kubectl "${kube_flags[@]}" config view)
  168. kube::test::if_has_string "${output_message}" 'kind: Config'
  169. # check that "rollout pause" supports --template output
  170. output_message=$(kubectl "${kube_flags[@]}" rollout pause deploy/deploy --template="{{ .metadata.name }}:")
  171. kube::test::if_has_string "${output_message}" 'deploy:'
  172. # check that "rollout history" supports --template output
  173. output_message=$(kubectl "${kube_flags[@]}" rollout history deploy/deploy --template="{{ .metadata.name }}:")
  174. kube::test::if_has_string "${output_message}" 'deploy:'
  175. # check that "rollout resume" supports --template output
  176. output_message=$(kubectl "${kube_flags[@]}" rollout resume deploy/deploy --template="{{ .metadata.name }}:")
  177. kube::test::if_has_string "${output_message}" 'deploy:'
  178. # check that "rollout undo" supports --template output
  179. output_message=$(kubectl "${kube_flags[@]}" rollout undo deploy/deploy --to-revision=1 --template="{{ .metadata.name }}:")
  180. kube::test::if_has_string "${output_message}" 'deploy:'
  181. # check that "config view" command supports --template output
  182. # and that commands that set a default output (yaml in this case),
  183. # default to "go-template" as their output format when a --template
  184. # value is provided, but no explicit --output format is given.
  185. output_message=$(kubectl "${kube_flags[@]}" config view --template="{{ .kind }}:")
  186. kube::test::if_has_string "${output_message}" 'Config'
  187. # check that running a command with both a --template flag and a
  188. # non-template --output prefers the non-template output value
  189. output_message=$(kubectl "${kube_flags[@]}" create configmap cm --dry-run --template="{{ .metadata.name }}:" --output yaml)
  190. kube::test::if_has_string "${output_message}" 'kind: ConfigMap'
  191. # cleanup
  192. kubectl delete cronjob pi "${kube_flags[@]}"
  193. kubectl delete pods --all "${kube_flags[@]}"
  194. kubectl delete rc cassandra "${kube_flags[@]}"
  195. kubectl delete clusterrole myclusterrole "${kube_flags[@]}"
  196. kubectl delete clusterrolebinding foo "${kube_flags[@]}"
  197. kubectl delete deployment deploy "${kube_flags[@]}"
  198. set +o nounset
  199. set +o errexit
  200. }