k8s.io/kubernetes@v1.29.3/test/cmd/exec.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2019 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 17 set -o errexit 18 set -o nounset 19 set -o pipefail 20 21 run_kubectl_exec_pod_tests() { 22 set -o nounset 23 set -o errexit 24 25 create_and_use_new_namespace 26 kube::log::status "Testing kubectl exec POD COMMAND" 27 28 ### Test execute non-existing POD 29 output_message=$(! kubectl exec abc date 2>&1) 30 # POD abc should error since it doesn't exist 31 kube::test::if_has_string "${output_message}" 'pods "abc" not found' 32 33 ### Test execute multiple resources 34 output_message=$(! kubectl exec -f - 2>&1 -- echo test << __EOF__ 35 apiVersion: v1 36 kind: Pod 37 metadata: 38 name: test 39 spec: 40 containers: 41 - name: nginx 42 image: nginx 43 --- 44 apiVersion: v1 45 kind: Pod 46 metadata: 47 name: test2 48 spec: 49 containers: 50 - name: nginx 51 image: nginx 52 __EOF__ 53 ) 54 kube::test::if_has_string "${output_message}" 'cannot exec into multiple objects at a time' 55 56 ### Test execute existing POD 57 # Create test-pod 58 kubectl create -f hack/testdata/pod.yaml 59 # Execute existing POD 60 output_message=$(! kubectl exec test-pod date 2>&1) 61 # POD test-pod is exists this is shouldn't have output not found 62 kube::test::if_has_not_string "${output_message}" 'pods "test-pod" not found' 63 # These must be pass the validate 64 kube::test::if_has_not_string "${output_message}" 'pod or type/name must be specified' 65 66 # Clean up 67 kubectl delete pods test-pod 68 69 set +o nounset 70 set +o errexit 71 } 72 73 run_kubectl_exec_resource_name_tests() { 74 set +o nounset 75 set +o errexit 76 77 create_and_use_new_namespace 78 kube::log::status "Testing kubectl exec TYPE/NAME COMMAND" 79 80 ### Test execute invalid resource type 81 output_message=$(! kubectl exec foo/bar date 2>&1) 82 # resource type foo should error since it's invalid 83 kube::test::if_has_string "${output_message}" 'error:' 84 85 ### Test execute non-existing resources 86 output_message=$(! kubectl exec deployments/bar date 2>&1) 87 # resource type foo should error since it doesn't exist 88 kube::test::if_has_string "${output_message}" '"bar" not found' 89 90 kubectl create -f hack/testdata/pod.yaml 91 kubectl create -f hack/testdata/frontend-replicaset.yaml 92 kubectl create -f hack/testdata/configmap.yaml 93 94 ### Test execute non-implemented resources 95 output_message=$(! kubectl exec configmap/test-set-env-config date 2>&1) 96 # resource type configmap should error since configmap not implemented to be attached 97 kube::test::if_has_string "${output_message}" 'not implemented' 98 99 ### Test execute exists and valid resource type. 100 # Just check the output, since test-cmd not run kubelet, pod never be assigned. 101 # and not really can run `kubectl exec` command 102 103 output_message=$(! kubectl exec pods/test-pod date 2>&1) 104 # POD test-pod is exists this is shouldn't have output not found 105 kube::test::if_has_not_string "${output_message}" 'not found' 106 # These must be pass the validate 107 kube::test::if_has_not_string "${output_message}" 'pod, type/name or --filename must be specified' 108 109 output_message=$(! kubectl exec replicaset/frontend date 2>&1) 110 # Replicaset frontend is valid and exists will select the first pod. 111 # and Shouldn't have output not found 112 kube::test::if_has_not_string "${output_message}" 'not found' 113 # These must be pass the validate 114 kube::test::if_has_not_string "${output_message}" 'pod, type/name or --filename must be specified' 115 116 # Clean up 117 kubectl delete pods/test-pod 118 kubectl delete replicaset/frontend 119 kubectl delete configmap/test-set-env-config 120 121 set +o nounset 122 set +o errexit 123 }