github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/kubernetes/k8s-exec.bats (about) 1 #!/usr/bin/env bats 2 # 3 # Copyright (c) 2020 Ant Financial 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 8 load "${BATS_TEST_DIRNAME}/../../.ci/lib.sh" 9 load "${BATS_TEST_DIRNAME}/tests_common.sh" 10 11 setup() { 12 get_pod_config_dir 13 pod_name="busybox" 14 first_container_name="first-test-container" 15 second_container_name="second-test-container" 16 } 17 18 @test "Kubectl exec" { 19 # Create the pod 20 kubectl create -f "${pod_config_dir}/busybox-pod.yaml" 21 22 # Get pod specification 23 kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name" 24 25 # Run commands in Pod 26 ## Cases for -it options 27 # TODO: enable -i option after updated to new CRI-O 28 # see: https://github.com/kata-containers/tests/issues/2770 29 # kubectl exec -i "$pod_name" -- ls -tl / 30 # kubectl exec -it "$pod_name" -- ls -tl / 31 kubectl exec "$pod_name" -- date 32 33 ## Case for stdin 34 kubectl exec -i "$pod_name" -- sh <<-EOF 35 echo abc > /tmp/abc.txt 36 grep abc /tmp/abc.txt 37 exit 38 EOF 39 40 ## Case for return value 41 ### Command return non-zero code 42 run bash -c "kubectl exec -i $pod_name -- sh <<-EOF 43 exit 123 44 EOF" 45 echo "run status: $status" 1>&2 46 echo "run output: $output" 1>&2 47 [ "$status" -eq 123 ] 48 49 ## Cases for target container 50 ### First container 51 container_name=$(kubectl exec $pod_name -c $first_container_name -- env | grep CONTAINER_NAME) 52 [ "$container_name" == "CONTAINER_NAME=$first_container_name" ] 53 54 ### Second container 55 container_name=$(kubectl exec $pod_name -c $second_container_name -- env | grep CONTAINER_NAME) 56 [ "$container_name" == "CONTAINER_NAME=$second_container_name" ] 57 58 } 59 60 teardown() { 61 # Debugging information 62 kubectl describe "pod/$pod_name" 63 64 kubectl delete pod "$pod_name" 65 }