8d419f
From 3c469acda5f5d62d56a16f075475c3b4f1da75e1 Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Fri, 25 Mar 2022 15:56:16 +0100
8d419f
Subject: [PATCH] test-systemctl-enable: use magic syntax to allow inverted
8d419f
 tests
8d419f
MIME-Version: 1.0
8d419f
Content-Type: text/plain; charset=UTF-8
8d419f
Content-Transfer-Encoding: 8bit
8d419f
8d419f
Inspired by 7910ec3bcde2ee0086b3e49f8aaa2a9f13f58d97.
8d419f
'! true' passes, because it's a conditional expression.
8d419f
But '( ! true )' fails, because '( … )' creates a subshell, i.e. a separate
8d419f
program, and '! true' becomes the return value of that program, and the whole
8d419f
thing apparently is not a conditional expression for the outer shell.
8d419f
8d419f
This is shorter, so let's just do this.
8d419f
8d419f
(cherry picked from commit d6c51c485abe0026a5da654fca5d6c1457c4587d)
8d419f
8d419f
Related: #2082131
8d419f
---
8d419f
 test/test-systemctl-enable.sh | 76 ++++++++++++++++++-----------------
8d419f
 1 file changed, 39 insertions(+), 37 deletions(-)
8d419f
8d419f
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
8d419f
index 0f66af309a..ecb433380e 100644
8d419f
--- a/test/test-systemctl-enable.sh
8d419f
+++ b/test/test-systemctl-enable.sh
8d419f
@@ -20,7 +20,7 @@ islink() {
8d419f
 }
8d419f
 
8d419f
 : '------enable nonexistent------------------------------------'
8d419f
-"$systemctl" --root="$root" enable test1.service && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable test1.service )
8d419f
 
8d419f
 : '------basic enablement--------------------------------------'
8d419f
 mkdir -p "$root/etc/systemd/system"
8d419f
@@ -86,7 +86,7 @@ Alias=test1-badalias.socket
8d419f
 Alias=test1-goodalias2.service
8d419f
 EOF
8d419f
 
8d419f
-"$systemctl" --root="$root" enable test1 && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable test1 )
8d419f
 test -h "$root/etc/systemd/system/default.target.wants/test1.service"
8d419f
 test -h "$root/etc/systemd/system/special.target.requires/test1.service"
8d419f
 test -e "$root/etc/systemd/system/test1-goodalias.service"
8d419f
@@ -98,7 +98,7 @@ test -e "$root/etc/systemd/system/test1-goodalias2.service"
8d419f
 test -h "$root/etc/systemd/system/test1-goodalias2.service"
8d419f
 
8d419f
 : '-------aliases in reeanble----------------------------------'
8d419f
-"$systemctl" --root="$root" reenable test1 && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" reenable test1 )
8d419f
 islink "$root/etc/systemd/system/default.target.wants/test1.service" "../test1.service"
8d419f
 islink "$root/etc/systemd/system/test1-goodalias.service" "test1.service"
8d419f
 
8d419f
@@ -154,7 +154,7 @@ test ! -e "$root/etc/systemd/system/sockets.target.wants/test2.socket"
8d419f
 : '-------link-------------------------------------------------'
8d419f
 # File doesn't exist yet
8d419f
 test ! -e "$root/link1.path"
8d419f
-"$systemctl" --root="$root" link '/link1.path' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" link '/link1.path' )
8d419f
 test ! -e "$root/etc/systemd/system/link1.path"
8d419f
 
8d419f
 cat >"$root/link1.path" <
8d419f
@@ -172,12 +172,12 @@ islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
 : '-------link already linked different path-------------------'
8d419f
 mkdir "$root/subdir"
8d419f
 cp "$root/link1.path" "$root/subdir/"
8d419f
-"$systemctl" --root="$root" link '/subdir/link1.path' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" link '/subdir/link1.path' )
8d419f
 islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
 
8d419f
 : '-------link bad suffix--------------------------------------'
8d419f
 cp "$root/link1.path" "$root/subdir/link1.suffix"
8d419f
-"$systemctl" --root="$root" link '/subdir/link1.suffix' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" link '/subdir/link1.suffix' )
8d419f
 test ! -e "$root/etc/systemd/system/link1.suffix"
8d419f
 
8d419f
 : '-------unlink by unit name----------------------------------'
8d419f
@@ -208,13 +208,13 @@ islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
 islink "$root/etc/systemd/system/paths.target.wants/link1.path" "../link1.path"
8d419f
 
8d419f
 : '-------enable already linked different path-----------------'
8d419f
-"$systemctl" --root="$root" enable '/subdir/link1.path' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable '/subdir/link1.path' )
8d419f
 islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
 islink "$root/etc/systemd/system/paths.target.wants/link1.path" "../link1.path"
8d419f
 
8d419f
 : '-------enable bad suffix------------------------------------'
8d419f
 cp "$root/link1.path" "$root/subdir/link1.suffix"
8d419f
-"$systemctl" --root="$root" enable '/subdir/link1.suffix' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable '/subdir/link1.suffix' )
8d419f
 test ! -e "$root/etc/systemd/system/link1.suffix"
8d419f
 test ! -e "$root/etc/systemd/system/paths.target.wants/link1.suffix"
8d419f
 
8d419f
@@ -264,14 +264,14 @@ test ! -h "$root/etc/systemd/system/services.target.wants/link3.service"
8d419f
 
8d419f
 : '-------enable on masked-------------------------------------'
8d419f
 ln -s "/dev/null" "$root/etc/systemd/system/masked.service"
