diff mbox series

[blktests,02/10] common/rc: introduce _systemctl_start() and _systemctl_stop()

Message ID 20250402070906.393160-3-shinichiro.kawasaki@wdc.com (mailing list archive)
State New
Headers show
Series nvme: test cases for TLS support | expand

Commit Message

Shinichiro Kawasaki April 2, 2025, 7:08 a.m. UTC
When test cases depend on specific systemctl services, the test cases
need to start the service. After the test case completion, it is better
to stop the service. However, if the service was already started and
active before executing the test cases, stopping the service will affect
test systems.

To avoid such affect on the test systems, introduce _systemctl_start()
and _systemctl_stop(). When _systemctl_start() check if the specified
service has already started or not. If the service has not yet started,
start it and record it in the global array SYSTEMCTL_UNITS_TO_STOP.
When _systemctl_stop() is called, stop the service recorded in the
array SYSTEMCTL_UNITS_TO_STOP.

Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
---
 common/rc | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
diff mbox series

Patch

diff --git a/common/rc b/common/rc
index ce7f975..068a676 100644
--- a/common/rc
+++ b/common/rc
@@ -13,6 +13,7 @@  shopt -s extglob
 . common/dm
 
 declare IO_URING_DISABLED
+declare -a SYSTEMCTL_UNITS_TO_STOP
 
 # If a test runs multiple "subtests", then each subtest should typically run
 # for TIMEOUT / number of subtests.
@@ -511,6 +512,26 @@  _have_systemctl_unit() {
 	return 0
 }
 
+_systemctl_start() {
+	local unit="$1"
+
+	if systemctl is-active --quiet "$unit"; then
+		return
+	fi
+
+	if systemctl start "$unit"; then
+		SYSTEMCTL_UNITS_TO_STOP+=("$unit")
+	fi
+}
+
+_systemctl_stop() {
+	local unit
+
+	for unit in "${SYSTEMCTL_UNITS_TO_STOP[@]}"; do
+		systemctl stop "$unit"
+	done
+}
+
 # Run the given command as NORMAL_USER
 _run_user() {
 	su "$NORMAL_USER" -c "$1"