#!/usr/bin/env bash # Copyright 2018 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -o errexit set -o nounset set -o pipefail run_template_output_tests() { set -o nounset set -o errexit kube::log::status "Testing --template support on commands" ### Test global request timeout option # Pre-condition: no POD exists create_and_use_new_namespace kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' # Command # check that create supports --template output kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml # Post-condition: valid-pod POD is created kubectl get "${kube_flags[@]}" pods -o json kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:' # check that patch command supports --template output output_message=$(kubectl "${kube_flags[@]}" patch --dry-run pods/valid-pod -p '{"patched":"value3"}' --type=merge --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'valid-pod:' # check that label command supports --template output output_message=$(kubectl "${kube_flags[@]}" label --dry-run pods/valid-pod label=value --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'valid-pod:' # check that annotate command supports --template output output_message=$(kubectl "${kube_flags[@]}" annotate --dry-run pods/valid-pod annotation=value --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'valid-pod:' # check that apply command supports --template output output_message=$(kubectl "${kube_flags[@]}" apply --dry-run -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'valid-pod:' # check that create command supports --template output output_message=$(kubectl "${kube_flags[@]}" create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --dry-run --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'valid-pod:' # check that autoscale command supports --template output output_message=$(kubectl "${kube_flags[@]}" autoscale --max=2 -f hack/testdata/scale-deploy-1.yaml --dry-run --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'scale-1:' # check that expose command supports --template output 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 }}:") kube::test::if_has_string "${output_message}" 'redis-slave:' # check that convert command supports --template output output_message=$(kubectl "${kube_flags[@]}" convert -f hack/testdata/deployment-revision1.yaml --output-version=apps/v1beta1 --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'nginx:' # check that run command supports --template output output_message=$(kubectl "${kube_flags[@]}" run --dry-run --template="{{ .metadata.name }}:" pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)') kube::test::if_has_string "${output_message}" 'pi:' # check that taint command supports --template output output_message=$(kubectl "${kube_flags[@]}" taint node 127.0.0.1 dedicated=foo:PreferNoSchedule --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" '127.0.0.1:' # untaint node kubectl taint node 127.0.0.1 dedicated- # check that "apply set-last-applied" command supports --template output kubectl "${kube_flags[@]}" create -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml 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 }}:") kube::test::if_has_string "${output_message}" 'cassandra:' # check that "auth reconcile" command supports --template output output_message=$(kubectl "${kube_flags[@]}" auth reconcile --dry-run -f test/fixtures/pkg/kubectl/cmd/auth/rbac-resource-plus.yaml --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'testing-CR:testing-CRB:testing-RB:testing-R:' # check that "create clusterrole" command supports --template output output_message=$(kubectl "${kube_flags[@]}" create clusterrole --template="{{ .metadata.name }}:" --verb get myclusterrole --non-resource-url /logs/ --resource pods) kube::test::if_has_string "${output_message}" 'myclusterrole:' # check that "create clusterrolebinding" command supports --template output output_message=$(kubectl "${kube_flags[@]}" create clusterrolebinding foo --clusterrole=myclusterrole --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'foo:' # check that "create configmap" command supports --template output output_message=$(kubectl "${kube_flags[@]}" create configmap cm --dry-run --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'cm:' # check that "create deployment" command supports --template output output_message=$(kubectl "${kube_flags[@]}" create deployment deploy --image=nginx --template="{{ .metadata.name }}:") kube::test::if_has_string "${output_message}" 'deploy:' # check that "create job" command supports --template output kubectl create "${kube_flags[@]}" -f - <