ac3a84
From 55e11475d421f90cc5c7290c6b5d394f952ba577 Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Tue, 15 Nov 2022 21:52:10 +0900
ac3a84
Subject: [PATCH] test: add test case for sysv-generator and invalid dependency
ac3a84
ac3a84
(cherry picked from commit 5f882cc3ab32636d9242effb2cefad20d92d2ec2)
ac3a84
ac3a84
Related: #2160477
ac3a84
---
ac3a84
 test/units/testsuite-26.sh | 116 ++++++++++++++++++++++++++++++++++++-
ac3a84
 1 file changed, 114 insertions(+), 2 deletions(-)
ac3a84
ac3a84
diff --git a/test/units/testsuite-26.sh b/test/units/testsuite-26.sh
ac3a84
index 7c7a12b1ae..a8e7a5abaa 100755
ac3a84
--- a/test/units/testsuite-26.sh
ac3a84
+++ b/test/units/testsuite-26.sh
ac3a84
@@ -3,10 +3,18 @@
ac3a84
 set -eux
ac3a84
 set -o pipefail
ac3a84
 
ac3a84
+# shellcheck source=test/units/assert.sh
ac3a84
+. "$(dirname "$0")"/assert.sh
ac3a84
+
ac3a84
+: >/failed
ac3a84
+
ac3a84
 at_exit() {
ac3a84
     if [[ -v UNIT_NAME && -e "/usr/lib/systemd/system/$UNIT_NAME" ]]; then
ac3a84
         rm -fv "/usr/lib/systemd/system/$UNIT_NAME"
ac3a84
     fi
ac3a84
+
ac3a84
+    rm -f /etc/init.d/issue-24990
ac3a84
+    return 0
ac3a84
 }
ac3a84
 
ac3a84
 trap at_exit EXIT
ac3a84
@@ -284,6 +292,110 @@ systemctl unset-environment IMPORT_THIS IMPORT_THIS_TOO
ac3a84
 (! systemctl show-environment | grep "^IMPORT_THIS=")
ac3a84
 (! systemctl show-environment | grep "^IMPORT_THIS_TOO=")
ac3a84
 
