Deleted Added
full compact
sh_helpers.sh (258289) sh_helpers.sh (273929)
1#
2# Automated Testing Framework (atf)
3#
4# Copyright (c) 2007 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1# Copyright (c) 2007 The NetBSD Foundation, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30# -------------------------------------------------------------------------
31# Helper tests for "t_cleanup".
32# -------------------------------------------------------------------------
33
34atf_test_case cleanup_pass cleanup
35cleanup_pass_head()
36{
37 atf_set "descr" "Helper test case for the t_cleanup test program"
38}
39cleanup_pass_body()
40{
41 touch $(atf_config_get tmpfile)
42}
43cleanup_pass_cleanup()
44{
45 if [ $(atf_config_get cleanup no) = yes ]; then
46 rm $(atf_config_get tmpfile)
47 fi
48}
49
50atf_test_case cleanup_fail cleanup
51cleanup_fail_head()
52{
53 atf_set "descr" "Helper test case for the t_cleanup test program"
54}
55cleanup_fail_body()
56{
57 touch $(atf_config_get tmpfile)
58 atf_fail "On purpose"
59}
60cleanup_fail_cleanup()
61{
62 if [ $(atf_config_get cleanup no) = yes ]; then
63 rm $(atf_config_get tmpfile)
64 fi
65}
66
67atf_test_case cleanup_skip cleanup
68cleanup_skip_head()
69{
70 atf_set "descr" "Helper test case for the t_cleanup test program"
71}
72cleanup_skip_body()
73{
74 touch $(atf_config_get tmpfile)
75 atf_skip "On purpose"
76}
77cleanup_skip_cleanup()
78{
79 if [ $(atf_config_get cleanup no) = yes ]; then
80 rm $(atf_config_get tmpfile)
81 fi
82}
83
84atf_test_case cleanup_curdir cleanup
85cleanup_curdir_head()
86{
87 atf_set "descr" "Helper test case for the t_cleanup test program"
88}
89cleanup_curdir_body()
90{
91 echo 1234 >oldvalue
92}
93cleanup_curdir_cleanup()
94{
95 test -f oldvalue && echo "Old value: $(cat oldvalue)"
96}
97
98atf_test_case cleanup_sigterm cleanup
99cleanup_sigterm_head()
100{
101 atf_set "descr" "Helper test case for the t_cleanup test program"
102}
103cleanup_sigterm_body()
104{
105 touch $(atf_config_get tmpfile)
106 kill $$
107 touch $(atf_config_get tmpfile).no
108}
109cleanup_sigterm_cleanup()
110{
111 rm $(atf_config_get tmpfile)
112}
113
114# -------------------------------------------------------------------------
115# Helper tests for "t_config".
116# -------------------------------------------------------------------------
117
118atf_test_case config_unset
119config_unset_head()
120{
121 atf_set "descr" "Helper test case for the t_config test program"
122}
123config_unset_body()
124{
125 if atf_config_has 'test'; then
126 atf_fail "Test variable already defined"
127 fi
128}
129
130atf_test_case config_empty
131config_empty_head()
132{
133 atf_set "descr" "Helper test case for the t_config test program"
134}
135config_empty_body()
136{
137 atf_check_equal "$(atf_config_get 'test')" ""
138}
139
140atf_test_case config_value
141config_value_head()
142{
143 atf_set "descr" "Helper test case for the t_config test program"
144}
145config_value_body()
146{
147 atf_check_equal "$(atf_config_get 'test')" "foo"
148}
149
150atf_test_case config_multi_value
151config_multi_value_head()
152{
153 atf_set "descr" "Helper test case for the t_config test program"
154}
155config_multi_value_body()
156{
157 atf_check_equal "$(atf_config_get 'test')" "foo bar"
158}
159
160# -------------------------------------------------------------------------
161# Helper tests for "t_expect".
162# -------------------------------------------------------------------------
163
164atf_test_case expect_pass_and_pass
165expect_pass_and_pass_body()
166{
167 atf_expect_pass
168}
169
170atf_test_case expect_pass_but_fail_requirement
171expect_pass_but_fail_requirement_body()
172{
173 atf_expect_pass
174 atf_fail "Some reason"
175}
176
177atf_test_case expect_pass_but_fail_check
178expect_pass_but_fail_check_body()
179{
180 atf_fail "Non-fatal failures not implemented"
181}
182
183atf_test_case expect_fail_and_fail_requirement
184expect_fail_and_fail_requirement_body()
185{
186 atf_expect_fail "Fail reason"
187 atf_fail "The failure"
188 atf_expect_pass
189}
190
191atf_test_case expect_fail_and_fail_check
192expect_fail_and_fail_check_body()
193{
194 atf_fail "Non-fatal failures not implemented"
195}
196
197atf_test_case expect_fail_but_pass
198expect_fail_but_pass_body()
199{
200 atf_expect_fail "Fail first"
201 atf_expect_pass
202}
203
204atf_test_case expect_exit_any_and_exit
205expect_exit_any_and_exit_body()
206{
207 atf_expect_exit -1 "Call will exit"
208 exit 0
209}
210
211atf_test_case expect_exit_code_and_exit
212expect_exit_code_and_exit_body()
213{
214 atf_expect_exit 123 "Call will exit"
215 exit 123
216}
217
218atf_test_case expect_exit_but_pass
219expect_exit_but_pass_body()
220{
221 atf_expect_exit -1 "Call won't exit"
222}
223
224atf_test_case expect_signal_any_and_signal
225expect_signal_any_and_signal_body()
226{
227 atf_expect_signal -1 "Call will signal"
228 kill -9 $$
229}
230
231atf_test_case expect_signal_no_and_signal
232expect_signal_no_and_signal_body()
233{
234 atf_expect_signal 1 "Call will signal"
235 kill -1 $$
236}
237
238atf_test_case expect_signal_but_pass
239expect_signal_but_pass_body()
240{
241 atf_expect_signal -1 "Call won't signal"
242}
243
244atf_test_case expect_death_and_exit
245expect_death_and_exit_body()
246{
247 atf_expect_death "Exit case"
248 exit 123
249}
250
251atf_test_case expect_death_and_signal
252expect_death_and_signal_body()
253{
254 atf_expect_death "Signal case"
255 kill -9 $$
256}
257
258atf_test_case expect_death_but_pass
259expect_death_but_pass_body()
260{
261 atf_expect_death "Call won't die"
262}
263
264atf_test_case expect_timeout_and_hang
265expect_timeout_and_hang_head()
266{
267 atf_set "timeout" "1"
268}
269expect_timeout_and_hang_body()
270{
271 atf_expect_timeout "Will overrun"
272 sleep 5
273}
274
275atf_test_case expect_timeout_but_pass
276expect_timeout_but_pass_head()
277{
278 atf_set "timeout" "1"
279}
280expect_timeout_but_pass_body()
281{
282 atf_expect_timeout "Will just exit"
283}
284
285# -------------------------------------------------------------------------
286# Helper tests for "t_meta_data".
287# -------------------------------------------------------------------------
288
289atf_test_case metadata_no_descr
290metadata_no_descr_head()
291{
292 :
293}
294metadata_no_descr_body()
295{
296 :
297}
298
299atf_test_case metadata_no_head
300metadata_no_head_body()
301{
302 :
303}
304
305# -------------------------------------------------------------------------
306# Helper tests for "t_srcdir".
307# -------------------------------------------------------------------------
308
309atf_test_case srcdir_exists
310srcdir_exists_head()
311{
312 atf_set "descr" "Helper test case for the t_srcdir test program"
313}
314srcdir_exists_body()
315{
316 [ -f "$(atf_get_srcdir)/datafile" ] || atf_fail "Cannot find datafile"
317}
318
319# -------------------------------------------------------------------------
320# Helper tests for "t_result".
321# -------------------------------------------------------------------------
322
323atf_test_case result_pass
324result_pass_body()
325{
326 echo "msg"
327}
328
329atf_test_case result_fail
330result_fail_body()
331{
332 echo "msg"
333 atf_fail "Failure reason"
334}
335
336atf_test_case result_skip
337result_skip_body()
338{
339 echo "msg"
340 atf_skip "Skipped reason"
341}
342
343# -------------------------------------------------------------------------
344# Main.
345# -------------------------------------------------------------------------
346
347atf_init_test_cases()
348{
349 # Add helper tests for t_cleanup.
350 atf_add_test_case cleanup_pass
351 atf_add_test_case cleanup_fail
352 atf_add_test_case cleanup_skip
353 atf_add_test_case cleanup_curdir
354 atf_add_test_case cleanup_sigterm
355
356 # Add helper tests for t_config.
357 atf_add_test_case config_unset
358 atf_add_test_case config_empty
359 atf_add_test_case config_value
360 atf_add_test_case config_multi_value
361
362 # Add helper tests for t_expect.
363 atf_add_test_case expect_pass_and_pass
364 atf_add_test_case expect_pass_but_fail_requirement
365 atf_add_test_case expect_pass_but_fail_check
366 atf_add_test_case expect_fail_and_fail_requirement
367 atf_add_test_case expect_fail_and_fail_check
368 atf_add_test_case expect_fail_but_pass
369 atf_add_test_case expect_exit_any_and_exit
370 atf_add_test_case expect_exit_code_and_exit
371 atf_add_test_case expect_exit_but_pass
372 atf_add_test_case expect_signal_any_and_signal
373 atf_add_test_case expect_signal_no_and_signal
374 atf_add_test_case expect_signal_but_pass
375 atf_add_test_case expect_death_and_exit
376 atf_add_test_case expect_death_and_signal
377 atf_add_test_case expect_death_but_pass
378 atf_add_test_case expect_timeout_and_hang
379 atf_add_test_case expect_timeout_but_pass
380
381 # Add helper tests for t_meta_data.
382 atf_add_test_case metadata_no_descr
383 atf_add_test_case metadata_no_head
384
385 # Add helper tests for t_srcdir.
386 atf_add_test_case srcdir_exists
387
388 # Add helper tests for t_result.
389 atf_add_test_case result_pass
390 atf_add_test_case result_fail
391 atf_add_test_case result_skip
392}
393
394# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
25
26# -------------------------------------------------------------------------
27# Helper tests for "t_cleanup".
28# -------------------------------------------------------------------------
29
30atf_test_case cleanup_pass cleanup
31cleanup_pass_head()
32{
33 atf_set "descr" "Helper test case for the t_cleanup test program"
34}
35cleanup_pass_body()
36{
37 touch $(atf_config_get tmpfile)
38}
39cleanup_pass_cleanup()
40{
41 if [ $(atf_config_get cleanup no) = yes ]; then
42 rm $(atf_config_get tmpfile)
43 fi
44}
45
46atf_test_case cleanup_fail cleanup
47cleanup_fail_head()
48{
49 atf_set "descr" "Helper test case for the t_cleanup test program"
50}
51cleanup_fail_body()
52{
53 touch $(atf_config_get tmpfile)
54 atf_fail "On purpose"
55}
56cleanup_fail_cleanup()
57{
58 if [ $(atf_config_get cleanup no) = yes ]; then
59 rm $(atf_config_get tmpfile)
60 fi
61}
62
63atf_test_case cleanup_skip cleanup
64cleanup_skip_head()
65{
66 atf_set "descr" "Helper test case for the t_cleanup test program"
67}
68cleanup_skip_body()
69{
70 touch $(atf_config_get tmpfile)
71 atf_skip "On purpose"
72}
73cleanup_skip_cleanup()
74{
75 if [ $(atf_config_get cleanup no) = yes ]; then
76 rm $(atf_config_get tmpfile)
77 fi
78}
79
80atf_test_case cleanup_curdir cleanup
81cleanup_curdir_head()
82{
83 atf_set "descr" "Helper test case for the t_cleanup test program"
84}
85cleanup_curdir_body()
86{
87 echo 1234 >oldvalue
88}
89cleanup_curdir_cleanup()
90{
91 test -f oldvalue && echo "Old value: $(cat oldvalue)"
92}
93
94atf_test_case cleanup_sigterm cleanup
95cleanup_sigterm_head()
96{
97 atf_set "descr" "Helper test case for the t_cleanup test program"
98}
99cleanup_sigterm_body()
100{
101 touch $(atf_config_get tmpfile)
102 kill $$
103 touch $(atf_config_get tmpfile).no
104}
105cleanup_sigterm_cleanup()
106{
107 rm $(atf_config_get tmpfile)
108}
109
110# -------------------------------------------------------------------------
111# Helper tests for "t_config".
112# -------------------------------------------------------------------------
113
114atf_test_case config_unset
115config_unset_head()
116{
117 atf_set "descr" "Helper test case for the t_config test program"
118}
119config_unset_body()
120{
121 if atf_config_has 'test'; then
122 atf_fail "Test variable already defined"
123 fi
124}
125
126atf_test_case config_empty
127config_empty_head()
128{
129 atf_set "descr" "Helper test case for the t_config test program"
130}
131config_empty_body()
132{
133 atf_check_equal "$(atf_config_get 'test')" ""
134}
135
136atf_test_case config_value
137config_value_head()
138{
139 atf_set "descr" "Helper test case for the t_config test program"
140}
141config_value_body()
142{
143 atf_check_equal "$(atf_config_get 'test')" "foo"
144}
145
146atf_test_case config_multi_value
147config_multi_value_head()
148{
149 atf_set "descr" "Helper test case for the t_config test program"
150}
151config_multi_value_body()
152{
153 atf_check_equal "$(atf_config_get 'test')" "foo bar"
154}
155
156# -------------------------------------------------------------------------
157# Helper tests for "t_expect".
158# -------------------------------------------------------------------------
159
160atf_test_case expect_pass_and_pass
161expect_pass_and_pass_body()
162{
163 atf_expect_pass
164}
165
166atf_test_case expect_pass_but_fail_requirement
167expect_pass_but_fail_requirement_body()
168{
169 atf_expect_pass
170 atf_fail "Some reason"
171}
172
173atf_test_case expect_pass_but_fail_check
174expect_pass_but_fail_check_body()
175{
176 atf_fail "Non-fatal failures not implemented"
177}
178
179atf_test_case expect_fail_and_fail_requirement
180expect_fail_and_fail_requirement_body()
181{
182 atf_expect_fail "Fail reason"
183 atf_fail "The failure"
184 atf_expect_pass
185}
186
187atf_test_case expect_fail_and_fail_check
188expect_fail_and_fail_check_body()
189{
190 atf_fail "Non-fatal failures not implemented"
191}
192
193atf_test_case expect_fail_but_pass
194expect_fail_but_pass_body()
195{
196 atf_expect_fail "Fail first"
197 atf_expect_pass
198}
199
200atf_test_case expect_exit_any_and_exit
201expect_exit_any_and_exit_body()
202{
203 atf_expect_exit -1 "Call will exit"
204 exit 0
205}
206
207atf_test_case expect_exit_code_and_exit
208expect_exit_code_and_exit_body()
209{
210 atf_expect_exit 123 "Call will exit"
211 exit 123
212}
213
214atf_test_case expect_exit_but_pass
215expect_exit_but_pass_body()
216{
217 atf_expect_exit -1 "Call won't exit"
218}
219
220atf_test_case expect_signal_any_and_signal
221expect_signal_any_and_signal_body()
222{
223 atf_expect_signal -1 "Call will signal"
224 kill -9 $$
225}
226
227atf_test_case expect_signal_no_and_signal
228expect_signal_no_and_signal_body()
229{
230 atf_expect_signal 1 "Call will signal"
231 kill -1 $$
232}
233
234atf_test_case expect_signal_but_pass
235expect_signal_but_pass_body()
236{
237 atf_expect_signal -1 "Call won't signal"
238}
239
240atf_test_case expect_death_and_exit
241expect_death_and_exit_body()
242{
243 atf_expect_death "Exit case"
244 exit 123
245}
246
247atf_test_case expect_death_and_signal
248expect_death_and_signal_body()
249{
250 atf_expect_death "Signal case"
251 kill -9 $$
252}
253
254atf_test_case expect_death_but_pass
255expect_death_but_pass_body()
256{
257 atf_expect_death "Call won't die"
258}
259
260atf_test_case expect_timeout_and_hang
261expect_timeout_and_hang_head()
262{
263 atf_set "timeout" "1"
264}
265expect_timeout_and_hang_body()
266{
267 atf_expect_timeout "Will overrun"
268 sleep 5
269}
270
271atf_test_case expect_timeout_but_pass
272expect_timeout_but_pass_head()
273{
274 atf_set "timeout" "1"
275}
276expect_timeout_but_pass_body()
277{
278 atf_expect_timeout "Will just exit"
279}
280
281# -------------------------------------------------------------------------
282# Helper tests for "t_meta_data".
283# -------------------------------------------------------------------------
284
285atf_test_case metadata_no_descr
286metadata_no_descr_head()
287{
288 :
289}
290metadata_no_descr_body()
291{
292 :
293}
294
295atf_test_case metadata_no_head
296metadata_no_head_body()
297{
298 :
299}
300
301# -------------------------------------------------------------------------
302# Helper tests for "t_srcdir".
303# -------------------------------------------------------------------------
304
305atf_test_case srcdir_exists
306srcdir_exists_head()
307{
308 atf_set "descr" "Helper test case for the t_srcdir test program"
309}
310srcdir_exists_body()
311{
312 [ -f "$(atf_get_srcdir)/datafile" ] || atf_fail "Cannot find datafile"
313}
314
315# -------------------------------------------------------------------------
316# Helper tests for "t_result".
317# -------------------------------------------------------------------------
318
319atf_test_case result_pass
320result_pass_body()
321{
322 echo "msg"
323}
324
325atf_test_case result_fail
326result_fail_body()
327{
328 echo "msg"
329 atf_fail "Failure reason"
330}
331
332atf_test_case result_skip
333result_skip_body()
334{
335 echo "msg"
336 atf_skip "Skipped reason"
337}
338
339# -------------------------------------------------------------------------
340# Main.
341# -------------------------------------------------------------------------
342
343atf_init_test_cases()
344{
345 # Add helper tests for t_cleanup.
346 atf_add_test_case cleanup_pass
347 atf_add_test_case cleanup_fail
348 atf_add_test_case cleanup_skip
349 atf_add_test_case cleanup_curdir
350 atf_add_test_case cleanup_sigterm
351
352 # Add helper tests for t_config.
353 atf_add_test_case config_unset
354 atf_add_test_case config_empty
355 atf_add_test_case config_value
356 atf_add_test_case config_multi_value
357
358 # Add helper tests for t_expect.
359 atf_add_test_case expect_pass_and_pass
360 atf_add_test_case expect_pass_but_fail_requirement
361 atf_add_test_case expect_pass_but_fail_check
362 atf_add_test_case expect_fail_and_fail_requirement
363 atf_add_test_case expect_fail_and_fail_check
364 atf_add_test_case expect_fail_but_pass
365 atf_add_test_case expect_exit_any_and_exit
366 atf_add_test_case expect_exit_code_and_exit
367 atf_add_test_case expect_exit_but_pass
368 atf_add_test_case expect_signal_any_and_signal
369 atf_add_test_case expect_signal_no_and_signal
370 atf_add_test_case expect_signal_but_pass
371 atf_add_test_case expect_death_and_exit
372 atf_add_test_case expect_death_and_signal
373 atf_add_test_case expect_death_but_pass
374 atf_add_test_case expect_timeout_and_hang
375 atf_add_test_case expect_timeout_but_pass
376
377 # Add helper tests for t_meta_data.
378 atf_add_test_case metadata_no_descr
379 atf_add_test_case metadata_no_head
380
381 # Add helper tests for t_srcdir.
382 atf_add_test_case srcdir_exists
383
384 # Add helper tests for t_result.
385 atf_add_test_case result_pass
386 atf_add_test_case result_fail
387 atf_add_test_case result_skip
388}
389
390# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4