1#!/usr/bin/env bash
2
3# Copyright 2018 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -o errexit
18set -o nounset
19set -o pipefail
20
21run_template_output_tests() {
22  set -o nounset
23  set -o errexit
24
25  kube::log::status "Testing --template support on commands"
26  ### Test global request timeout option
27  # Pre-condition: no POD exists
28  create_and_use_new_namespace
29  kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
30  # Command
31  # check that create supports --template output
32  kubectl create "${kube_flags[@]:?}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml
33  # Post-condition: valid-pod POD is created
34  kubectl get "${kube_flags[@]:?}" pods -o json
35  kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" 'valid-pod:'
36
37  # check that patch command supports --template output
38  output_message=$(kubectl "${kube_flags[@]:?}" patch --dry-run=client pods/valid-pod -p '{"patched":"value3"}' --type=merge --template="{{ .metadata.name }}:")
39  kube::test::if_has_string "${output_message}" 'valid-pod:'
40
41  # check that label command supports --template output
42  output_message=$(kubectl "${kube_flags[@]:?}" label --dry-run=client pods/valid-pod label=value --template="{{ .metadata.name }}:")
43  kube::test::if_has_string "${output_message}" 'valid-pod:'
44
45  # check that annotate command supports --template output
46  output_message=$(kubectl "${kube_flags[@]:?}" annotate --dry-run=client pods/valid-pod annotation=value --template="{{ .metadata.name }}:")
47  kube::test::if_has_string "${output_message}" 'valid-pod:'
48
49  # check that apply command supports --template output
50  output_message=$(kubectl "${kube_flags[@]:?}" apply --dry-run=client -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --template="{{ .metadata.name }}:")
51  kube::test::if_has_string "${output_message}" 'valid-pod:'
52
53  # check that create command supports --template output
54  output_message=$(kubectl "${kube_flags[@]:?}" create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --dry-run=client --template="{{ .metadata.name }}:")
55  kube::test::if_has_string "${output_message}" 'valid-pod:'
56
57  # check that autoscale command supports --template output
58  output_message=$(kubectl "${kube_flags[@]:?}" autoscale --max=2 -f hack/testdata/scale-deploy-1.yaml --dry-run=client --template="{{ .metadata.name }}:")
59  kube::test::if_has_string "${output_message}" 'scale-1:'
60
61  # check that expose command supports --template output
62  output_message=$(kubectl "${kube_flags[@]:?}" expose -f hack/testdata/redis-slave-replicaset.yaml --save-config --port=80 --target-port=8000 --dry-run=client --template="{{ .metadata.name }}:")
63  kube::test::if_has_string "${output_message}" 'redis-slave:'
64
65  # check that run command supports --template output
66  output_message=$(kubectl "${kube_flags[@]:?}" run --dry-run=client --template="{{ .metadata.name }}:" pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)')
67  kube::test::if_has_string "${output_message}" 'pi:'
68
69  # check that taint command supports --template output
70  output_message=$(kubectl "${kube_flags[@]:?}" taint node 127.0.0.1 dedicated=foo:PreferNoSchedule --template="{{ .metadata.name }}:")
71  kube::test::if_has_string "${output_message}" '127.0.0.1:'
72  # untaint node
73  kubectl taint node 127.0.0.1 dedicated-
74
75  # check that "apply set-last-applied" command supports --template output
76  kubectl "${kube_flags[@]:?}" create -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml
77  output_message=$(kubectl "${kube_flags[@]:?}" apply set-last-applied -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml --dry-run=client --create-annotation --template="{{ .metadata.name }}:")
78  kube::test::if_has_string "${output_message}" 'cassandra:'
79
80  # check that "auth reconcile" command supports --template output
81  output_message=$(kubectl "${kube_flags[@]:?}" auth reconcile --dry-run=client -f test/fixtures/pkg/kubectl/cmd/auth/rbac-resource-plus.yaml --template="{{ .metadata.name }}:")
82  kube::test::if_has_string "${output_message}" 'testing-CR:testing-CRB:testing-RB:testing-R:'
83
84  # check that "create clusterrole" command supports --template output
85  output_message=$(kubectl "${kube_flags[@]:?}" create clusterrole --template="{{ .metadata.name }}:" --verb get myclusterrole  --non-resource-url /logs/ --resource pods)
86  kube::test::if_has_string "${output_message}" 'myclusterrole:'
87
88  # check that "create clusterrolebinding" command supports --template output
89  output_message=$(kubectl "${kube_flags[@]:?}" create clusterrolebinding foo --clusterrole=myclusterrole --template="{{ .metadata.name }}:")
90  kube::test::if_has_string "${output_message}" 'foo:'
91
92  # check that "create configmap" command supports --template output
93  output_message=$(kubectl "${kube_flags[@]:?}" create configmap cm --dry-run=client --template="{{ .metadata.name }}:")
94  kube::test::if_has_string "${output_message}" 'cm:'
95
96  # check that "create deployment" command supports --template output
97  output_message=$(kubectl "${kube_flags[@]:?}" create deployment deploy --image=nginx --template="{{ .metadata.name }}:")
98  kube::test::if_has_string "${output_message}" 'deploy:'
99
100  # check that "create job" command supports --template output
101  kubectl create "${kube_flags[@]:?}" -f - <<EOF
102apiVersion: batch/v1
103kind: CronJob
104metadata:
105  name: pi
106spec:
107  schedule: "*/10 * * * *"
108  jobTemplate:
109    spec:
110      template:
111        metadata:
112          labels:
113            parent: "pi"
114        spec:
115          containers:
116          - name: pi
117            image: perl
118            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
119          restartPolicy: OnFailure
120EOF
121  output_message=$(kubectl "${kube_flags[@]:?}" create job foo --from=cronjob/pi --dry-run=client --template="{{ .metadata.name }}:")
122  kube::test::if_has_string "${output_message}" 'foo:'
123
124  # check that "create namespace" command supports --template output
125  output_message=$(kubectl "${kube_flags[@]:?}" create ns bar --dry-run=client --template="{{ .metadata.name }}:")
126  kube::test::if_has_string "${output_message}" 'bar:'
127
128  # check that "create namespace" command supports --template output
129  output_message=$(kubectl "${kube_flags[@]:?}" create rolebinding foo --clusterrole=myclusterrole --dry-run=client --template="{{ .metadata.name }}:")
130  kube::test::if_has_string "${output_message}" 'foo:'
131
132  # check that "create role" command supports --template output
133  output_message=$(kubectl "${kube_flags[@]:?}" create role --dry-run=client --template="{{ .metadata.name }}:" --verb get myrole --resource pods)
134  kube::test::if_has_string "${output_message}" 'myrole:'
135
136  # check that "create quota" command supports --template output
137  output_message=$(kubectl "${kube_flags[@]:?}" create quota foo --dry-run=client --template="{{ .metadata.name }}:")
138  kube::test::if_has_string "${output_message}" 'foo:'
139
140  # check that "create priorityclass" command supports --template output
141  output_message=$(kubectl "${kube_flags[@]:?}" create priorityclass foo --dry-run=client --template="{{ .metadata.name }}:")
142  kube::test::if_has_string "${output_message}" 'foo:'
143
144  # check that "create poddisruptionbudget" command supports --template output
145  output_message=$(kubectl "${kube_flags[@]:?}" create poddisruptionbudget foo --dry-run=client --selector=foo --min-available=1 --template="{{ .metadata.name }}:")
146  kube::test::if_has_string "${output_message}" 'foo:'
147
148  # check that "create serviceaccount" command supports --template output
149  output_message=$(kubectl "${kube_flags[@]:?}" create serviceaccount foo --dry-run=client --template="{{ .metadata.name }}:")
150  kube::test::if_has_string "${output_message}" 'foo:'
151
152  # check that "set env" command supports --template output
153  output_message=$(kubectl "${kube_flags[@]:?}" set env pod/valid-pod --dry-run=client A=B --template="{{ .metadata.name }}:")
154  kube::test::if_has_string "${output_message}" 'valid-pod:'
155
156  # check that "set image" command supports --template output
157  output_message=$(kubectl "${kube_flags[@]:?}" set image pod/valid-pod --dry-run=client kubernetes-serve-hostname=nginx --template="{{ .metadata.name }}:")
158  kube::test::if_has_string "${output_message}" 'valid-pod:'
159
160  # check that "set resources" command supports --template output
161  output_message=$(kubectl "${kube_flags[@]:?}" set resources pod/valid-pod --limits=memory=256Mi --dry-run=client --template="{{ .metadata.name }}:")
162  kube::test::if_has_string "${output_message}" 'valid-pod:'
163
164  # check that "set selector" command supports --template output
165  output_message=$(kubectl "${kube_flags[@]:?}" set selector -f hack/testdata/kubernetes-service.yaml A=B --local --dry-run=client --template="{{ .metadata.name }}:")
166  kube::test::if_has_string "${output_message}" 'kubernetes:'
167
168  # check that "set serviceaccount" command supports --template output
169  output_message=$(kubectl "${kube_flags[@]:?}" set serviceaccount pod/valid-pod deployer --dry-run=client --template="{{ .metadata.name }}:")
170  kube::test::if_has_string "${output_message}" 'valid-pod:'
171
172  # check that "set subject" command supports --template output
173  output_message=$(kubectl "${kube_flags[@]:?}" set subject clusterrolebinding/foo --user=foo --dry-run=client --template="{{ .metadata.name }}:")
174  kube::test::if_has_string "${output_message}" 'foo:'
175
176  # check that "create secret docker-registry" command supports --template output
177  output_message=$(kubectl "${kube_flags[@]:?}" create secret docker-registry foo --docker-username user --docker-password pass --docker-email foo@bar.baz --dry-run=client --template="{{ .metadata.name }}:")
178  kube::test::if_has_string "${output_message}" 'foo:'
179
180  # check that "create secret generic" command supports --template output
181  output_message=$(kubectl "${kube_flags[@]:?}" create secret generic foo --from-literal=key1=value1 --dry-run=client --template="{{ .metadata.name }}:")
182  kube::test::if_has_string "${output_message}" 'foo:'
183
184  # check that "create secret tls" command supports --template output
185  output_message=$(kubectl "${kube_flags[@]:?}" create secret tls --dry-run=client foo --key=hack/testdata/tls.key --cert=hack/testdata/tls.crt --template="{{ .metadata.name }}:")
186  kube::test::if_has_string "${output_message}" 'foo:'
187
188  # check that "create service clusterip" command supports --template output
189  output_message=$(kubectl "${kube_flags[@]:?}" create service clusterip foo --dry-run=client --tcp=8080 --template="{{ .metadata.name }}:")
190  kube::test::if_has_string "${output_message}" 'foo:'
191
192  # check that "create service externalname" command supports --template output
193  output_message=$(kubectl "${kube_flags[@]:?}" create service externalname foo --dry-run=client --external-name=bar --template="{{ .metadata.name }}:")
194  kube::test::if_has_string "${output_message}" 'foo:'
195
196  # check that "create service loadbalancer" command supports --template output
197  output_message=$(kubectl "${kube_flags[@]:?}" create service loadbalancer foo --dry-run=client --tcp=8080 --template="{{ .metadata.name }}:")
198  kube::test::if_has_string "${output_message}" 'foo:'
199
200  # check that "create service nodeport" command supports --template output
201  output_message=$(kubectl "${kube_flags[@]:?}" create service nodeport foo --dry-run=client --tcp=8080 --template="{{ .metadata.name }}:")
202  kube::test::if_has_string "${output_message}" 'foo:'
203
204  # check that "config view" ouputs "yaml" as its default output format
205  output_message=$(kubectl "${kube_flags[@]:?}" config view)
206  kube::test::if_has_string "${output_message}" 'kind: Config'
207
208  # check that "rollout pause" supports --template output
209  output_message=$(kubectl "${kube_flags[@]:?}" rollout pause deploy/deploy --template="{{ .metadata.name }}:")
210  kube::test::if_has_string "${output_message}" 'deploy:'
211
212   # check that "rollout history" supports --template output
213  output_message=$(kubectl "${kube_flags[@]:?}" rollout history deploy/deploy --template="{{ .metadata.name }}:")
214  kube::test::if_has_string "${output_message}" 'deploy:'
215
216  # check that "rollout resume" supports --template output
217  output_message=$(kubectl "${kube_flags[@]:?}" rollout resume deploy/deploy --template="{{ .metadata.name }}:")
218  kube::test::if_has_string "${output_message}" 'deploy:'
219
220  # check that "rollout undo" supports --template output
221  output_message=$(kubectl "${kube_flags[@]:?}" rollout undo deploy/deploy --to-revision=1 --template="{{ .metadata.name }}:")
222  kube::test::if_has_string "${output_message}" 'deploy:'
223
224  # check that "config view" command supports --template output
225  # and that commands that set a default output (yaml in this case),
226  # default to "go-template" as their output format when a --template
227  # value is provided, but no explicit --output format is given.
228  output_message=$(kubectl "${kube_flags[@]:?}" config view --template="{{ .kind }}:")
229  kube::test::if_has_string "${output_message}" 'Config'
230
231  # check that running a command with both a --template flag and a
232  # non-template --output prefers the non-template output value
233  output_message=$(kubectl "${kube_flags[@]:?}" create configmap cm --dry-run=client --template="{{ .metadata.name }}:" --output yaml)
234  kube::test::if_has_string "${output_message}" 'kind: ConfigMap'
235
236  # cleanup
237  kubectl delete cronjob pi "${kube_flags[@]:?}"
238  kubectl delete pods --all "${kube_flags[@]:?}"
239  kubectl delete rc cassandra "${kube_flags[@]:?}"
240  kubectl delete clusterrole myclusterrole "${kube_flags[@]:?}"
241  kubectl delete clusterrolebinding foo "${kube_flags[@]:?}"
242  kubectl delete deployment deploy "${kube_flags[@]:?}"
243
244  set +o nounset
245  set +o errexit
246}
247