From caee12c79691be8535ef02b4f5b237bbc9116e12 Mon Sep 17 00:00:00 2001
From: Dan Mihai <dmihai@microsoft.com>
Date: Fri, 7 Mar 2025 19:09:59 +0000
Subject: [PATCH] tests: k8s: add function to log exec output

grep_pod_exec_output invokes "kubectl exec", logs its output, and checks
that a grep pattern is present in the output.

Signed-off-by: Dan Mihai <dmihai@microsoft.com>
---
 tests/integration/kubernetes/tests_common.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/tests/integration/kubernetes/tests_common.sh b/tests/integration/kubernetes/tests_common.sh
index 0a9bf82923..aa6c7a6c9e 100644
--- a/tests/integration/kubernetes/tests_common.sh
+++ b/tests/integration/kubernetes/tests_common.sh
@@ -378,3 +378,21 @@ teardown_common() {
 		exec_host "${node}" journalctl -x -t "kata" --since '"'$node_start_time'"' || true
 	fi
 }
+
+# Invoke "kubectl exec", log its output, and check that a grep pattern is present in the output.
+#
+# Parameters:
+#	$1	- pod name
+#	$2	- the grep pattern
+#	$3+	- the command to execute using "kubectl exec"
+#
+grep_pod_exec_output() {
+	local -r pod_name="$1"
+	shift
+	local -r grep_arg="$1"
+	shift
+
+	local -r pod_env=$(kubectl exec "${pod_name}" -- "$@")
+	info "pod_env: ${pod_env}"
+	echo "${pod_env}" | grep "${grep_arg}"
+}