ac3a84
-echo OK >/testok
ac3a84
+# test for sysv-generator (issue #24990)
ac3a84
+if [[ -x /usr/lib/systemd/system-generators/systemd-sysv-generator ]]; then
ac3a84
+
ac3a84
+    # invalid dependency
ac3a84
+    cat >/etc/init.d/issue-24990 <<\EOF
ac3a84
+#!/bin/bash
ac3a84
+
ac3a84
+### BEGIN INIT INFO
ac3a84
+# Provides:test1 test2
ac3a84
+# Required-Start:test1 $remote_fs $network
ac3a84
+# Required-Stop:test1 $remote_fs $network
ac3a84
+# Description:Test
ac3a84
+# Short-Description: Test
ac3a84
+### END INIT INFO
ac3a84
+
ac3a84
+case "$1" in
ac3a84
+    start)
ac3a84
+        echo "Starting issue-24990.service"
ac3a84
+        sleep 1000 &
ac3a84
+        ;;
ac3a84
+    stop)
ac3a84
+        echo "Stopping issue-24990.service"
ac3a84
+        sleep 10 &
ac3a84
+        ;;
ac3a84
+    *)
ac3a84
+        echo "Usage: service test {start|stop|restart|status}"
ac3a84
+        ;;
ac3a84
+esac
ac3a84
+EOF
ac3a84
+
ac3a84
+    chmod +x /etc/init.d/issue-24990
ac3a84
+    systemctl daemon-reload
ac3a84
+    [[ -L /run/systemd/generator.late/test1.service ]]
ac3a84
+    [[ -L /run/systemd/generator.late/test2.service ]]
ac3a84
+    assert_eq "$(readlink -f /run/systemd/generator.late/test1.service)" "/run/systemd/generator.late/issue-24990.service"
ac3a84
+    assert_eq "$(readlink -f /run/systemd/generator.late/test2.service)" "/run/systemd/generator.late/issue-24990.service"
ac3a84
+    output=$(systemctl cat issue-24990)
ac3a84
+    assert_in "SourcePath=/etc/init.d/issue-24990" "$output"
ac3a84
+    assert_in "Description=LSB: Test" "$output"
ac3a84
+    assert_in "After=test1.service" "$output"
ac3a84
+    assert_in "After=remote-fs.target" "$output"
ac3a84
+    assert_in "After=network-online.target" "$output"
ac3a84
+    assert_in "Wants=network-online.target" "$output"
ac3a84
+    assert_in "ExecStart=/etc/init.d/issue-24990 start" "$output"
ac3a84
+    assert_in "ExecStop=/etc/init.d/issue-24990 stop" "$output"
ac3a84
+    systemctl status issue-24990 || :
ac3a84
+    systemctl show issue-24990
ac3a84
+    assert_not_in "issue-24990.service" "$(systemctl show --property=After --value)"
ac3a84
+    assert_not_in "issue-24990.service" "$(systemctl show --property=Before --value)"
ac3a84
+
ac3a84
+    if ! systemctl is-active network-online.target; then
ac3a84
+        systemctl start network-online.target
ac3a84
+    fi
ac3a84
+
ac3a84
+    systemctl restart issue-24990
ac3a84
+    systemctl stop issue-24990
ac3a84
+
ac3a84
+    # valid dependency
ac3a84
+    cat >/etc/init.d/issue-24990 <<\EOF
ac3a84
+#!/bin/bash
ac3a84
+
ac3a84
+### BEGIN INIT INFO
ac3a84
+# Provides:test1 test2
ac3a84
+# Required-Start:$remote_fs
ac3a84
+# Required-Stop:$remote_fs
ac3a84
+# Description:Test
ac3a84
+# Short-Description: Test
ac3a84
+### END INIT INFO
ac3a84
+
ac3a84
+case "$1" in
ac3a84
+    start)
ac3a84
+        echo "Starting issue-24990.service"
ac3a84
+        sleep 1000 &
ac3a84
+        ;;
ac3a84
+    stop)
ac3a84
+        echo "Stopping issue-24990.service"
ac3a84
+        sleep 10 &
ac3a84
+        ;;
ac3a84
+    *)
ac3a84
+        echo "Usage: service test {start|stop|restart|status}"
ac3a84
+        ;;
ac3a84
+esac
ac3a84
+EOF
ac3a84
+
ac3a84
+    chmod +x /etc/init.d/issue-24990
ac3a84
+    systemctl daemon-reload
ac3a84
+    [[ -L /run/systemd/generator.late/test1.service ]]
ac3a84
+    [[ -L /run/systemd/generator.late/test2.service ]]
ac3a84
+    assert_eq "$(readlink -f /run/systemd/generator.late/test1.service)" "/run/systemd/generator.late/issue-24990.service"
ac3a84
+    assert_eq "$(readlink -f /run/systemd/generator.late/test2.service)" "/run/systemd/generator.late/issue-24990.service"
ac3a84
+    output=$(systemctl cat issue-24990)
ac3a84
+    assert_in "SourcePath=/etc/init.d/issue-24990" "$output"
ac3a84
+    assert_in "Description=LSB: Test" "$output"
ac3a84
+    assert_in "After=remote-fs.target" "$output"
ac3a84
+    assert_in "ExecStart=/etc/init.d/issue-24990 start" "$output"
ac3a84
+    assert_in "ExecStop=/etc/init.d/issue-24990 stop" "$output"
ac3a84
+    systemctl status issue-24990 || :
ac3a84
+    systemctl show issue-24990
ac3a84
+    assert_not_in "issue-24990.service" "$(systemctl show --property=After --value)"
ac3a84
+    assert_not_in "issue-24990.service" "$(systemctl show --property=Before --value)"
ac3a84
+
ac3a84
+    systemctl restart issue-24990
ac3a84
+    systemctl stop issue-24990
ac3a84
+fi
ac3a84
 
ac3a84
-exit 0
ac3a84
+touch /testok
ac3a84
+rm /failed