github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/integration/kubernetes/k8s-block-volume.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 15 pod_name="pod-block-pv" 16 volume_name="block-loop-pv" 17 volume_claim="block-loop-pvc" 18 ctr_dev_path="/dev/xda" 19 vol_capacity="500M" 20 21 # Create Loop Device 22 tmp_disk_image=$(mktemp --tmpdir disk.XXXXXX.img) 23 sudo truncate "$tmp_disk_image" --size "$vol_capacity" 24 loop_dev=$(sudo losetup -f) 25 sudo losetup "$loop_dev" "$tmp_disk_image" 26 } 27 28 @test "Block Storage Support" { 29 # Create Storage Class 30 kubectl create -f volume/local-storage.yaml 31 32 # Create Persistent Volume 33 tmp_pv_yaml=$(mktemp --tmpdir block_persistent_vol.XXXXX.yaml) 34 sed -e "s|LOOP_DEVICE|${loop_dev}|" volume/block-loop-pv.yaml > "$tmp_pv_yaml" 35 sed -i "s|HOSTNAME|$(hostname)|" "$tmp_pv_yaml" 36 sed -i "s|CAPACITY|${vol_capacity}|" "$tmp_pv_yaml" 37 kubectl create -f "$tmp_pv_yaml" 38 cmd="kubectl get pv/${volume_name} | grep Available" 39 waitForProcess "$wait_time" "$sleep_time" "$cmd" 40 41 # Create Persistent Volume Claim 42 tmp_pvc_yaml=$(mktemp --tmpdir block_persistent_vol.XXXXX.yaml) 43 sed -e "s|CAPACITY|${vol_capacity}|" volume/block-loop-pvc.yaml > "$tmp_pvc_yaml" 44 kubectl create -f "$tmp_pvc_yaml" 45 46 # Create Workload using Volume 47 tmp_pod_yaml=$(mktemp --tmpdir pod-pv.XXXXX.yaml) 48 sed -e "s|DEVICE_PATH|${ctr_dev_path}|" "${pod_config_dir}/${pod_name}.yaml" > "$tmp_pod_yaml" 49 kubectl create -f "$tmp_pod_yaml" 50 kubectl wait --for condition=ready --timeout=$timeout "pod/${pod_name}" 51 52 # Verify persistent volume claim is bound 53 kubectl get "pvc/${volume_claim}" | grep "Bound" 54 55 # make fs, mount device and write on it 56 kubectl exec "$pod_name" -- sh -c "mkfs.ext4 $ctr_dev_path" 57 ctr_mount_path="/mnt" 58 ctr_message="Hello World" 59 ctr_file="${ctr_mount_path}/file.txt" 60 kubectl exec "$pod_name" -- sh -c "mount $ctr_dev_path $ctr_mount_path" 61 kubectl exec "$pod_name" -- sh -c "echo $ctr_message > $ctr_file" 62 kubectl exec "$pod_name" -- sh -c "grep '$ctr_message' $ctr_file" 63 } 64 65 teardown() { 66 # Debugging information 67 kubectl describe "pod/$pod_name" 68 69 # Delete k8s resources 70 kubectl delete pod "$pod_name" 71 kubectl delete pvc "$volume_claim" 72 kubectl delete pv "$volume_name" 73 kubectl delete storageclass local-storage 74 75 # Delete temporary yaml files 76 rm -f "$tmp_pv_yaml" 77 rm -f "$tmp_pvc_yaml" 78 rm -f "$tmp_pod_yaml" 79 80 # Remove image and loop device 81 sudo losetup -d "$loop_dev" 82 rm -f "$tmp_disk_image" 83 }