From 6bd770760c36a6ca2c136b920f9c712da5b755fc Mon Sep 17 00:00:00 2001 From: Hongyu Ning Date: Wed, 27 Sep 2023 15:17:10 +0800 Subject: [PATCH] [New][guest-test]: test_executor initial commit test_executor includes basic framework to prepare and run tests in guest vm based on $TESTCASE Signed-off-by: Hongyu Ning --- guest-test/guest.test_executor.sh | 111 ++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100755 guest-test/guest.test_executor.sh diff --git a/guest-test/guest.test_executor.sh b/guest-test/guest.test_executor.sh new file mode 100755 index 00000000..4bfdf368 --- /dev/null +++ b/guest-test/guest.test_executor.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only +# Copyright (c) 2023 Intel Corporation + +# Author: Hongyu Ning +# +# History: 24, Aug., 2023 - Hongyu Ning - creation + + +# @desc This script prepare and run $TESTCASE in Guest VM + +###################### Variables ###################### +SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" +echo "$SCRIPT_DIR" +GUEST_TEST_DIR="/root/guest_test/" + +###################### Functions ###################### + +# function based on sshpass to scp common.sh and $1 test_script.sh to Guest VM +guest_test_prepare() { + rm -rf common.sh + wget https://raw.githubusercontent.com/intel/lkvs/main/common/common.sh + sshpass -e ssh -p "$PORT" -o StrictHostKeyChecking=no root@localhost << EOF + rm -rf $GUEST_TEST_DIR + mkdir $GUEST_TEST_DIR +EOF + sshpass -e scp -P "$PORT" -o StrictHostKeyChecking=no common.sh root@localhost:$GUEST_TEST_DIR + sshpass -e scp -P "$PORT" -o StrictHostKeyChecking=no $1 root@localhost:$GUEST_TEST_DIR + test_print_trc "Guest VM test script prepare complete" +} + +# function based on sshpass to scp $1 source_code_dir and compile $2 test_binary in Guest VM +guest_test_source_code() { + sshpass -e scp -P "$PORT" -o StrictHostKeyChecking=no -r $1 root@localhost:$GUEST_TEST_DIR + sshpass -e ssh -p "$PORT" -o StrictHostKeyChecking=no root@localhost << EOF + source $GUEST_TEST_DIR/common.sh + cd $GUEST_TEST_DIR/$1 + make || die "Failed to compile source code $1" + if [ -f $2 ]; then + chmod a+x $2 + cp $2 $GUEST_TEST_DIR + else + die "Can't find test binary $2" + fi +EOF + test_print_trc "Guest VM test source code and binary prepare complete" +} + +# function based on sshpass to execute $1 test_script.sh and potential $2 script params in Guest VM +guest_test_entry() { + sshpass -e ssh -p "$PORT" -o StrictHostKeyChecking=no root@localhost << EOF + source $GUEST_TEST_DIR/common.sh + cd $GUEST_TEST_DIR + test_print_trc "guest_test_entry args 1: $1" + test_print_trc "guest_test_entry args 2: $2" + ./$1 $2 +EOF +ERR_NUM=$? +if [ $ERR_NUM -eq 0 ] || [ $ERR_NUM -eq 255 ]; then + return 0 +else + return 1 +fi +} + +# function based on sshpass to close VM +guest_test_close() { + sshpass -e ssh -p "$PORT" -o StrictHostKeyChecking=no root@localhost << EOF + source $GUEST_TEST_DIR/common.sh + test_print_trc "guest test complete, close VM now" + systemctl reboot --reboot-argument=now +EOF + test_print_trc "Guest VM closed properly after test" +} + +###################### Do Works ###################### +cd "$(dirname "$0")" 2>/dev/null || exit 1 +source ../.env + +# get test scenario config for test_executor +source $SCRIPT_DIR/test_params.py + +cd $SCRIPT_DIR +# select test_functions by $TEST_SCENARIO +case "$TESTCASE" in + TD_BOOT) + guest_test_prepare tdx_guest_boot_check.sh + guest_test_entry tdx_guest_boot_check.sh "-v $VCPU -s $SOCKETS -m $MEM" || \ + die "Failed on TD_BOOT test tdx_guest_boot_check.sh -v $VCPU -s $SOCKETS -m $MEM" + if [[ $GCOV == "off" ]]; then + guest_test_close + fi + ;; + GUEST_TESTCASE_EXAMPLE) + guest_test_prepare guest_test.sh + guest_test_source_code test_source_code_dir_example test_binary_example + guest_test_entry guest_test.sh "-t $TESTCASE" || \ + die "Failed on GUEST_TESTCASE_EXAMPLE guest_test.sh -t $TESTCASE" + if [[ $GCOV == "off" ]]; then + guest_test_close + fi + ;; + :) + test_print_err "Must specify the test scenario option by [-t]" + usage && exit 1 + ;; + \?) + test_print_err "Input test case option $TESTCASE is not supported" + usage && exit 1 + ;; +esac \ No newline at end of file