From a1c37d19c61c998cd7a271746c2a7ccbb51f5af2 Mon Sep 17 00:00:00 2001
From: jinsaihang <jinsaihang@h-partners.com>
Date: Thu, 6 Feb 2025 11:53:36 +0800
Subject: [PATCH] fix some test cases

Signed-off-by: jinsaihang <jinsaihang@h-partners.com>

diff --git a/Makefile b/Makefile
index cd22f7a..29c4a53 100644
--- a/Makefile
+++ b/Makefile
@@ -52,6 +52,7 @@ dirs:
 	mkdir -p $(SYSTEMDDIR)
 	mkdir -p $(LOGCRON)
 	mkdir -p $(LIBINSTALLDIR)
+	mkdir -p $(ETCDIR)/sysSentry/
 	install -d -m 700 $(ETCDIR)/sysSentry/
 	install -d -m 700 $(ETCDIR)/sysSentry/tasks/
 	install -d -m 700 $(ETCDIR)/sysSentry/plugins/
diff --git a/selftest/test/test_xalarm_read_config_exception.sh b/selftest/test/test_xalarm_read_config_exception.sh
index d31d53b..46cc188 100644
--- a/selftest/test/test_xalarm_read_config_exception.sh
+++ b/selftest/test/test_xalarm_read_config_exception.sh
@@ -12,7 +12,8 @@ set +e
 function pre_test() {
     cp /etc/sysSentry/xalarm.conf /etc/sysSentry/xalarm.conf.bak
     rm -f ./tmp_log
-    systemctl stop xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 function do_test() {
@@ -20,7 +21,7 @@ function do_test() {
     # 异常的alarm id
     start_line=$(expr $(wc -l < /var/log/sysSentry/xalarm.log) + 1)
     echo -e "[filter]\nid_mask = 999,9999,aaa,test-and-run,7869-2431" > /etc/sysSentry/xalarm.conf
-    systemctl start xalarmd.service
+    xalarmd
     end_line=$(wc -l < /var/log/sysSentry/xalarm.log)
     sed -n "${start_line}, ${end_line}p" /var/log/sysSentry/xalarm.log >> ./tmp_log
     wait_cmd_ok "grep \"invalid alarm id 999, ignored\" ./tmp_log" 1 3
@@ -41,7 +42,9 @@ function do_test() {
     rm -f /etc/sysSentry/xalarm.conf
     start_line=$(expr $(wc -l < /var/log/sysSentry/xalarm.log) + 1)
     echo -e "[no_filter]\nid_mask = 123" > /etc/sysSentry/xalarm.conf
-    systemctl restart xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
+    xalarmd
     end_line=$(wc -l < /var/log/sysSentry/xalarm.log)
     sed -n "${start_line}, ${end_line}p" /var/log/sysSentry/xalarm.log >> ./tmp_log
     wait_cmd_ok "grep \"no filter conf\" ./tmp_log" 1 3
@@ -50,7 +53,9 @@ function do_test() {
     rm -f /etc/sysSentry/xalarm.conf
     start_line=$(expr $(wc -l < /var/log/sysSentry/xalarm.log) + 1)
     echo -e "[filter]\nid_mask_number = 123" > /etc/sysSentry/xalarm.conf
-    systemctl restart xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
+    xalarmd
     end_line=$(wc -l < /var/log/sysSentry/xalarm.log)
     sed -n "${start_line}, ${end_line}p" /var/log/sysSentry/xalarm.log >> ./tmp_log
     wait_cmd_ok "grep \"no id_mask conf\" ./tmp_log" 1 3
@@ -62,7 +67,8 @@ function post_test() {
     cat ./tmp_log
     rm -f /etc/sysSentry/xalarm.conf ./tmp_log
     cp /etc/sysSentry/xalarm.conf.bak /etc/sysSentry/xalarm.conf
-    systemctl stop xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 run_testcase
diff --git a/selftest/test/test_xalarm_register_and_unregister_exception.sh b/selftest/test/test_xalarm_register_and_unregister_exception.sh
index 5e90995..86dde59 100644
--- a/selftest/test/test_xalarm_register_and_unregister_exception.sh
+++ b/selftest/test/test_xalarm_register_and_unregister_exception.sh
@@ -13,7 +13,7 @@ function pre_test() {
     rm -rf ./checklog test/xalarm/unreg_demo test/xalarm/send_demo test/xalarm/reg_demo
     gcc test/xalarm/reg_demo.c -o test/xalarm/reg_demo -lxalarm
     gcc test/xalarm/unreg_demo.c -o test/xalarm/unreg_demo -lxalarm
-    systemctl start xalarmd.service
+    xalarmd 
 }
 
 function do_test() {
@@ -33,10 +33,12 @@ function do_test() {
 }
 
 function post_test() {
-    kill -9 $(pgrep -w unreg_demo)
+    kill $(pgrep -w unreg_demo)
+    sleep 1
     cat ./checklog
     rm -rf ./checklog test/xalarm/unreg_demo test/xalarm/reg_demo
-    systemctl stop xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 run_testcase
diff --git a/selftest/test/test_xalarm_restart_exception.sh b/selftest/test/test_xalarm_restart_exception.sh
index cc24435..236e4c3 100644
--- a/selftest/test/test_xalarm_restart_exception.sh
+++ b/selftest/test/test_xalarm_restart_exception.sh
@@ -11,7 +11,8 @@ set +e
 
 function pre_test() {
     rm -f ./tmp_log
-    systemctl stop xalarmd
+    kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 function do_test() {
@@ -21,7 +22,7 @@ function do_test() {
     /usr/bin/xalarmd
     end_line=$(wc -l < /var/log/sysSentry/xalarm.log)
 
-    process_num=$(ps -ef | grep xalarmd | grep -v grep | wc -l)
+    process_num=$(ps -ef | grep xalarmd | grep -v grep | grep -v defunct | wc -l)
     echo "xalarm process num is $process_num" >> ./tmp_log
     expect_eq $process_num 1 "check xalarmd can launch multiple times"
 
@@ -35,6 +36,7 @@ function do_test() {
 function post_test() {
     rm -f ./tmp_log
     kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 run_testcase
diff --git a/selftest/test/test_xalarm_send_message.sh b/selftest/test/test_xalarm_send_message.sh
index 310fa53..12496d2 100644
--- a/selftest/test/test_xalarm_send_message.sh
+++ b/selftest/test/test_xalarm_send_message.sh
@@ -13,7 +13,9 @@ function pre_test() {
     rm -rf ./checklog ./tmp_log test/xalarm/send_demo test/xalarm/reg_demo
     gcc test/xalarm/send_demo.c -o test/xalarm/send_demo -lxalarm
     gcc test/xalarm/reg_demo.c -o test/xalarm/reg_demo -lxalarm
-    systemctl start xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
+    xalarmd
 }
 
 function do_test() {
@@ -70,7 +72,8 @@ function do_test() {
     expect_eq $? 0 "check unregister xalarm"
 
     # 停止xalarmd服务后发消息
-    systemctl stop xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
 
     ./test/xalarm/send_demo 1001 1 2 "cpu usage high warning" >> checklog 2>&1 &
     wait_cmd_ok "grep \"xalarm_Report: sendto failed errno: 2\" ./checklog" 1 3
@@ -78,10 +81,12 @@ function do_test() {
 }
 
 function post_test() {
-    kill -9 $(pgrep -w reg_demo)
+    kill $(pgrep -w xalarmd)
+    sleep 1
     cat ./checklog
     rm -rf ./checklog ./tmp_log test/xalarm/send_demo test/xalarm/reg_demo
-    systemctl stop xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 run_testcase
diff --git a/selftest/test/test_xalarm_upgrade_exception.sh b/selftest/test/test_xalarm_upgrade_exception.sh
index 200fd89..32b2549 100644
--- a/selftest/test/test_xalarm_upgrade_exception.sh
+++ b/selftest/test/test_xalarm_upgrade_exception.sh
@@ -13,7 +13,7 @@ function pre_test() {
     rm -rf ./checklog test/xalarm/upgrade_demo test/xalarm/send_demo
     gcc test/xalarm/upgrade_demo.c -o test/xalarm/upgrade_demo -lxalarm
     gcc test/xalarm/send_demo.c -o test/xalarm/send_demo -lxalarm
-    systemctl start xalarmd.service
+    xalarmd
 }
 
 function do_test() {
@@ -40,9 +40,11 @@ function do_test() {
 
 function post_test() {
     kill -9 $(pgrep -w upgrade_demo)
+    sleep 1
     cat ./checklog
     rm -rf ./checklog test/xalarm/upgrade_demo test/xalarm/send_demo
-    systemctl stop xalarmd.service
+    kill $(pgrep -w xalarmd)
+    sleep 1
 }
 
 run_testcase
-- 
2.27.0

