Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/systemtap-service.exp

raw
1 set test "stap-service"
2
3 if {! [installtest_p]} { return }
4 if {[expr 0 != [exec id -u]]} {
5 untested "${test} (non-root)"
6 return
7 }
8
9 # First find the "etc/systemtap" directory. This needs to work well
10 # with devtoolset too.
11 catch {exec readlink -f $env(SYSTEMTAP_PATH)/../../etc/systemtap} __etc_systemtap
12 if {! [file exists $__etc_systemtap]} {
13 catch {exec readlink -f $env(SYSTEMTAP_PATH)/../etc/systemtap} __etc_systemtap
14 }
15 if {! [file exists $__etc_systemtap]} {
16 untested "${test} - can't find systemtap etc directory"
17 return
18 }
19 if {! [regexp "/etc/systemtap$" "$__etc_systemtap"]} {
20 fail "${test} - weird etc/systemtap dir ($__etc_systemtap)"
21 return
22 }
23 pass "${test} - found systemtap etc directory"
24
25 set __conf_d $__etc_systemtap/conf.d
26 set __script_d $__etc_systemtap/script.d
27 set __config $__etc_systemtap/config
28 set __servicename systemtap
29
30 for {set i 2} {$i<9} {incr i} {
31 if {! [catch {exec scl_enabled devtoolset-$i}]} {
32 set __servicename devtoolset-$i-systemtap
33 }
34 }
35
36 # If there is no service file, then we have nothing to test. This is usually
37 # the case when stap is built from source. On Fedora and RHEL the service is
38 # provided by the systemtap-initscript RPM package.
39
40 set __servicefile /etc/rc.d/init.d/$__servicename
41
42 if {! [file exists $__servicefile]} {
43 untested "${test} (service file $__servicefile present)"
44 return
45 }
46
47 verbose -log "Using confdir $__conf_d"
48 verbose -log "Using scriptdir $__script_d"
49 verbose -log "Using service name $__servicename"
50 verbose -log "Using service file $__servicefile"
51
52 # back up existing configuration
53 catch {exec rm -rf $__etc_systemtap.backup}
54 if {[catch {exec mv $__etc_systemtap $__etc_systemtap.backup} msg ]} {
55 fail "${test} - failed creating configuration backup ($msg)"
56 return
57 }
58 if {[catch {exec mkdir -p $__conf_d $__script_d}]} {
59 fail "${test} - failed creating directories $__conf_d $__script_d"
60 }
61 if {[catch {exec touch $__config}]} {
62 fail "${test} - failed creating file $__config"
63 }
64
65 proc check_exitcode {command expecode} {
66 global errorCode test __subtest
67 verbose -log "Running command:\n $command"
68 # check for zero/non-zero exitcode and error message
69 set gotecode [catch {eval exec "$command"} msg]
70 # to obtain exit code > 1 we need different mechanism
71 set goterrcode [lindex $errorCode 2]
72 if {$gotecode == 1 && $goterrcode > 1} {set gotecode $goterrcode}
73 if {$gotecode == $expecode} {
74 pass "${test}::${__subtest} ($command)"
75 verbose -log "Got expected exit code $expecode"
76 } else {
77 fail "${test}::${__subtest} ($command)"
78 verbose -log "Expected $expecode, got $gotecode"
79 verbose -log "Error message was: $msg"
80 }
81 }
82
83 proc check_file_content {file content timeoutsec} {
84 global test
85 verbose -log "checking for $content in $file"
86 if {! [file exists "$file"]} {
87 fail "$test (file $file not found)"
88 return
89 }
90 set found 0
91 for {set i 0} {$i<$timeoutsec} {incr i} {
92 set command "grep $content $file"
93 if {! [catch {eval exec "$command"}]} {
94 set found 1
95 break
96 }
97 sleep 1
98 }
99 if {$found} {
100 pass "checking for $content in $file"
101 } else {
102 fail "checking for $content in $file"
103 }
104 }
105
106 # Testing exitcodes of basic commands with no scripts
107 set __subtest "no script"
108 check_exitcode "service $__servicename stop" 0
109 check_exitcode "service $__servicename cleanup -y" 0
110 sleep 5
111 check_exitcode "service $__servicename start" 0
112 check_exitcode "service $__servicename status" 3
113 check_exitcode "service $__servicename restart" 0
114 check_exitcode "service $__servicename status" 3
115 check_exitcode "service $__servicename cleanup -y" 0
116 check_exitcode "service $__servicename compile" 0
117 check_exitcode "service $__servicename cleanup -y" 0
118 check_exitcode "service $__servicename stop" 0
119
120
121 # Testing basic commands with one script
122 set __subtest "one script"
123 catch {exec rm -f /tmp/stap-script1.log}
124
125 set file [open "$__script_d/script1.stp" w]
126 puts $file "probe timer.ms(500){ print(\"script1out\\n\"); }"
127 close $file
128
129 set file [open "$__conf_d/script1.conf" w]
130 puts $file "script1_OPT='-o /tmp/stap-script1.log'"
131 close $file
132
133 check_exitcode "service $__servicename start" 0
134 check_file_content "/tmp/stap-script1.log" "script1out" 30
135 check_exitcode "service $__servicename status" 0
136 check_exitcode "service $__servicename restart" 0
137 check_exitcode "service $__servicename status" 0
138 catch {exec rm -f /tmp/stap-script1.log}
139 check_exitcode "service $__servicename restart" 0
140 check_file_content "/tmp/stap-script1.log" "script1out" 30
141 check_exitcode "service $__servicename cleanup -y" 0
142 check_exitcode "service $__servicename compile" 0
143 check_exitcode "service $__servicename start" 0
144 check_exitcode "service $__servicename stop" 0
145 check_exitcode "service $__servicename cleanup -y" 0
146
147
148 # Testing basic commands with two scripts
149 set file [open "$__script_d/script2.stp" w]
150 puts $file "probe timer.ms(500){ print(\"script2out\\n\"); }"
151 close $file
152
153 set file [open "$__conf_d/script2.conf" w]
154 puts $file "script2_OPT='-o /tmp/stap-script2.log'"
155 close $file
156
157 catch {exec echo > /tmp/stap-script1.log}
158 catch {exec echo > /tmp/stap-script2.log}
159
160 # --- both scripts stopped
161 set __subtest "both scripts stopped"
162 check_exitcode "grep script1out /tmp/stap-script1.log" 1
163 check_exitcode "grep script2out /tmp/stap-script2.log" 1
164
165 verbose -log "running 'service $__servicename status' ..."
166 eval spawn "service $__servicename status"
167 expect {
168 -timeout 90
169 -re {^script1\ is\ stopped\r\n} {
170 pass "${test}::${__subtest} (script1 is stopped)"
171 exp_continue
172 }
173 -re {^script2\ is\ stopped\r\n} {
174 pass "${test}::${__subtest} (script2 is stopped)"
175 exp_continue
176 }
177 -re {^[^\r\n]+\r\n} {
178 fail "${test}::${__subtest} (unexpected output)"
179 exp_continue
180 }
181 timeout { fail "${test}::${__subtest} (timeout)" }
182 }
183 catch {close}; catch {wait}
184
185 # --- only script1 runs
186 set __subtest "only script1 runs"
187
188 check_exitcode "service $__servicename start script1" 0
189 check_file_content "/tmp/stap-script1.log" "script1out" 30
190
191 verbose -log "running 'service $__servicename status' ..."
192 eval spawn "service $__servicename status"
193 expect {
194 -timeout 90
195 -re {^script1\([0-9]+\)\ is\ running...\r\n} {
196 pass "${test}::${__subtest} (script1 is running)"
197 exp_continue
198 }
199 -re {^script2\ is\ stopped\r\n} {
200 pass "${test}::${__subtest} (script2 is stopped)"
201 exp_continue
202 }
203 -re {^[^\r\n]+\r\n} {
204 fail "${test}::${__subtest} (unexpected output)"
205 exp_continue
206 }
207 timeout { fail "${test}::${__subtest} (timeout)" }
208 }
209 catch {close}; catch {wait}
210
211 check_file_content "/tmp/stap-script1.log" "script1out" 30
212 check_exitcode "grep script2out /tmp/stap-script2.log" 1
213
214 # --- only script2 runs
215 set __subtest "only script2 runs"
216
217 check_exitcode "service $__servicename stop script1" 0
218 catch {exec echo > /tmp/stap-script1.log}
219 check_exitcode "service $__servicename start script2" 0
220 check_file_content "/tmp/stap-script2.log" "script2out" 30
221
222 verbose -log "running 'service $__servicename status' ... "
223 eval spawn "service $__servicename status"
224 expect {
225 -timeout 90
226 -re {^script1\ is\ stopped\r\n} {
227 pass "${test}::${__subtest} (script1 is stopped)"
228 exp_continue
229 }
230 -re {^script2\([0-9]+\)\ is\ running...\r\n} {
231 pass "${test}::${__subtest} (script2 is running)"
232 exp_continue
233 }
234 -re {^[^\r\n]+\r\n} {
235 fail "${test}::${__subtest} (unexpected output)"
236 exp_continue
237 }
238 timeout { fail "${test}::${__subtest} (timeout)" }
239 }
240 catch {close}; catch {wait}
241
242 check_file_content "/tmp/stap-script2.log" "script2out" 30
243 check_exitcode "grep script1out /tmp/stap-script1.log" 1
244
245 # --- both scripts run
246 set __subtest "both scripts run"
247 check_exitcode "service $__servicename stop script2" 0
248 catch {exec echo > /tmp/stap-script1.log}
249 catch {exec echo > /tmp/stap-script2.log}
250 check_exitcode "service $__servicename start" 0
251 check_file_content "/tmp/stap-script1.log" "script1out" 30
252 check_file_content "/tmp/stap-script2.log" "script2out" 30
253
254 verbose -log "running 'service $__servicename status' ... "
255 eval spawn "service $__servicename status"
256 expect {
257 -timeout 90
258 -re {^script1\([0-9]+\)\ is\ running...\r\n} {
259 pass "${test}::${__subtest} (script1 is running)"
260 exp_continue
261 }
262 -re {^script2\([0-9]+\)\ is\ running...\r\n} {
263 pass "${test}::${__subtest} (script2 is running)"
264 exp_continue
265 }
266 -re {^[^\r\n]+\r\n} {
267 fail "${test}::${__subtest} (unexpected output)"
268 exp_continue
269 }
270 timeout { fail "${test}::${__subtest} (timeout)" }
271 }
272 catch {close}; catch {wait}
273
274 check_exitcode "service $__servicename stop" 0
275
276 # remove test1 and test2 scripts
277 catch {exec rm -f "$__script_d/script1.stp" "$__conf_d/script1.conf"}
278 catch {exec rm -f "$__script_d/script2.stp" "$__conf_d/script2.conf"}
279 catch {exec rm -f "/tmp/stap-script1.log" "/tmp/stap-script2.log"}
280
281 # Testing script with bad name (bz1109084)
282 set __subtest "bz1109084"
283 set __tmp_log "/tmp/stap-service-test-log"
284 catch {exec rm -f $__tmp_log}
285
286 set file [open "$__script_d/script1-blah.stp" w]
287 puts $file "probe timer.ms(500){ print(\"script1out\\n\"); }"
288 close $file
289
290 set file [open "$__config" w]
291 puts $file "LOG_FILE=$__tmp_log"
292 close $file
293
294 sleep 5
295 check_exitcode "service $__servicename start" 0
296 sleep 5
297 check_exitcode "grep \"not a valid script name\" $__tmp_log" 0
298 catch {exec rm -f $__tmp_log}
299
300 set __subtest "boot time probing"
301 if {[bootprobing_p]} {
302 # --- test the boot time probing feature to some extent (without actually rebooting)
303 check_exitcode "service $__servicename cleanup -y" 0
304
305 set file [open "$__script_d/script3.stp" w]
306 puts $file "probe timer.ms(500){ print(\"script3out\\n\"); }"
307 close $file
308
309 set file [open "$__conf_d/script3.conf" w]
310 puts $file "script3_OPT=''"
311 close $file
312
313 set file [open "$__script_d/script4.stp" w]
314 puts $file "function __return_three:long() %{ STAP_RETURN(3); %}"
315 puts $file "probe timer.ms(500){ printf(\"script%dout\\n\", __return_three()); }"
316 close $file
317
318 set file [open "$__conf_d/script4.conf" w]
319 puts $file "script4_OPT=''"
320 close $file
321
322 # --- install script3
323 set __subtest "boot probing - install script3"
324 verbose -log "running 'service $__servicename onboot script3' ..."
325 eval spawn "service $__servicename onboot script3"
326 set __counter 0
327 expect {
328 -timeout 160
329 -re {^ Compiling script3[^\r\n]+done\r\n} {
330 incr __counter
331 exp_continue
332 }
333 -re {^ Checking options script3[^\r\n]+done\r\n} {
334 incr __counter
335 exp_continue
336 }
337 -re {^ Creating initramfs with script3[^\r\n]+done\r\n} {
338 incr __counter
339 exp_continue
340 }
341 -re {^[^\r\n]+\r\n} {
342 fail "${test}::${__subtest} (unexpected output)"
343 exp_continue
344 }
345 timeout { fail "${test}::${__subtest} (timeout)" }
346 }
347 catch {close}; catch {wait}
348 if {$__counter == 3} {
349 pass "${test}::${__subtest}"
350 } else {
351 fail "${test}::${__subtest} ($__counter)"
352 }
353
354
355 # --- fail installing bad script4 (the -g option missing in script4.conf)
356 set __subtest "boot probing - fail installing bad script4"
357 verbose -log "running 'service $__servicename onboot script4' ..."
358 catch {eval exec "service $__servicename onboot script4"} msg
359 if {[regexp {Compiling script4[^\r\n]+error} $msg]} {
360 pass "${test}::${__subtest}"
361 } else {
362 fail "${test}::${__subtest}"
363 }
364
365 # --- fix bad scipt4.conf
366 set file [open "$__conf_d/script4.conf" w]
367 verbose -log "fixing script4.conf ..."
368 puts $file "script4_OPT='-g'"
369 close $file
370
371 # --- check installing fixed script4
372 set __subtest "boot probing - install script4"
373 verbose -log "running 'service $__servicename onboot script4' ..."
374 eval spawn "service $__servicename onboot script4"
375 set __counter 0
376 expect {
377 -timeout 160
378 -re {^ Compiling script4[^\r\n]+done\r\n} {
379 incr __counter
380 exp_continue
381 }
382 -re {^ Checking options script4[^\r\n]+done\r\n} {
383 incr __counter
384 exp_continue
385 }
386 -re {^ Creating initramfs with script4[^\r\n]+done\r\n} {
387 incr __counter
388 exp_continue
389 }
390 -re {^[^\r\n]+\r\n} {
391 fail "${test}::${__subtest} (unexpected output)"
392 exp_continue
393 }
394 timeout { fail "${test}::${__subtest} (timeout)" }
395 }
396 catch {close}; catch {wait}
397 if {$__counter == 3} {
398 pass "${test}::${__subtest}"
399 } else {
400 fail "${test}::${__subtest} ($__counter)"
401 }
402
403 # --- check presence of script4 in initramfs
404 set __subtest "boot probing - check script4 in initramfs"
405 check_exitcode "lsinitrd | grep script4" 0
406
407 # --- remove all scripts from initramfs
408 set __subtest "boot probing - remove scripts"
409 verbose -log "running 'service $__servicename onboot' ..."
410 eval spawn "service $__servicename onboot"
411 set __counter 0
412 expect {
413 -timeout 160
414 -re {^ +Creating initramfs without scripts[^\r\n]+done\r\n} {
415 incr __counter
416 exp_continue
417 }
418 -re {^[^\r\n]+\r\n} {
419 fail "${test}::${__subtest} (unexpected output)"
420 exp_continue
421 }
422 timeout { fail "${test}::${__subtest} (timeout)" }
423 }
424 catch {close}; catch {wait}
425 if {$__counter == 1} {
426 pass "${test}::${__subtest}"
427 } else {
428 fail "${test}::${__subtest} ($__counter)"
429 }
430
431 # --- check non-presence of script4 in initramfs
432 set __subtest "boot probing - check script4 not in initramfs anymore"
433 check_exitcode "lsinitrd | grep script4" 1
434
435 # clean up script3, script4
436 catch {exec rm -f "$__script_d/script3.stp" "$__conf_d/script3.conf"}
437 catch {exec rm -f "$__script_d/script4.stp" "$__conf_d/script4.conf"}
438 } else {
439 untested "${test}::${__subtest}"
440 }
441
442 # restore configuration
443 catch {exec rm -rf $__etc_systemtap}
444 if {[catch {exec mv $__etc_systemtap.backup $__etc_systemtap} msg]} {
445 fail "${test} - failed restoring backed up configuration ($msg)"
446 }
447