8d419f
-"$systemctl" --root="$root" enable 'masked.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
-"$systemctl" --root="$root" enable '/etc/systemd/system/masked.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable 'masked.service' )
8d419f
+( ! "$systemctl" --root="$root" enable '/etc/systemd/system/masked.service' )
8d419f
 
8d419f
 : '-------enable on masked alias-------------------------------'
8d419f
 test -h "$root/etc/systemd/system/masked.service"
8d419f
 ln -s "masked.service" "$root/etc/systemd/system/masked-alias.service"
8d419f
-"$systemctl" --root="$root" enable 'masked-alias.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
-"$systemctl" --root="$root" enable '/etc/systemd/system/masked-alias.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable 'masked-alias.service' )
8d419f
+( ! "$systemctl" --root="$root" enable '/etc/systemd/system/masked-alias.service' )
8d419f
 
8d419f
 : '-------issue 22000: link in subdirectory--------------------'
8d419f
 mkdir -p "$root/etc/systemd/system/myown.d"
8d419f
@@ -286,7 +286,7 @@ WantedBy=services.target
8d419f
 Also=link5-also.service
8d419f
 EOF
8d419f
 
8d419f
-"$systemctl" --root="$root" enable 'link5.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable 'link5.service' )
8d419f
 test ! -h "$root/etc/systemd/system/services.target.wants/link5.service"
8d419f
 test ! -h "$root/etc/systemd/system/services.target.wants/link5-also.service"
8d419f
 
8d419f
@@ -301,7 +301,7 @@ WantedBy=services.target
8d419f
 EOF
8d419f
 
8d419f
 # No instance here — this can't succeed.
8d419f
-"$systemctl" --root="$root" enable 'templ1@.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable 'templ1@.service' )
8d419f
 test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
8d419f
 
8d419f
 "$systemctl" --root="$root" enable 'templ1@one.service'
8d419f
@@ -428,7 +428,7 @@ Alias=link4alias.service
8d419f
 Alias=link4alias2.service
8d419f
 EOF
8d419f
 
8d419f
-"$systemctl" --root="$root" enable 'link4.service' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! "$systemctl" --root="$root" enable 'link4.service' )
8d419f
 test ! -h "$root/etc/systemd/system/link4.service"  # this is our file
8d419f
 test ! -h "$root/etc/systemd/system/link4@.service"
8d419f
 test ! -h "$root/etc/systemd/system/link4@inst.service"
8d419f
@@ -571,12 +571,12 @@ check_alias a "$(uname -m | tr '_' '-')"
8d419f
 test ! -e "$root/etc/os-release"
8d419f
 test ! -e "$root/usr/lib/os-release"
8d419f
 
8d419f
-check_alias A '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias B '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias M '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias o '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias w '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias W '' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! check_alias A '' )
8d419f
+( ! check_alias B '' )
8d419f
+( ! check_alias M '' )
8d419f
+( ! check_alias o '' )
8d419f
+( ! check_alias w '' )
8d419f
+( ! check_alias W '' )
8d419f
 
8d419f
 cat >"$root/etc/os-release" <
8d419f
 # empty
8d419f
@@ -609,19 +609,19 @@ check_alias W 'right'
8d419f
 check_alias b "$(systemd-id128 boot-id)"
8d419f
 
8d419f
 # Specifiers not available for [Install]
8d419f
-check_alias C '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias E '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias f '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias h '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias I '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias J '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias L '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias P '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias s '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias S '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias t '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias T '' && { echo "Expected failure" >&2; exit 1; }
8d419f
-check_alias V '' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! check_alias C '' )
8d419f
+( ! check_alias E '' )
8d419f
+( ! check_alias f '' )
8d419f
+( ! check_alias h '' )
8d419f
+( ! check_alias I '' )
8d419f
+( ! check_alias J '' )
8d419f
+( ! check_alias L '' )
8d419f
+( ! check_alias P '' )
8d419f
+( ! check_alias s '' )
8d419f
+( ! check_alias S '' )
8d419f
+( ! check_alias t '' )
8d419f
+( ! check_alias T '' )
8d419f
+( ! check_alias V '' )
8d419f
 
8d419f
 check_alias g root
8d419f
 check_alias G 0
8d419f
@@ -635,7 +635,7 @@ check_alias j 'link6'
8d419f
 check_alias l "$(uname -n | sed 's/\..*//')"
8d419f
 
8d419f
 test ! -e "$root/etc/machine-id"
8d419f
-check_alias m '' && { echo "Expected failure" >&2; exit 1; }
8d419f
+( ! check_alias m '' )
8d419f
 
8d419f
 systemd-id128 new >"$root/etc/machine-id"
8d419f
 check_alias m "$(cat "$root/etc/machine-id")"
8d419f
@@ -647,9 +647,11 @@ check_alias p 'some-some-link6'
8d419f
 
8d419f
 check_alias v "$(uname -r)"
8d419f
 
8d419f
-check_alias % '%' && { echo "Expected failure because % is not legal in unit name" >&2; exit 1; }
8d419f
+# % is not legal in unit name
8d419f
+( ! check_alias % '%' )
8d419f
 
8d419f
-check_alias z 'z' && { echo "Expected failure because %z is not known" >&2; exit 1; }
8d419f
+# %z is not defined
8d419f
+( ! check_alias z 'z' )
8d419f
 
8d419f
 : '-------specifiers in WantedBy-------------------------------'
8d419f
 # We don't need to repeat all the tests. Let's do a basic check that specifier