...
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
21# Runs tests related to kubectl create --dry-run.
22run_kubectl_create_dry_run_tests() {
23 set -o nounset
24 set -o errexit
25
26 create_and_use_new_namespace
27 kube::log::status "Testing kubectl create dry-run"
28
29 # Pre-Condition: no POD exists
30 kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
31 # dry-run create
32 kubectl create --dry-run=client -f hack/testdata/pod.yaml "${kube_flags[@]:?}"
33 kubectl create --dry-run=server -f hack/testdata/pod.yaml "${kube_flags[@]:?}"
34 # check no POD exists
35 kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
36
37 set +o nounset
38 set +o errexit
39}
40
41# Runs tests related to kubectl create --filename(-f) --selector(-l).
42run_kubectl_create_filter_tests() {
43 set -o nounset
44 set -o errexit
45
46 create_and_use_new_namespace
47 kube::log::status "Testing kubectl create filter"
48 ## kubectl create -f with label selector should only create matching objects
49 # Pre-Condition: no POD exists
50 kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
51 # create
52 kubectl create -l unique-label=bingbang -f hack/testdata/filter "${kube_flags[@]:?}"
53 # check right pod exists
54 kube::test::get_object_assert 'pods selector-test-pod' "{{${labels_field:?}.name}}" 'selector-test-pod'
55 # check wrong pod doesn't exist
56 output_message=$(! kubectl get pods selector-test-pod-dont-apply 2>&1 "${kube_flags[@]}")
57 kube::test::if_has_string "${output_message}" 'pods "selector-test-pod-dont-apply" not found'
58 # cleanup
59 kubectl delete pods selector-test-pod
60
61 set +o nounset
62 set +o errexit
63}
64
65run_kubectl_create_error_tests() {
66 set -o nounset
67 set -o errexit
68
69 create_and_use_new_namespace
70 kube::log::status "Testing kubectl create with error"
71
72 # Passing no arguments to create is an error
73 ! kubectl create || exit 1
74
75 # Posting a pod to namespaces should fail. Also tests --raw forcing the post location
76 grep -q 'the object provided is unrecognized (must be of type Namespace)' <<< "$( kubectl create "${kube_flags[@]}" --raw /api/v1/namespaces -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --v=8 2>&1 )"
77
78 grep -q "raw and --edit are mutually exclusive" <<< "$( kubectl create "${kube_flags[@]}" --raw /api/v1/namespaces -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --edit 2>&1 )"
79
80 set +o nounset
81 set +o errexit
82}
83
84# Runs kubectl create job tests
85run_create_job_tests() {
86 set -o nounset
87 set -o errexit
88
89 create_and_use_new_namespace
90
91 # Test kubectl create job
92 kubectl create job test-job --image=registry.k8s.io/nginx:test-cmd
93 # Post-Condition: job nginx is created
94 kube::test::get_object_assert 'job test-job' "{{${image_field0:?}}}" 'registry.k8s.io/nginx:test-cmd'
95 # Clean up
96 kubectl delete job test-job "${kube_flags[@]}"
97
98 # Test kubectl create job with command
99 kubectl create job test-job-pi "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(20)'
100 kube::test::get_object_assert 'job test-job-pi' "{{$image_field0}}" "$IMAGE_PERL"
101 # Clean up
102 kubectl delete job test-job-pi
103
104 # Test kubectl create job from cronjob
105 # Pre-Condition: create a cronjob
106 kubectl create cronjob test-pi --schedule="* */5 * * *" "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(10)'
107 kubectl create job my-pi --from=cronjob/test-pi
108 # Post-condition: container args contain expected command
109 output_message=$(kubectl get job my-pi -o go-template='{{(index .spec.template.spec.containers 0).command}}' "${kube_flags[@]}")
110 kube::test::if_has_string "${output_message}" "perl -Mbignum=bpi -wle print bpi(10)"
111
112 # Clean up
113 kubectl delete job my-pi
114 kubectl delete cronjob test-pi
115
116 set +o nounset
117 set +o errexit
118}
119
120run_kubectl_create_kustomization_directory_tests() {
121 set -o nounset
122 set -o errexit
123
124 ## kubectl create -k <dir> for kustomization directory
125 # Pre-Condition: No configmaps with name=test-the-map, no Deployment, Service exist
126 kube::test::get_object_assert 'configmaps --field-selector=metadata.name=test-the-map' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
127 kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
128 kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
129 # Command
130 kubectl create -k hack/testdata/kustomize
131 # Post-condition: test-the-map, test-the-deployment, test-the-service exist
132
133 # Check that all items in the list are printed
134 kube::test::get_object_assert 'configmap test-the-map' "{{${id_field}}}" 'test-the-map'
135 kube::test::get_object_assert 'deployment test-the-deployment' "{{${id_field}}}" 'test-the-deployment'
136 kube::test::get_object_assert 'service test-the-service' "{{${id_field}}}" 'test-the-service'
137
138 # cleanup
139 kubectl delete -k hack/testdata/kustomize
140
141 set +o nounset
142 set +o errexit
143}
144
145has_one_of_error_message() {
146 local message=$1
147 local match1=$2
148 local match2=$3
149
150 if (grep -q "${match1}" <<< "${message}") || (grep -q "${match2}" <<< "${message}"); then
151 echo "Successful"
152 echo "message:${message}"
153 echo "has either:${match1}"
154 echo "or:${match2}"
155 return 0
156 else
157 echo "FAIL!"
158 echo "message:${message}"
159 echo "has neither:${match1}"
160 echo "nor:${match2}"
161 caller
162 return 1
163 fi
164}
165
166# Runs tests related to kubectl create --validate
167run_kubectl_create_validate_tests() {
168 set -o nounset
169 set -o errexit
170
171 create_and_use_new_namespace
172
173 ## test --validate no value expects default strict is used
174 kube::log::status "Testing kubectl create --validate"
175 # create and verify
176 output_message=$(! kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate 2>&1)
177 has_one_of_error_message "${output_message}" 'strict decoding error' 'error validating data'
178
179 ## test --validate=true
180 kube::log::status "Testing kubectl create --validate=true"
181 # create and verify
182 output_message=$(! kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate=true 2>&1)
183 has_one_of_error_message "${output_message}" 'strict decoding error' 'error validating data'
184
185 ## test --validate=false
186 kube::log::status "Testing kubectl create --validate=false"
187 # create and verify
188 output_message=$(kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate=false)
189 kube::test::if_has_string "${output_message}" "deployment.apps/invalid-nginx-deployment created"
190 # cleanup
191 kubectl delete deployment invalid-nginx-deployment
192
193 ## test --validate=strict
194 kube::log::status "Testing kubectl create --validate=strict"
195 # create and verify
196 output_message=$(! kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate=strict 2>&1)
197 has_one_of_error_message "${output_message}" 'strict decoding error' 'error validating data'
198
199 ## test --validate=warn
200 kube::log::status "Testing kubectl create --validate=warn"
201 # create and verify
202 output_message=$(kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate=warn)
203 kube::test::if_has_string "${output_message}" "deployment.apps/invalid-nginx-deployment created"
204 # cleanup
205 kubectl delete deployment invalid-nginx-deployment
206
207 ## test --validate=ignore
208 kube::log::status "Testing kubectl create --validate=ignore"
209 # create and verify
210 output_message=$(kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate=ignore)
211 kube::test::if_has_string "${output_message}" "deployment.apps/invalid-nginx-deployment created"
212 # cleanup
213 kubectl delete deployment invalid-nginx-deployment
214
215 ## test default is strict validation
216 kube::log::status "Testing kubectl create"
217 # create and verify
218 output_message=$(! kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml 2>&1)
219 has_one_of_error_message "${output_message}" 'strict decoding error' 'error validating data'
220
221 ## test invalid validate value
222 kube::log::status "Testing kubectl create --validate=foo"
223 # create and verify
224 output_message=$(! kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate=foo 2>&1)
225 kube::test::if_has_string "${output_message}" 'invalid - validate option "foo"'
226
227 set +o nounset
228 set +o errexit
229}
View as plain text