]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-04.sh
562f3647009161f0c5a78c59e91229f44975e73a
[thirdparty/systemd.git] / test / units / testsuite-04.sh
1 #!/usr/bin/env bash
2 set -eux
3 set -o pipefail
4
5 # Limit the maximum journal size
6 trap "journalctl --rotate --vacuum-size=16M" EXIT
7
8 # Rotation/flush test, see https://github.com/systemd/systemd/issues/19895
9 journalctl --relinquish-var
10 for _ in {0..50}; do
11 dd if=/dev/urandom bs=1M count=1 | base64 | systemd-cat
12 done
13 journalctl --rotate
14 journalctl --flush
15 journalctl --sync
16
17 # Reset the ratelimit buckets for the subsequent tests below.
18 systemctl restart systemd-journald
19
20 # Test stdout stream
21
22 # Skip empty lines
23 ID=$(journalctl --new-id128 | sed -n 2p)
24 : >/expected
25 printf $'\n\n\n' | systemd-cat -t "$ID" --level-prefix false
26 journalctl --sync
27 journalctl -b -o cat -t "$ID" >/output
28 cmp /expected /output
29
30 ID=$(journalctl --new-id128 | sed -n 2p)
31 : >/expected
32 printf $'<5>\n<6>\n<7>\n' | systemd-cat -t "$ID" --level-prefix true
33 journalctl --sync
34 journalctl -b -o cat -t "$ID" >/output
35 cmp /expected /output
36
37 # Remove trailing spaces
38 ID=$(journalctl --new-id128 | sed -n 2p)
39 printf "Trailing spaces\n">/expected
40 printf $'<5>Trailing spaces \t \n' | systemd-cat -t "$ID" --level-prefix true
41 journalctl --sync
42 journalctl -b -o cat -t "$ID" >/output
43 cmp /expected /output
44
45 ID=$(journalctl --new-id128 | sed -n 2p)
46 printf "Trailing spaces\n">/expected
47 printf $'Trailing spaces \t \n' | systemd-cat -t "$ID" --level-prefix false
48 journalctl --sync
49 journalctl -b -o cat -t "$ID" >/output
50 cmp /expected /output
51
52 # Don't remove leading spaces
53 ID=$(journalctl --new-id128 | sed -n 2p)
54 printf $' \t Leading spaces\n'>/expected
55 printf $'<5> \t Leading spaces\n' | systemd-cat -t "$ID" --level-prefix true
56 journalctl --sync
57 journalctl -b -o cat -t "$ID" >/output
58 cmp /expected /output
59
60 ID=$(journalctl --new-id128 | sed -n 2p)
61 printf $' \t Leading spaces\n'>/expected
62 printf $' \t Leading spaces\n' | systemd-cat -t "$ID" --level-prefix false
63 journalctl --sync
64 journalctl -b -o cat -t "$ID" >/output
65 cmp /expected /output
66
67 # --output-fields restricts output
68 ID=$(journalctl --new-id128 | sed -n 2p)
69 printf $'foo' | systemd-cat -t "$ID" --level-prefix false
70 journalctl --sync
71 journalctl -b -o export --output-fields=MESSAGE,FOO --output-fields=PRIORITY,MESSAGE -t "$ID" >/output
72 [[ $(grep -c . /output) -eq 6 ]]
73 grep -q '^__CURSOR=' /output
74 grep -q '^MESSAGE=foo$' /output
75 grep -q '^PRIORITY=6$' /output
76 grep '^FOO=' /output && { echo 'unexpected success'; exit 1; }
77 grep '^SYSLOG_FACILITY=' /output && { echo 'unexpected success'; exit 1; }
78
79 # `-b all` negates earlier use of -b (-b and -m are otherwise exclusive)
80 journalctl -b -1 -b all -m >/dev/null
81
82 # -b always behaves like -b0
83 journalctl -q -b-1 -b0 | head -1 >/expected
84 journalctl -q -b-1 -b | head -1 >/output
85 cmp /expected /output
86 # ... even when another option follows (both of these should fail due to -m)
87 { journalctl -ball -b0 -m 2>&1 || :; } | head -1 >/expected
88 { journalctl -ball -b -m 2>&1 || :; } | head -1 >/output
89 cmp /expected /output
90
91 # https://github.com/systemd/systemd/issues/13708
92 ID=$(systemd-id128 new)
93 systemd-cat -t "$ID" bash -c 'echo parent; (echo child) & wait' &
94 PID=$!
95 wait %%
96 journalctl --sync
97 # We can drop this grep when https://github.com/systemd/systemd/issues/13937
98 # has a fix.
99 journalctl -b -o export -t "$ID" --output-fields=_PID | grep '^_PID=' >/output
100 [[ $(grep -c . /output) -eq 2 ]]
101 grep -q "^_PID=$PID" /output
102 grep -vq "^_PID=$PID" /output
103
104 # https://github.com/systemd/systemd/issues/15654
105 ID=$(journalctl --new-id128 | sed -n 2p)
106 printf "This will\nusually fail\nand be truncated\n">/expected
107 systemd-cat -t "$ID" /bin/sh -c 'env echo -n "This will";echo;env echo -n "usually fail";echo;env echo -n "and be truncated";echo;'
108 journalctl --sync
109 journalctl -b -o cat -t "$ID" >/output
110 cmp /expected /output
111 [[ $(journalctl -b -o cat -t "$ID" --output-fields=_TRANSPORT | grep -Pc "^stdout$") -eq 3 ]]
112 [[ $(journalctl -b -o cat -t "$ID" --output-fields=_LINE_BREAK | grep -Pc "^pid-change$") -eq 3 ]]
113 [[ $(journalctl -b -o cat -t "$ID" --output-fields=_PID | sort -u | grep -c "^.*$") -eq 3 ]]
114 [[ $(journalctl -b -o cat -t "$ID" --output-fields=MESSAGE | grep -Pc "^(This will|usually fail|and be truncated)$") -eq 3 ]]
115
116 # test that LogLevelMax can also suppress logging about services, not only by services
117 systemctl start silent-success
118 journalctl --sync
119 [[ -z "$(journalctl -b -q -u silent-success.service)" ]]
120
121 # Add new tests before here, the journald restarts below
122 # may make tests flappy.
123
124 # Don't lose streams on restart
125 systemctl start forever-print-hola
126 sleep 3
127 systemctl restart systemd-journald
128 sleep 3
129 systemctl stop forever-print-hola
130 [[ ! -f "/i-lose-my-logs" ]]
131
132 # https://github.com/systemd/systemd/issues/4408
133 rm -f /i-lose-my-logs
134 systemctl start forever-print-hola
135 sleep 3
136 systemctl kill --signal=SIGKILL systemd-journald
137 sleep 3
138 [[ ! -f "/i-lose-my-logs" ]]
139
140 # https://github.com/systemd/systemd/issues/15528
141 journalctl --follow --file=/var/log/journal/*/* | head -n1 || [[ $? -eq 1 ]]
142
143 touch /testok