diff --git a/tests/integration/kubernetes/k8s-volume.bats b/tests/integration/kubernetes/k8s-volume.bats new file mode 100644 index 0000000000..0489ff4619 --- /dev/null +++ b/tests/integration/kubernetes/k8s-volume.bats @@ -0,0 +1,67 @@ +#!/usr/bin/env bats +# +# Copyright (c) 2018 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# + +load "${BATS_TEST_DIRNAME}/../../common.bash" +load "${BATS_TEST_DIRNAME}/tests_common.sh" +TEST_INITRD="${TEST_INITRD:-no}" + +setup() { + [ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}" + + get_pod_config_dir + + tmp_file=$(exec_host mktemp -d /tmp/data.XXXX) + pod_yaml=$(mktemp --tmpdir pod_config.XXXXXX.yaml) + msg="Hello from Kubernetes" + exec_host "echo $msg > $tmp_file/index.html" + pod_name="pv-pod" + # Define temporary file at yaml + sed -e "s|tmp_data|${tmp_file}|g" ${pod_config_dir}/pv-volume.yaml > "$pod_yaml" +} + +@test "Create Persistent Volume" { + [ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}" + + volume_name="pv-volume" + volume_claim="pv-claim" + + # Create the persistent volume + kubectl create -f "$pod_yaml" + + # Check the persistent volume is Available + cmd="kubectl get pv $volume_name | grep Available" + waitForProcess "$wait_time" "$sleep_time" "$cmd" + + # Create the persistent volume claim + kubectl create -f "${pod_config_dir}/volume-claim.yaml" + + # Check the persistent volume claim is Bound. + cmd="kubectl get pvc $volume_claim | grep Bound" + waitForProcess "$wait_time" "$sleep_time" "$cmd" + + # Create pod + kubectl create -f "${pod_config_dir}/pv-pod.yaml" + + # Check pod creation + kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name" + + cmd="cat /mnt/index.html" + kubectl exec $pod_name -- sh -c "$cmd" | grep "$msg" +} + +teardown() { + [ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}" + + # Debugging information + kubectl describe "pod/$pod_name" + + kubectl delete pod "$pod_name" + kubectl delete pvc "$volume_claim" + kubectl delete pv "$volume_name" + rm -f "$pod_yaml" + exec_host rm -rf "$tmp_file" +} diff --git a/tests/integration/kubernetes/run_kubernetes_tests.sh b/tests/integration/kubernetes/run_kubernetes_tests.sh index 0a4e8cbed5..1dbbc7394a 100644 --- a/tests/integration/kubernetes/run_kubernetes_tests.sh +++ b/tests/integration/kubernetes/run_kubernetes_tests.sh @@ -51,6 +51,7 @@ else "k8s-sysctls.bats" \ "k8s-security-context.bats" \ "k8s-shared-volume.bats" \ + "k8s-volume.bats" \ "k8s-nginx-connectivity.bats" \ ) fi diff --git a/tests/integration/kubernetes/runtimeclass_workloads/pv-pod.yaml b/tests/integration/kubernetes/runtimeclass_workloads/pv-pod.yaml new file mode 100644 index 0000000000..6a165b9712 --- /dev/null +++ b/tests/integration/kubernetes/runtimeclass_workloads/pv-pod.yaml @@ -0,0 +1,26 @@ +# +# Copyright (c) 2018 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# +kind: Pod +apiVersion: v1 +metadata: + name: pv-pod +spec: + terminationGracePeriodSeconds: 0 + runtimeClassName: kata + volumes: + - name: pv-storage + persistentVolumeClaim: + claimName: pv-claim + containers: + - name: pv-container + image: quay.io/prometheus/busybox:latest + ports: + command: + - sleep + - "120" + volumeMounts: + - mountPath: "/mnt/" + name: pv-storage diff --git a/tests/integration/kubernetes/runtimeclass_workloads/pv-volume.yaml b/tests/integration/kubernetes/runtimeclass_workloads/pv-volume.yaml new file mode 100644 index 0000000000..e677d5af4b --- /dev/null +++ b/tests/integration/kubernetes/runtimeclass_workloads/pv-volume.yaml @@ -0,0 +1,19 @@ +# +# Copyright (c) 2018 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# +kind: PersistentVolume +apiVersion: v1 +metadata: + name: pv-volume + labels: + type: local +spec: + storageClassName: manual + capacity: + storage: 10Gi + accessModes: + - ReadWriteOnce + hostPath: + path: "tmp_data" diff --git a/tests/integration/kubernetes/runtimeclass_workloads/volume-claim.yaml b/tests/integration/kubernetes/runtimeclass_workloads/volume-claim.yaml new file mode 100644 index 0000000000..e523e29dec --- /dev/null +++ b/tests/integration/kubernetes/runtimeclass_workloads/volume-claim.yaml @@ -0,0 +1,16 @@ +# +# Copyright (c) 2018 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: pv-claim +spec: + storageClassName: manual + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 3Gi diff --git a/tests/integration/kubernetes/tests_common.sh b/tests/integration/kubernetes/tests_common.sh index bcf686241c..b6d448cac1 100644 --- a/tests/integration/kubernetes/tests_common.sh +++ b/tests/integration/kubernetes/tests_common.sh @@ -37,3 +37,16 @@ get_pod_config_dir() { pod_config_dir="${BATS_TEST_DIRNAME}/runtimeclass_workloads_work" info "k8s configured to use runtimeclass" } + +# Runs a command in the host filesystem. +exec_host() { + node="$(kubectl get node -o name)" + # `kubectl debug` always returns 0, so we hack it to return the right exit code. + command="$@" + command+='; echo -en \\n$?' + output="$(kubectl debug -qit "${node}" --image=alpine:latest -- chroot /host bash -c "${command}")" + kubectl get pods -o name | grep node-debugger | xargs kubectl delete > /dev/null + exit_code="$(echo "${output}" | tail -1)" + echo "$(echo "${output}" | head -n -1)" + return ${exit_code} +}