44
55# Function to check if systemd is running with PID 1
66directory=$( dirname " $0 " )
7- check_systemd_pid () {
8- if [ " $( ps -p 1 -o comm=) " = " systemd" ]; then
9- echo " PASS: Check systemd PID" > $directory /CheckSystemdPID.res
10- else
11- echo " FAIL: Check systemd PID" > $directory /CheckSystemdPID.res
7+
8+ # Robustly find and source init_env
9+ SCRIPT_DIR=" $( cd " $( dirname " $0 " ) " && pwd) "
10+ INIT_ENV=" "
11+ SEARCH=" $SCRIPT_DIR "
12+ while [ " $SEARCH " != " /" ]; do
13+ if [ -f " $SEARCH /init_env" ]; then
14+ INIT_ENV=" $SEARCH /init_env"
15+ break
1216 fi
17+ SEARCH=$( dirname " $SEARCH " )
18+ done
19+
20+ if [ -z " $INIT_ENV " ]; then
21+ echo " [ERROR] Could not find init_env (starting at $SCRIPT_DIR )" >&2
22+ exit 1
23+ fi
24+
25+ # Only source if not already loaded (idempotent)
26+ if [ -z " $__INIT_ENV_LOADED " ]; then
27+ # shellcheck disable=SC1090
28+ . " $INIT_ENV "
29+ fi
30+ # Always source functestlib.sh, using $TOOLS exported by init_env
31+ # shellcheck disable=SC1090,SC1091
32+ . " $TOOLS /functestlib.sh"
33+
34+ ANY_SUBTEST_FAILED=" false"
35+
36+
37+ check_systemd_pid () {
38+ TESTNAME=" CheckSystemdPID"
39+ res_file=" ./$TESTNAME .res"
40+ log_info " ----------------------------------------------------"
41+ log_info " -------- Starting $TESTNAME Functional Test --------"
42+ if [ " $( ps -p 1 -o comm=) " = " systemd" ]; then
43+ log_pass " Systemd init started with PID 1"
44+ echo " $TESTNAME PASS" > " $res_file "
45+ else
46+ log_fail " Systemd init did not start with PID 1"
47+ echo " $TESTNAME FAIL" > " $res_file "
48+ ANY_SUBTEST_FAILED=" true"
49+ fi
50+ log_info " ----------------------------------------------------"
51+ log_info " -------- Stopping $TESTNAME Functional Test --------"
1352}
1453
1554# Function to check if systemctl stop command works for systemd-user-sessions.service
55+
1656check_systemctl_stop () {
17- systemctl stop systemd-user-sessions.service
18- if systemctl is-active --quiet systemd-user-sessions.service; then
19- echo " FAIL: Systemctl Stop Service" > $directory /CheckSystemctlStop.res
20- else
21- echo " PASS: Systemctl Stop Service" > $directory /CheckSystemctlStop.res
22- fi
57+ TESTNAME=" CheckSystemctlStop"
58+ res_file=" ./$TESTNAME .res"
59+ log_info " ----------------------------------------------------"
60+ log_info " -------- Starting $TESTNAME Functional Test --------"
61+ systemctl stop systemd-user-sessions.service
62+ if systemctl is-active --quiet systemd-user-sessions.service; then
63+ log_fail " Not able to stop the service systemd-user-sessions with systemctl"
64+ echo " $TESTNAME FAIL" > " $res_file "
65+ ANY_SUBTEST_FAILED=" true"
66+ else
67+ log_pass " Able to stop the service systemd-user-sessions with systemctl"
68+ echo " $TESTNAME PASS" > " $res_file "
69+ fi
70+ log_info " ----------------------------------------------------"
71+ log_info " -------- Stopping $TESTNAME Functional Test --------"
2372}
2473
2574# Function to check if systemctl start command works for systemd-user-sessions.service
2675check_systemctl_start () {
27- systemctl start systemd-user-sessions.service
28- if systemctl is-active --quiet systemd-user-sessions.service; then
29- echo " PASS: Systemctl Start Service" > $directory /CheckSystemctlStart.res
30- else
31- echo " FAIL: Systemctl Start Service" > $directory /CheckSystemctlStart.res
32- fi
76+ TESTNAME=" CheckSystemctlStart"
77+ res_file=" ./$TESTNAME .res"
78+ log_info " ----------------------------------------------------"
79+ log_info " -------- Starting $TESTNAME Functional Test --------"
80+ systemctl start systemd-user-sessions.service
81+ if systemctl is-active --quiet systemd-user-sessions.service; then
82+ log_pass " Service started successfully with systemctl command"
83+ echo " $TESTNAME PASS" > " $res_file "
84+ else
85+ log_fail " Failed to start service with systemctl command"
86+ echo " $TESTNAME FAIL" > " $res_File "
87+ ANY_SUBTEST_FAILED=" true"
88+ fi
89+ log_info " ----------------------------------------------------"
90+ log_info " -------- Stopping $TESTNAME Functional Test --------"
3391}
3492
3593# Function to check for any failed services and print them
3694check_failed_services () {
37- failed_services=$( systemctl --failed --no-legend --plain | awk ' {print $1}' )
38- if [ -z " $failed_services " ]; then
39- echo " PASS: Check failed services" > $directory /CheckFailedServices.res
40- else
41- echo " FAIL: Check failed services" > $directory /CheckFailedServices.res
42- echo " $failed_services "
43- fi
44-
95+ TESTNAME=" CheckFailedServices"
96+ res_file=" ./$TESTNAME .res"
97+ log_info " ----------------------------------------------------"
98+ log_info " -------- Starting $TESTNAME Functional Test --------"
99+ failed_services=$( systemctl --failed --no-legend --plain | awk ' {print $1}' )
100+ if [ -z " $failed_services " ]; then
101+ log_pass " No service is in failed state on device"
102+ echo " $TESTNAME PASS" > " $res_file "
103+ else
104+ log_fail " ------List of failed service--------"
105+ log_fail " $failed_services "
106+ log_fail " ------------------------------------"
107+ echo " $TESTNAME FAIL" > " $res_file "
108+ ANY_SUBTEST_FAILED=" true"
109+ fi
110+ log_info " ----------------------------------------------------"
111+ log_info " -------- Stopping $TESTNAME Functional Test --------"
45112}
46113
47114# Call the functions
@@ -50,3 +117,10 @@ check_systemctl_stop
50117check_systemctl_start
51118check_failed_services
52119
120+
121+ if [ " $ANY_SUBTEST_FAILED " = " true" ]; then
122+ exit 1
123+ else
124+ exit 0
125+ fi
126+
0 commit comments