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