github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/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  	export KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}"
    13  	get_pod_config_dir
    14  	pod_name="busybox"
    15  	first_container_name="first-test-container"
    16  	second_container_name="second-test-container"
    17  }
    18  
    19  @test "Kubectl exec" {
    20  	# Create the pod
    21  	kubectl create -f "${pod_config_dir}/busybox-pod.yaml"
    22  
    23  	# Get pod specification
    24  	kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name"
    25  
    26  	# Run commands in Pod
    27  	## Cases for -it options
    28  	# TODO: enable -i option after updated to new CRI-O
    29  	# see: https://github.com/kata-containers/tests/issues/2770
    30  	# kubectl exec -i "$pod_name" -- ls -tl /
    31  	# kubectl exec -it "$pod_name" -- ls -tl /
    32  	kubectl exec "$pod_name" -- date
    33  
    34  	## Case for stdin
    35  	kubectl exec -i "$pod_name" -- sh <<EOC
    36  echo abc > /tmp/abc.txt
    37  grep abc /tmp/abc.txt
    38  exit
    39  EOC
    40  
    41  	## Case for return value
    42  	### Command return non-zero code
    43  	run bash -c "kubectl exec -i $pod_name -- sh <<EOC
    44  exit 123
    45  EOC"
    46  	echo "run status: $status" 1>&2
    47  	echo "run output: $output" 1>&2
    48  	[ "$status" -eq 123 ]
    49  
    50  	## Cases for target container
    51  	### First container
    52  	container_name=$(kubectl exec $pod_name -c $first_container_name -- env | grep CONTAINER_NAME)
    53  	[ "$container_name" == "CONTAINER_NAME=$first_container_name" ]
    54  
    55  	### Second container
    56  	container_name=$(kubectl exec $pod_name -c $second_container_name -- env | grep CONTAINER_NAME)
    57  	[ "$container_name" == "CONTAINER_NAME=$second_container_name" ]
    58  
    59  }
    60  
    61  teardown() {
    62  	# Debugging information
    63  	kubectl describe "pod/$pod_name"
    64  
    65  	kubectl delete pod "$pod_name"
    66  }