Skip to content

Commit ee6b3d1

Browse files
daandemeyeryuwata
authored andcommitted
test: Stop using grep -q in integration test
When a TTY is attached to the test unit, grep -q will generate SIGPIPE for the previous command in the pipeline which in combo with `pipefail` will cause the command to fail with exit status 141 which will fail the test. Replace with >/dev/null to avoid this from happening. See also https://www.gnu.org/software/grep/manual/html_node/Usage.html > There is a related problem with Bash’s set -e -o pipefail. Since grep > does not always read all its input, a command outputting to a pipe read > by grep can fail when grep exits before reading all its input, and the > command’s failure can cause Bash to exit. Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>
1 parent d065fdd commit ee6b3d1

43 files changed

Lines changed: 281 additions & 277 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/CODING_STYLE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,9 @@ SPDX-License-Identifier: LGPL-2.1-or-later
999999

10001000
- When modifying existing tests, please convert the test to use the new assertion
10011001
macros from `tests.h` if it is not already using those.
1002+
1003+
## Integration Tests
1004+
1005+
- Never use `grep -q` in a pipeline, use `grep >/dev/null` instead. The former
1006+
will generate `SIGPIPE` for the previous command in the pipeline when it finds
1007+
a match which will cause the test to fail unexpectedly.

src/kernel-install/test-kernel-install.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,17 @@ EOF
149149
test -f "$uki"
150150

151151
if [ -x "$bootctl" ]; then
152-
"$bootctl" kernel-inspect "$uki" | grep -qE 'Kernel Type: +uki$'
153-
"$bootctl" kernel-inspect "$uki" | grep -qE 'Version: +1\.1\.3$'
154-
"$bootctl" kernel-inspect "$uki" | grep -qE 'Cmdline: +opt1 opt2$'
152+
"$bootctl" kernel-inspect "$uki" | grep -E 'Kernel Type: +uki$' >/dev/null
153+
"$bootctl" kernel-inspect "$uki" | grep -E 'Version: +1\.1\.3$' >/dev/null
154+
"$bootctl" kernel-inspect "$uki" | grep -E 'Cmdline: +opt1 opt2$' >/dev/null
155155
fi
156156

157-
"$ukify" inspect "$uki" | grep -qE '^.sbat'
158-
"$ukify" inspect "$uki" | grep -qE '^.cmdline'
159-
"$ukify" inspect "$uki" | grep -qE '^.uname'
160-
"$ukify" inspect "$uki" | grep -qE '^.initrd'
161-
"$ukify" inspect "$uki" | grep -qE '^.linux'
162-
"$ukify" inspect "$uki" | grep -qE '^.dtb'
157+
"$ukify" inspect "$uki" | grep -E '^.sbat' >/dev/null
158+
"$ukify" inspect "$uki" | grep -E '^.cmdline' >/dev/null
159+
"$ukify" inspect "$uki" | grep -E '^.uname' >/dev/null
160+
"$ukify" inspect "$uki" | grep -E '^.initrd' >/dev/null
161+
"$ukify" inspect "$uki" | grep -E '^.linux' >/dev/null
162+
"$ukify" inspect "$uki" | grep -E '^.dtb' >/dev/null
163163

164164
rm "$D/sources/install.conf.d/override.conf"
165165
fi

test/units/TEST-04-JOURNAL.SYSTEMD_JOURNAL_COMPRESS.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ EOF
2828
ID="$(systemd-id128 new)"
2929
systemd-cat -t "$ID" bash -c "for ((i=0;i<100;i++)); do echo -n hoge with ${c}; done; echo"
3030
journalctl --sync
31-
timeout 10 bash -c "until SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /var/log/journal/$MACHINE_ID/system.journal 2>&1 | grep -q -F 'compress=${c}'; do sleep .5; done"
31+
timeout 10 bash -c "until SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /var/log/journal/$MACHINE_ID/system.journal 2>&1 | grep -F 'compress=${c}' >/dev/null; do sleep .5; done"
3232

3333
# $SYSTEMD_JOURNAL_COMPRESS= also works for journal-remote
3434
if [[ -x /usr/lib/systemd/systemd-journal-remote ]]; then
3535
for cc in NONE XZ LZ4 ZSTD; do
3636
rm -f /tmp/foo.journal
3737
SYSTEMD_JOURNAL_COMPRESS="${cc}" /usr/lib/systemd/systemd-journal-remote --split-mode=none -o /tmp/foo.journal --getter="journalctl -b -o export -t $ID"
38-
SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /tmp/foo.journal 2>&1 | grep -q -F "compress=${cc}"
39-
journalctl -t "$ID" -o cat --file /tmp/foo.journal | grep -q -F "hoge with ${c}"
38+
SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /tmp/foo.journal 2>&1 | grep -F "compress=${cc}" >/dev/null
39+
journalctl -t "$ID" -o cat --file /tmp/foo.journal | grep -F "hoge with ${c}" >/dev/null
4040
done
4141
fi
4242
done

test/units/TEST-04-JOURNAL.fss.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -o pipefail
55

66
# Forward Secure Sealing
77

8-
if ! journalctl --version | grep -qF +GCRYPT; then
8+
if ! journalctl --version | grep -F +GCRYPT >/dev/null; then
99
echo "Built without gcrypt, skipping the FSS tests"
1010
exit 0
1111
fi

