github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/kubernetes/k8s-copy-file.bats (about)

     1  #!/usr/bin/env bats
     2  #
     3  # Copyright (c) 2019 Intel Corporation
     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  	file_name="file.txt"
    14  	content="Hello"
    15  }
    16  
    17  @test "Copy file in a pod" {
    18  	# Create pod
    19  	pod_name="pod-copy-file-from-host"
    20  	ctr_name="ctr-copy-file-from-host"
    21  
    22  	pod_config=$(mktemp --tmpdir pod_config.XXXXXX.yaml)
    23  	cp "$pod_config_dir/busybox-template.yaml" "$pod_config"
    24  	sed -i "s/POD_NAME/$pod_name/" "$pod_config"
    25  	sed -i "s/CTR_NAME/$ctr_name/" "$pod_config"
    26  
    27  	kubectl create -f "${pod_config}"
    28  
    29  	# Check pod creation
    30  	kubectl wait --for=condition=Ready --timeout=$timeout pod $pod_name
    31  
    32  	# Create a file
    33  	echo "$content" > "$file_name"
    34  
    35  	# Copy file into a pod
    36  	kubectl cp "$file_name" $pod_name:/tmp
    37  
    38  	# Print environment variables
    39  	kubectl exec $pod_name -- sh -c "cat /tmp/$file_name | grep $content"
    40  }
    41  
    42  @test "Copy from pod to host" {
    43  	# Create pod
    44  	pod_name="pod-copy-file-to-host"
    45  	ctr_name="ctr-copy-file-to-host"
    46  
    47  	pod_config=$(mktemp --tmpdir pod_config.XXXXXX.yaml)
    48  	cp "$pod_config_dir/busybox-template.yaml" "$pod_config"
    49  	sed -i "s/POD_NAME/$pod_name/" "$pod_config"
    50  	sed -i "s/CTR_NAME/$ctr_name/" "$pod_config"
    51  
    52  	kubectl create -f "${pod_config}"
    53  
    54  	# Check pod creation
    55  	kubectl wait --for=condition=Ready --timeout=$timeout pod $pod_name
    56  
    57  	kubectl logs "$pod_name" || true
    58  	kubectl describe pod "$pod_name" || true
    59  	kubectl get pods --all-namespaces
    60  
    61  	# Create a file in the pod
    62  	kubectl exec "$pod_name" -- sh -c "cd /tmp && echo $content > $file_name"
    63  
    64  	kubectl logs "$pod_name" || true
    65  	kubectl describe pod "$pod_name" || true
    66  	kubectl get pods --all-namespaces
    67  
    68  	# Copy file from pod to host
    69  	kubectl cp "$pod_name":/tmp/"$file_name" "$file_name"
    70  
    71  	# Verify content
    72  	cat "$file_name" | grep "$content"
    73  }
    74  
    75  teardown() {
    76  	# Debugging information
    77  	kubectl describe "pod/$pod_name"
    78  
    79  	rm -f "$file_name"
    80  	kubectl delete pod "$pod_name"
    81  
    82  	rm -f "$pod_config"
    83  }