From 836b708b4d196e7b9970893f73942a29dfc808b5 Mon Sep 17 00:00:00 2001 From: shixuantong Date: Fri, 10 Jan 2025 16:13:06 +0800 Subject: [PATCH] add cpu_sentry tests --- selftest/libs/cpu_sentry_lib.sh | 124 ++++++++++++++++++ selftest/libs/expect.sh | 15 ++- selftest/libs/lib.sh | 6 +- selftest/test/inspector_test/Makefile | 9 ++ selftest/test/inspector_test/inspector_test.c | 79 +++++++++++ .../test_cpu_sentry_can_not_concurrent.sh | 40 ++++++ selftest/test/test_cpu_sentry_cat_cli_001.sh | 44 +++++++ selftest/test/test_cpu_sentry_cat_cli_002.sh | 44 +++++++ selftest/test/test_cpu_sentry_cat_cli_003.sh | 44 +++++++ selftest/test/test_cpu_sentry_cat_cli_004.sh | 44 +++++++ selftest/test/test_cpu_sentry_config_001.sh | 64 +++++++++ selftest/test/test_cpu_sentry_config_002.sh | 64 +++++++++ selftest/test/test_cpu_sentry_config_003.sh | 64 +++++++++ .../test/test_cpu_sentry_longstress_001.sh | 41 ++++++ .../test/test_cpu_sentry_longstress_002.sh | 50 +++++++ .../test_cpu_sentry_proc_abnormal_exit_001.sh | 43 ++++++ selftest/test/test_cpu_sentry_time_jump.sh | 41 ++++++ selftest/testsuite_skip_list | 12 ++ 18 files changed, 824 insertions(+), 4 deletions(-) create mode 100644 selftest/libs/cpu_sentry_lib.sh create mode 100644 selftest/test/inspector_test/Makefile create mode 100644 selftest/test/inspector_test/inspector_test.c create mode 100644 selftest/test/test_cpu_sentry_can_not_concurrent.sh create mode 100644 selftest/test/test_cpu_sentry_cat_cli_001.sh create mode 100644 selftest/test/test_cpu_sentry_cat_cli_002.sh create mode 100644 selftest/test/test_cpu_sentry_cat_cli_003.sh create mode 100644 selftest/test/test_cpu_sentry_cat_cli_004.sh create mode 100644 selftest/test/test_cpu_sentry_config_001.sh create mode 100644 selftest/test/test_cpu_sentry_config_002.sh create mode 100644 selftest/test/test_cpu_sentry_config_003.sh create mode 100644 selftest/test/test_cpu_sentry_longstress_001.sh create mode 100644 selftest/test/test_cpu_sentry_longstress_002.sh create mode 100644 selftest/test/test_cpu_sentry_proc_abnormal_exit_001.sh create mode 100644 selftest/test/test_cpu_sentry_time_jump.sh diff --git a/selftest/libs/cpu_sentry_lib.sh b/selftest/libs/cpu_sentry_lib.sh new file mode 100644 index 0000000..44337a7 --- /dev/null +++ b/selftest/libs/cpu_sentry_lib.sh @@ -0,0 +1,124 @@ +#!/bin/bash +# Copyright (c), 2023-2025, Huawei Tech. Co., Ltd. +# Description: + +source "libs/wait.sh" +source "libs/expect.sh" + +DEFAULT_PATROL_SECOND=60 +DEFAULT_CPU_UTILITY=100 +DEFAULT_PATROL_TIMES=1 +SYSSENTRY_FILES=("/var/run/sysSentry/control.sock" "/var/run/sysSentry/heartbeat.sock" "/var/run/sysSentry/result.sock" "/var/run/sysSentry/sysSentry.pid") + +function start_sysSentry() { + syssentry & + expect_eq "$?" 0 "exec syssentry failed" + sleep 3 + for file in "${SYSSENTRY_FILES[@]}"; do + wait_cmd_ok "ls ${file}" 1 5 + [[ "$?" -ne 0 ]] && return 1 + done + return 0 +} + +function stop_sysSentry() { + kill -9 $(pgrep -w syssentry) + expect_eq "$?" 0 "kill syssentry failed" + wait_cmd_nok "ls /var/run/sysSentry/sysSentry.pid" 1 5 + [[ "$?" -ne 0 ]] && return 1 + return 0 +} + +function pre_cpu_sentry() { + cp /etc/sysSentry/plugins/cpu_sentry.ini /etc/sysSentry/plugins/cpu_sentry.ini_bak + + cpu_num=$(nproc) + expect_ge "${cpu_num}" 4 "check cpu number, the number in the test environment must be greater than 4" + + start_sysSentry + expect_eq "$?" 0 "start sysSentry failed" + + pre_cpu_sentry_ko + expect_eq "$?" 0 "insmod ko failed" + + echo 0 > /sys/module/inspector_test/parameters/g_probability + expect_eq "$?" 0 "set /sys/module/inspector_test/parameters/g_probability failed" +} + +function clean_cpu_sentry() { + mv /etc/sysSentry/plugins/cpu_sentry.ini_bak /etc/sysSentry/plugins/cpu_sentry.ini -f + # online cpu + cpu_num=$(ls /sys/devices/system/cpu/ | grep -oP 'cpu\d+' | wc -l) + for ((i=1; i /sys/devices/system/cpu/cpu$i/online + fi + done + stop_sysSentry + rm_cpu_sentry_ko +} + +function check_cpu_offline() { + local cpu_list="$1" + IFS="," read -ra addr <<< "${cpu_list}" + for cpu in "${addr[@]}"; do + echo "${cpu}" | grep "-" + if [[ "$?" -eq 0 ]]; then + cpu=$(echo "${cpu}" | sed 's/-/ /') + for cpu_2 in $(eval seq "${cpu}"); do + status=$(cat /sys/devices/system/cpu/cpu"${cpu_2}"/online) + expect_eq "${status}" 0 "cpu ${cpu} status is offline" + done + else + status=$(cat /sys/devices/system/cpu/cpu"${cpu}"/online) + expect_eq "${status}" 0 "cpu ${cpu} status is offline" + fi + done +} + +function cpu_online() { + local cpu_list="$1" + IFS=","read-raaddr<<<"${cpu_list}" + for cpu in "${addr[@]}"; do + echo "${cpu}" |grep "-" + if [[ "$?" -eq 0 ]]; then + cpu=$(echo "${cpu}" | sed 's/-/ /') + for cpu_2 in $(eval seq "${cpu}"); do + echo 1 > /sys/devices/system/cpu/cpu"${cpu_2}"/online + expect_eq "$?" 0 "cpu ${cpu} online" + done + else + echo 1 > /sys/devices/system/cpu/cpu"${cpu}"/online + expect_eq "$?" 0 "cpu ${cpu} online" + fi + done +} + +function pre_cpu_sentry_ko() { + modprobe cpu_inspect || return 1 + if [[ ! -e test/inspector_test/inspector_test.ko ]]; then + cd test/inspector_test/ + make || return 1 + cd - + fi + insmod test/inspector_test/inspector_test.ko || return 1 +} + +function rm_cpu_sentry_ko() { + rmmod inspector_test + rmmod cpu_inspect +} + +function set_cpu_sentry_param() { + local param_name="$1" + local value="$2" + sed -i "s/${param_name} = .*/${param_name} = ${value}/g" /etc/sysSentry/plugins/cpu_sentry.ini +} + +function expect_cpu_sentry_task_status_eq() { + local task_name="cpu_sentry" + local expect_status="$1" + expect_task_status_eq "${task_name}" "${expect_status}" +} diff --git a/selftest/libs/expect.sh b/selftest/libs/expect.sh index a084541..eee9821 100644 --- a/selftest/libs/expect.sh +++ b/selftest/libs/expect.sh @@ -67,6 +67,19 @@ function expect_gt() { fi } +function expect_ge() { + local actual=${1:-0} + local expect=${2:-1} + local msg=${3:-} + if [ "${actual}" -ge "${expect}" ]; then + return 0 + else + ((++syssentry_expects_failed)) + log_error "expect_ge(${actual}, ${expect}, msg=${msg}) - $(get_file_line)" + return 1 + fi +} + function expect_true() { local cmd=$1 local msg=$2 @@ -126,7 +139,7 @@ function expect_task_status_eq() { local expect_status="$2" local status for i in $(seq 3); do - status=$(sentryctl status ${task_name} | awk '{print $2}') + status=$(sentryctl status "${task_name}" | awk '{print $2}') [[ "${status}" == "${expect_status}" ]] && break sleep 1 done diff --git a/selftest/libs/lib.sh b/selftest/libs/lib.sh index edf7e81..676a9b3 100644 --- a/selftest/libs/lib.sh +++ b/selftest/libs/lib.sh @@ -6,16 +6,16 @@ OLD_PATH=$(pwd) function run_testcase() { local testcasename=$1 - trap post_test EXIT INT TERM + trap post_test EXIT INT TERM KILL pre_test do_test if [ "$(get_expects_failed_num)" != "0" ]; then echo "$testcasename check failed!!" - exit 1 + exit 1 else echo "$testcasename check successful!!" - exit 0 + exit 0 fi } diff --git a/selftest/test/inspector_test/Makefile b/selftest/test/inspector_test/Makefile new file mode 100644 index 0000000..e64702b --- /dev/null +++ b/selftest/test/inspector_test/Makefile @@ -0,0 +1,9 @@ +KDIR ?= /lib/modules/$(shell uname -r)/build/ + +obj-m := inspector_test.o + +all: + $(MAKE) -C $(KDIR) M=$(PWD); + +clean: + $(MAKE) -C $(KDIR) M=$(PWD) clean; diff --git a/selftest/test/inspector_test/inspector_test.c b/selftest/test/inspector_test/inspector_test.c new file mode 100644 index 0000000..91ccf8a --- /dev/null +++ b/selftest/test/inspector_test/inspector_test.c @@ -0,0 +1,79 @@ +/* + * inspect-generic: cpu_inspect inspector for debug only + * + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2025, All rightes reserved. + * + * Author: Yu Liao + * + */ + +#include +#include +#include +#include +#include + +static unsigned int g_probability = 5; +static unsigned int g_delayTime = 3; +static unsigned int g_maxProbability = 1000000; + +int testcase1(void) +{ + mdelay(g_delayTime); + return 0; +} + +int testcase2(void) +{ + mdelay(g_delayTime); + return 0; +} + +int testcase3(void) +{ + mdelay(g_delayTime); + if (g_probability > g_maxProbability) + g_probability = g_maxProbability; + + return (get_random_u64() % (g_maxProbability / g_probability)) ? 0 : 1; +} + +int (*testcases[])(void) = { + testcase1, + testcase2, + testcase3 +}; + +static const unsigned int case_num = ARRAY_SIZE(testcases); + +static int nonconf_start_inspect_group(unsigned int group) +{ + if (testcases[group]()) + return group; + + return 0; +} + +static struct cpu_inspector nonconf_inspector = { + .name = "test", + .start_inspect = nonconf_start_inspect_group, +}; + +/** + * init_ladder - initializes the governor + */ +static __init int init_nonconf(void) +{ + nonconf_inspector.group_num = case_num; + return cpuinspect_register_inspector(&nonconf_inspector); +} + +static __exit void exit_nonconf(void) +{ + cpuinspect_unregister_inspector(&nonconf_inspector); +} + +MODULE_LICENSE("GPL"); +module_param(g_probability, uint, 0644); +module_init(init_nonconf); +module_exit(exit_nonconf); diff --git a/selftest/test/test_cpu_sentry_can_not_concurrent.sh b/selftest/test/test_cpu_sentry_can_not_concurrent.sh new file mode 100644 index 0000000..f38e104 --- /dev/null +++ b/selftest/test/test_cpu_sentry_can_not_concurrent.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: concurrent cpu_sentry is not supported +# Author: chuchengkai +# Create: 2024-07-29 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + sentryctl start cpu_sentry + expect_eq "$?" 0 + expect_cpu_sentry_task_status_eq "RUNNING" + # 'sentryctl' cmd can not be concurrently executed + info=$(sentryctl start cpu_sentry) + expect_eq "$?" 0 + echo "${info}" | grep "task is running, please wait" + expect_eq "$?" 0 + # 'cat-cli' cmd can not be concurrently executed + info=$(cat-cli -m 0x0001 --cpulist 0 --patrol_second 60) + expect_ne "$?" 0 + echo "${info}" |grep "AN cpu patrol instance is already running" + expect_eq "$?" 0 + # the kernel interface can not be invoked concurrently + echo 1 > /sys/devices/system/cpu/cpuinspect/start_patrol + expect_ne "$?" 0 + + sentryctl stop cpu_sentry + expect_cpu_sentry_task_status_eq "EXITED" +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_cat_cli_001.sh b/selftest/test/test_cpu_sentry_cat_cli_001.sh new file mode 100644 index 0000000..90fa004 --- /dev/null +++ b/selftest/test/test_cpu_sentry_cat_cli_001.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test cat-cli cmd --patrol_module param +# Author: chuchengkai +# Create: 2024-07-19 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("0x0001") + invalid_list=("0x0000", "0x0002" "1234" "1.1" "abcd" "#$%" "") + # check valid value for --patrol_module + for arg in "${valid_list[@]}"; do + cat-cli -m "${arg}" --cpulist 0 --patrol_second 1 + expect_eq "$?" 0 "valid arg ${arg} test fail" + + cat-cli --patrol_module "${arg}" --cpulist 0 --patrol_second 1 + expect_eq "$?" 0 "valid arg ${arg} test fail" + done + + # check invalid value for --patrol_module + for arg in "${invalid_list[@]}"; do + cat-cli -m "${arg}" --cpulist 0 --patrol_second 1 + expect_ne "$?" 0 "invalid arg ${arg} test fail" + + cat-cli --patrol_module "${arg}" --cpulist 0 --patrol_second 1 + expect_ne "$?" 0 "invalid arg ${arg} test fail" + done + + # --patrol_module not set + cat-cli -l 0 --patrol_second 1 + expect_ne "$?" 0 "not set --patrol_module, test failed" +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_cat_cli_002.sh b/selftest/test/test_cpu_sentry_cat_cli_002.sh new file mode 100644 index 0000000..b27a6a5 --- /dev/null +++ b/selftest/test/test_cpu_sentry_cat_cli_002.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test cat-cli cmd --cpulist param +# Author: chuchengkai +# Create: 2024-07-19 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("1" "1,3" "1-3") + invalid_list=("9999", "1;2" "1.1" "abcd" "#$%" "") + # check valid value for --cpulist + for arg in "${valid_list[@]}"; do + cat-cli -m 0x0001 -l "${arg}" --patrol_second 1 + expect_eq "$?" 0 "valid arg ${arg} test fail" + + cat-cli -m 0x0001 --cpulist "${arg}" --patrol_second 1 + expect_eq "$?" 0 "valid arg ${arg} test fail" + done + + # check invalid value for --cpulist + for arg in "${invalid_list[@]}"; do + cat-cli -m 0x0001 -l "${arg}" --patrol_second 1 + expect_ne "$?" 0 "invalid arg ${arg} test fail" + + cat-cli -m 0x0001 --cpulist "${arg}" --patrol_second 1 + expect_ne "$?" 0 "invalid arg ${arg} test fail" + done + + # --cpulist not set + cat-cli -m 0x0001 --patrol_second 1 + expect_ne "$?" 0 "not set --cpulist, test failed" +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_cat_cli_003.sh b/selftest/test/test_cpu_sentry_cat_cli_003.sh new file mode 100644 index 0000000..8c8e53c --- /dev/null +++ b/selftest/test/test_cpu_sentry_cat_cli_003.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test cat-cli cmd --patrol_second param +# Author: chuchengkai +# Create: 2024-07-19 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("1" "100" "2147483647") + invalid_list=("0", "2147483648" "-1" "1.1" "abcd" "#$%" "") + # check valid value for --patrol_second + for arg in "${valid_list[@]}"; do + timeout 0.5 cat-cli -m 0x0001 --cpulist 0 -t "${arg}" + expect_eq "$?" 124 "valid arg ${arg} test fail" + + timeout 0.5 cat-cli -m 0x0001 --cpulist 0 --patrol_second "${arg}" + expect_eq "$?" 124 "valid arg ${arg} test fail" + done + + # check invalid value for --patrol_second + for arg in "${invalid_list[@]}"; do + cat-cli -m 0x0001 --cpulist 0 -t "${arg}" + expect_ne "$?" 0 "invalid arg ${arg} test fail" + + cat-cli -m 0x0001 --cpulist 0 --patrol_second "${arg}" + expect_ne "$?" 0 "invalid arg ${arg} test fail" + done + + # --patrol_second not set + cat-cli -m 0x0001 -l 1 + expect_ne "$?" 0 "not set --patrol_second, test failed" +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_cat_cli_004.sh b/selftest/test/test_cpu_sentry_cat_cli_004.sh new file mode 100644 index 0000000..2280862 --- /dev/null +++ b/selftest/test/test_cpu_sentry_cat_cli_004.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test cat-cli cmd --cpu_utility param +# Author: chuchengkai +# Create: 2024-07-19 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("1" "100" "50") + invalid_list=("0", "101" "-1" "1.1" "abcd" "#$%" "") + # check valid value for --cpu_utility + for arg in "${valid_list[@]}"; do + cat-cli -m 0x0001 --cpulist 0 --patrol_second 1 --cpu_utility "${arg}" + expect_eq "$?" 0 "valid arg ${arg} test fail" + + cat-cli -m 0x0001 --cpulist 0 --patrol_second 1 -u "${arg}" + expect_eq "$?" 0 "valid arg ${arg} test fail" + done + + # check invalid value for --cpu_utility + for arg in "${invalid_list[@]}"; do + cat-cli -m 0x0001 --cpulist 0 --patrol_second 1 --cpu_utility "${arg}" + expect_ne "$?" 0 "invalid arg ${arg} test fail" + + cat-cli -m 0x0001 --cpulist 0 --patrol_second 1 -u "${arg}" + expect_ne "$?" 0 "invalid arg ${arg} test fail" + done + + # --cpu_utility not set + cat-cli -m 0x0001 -l 0 -t 1 + expect_eq "$?" 0 "not set --cpu_utility, test failed" +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_config_001.sh b/selftest/test/test_cpu_sentry_config_001.sh new file mode 100644 index 0000000..01f1ef2 --- /dev/null +++ b/selftest/test/test_cpu_sentry_config_001.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test cpu_list param +# Author: chuchengkai +# Create: 2024-07-22 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("1" "1,3" "1-3") + invalid_list=("9999", "1;2" "1.1" "abcd" "#$%" "") + # check valid value for cpu_list + for arg in "${valid_list[@]}"; do + set_cpu_sentry_param "cpu_list" "${arg}" + + sentryctl start cpu_sentry + expect_eq "$?" 0 "valid arg ${arg} test fail, start task failed" + + wait_cmd_ok "ps -ef | grep -w 'cat-cli' | grep '\-l ${arg}'" 1 5 + expect_eq "$?" 0 "valid arg ${arg} test fail, check cat-cli exist failed" + + expect_cpu_sentry_task_status_eq "RUNNING" "valid arg ${arg} test fail, task status should be running, but it's not" + + sentryctl stop cpu_sentry + expect_eq "$?" 0 "valid arg ${arg} test fail, stop task failed" + + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "valid arg ${arg} test fail, check cat-cli do not exist failed" + done + + # check invalid value for cpu_test + for arg in "${invalid_list[@]}"; do + set_cpu_sentry_param "cpu_list" "${arg}" + sentryctl start cpu_sentry + expect_eq "$?" 0 + + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "invalid arg ${arg} test fail, check cat-cli process" + + wait_cmd_nok "ps -ef | grep -w 'cpu_sentry' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "invalid arg ${arg} test fail, check cp_sentry process" + done + + # cpu_list do not set + sed -i "s/cpu_list = .*//g" /etc/sysSentry/plugins/cpu_sentry.ini + sentryctl start cpu_sentry + expect_eq "$?" 0 + + expect_cpu_sentry_task_status_eq "RUNNING" "cpu_list test failed, don't set it in cpu_sentry.ini" + + sentryctl stop cpu_sentry + expect_eq "$?" 0 +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_config_002.sh b/selftest/test/test_cpu_sentry_config_002.sh new file mode 100644 index 0000000..cfcffd3 --- /dev/null +++ b/selftest/test/test_cpu_sentry_config_002.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test patrol_second param +# Author: chuchengkai +# Create: 2024-07-22 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("1" "100" "2147483647") + invalid_list=("0", "2147483648" "-1" "1,1" "abcd" "#$%" "") + # check valid value for patrol_second + for arg in "${valid_list[@]}"; do + set_cpu_sentry_param "patrol_second" "${arg}" + + sentryctl start cpu_sentry + expect_eq "$?" 0 "valid arg ${arg} test fail, start task failed" + + wait_cmd_ok "ps -ef | grep -w 'cat-cli' | grep '\-t ${arg}'" 1 5 + expect_eq "$?" 0 "valid arg ${arg} test fail, check cat-cli exist failed" + + expect_cpu_sentry_task_status_eq "RUNNING" "valid arg ${arg} test fail, task status should be running, but it's not" + + sentryctl stop cpu_sentry + expect_eq "$?" 0 "valid arg ${arg} test fail, stop task failed" + + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "valid arg ${arg} test fail, check cat-cli do not exist failed" + done + + # check invalid value for patrol_second + for arg in "${invalid_list[@]}"; do + set_cpu_sentry_param "patrol_second" "${arg}" + sentryctl start cpu_sentry + expect_eq "$?" 0 + + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "invalid arg ${arg} test fail, check cat-cli process" + + wait_cmd_nok "ps -ef | grep -w 'cpu_sentry' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "invalid arg ${arg} test fail, check cp_sentry process" + done + + # patrol_second do not set + sed -i "s/patrol_second = .*//g" /etc/sysSentry/plugins/cpu_sentry.ini + sentryctl start cpu_sentry + expect_eq "$?" 0 + + expect_cpu_sentry_task_status_eq "RUNNING" "patrol_second test failed, don't set it in cpu_sentry.ini" + + sentryctl stop cpu_sentry + expect_eq "$?" 0 +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_config_003.sh b/selftest/test/test_cpu_sentry_config_003.sh new file mode 100644 index 0000000..c02b590 --- /dev/null +++ b/selftest/test/test_cpu_sentry_config_003.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test cpu_utility param +# Author: chuchengkai +# Create: 2024-07-22 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + valid_list=("1" "100" "50") + invalid_list=("0", "101" "-1" "1,1" "abcd" "#$%" "") + # check valid value for cpu_utility + for arg in "${valid_list[@]}"; do + set_cpu_sentry_param "cpu_utility" "${arg}" + + sentryctl start cpu_sentry + expect_eq "$?" 0 "valid arg ${arg} test fail, start task failed" + + wait_cmd_ok "ps -ef | grep -w 'cat-cli' | grep '\-u ${arg}'" 1 5 + expect_eq "$?" 0 "valid arg ${arg} test fail, check cat-cli exist failed" + + expect_cpu_sentry_task_status_eq "RUNNING" "valid arg ${arg} test fail, task status should be running, but it's not" + + sentryctl stop cpu_sentry + expect_eq "$?" 0 "valid arg ${arg} test fail, stop task failed" + + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "valid arg ${arg} test fail, check cat-cli do not exist failed" + done + + # check invalid value for cpu_utility + for arg in "${invalid_list[@]}"; do + set_cpu_sentry_param "cpu_utility" "${arg}" + sentryctl start cpu_sentry + expect_eq "$?" 0 + + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "invalid arg ${arg} test fail, check cat-cli process" + + wait_cmd_nok "ps -ef | grep -w 'cpu_sentry' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "invalid arg ${arg} test fail, check cp_sentry process" + done + + # cpu_utility do not set + sed -i "s/cpu_utility = .*//g" /etc/sysSentry/plugins/cpu_sentry.ini + sentryctl start cpu_sentry + expect_eq "$?" 0 + + expect_cpu_sentry_task_status_eq "RUNNING" "patrol_second test failed, don't set it in cpu_sentry.ini" + + sentryctl stop cpu_sentry + expect_eq "$?" 0 +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_longstress_001.sh b/selftest/test/test_cpu_sentry_longstress_001.sh new file mode 100644 index 0000000..d7aa0a2 --- /dev/null +++ b/selftest/test/test_cpu_sentry_longstress_001.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: long-term stability test +# Author: chuchengkai +# Create: 2024-07-29 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry + echo 1000000 > /sys/module/inspector_test/parameters/g_probability + dmesg -c > /dev/null +} + +function do_test() { + test_cpu=1 + set_cpu_sentry_param "cpu_list" "${test_cpu}" + # test.sh set timeout is 300s + set_cpu_sentry_param "patrol_second" 280 + sentryctl start cpu_sentry + expect_eq "$?" 0 + state="RUNNING" + expect_cpu_sentry_task_status_eq "RUNNING" + while [[ "${state}" == "RUNNING" ]]; do + wait_cmd_ok "cat /sys/devices/system/cpu/cpu${test_cpu}/online |grep 0" 1 10 + expect_eq "$?" 0 + cpu_online "${test_cpu}" + sleep 1 + state=$(sentryctl status cpu_sentry | awk '{print $2}') + [[ "${sysSentry_expects_failed}" -ne 0 ]] && break + done + dmesg -T | grep -i 'call trace' + expect_ne "$?" 0 +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_longstress_002.sh b/selftest/test/test_cpu_sentry_longstress_002.sh new file mode 100644 index 0000000..f36d7d5 --- /dev/null +++ b/selftest/test/test_cpu_sentry_longstress_002.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: long-term stability test +# Author: chuchengkai +# Create: 2024-09-05 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry + echo 1000000 > /sys/module/inspector_test/parameters/g_probability + tail -n 0 -f /var/log/sysSentry/sysSentry.log > /tmp/cpu_sentry.log & + log_pid="$!" + dmesg -c > /dev/null +} + +function do_test() { + times=100 + test_cpu=1 + set_cpu_sentry_param "cpu_list" "${test_cpu}" + set_cpu_sentry_param "patrol_second" 1 + for arg in $(seq "${times}"); do + echo "" > /tmp/cpu_sentry.log + # remove and install the ko + rmmod cpu_inspect + expect_ne "$?" 0 "remove cpu_inspect.ko success, expect failed" + insmod /lib/modules/"$(uname -r)"/kernel/drivers/cpuinspect/cpu_inspect.ko.xz + expect_ne "$?" 0 "insmod cpu_inspect.ko success, expect failed" + # run task, check cpu + sentryctl start cpu_sentry + expect_eq "$?" 0 + expect_cpu_sentry_task_status_eq "RUNNING" + wait_cmd_ok "cat /tmp/cpu_sentry.log | grep isolated_cpu_list.*${test_cpu}" 1 9 + expect_eq "$?" 0 "check log info failed" + check_cpu_offline "${test_cpu}" + cpu_online "${test_cpu}" + [[ "${sysSentry_expects_failed}" -ne 0 ]] && break + done + dmesg -T | grep -i 'call trace' + expect_ne "$?" 0 +} + +function post_test() { + kill -9 "${log_pid}" + rm -rf /tmp/cpu_sentry.log + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_proc_abnormal_exit_001.sh b/selftest/test/test_cpu_sentry_proc_abnormal_exit_001.sh new file mode 100644 index 0000000..45bdb83 --- /dev/null +++ b/selftest/test/test_cpu_sentry_proc_abnormal_exit_001.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test process abnormal exit +# Author: chuchengkai +# Create: 2024-09-05 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + test_with_sig 2 "FAILED. config may be incorrect or the command may be invalid/killed!" + test_with_sig 15 "FAILED. config may be incorrect or the command may be invalid/killed!" + test_with_sig 9 +} + +function test_with_sig() { + local sig="$1" + local msg="$2" + sentryctl start cpu_sentry + expect_eq "$?" 0 + + wait_cmd_ok "ps -ef | grep 'cpu_sentry' | grep -v 'grep'" 1 6 + wait_cmd_ok "ps -ef | grep 'cat-cli' | grep -v 'grep'" 1 6 + + expect_cpu_sentry_task_status_eq "RUNNING" + + kill -"${sig}" "$(pgrep cpu_sentry)" + wait_cmd_ok "sentryctl get_result cpu_sentry | grep 'error_msg' | grep '\"${msg}\"'" 1 5 + expect_eq "$?" 0 "check result failed" + + wait_cmd_nok "ps -ef | grep 'cpu_sentry' | grep -v 'grep'" 1 6 + wait_cmd_nok "ps -ef | grep 'cat-cli' | grep -v 'grep'" 1 6 +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/test/test_cpu_sentry_time_jump.sh b/selftest/test/test_cpu_sentry_time_jump.sh new file mode 100644 index 0000000..4f0cd25 --- /dev/null +++ b/selftest/test/test_cpu_sentry_time_jump.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# Copyright (C), 2024-2025, Huawei Tech. Co., Ltd. +# Description: test time jump +# Author: chuchengkai +# Create: 2024-07-22 + +source "libs/lib.sh" +source "libs/cpu_sentry_lib.sh" + +function pre_test() { + pre_cpu_sentry +} + +function do_test() { + test_time=10 + set_cpu_sentry_param "patrol_second" "${test_time}" + sentryctl start cpu_sentry + expect_eq "$?" 0 + expect_cpu_sentry_task_status_eq "RUNNING" + # time jump after 1000min + date -s "1000 minute" + expect_cpu_sentry_task_status_eq "RUNNING" "after the time changes for 1000min, check status failed" + wait_cmd_ok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "after the time changes for 1000min, check cat-cli failed" + # time recovery + hwclock -s + expect_cpu_sentry_task_status_eq "RUNNING" "after the time is restored, check status failed" + wait_cmd_ok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "after the time is restored, check cat-cli failed" + # the run time of the task is not affected + sleep ${test_time} + expect_cpu_sentry_task_status_eq "EXITED" + wait_cmd_nok "ps -ef | grep -w 'cat-cli' | grep -v 'grep'" 1 5 + expect_eq "$?" 0 "after sleeping, task is not complete" +} + +function post_test() { + clean_cpu_sentry +} + +run_testcase diff --git a/selftest/testsuite_skip_list b/selftest/testsuite_skip_list index bb67c88..9789012 100644 --- a/selftest/testsuite_skip_list +++ b/selftest/testsuite_skip_list @@ -6,3 +6,15 @@ # testsuite:arch1 # testsuite:arch2 test_cpu_sentry:x86_64 +test_cpu_sentry_cat_cli_001:x86_64 +test_cpu_sentry_cat_cli_002:x86_64 +test_cpu_sentry_cat_cli_003:x86_64 +test_cpu_sentry_cat_cli_004:x86_64 +test_cpu_sentry_config_001:x86_64 +test_cpu_sentry_config_002:x86_64 +test_cpu_sentry_config_003:x86_64 +test_cpu_sentry_time_jump:x86_64 +test_cpu_sentry_can_not_concurrent:x86_64 +test_cpu_sentry_longstress_001:x86_64 +test_cpu_sentry_longstress_002:x86_64 +test_cpu_sentry_proc_abnormal_exit_001:x86_64 -- Gitee