test/units/TEST-04-JOURNAL.journal.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ grep -q '^PRIORITY=6$' /tmp/output
6060
ID="$(systemd-id128 new)"
6161
echo -e 'HEAD\nTAIL\nTAIL' | systemd-cat -t "$ID"
6262
journalctl --sync
63-
journalctl -b -t "$ID" | grep -q HEAD
64-
journalctl -b -t "$ID" | grep -q TAIL
65-
journalctl -b -t "$ID" --truncate-newline | grep -q HEAD
66-
journalctl -b -t "$ID" --truncate-newline | grep -q -v TAIL
63+
journalctl -b -t "$ID" | grep HEAD >/dev/null
64+
journalctl -b -t "$ID" | grep TAIL >/dev/null
65+
journalctl -b -t "$ID" --truncate-newline | grep HEAD >/dev/null
66+
journalctl -b -t "$ID" --truncate-newline | grep -v TAIL >/dev/null
6767

6868
# '-b all' negates earlier use of -b (-b and -m are otherwise exclusive)
6969
journalctl -b -1 -b all -m >/dev/null

test/units/TEST-07-PID1.exec-context.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ if ! systemd-detect-virt -cq; then
175175
systemd-run --wait --pipe --unit "$SERVICE_NAME" "${ARGUMENTS[@]}" \
176176
bash -xec "test -r /dev/null; test ! -w /dev/null; test ! -r $LODEV; test -w $LODEV; test ! -r /dev/tty; test ! -w /dev/tty"
177177

178-
if ! systemctl --version | grep -qF -- "-BPF_FRAMEWORK"; then
178+
if ! systemctl --version | grep -F -- "-BPF_FRAMEWORK" >/dev/null; then
179179
# SocketBind*=
180180
ARGUMENTS=(
181181
-p SocketBindAllow=

test/units/TEST-07-PID1.private-bpf.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ check_mount_opts 'BPFDelegatePrograms=BPFProgTypeTracepoint,BPFProgTypeXdp,BPFPr
5252
check_mount_opts 'BPFDelegateAttachments=BPFFlowDissector,BPFCgroupSysctl,BPFNetfilter' 'delegate_attachs=flow_dissector:cgroup_sysctl:netfilter'
5353

5454
# Building test-bpf-token requires BPF support
55-
if systemctl --version | grep -q -- -BPF_FRAMEWORK; then
55+
if systemctl --version | grep -- -BPF_FRAMEWORK >/dev/null; then
5656
exit 0
5757
fi
5858

test/units/TEST-10-MOUNT.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ check_dependencies() {
8989
# again with the userspace options. Typically, the window between the two calls is very short, but when
9090
# the mount event source is ratelimited after the first event, processing the second event may be delayed
9191
# about 1 second. Hence, here we need to wait for a while.
92-
timeout 10 bash -c 'until systemctl show --property=After --value tmp-deptest.mount | grep -q -F remote-fs-pre.target; do sleep .1; done'
92+
timeout 10 bash -c 'until systemctl show --property=After --value tmp-deptest.mount | grep -F remote-fs-pre.target >/dev/null; do sleep .1; done'
9393
after=$(systemctl show --property=After --value tmp-deptest.mount)
9494
assert_not_in "local-fs-pre.target" "$after"
9595
assert_in "remote-fs-pre.target" "$after"

test/units/TEST-13-NSPAWN.machined.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ timeout 30 bash -c "while varlinkctl call /run/systemd/machine/io.systemd.Machin
308308
# test io.systemd.Machine.List with sshAddress and sshPrivateKeyPath fields
309309
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.Register '{"name": "registered-container", "class": "container", "sshAddress": "localhost", "sshPrivateKeyPath": "/non-existent"}'
310310
timeout 30 bash -c "until varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{\"name\":\"registered-container\"}'; do sleep 0.5; done"
311-
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"registered-container"}' | jq '.sshAddress' | grep -q 'localhost'
312-
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"registered-container"}' | jq '.sshPrivateKeyPath' | grep -q 'non-existent'
311+
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"registered-container"}' | jq '.sshAddress' | grep 'localhost' >/dev/null
312+
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"registered-container"}' | jq '.sshPrivateKeyPath' | grep 'non-existent' >/dev/null
313313
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.Unregister '{"name": "registered-container"}'
314314

315315
# test io.systemd.Machine.List with addresses, OSRelease, and UIDShift fields
@@ -369,7 +369,7 @@ journalctl --sync
369369
(! journalctl -u systemd-machined.service --since="$TS" --grep 'Connection busy')
370370
machinectl terminate container-without-os-release
371371

372-
(ip addr show lo | grep -q 192.168.1.100) || ip address add 192.168.1.100/24 dev lo
372+
(ip addr show lo | grep 192.168.1.100 >/dev/null) || ip address add 192.168.1.100/24 dev lo
373373
(! varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name": ".host"}' | grep 'addresses')
374374
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name": ".host", "acquireMetadata": "yes"}' | grep 'addresses'
375375
(! varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name": ".host"}' | grep 'OSRelease')

test/units/TEST-17-UDEV.IMPORT.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ udevadm control --reload
1414
SYSTEMD_LOG_LEVEL=debug udevadm trigger --verbose --settle --action add /dev/null
1515

1616
test -f /run/udev/data/c1:3
17-
udevadm info /dev/null | grep -q 'E: HOGE=aa\\x20\\x20\\x20bb'
18-
udevadm info /dev/null | grep -q 'E: FOO=\\x20aaa\\x20'
17+
udevadm info /dev/null | grep 'E: HOGE=aa\\x20\\x20\\x20bb' >/dev/null
18+
udevadm info /dev/null | grep 'E: FOO=\\x20aaa\\x20' >/dev/null
1919

2020
rm /run/udev/rules.d/50-testsuite.rules
2121
udevadm control --reload

0 commit comments

Comments
 (0)