From 428daf9ebc02fd114b5dfbb90ce3fd19eff975f8 Mon Sep 17 00:00:00 2001 From: Wainer dos Santos Moschetta Date: Thu, 19 Oct 2023 14:48:50 -0300 Subject: [PATCH] tests/k8s: add utilities functions for the tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The following functions were copied from CCv0's branch test's integration/kubernetes/confidential/lib.sh. I did just smalls refactorings (shortened their names and delinted shellcheck warnings): - k8s_delete_all_pods_if_any_exists() - k8s_wait_pod_be_ready() - k8s_create_pod() - assert_pod_fail() Co-authored-by: Fabiano FidĂȘncio Co-authored-by: Georgina Kinge Co-authored-by: Jordan Jackson Co-authored-by: Megan Wright Signed-off-by: Wainer dos Santos Moschetta Co-authored-by: Wang, Arron --- tests/integration/kubernetes/lib.sh | 74 +++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/integration/kubernetes/lib.sh diff --git a/tests/integration/kubernetes/lib.sh b/tests/integration/kubernetes/lib.sh new file mode 100644 index 000000000..57fbf2708 --- /dev/null +++ b/tests/integration/kubernetes/lib.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# Copyright (c) 2021, 2022 IBM Corporation +# Copyright (c) 2022, 2023 Red Hat +# +# SPDX-License-Identifier: Apache-2.0 +# +# This provides generic functions to use in the tests. +# +set -e + +# Delete all pods if any exist, otherwise just return +# +k8s_delete_all_pods_if_any_exists() { + [ -z "$(kubectl get --no-headers pods)" ] || \ + kubectl delete --all pods +} + +# Wait until the pod is not 'Ready'. Fail if it hits the timeout. +# +# Parameters: +# $1 - the sandbox ID +# $2 - wait time in seconds. Defaults to 120. (optional) +# +k8s_wait_pod_be_ready() { + local pod_name="$1" + local wait_time="${2:-120}" + + kubectl wait --timeout="${wait_time}s" --for=condition=ready "pods/$pod_name" +} + +# Create a pod and wait it be ready, otherwise fail. +# +# Parameters: +# $1 - the pod configuration file. +# +k8s_create_pod() { + local config_file="$1" + local pod_name="" + + if [ ! -f "${config_file}" ]; then + echo "Pod config file '${config_file}' does not exist" + return 1 + fi + + kubectl apply -f "${config_file}" + if ! pod_name=$(kubectl get pods -o jsonpath='{.items..metadata.name}'); then + echo "Failed to create the pod" + return 1 + fi + + if ! k8s_wait_pod_be_ready "$pod_name"; then + # TODO: run this command for debugging. Maybe it should be + # guarded by DEBUG=true? + kubectl get pods "$pod_name" + return 1 + fi +} + +# Create a pod then assert it fails to run. Use in tests that you expect the +# pod creation to fail. +# +# Note: a good testing practice is to afterwards check that the pod creation +# failed because of the expected reason. +# +# Parameters: +# $1 - the pod configuration file. +# +assert_pod_fail() { + local container_config="$1" + echo "In assert_pod_fail: $container_config" + + echo "Attempt to create the container but it should fail" + ! k8s_create_pod "$container_config" || /bin/false +} \ No newline at end of file