Debian Codegraph @

debian/tests/test-checkrestart

raw
1 #!/bin/bash -ue
2 test_checkrestart(){
3 echo "** Testing: 'checkrestart $*'"
4 local d
5
6 for d in /tmp /usr/lib; do
7 if [ -e "$d/_checkrestart_test" ]; then
8 echo "$0: ERROR: Should not exist: $d/_checkrestart_test" >&2
9 exit 1
10 fi
11 done
12
13 # Create temp files and then unlink them - now this script will be
14 # detetected as using deleted files
15 exec 3> /tmp/_checkrestart_test
16 unlink /tmp/_checkrestart_test
17
18 exec 4> /usr/lib/_checkrestart_test
19 unlink /usr/lib/_checkrestart_test
20
21 for d in /tmp /usr/lib; do
22 if [ -e "$d/_checkrestart_test" ]; then
23 echo "$0: ERROR: Should not exist: $d/_checkrestart_test" >&2
24 exit 1
25 fi
26 done
27 (
28 # subprocesses inherit all open fd's so we need to run the
29 # test from a subshell or all of checkrestart, tee and grep
30 # would also be detected as 'using' the deleted files
31 exec 3>&-
32 exec 4>&-
33 echo "*** Running: 'checkrestart $*'"
34 : > output
35 checkrestart "$@" | tee output
36 local exit_status=${PIPESTATUS[0]}
37 echo "*** Looking for: '$EXPECTED'"
38 if grep -E --text "$EXPECTED" output; then
39 echo "** PASS: 'checkrestart $*' (exit: $exit_status)"
40 else
41 echo >&2 "** FAIL: 'checkrestart $*' failed (checkrestart exit=$exit_status; grep code=$?)"
42 fi
43
44 )
45 # clean up: close fds
46 exec 3>&-
47 exec 4>&-
48 echo
49 }
50
51 echo "* Starting $0"
52 echo "** (Info on environment follows)"
53 export ME="$0"
54 export PID="$$"
55 env
56
57 echo "Unfortunately, testing has shown that sometimes the debian salsa.debian.org infrastructure does
58 actually have processes using deleted files. And this also affects testing autopkgtests inside a schroot.
59
60 So we can't simply run checkrestart and expect to find
61 nothing.
62
63 So we have to mock the results of lsof to only report info on the current process, and ignore
64 everything else that happens to be running."
65
66 lsof=/usr/local/sbin/lsof
67
68 if [ -f "$lsof" ]; then
69 echo "ERROR: $lsof should not exist" >&2
70 exit 9
71 fi
72
73 cat > "$lsof" <<EOF
74 #!/bin/sh
75 exec /usr/bin/lsof -p $PID "\$@"
76 EOF
77 cat "$lsof"
78 chmod +x "$lsof"
79 echo
80 echo "Checking it works:"
81 type -p lsof
82 lsof
83
84
85 echo "** Starting test: checkrestart"
86 echo "This first test assumes that there is nothing using deleted files"
87 echo "If this assumption is wrong, likely all following tests will fail too"
88 echo "so if this test fails, either something is very wrong with checkrestart"
89 echo "or the tests (or system they run on) may be at fault."
90 : > output
91 checkrestart | tee output
92 if grep -q "0 non-ignored" output; then
93 echo "*** PASS"
94 else
95 echo >&2 "*** FAIL: test 'checkrestart' failed"
96 fi
97
98
99 EXPECTED="1 non-ignored " test_checkrestart
100 EXPECTED="RESTART_NEEDED: 1" test_checkrestart --machine
101 EXPECTED="1 non-ignored " test_checkrestart --terse
102
103 # -f - list deleted files
104 EXPECTED="/usr/lib/_checkrestart_test" test_checkrestart --show-files
105 TAB=$(printf '\t')
106 EXE="(/usr)?/bin/bash" # path could be /bin/bash or /usr/bin/bash depending on usrmerge
107 CMDLINE="\['/bin/bash', '-ue', '$ME'\]"
108
109 EXPECTED="^file${TAB}/usr/lib/_checkrestart_test${TAB}[0-9]+${TAB}${ME}${TAB}${EXE}${TAB}${CMDLINE}${TAB}${TAB}Script\$" test_checkrestart --machine --show-files
110
111 # --terse gives one line
112 # -v implies -f
113 # -t overides -f
114 # -v does work with -t
115 EXPECTED="1 non-ignored" test_checkrestart --terse --show-files
116 EXPECTED="1 non-ignored" test_checkrestart --show-files -t
117 EXPECTED="1 non-ignored" test_checkrestart --terse --all
118 # -a means nothing is ignored, so -v doesnt show much more
119 EXPECTED="not part of any package" test_checkrestart --terse --all -v
120 # without -a we see why things were ignored
121 EXPECTED="/tmp/_checkrestart_test" test_checkrestart --terse -v
122
123 # -i - ignore package ('Unpackaged' counts as a package)
124 EXPECTED="0 non-ignored" test_checkrestart -i Unpackaged -d
125
126 # -b
127 ignore_list=$(dirname "$(realpath "$0")")/files-to-ignore.list
128
129 EXPECTED="0 non-ignored " test_checkrestart -b "$ignore_list"
130 EXPECTED="Ignoring deleted file" test_checkrestart -b "$ignore_list" --verbose
131 # -v
132 EXPECTED="ignored_file" test_checkrestart -b "$ignore_list" --verbose --machine
133
134 # --packages means only deleted files and programs from packages are reported
135 EXPECTED="0 non-ignored" test_checkrestart -p
136 EXPECTED="Ignoring program" test_checkrestart -pv
137 EXPECTED="0 non-ignored" test_checkrestart -pv
138
139
140 # --all should also find the file in /tmp as well as the one in /usr
141 EXPECTED="1 non-ignored " test_checkrestart --all
142 EXPECTED="/tmp/_checkrestart_test" test_checkrestart --all -f
143 EXPECTED="^OTHER${TAB}Unpackaged: test-checkrestart${TAB}[0-9]+${TAB}$ME${TAB}${EXE}${TAB}${CMDLINE}${TAB}${TAB}Script\$" test_checkrestart --all --machine
144
145 EXPECTED="PROCESSES_ALL: 1" test_checkrestart --all --verbose --machine
146 EXPECTED="^file${TAB}/tmp/_checkrestart_test${TAB}[0-9]+${TAB}${ME}${TAB}${EXE}${TAB}${CMDLINE}${TAB}${TAB}Script\$" test_checkrestart --all --machine -f
147
148 # -x (and -a)
149 EXPECTED="0 non-ignored " test_checkrestart -a -x 'package:^Unpackaged' -d -x unit: -x suggested:. -x initscript:.
150 EXPECTED="0 non-ignored " test_checkrestart -a -x "program:^$ME\$" -d
151 EXPECTED="0 non-ignored " test_checkrestart -a -x 'file:^/(tmp|usr/lib)/_checkrestart_test$' -d
152 EXPECTED="0 non-ignored " test_checkrestart -a -x "pid:^$PID\$" -d
153
154
155
156
157 # -a can be combined with -b or -p - order matters
158 EXPECTED="0 non-ignored " test_checkrestart --all -p
159
160 # all files, but only from package
161 EXPECTED="0 non-ignored " test_checkrestart -p -a
162 EXPECTED="1 non-ignored " test_checkrestart -b "$ignore_list" --all
163 EXPECTED="/tmp/_checkrestart_test" test_checkrestart --all -b "$ignore_list" -v
164 # -a before -b does not ignore
165 EXPECTED="^file${TAB}/usr/lib/_checkrestart_test${TAB}[0-9]+${TAB}${ME}${TAB}${EXE}${TAB}${CMDLINE}${TAB}${TAB}Script\$" test_checkrestart --machine --verbose -b "$ignore_list" -a
166
167
168 # reboot-required (no pkgs)
169 for f in /run/reboot-required /run/reboot-required.pkgs; do
170 if [ -e "$f" ]; then
171 echo "ERROR: $f should not exist" >&2
172 exit 2
173 fi
174 done
175
176 touch /run/reboot-required
177 EXPECTED="needs rebooting"
178 test_checkrestart
179 # still shows progs needing restart
180 EXPECTED="^1 non-ignored"
181 test_checkrestart
182
183 EXPECTED="needs rebooting"
184 test_checkrestart -t
185 EXPECTED="; 1 non-ignored"
186 test_checkrestart -t
187
188 EXPECTED="REBOOT_NEEDED: Yes; MISSING"
189 test_checkrestart -m
190 EXPECTED="RESTART_NEEDED: 1"
191 test_checkrestart -m
192
193 EXPECTED="REBOOT_NEEDED: Yes; MISSING"
194 test_checkrestart -tm
195 EXPECTED="RESTART_NEEDED: 1"
196 test_checkrestart -m
197
198 # with pkgs as well
199 echo -e "aaaa\nbbbb" >> /run/reboot-required.pkgs
200 EXPECTED="REQUESTED_REBOOT: bbbb"
201 test_checkrestart -m
202 EXPECTED="^${TAB}aaaa$"
203 test_checkrestart
204
205
206 echo "* Testsuite ended ($0)"
207 echo "Cleaning up"
208 rm -f /run/reboot-required /run/reboot-required.pkgs "$lsof" && echo "DONE."
209 exit 0