]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/atf/test-programs/sh_helpers.sh
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / atf / test-programs / sh_helpers.sh
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.
28 #
29
30 # -------------------------------------------------------------------------
31 # Helper tests for "t_cleanup".
32 # -------------------------------------------------------------------------
33
34 atf_test_case cleanup_pass cleanup
35 cleanup_pass_head()
36 {
37     atf_set "descr" "Helper test case for the t_cleanup test program"
38 }
39 cleanup_pass_body()
40 {
41     touch $(atf_config_get tmpfile)
42 }
43 cleanup_pass_cleanup()
44 {
45     if [ $(atf_config_get cleanup no) = yes ]; then
46         rm $(atf_config_get tmpfile)
47     fi
48 }
49
50 atf_test_case cleanup_fail cleanup
51 cleanup_fail_head()
52 {
53     atf_set "descr" "Helper test case for the t_cleanup test program"
54 }
55 cleanup_fail_body()
56 {
57     touch $(atf_config_get tmpfile)
58     atf_fail "On purpose"
59 }
60 cleanup_fail_cleanup()
61 {
62     if [ $(atf_config_get cleanup no) = yes ]; then
63         rm $(atf_config_get tmpfile)
64     fi
65 }
66
67 atf_test_case cleanup_skip cleanup
68 cleanup_skip_head()
69 {
70     atf_set "descr" "Helper test case for the t_cleanup test program"
71 }
72 cleanup_skip_body()
73 {
74     touch $(atf_config_get tmpfile)
75     atf_skip "On purpose"
76 }
77 cleanup_skip_cleanup()
78 {
79     if [ $(atf_config_get cleanup no) = yes ]; then
80         rm $(atf_config_get tmpfile)
81     fi
82 }
83
84 atf_test_case cleanup_curdir cleanup
85 cleanup_curdir_head()
86 {
87     atf_set "descr" "Helper test case for the t_cleanup test program"
88 }
89 cleanup_curdir_body()
90 {
91     echo 1234 >oldvalue
92 }
93 cleanup_curdir_cleanup()
94 {
95     test -f oldvalue && echo "Old value: $(cat oldvalue)"
96 }
97
98 atf_test_case cleanup_sigterm cleanup
99 cleanup_sigterm_head()
100 {
101     atf_set "descr" "Helper test case for the t_cleanup test program"
102 }
103 cleanup_sigterm_body()
104 {
105     touch $(atf_config_get tmpfile)
106     kill $$
107     touch $(atf_config_get tmpfile).no
108 }
109 cleanup_sigterm_cleanup()
110 {
111     rm $(atf_config_get tmpfile)
112 }
113
114 atf_test_case cleanup_fork cleanup
115 cleanup_fork_head()
116 {
117     atf_set "descr" "Helper test case for the t_cleanup test program"
118 }
119 cleanup_fork_body()
120 {
121     :
122 }
123 cleanup_fork_cleanup()
124 {
125     exec 1>out
126     exec 2>err
127     exec 3>res
128     rm -f out err res
129 }
130
131 # -------------------------------------------------------------------------
132 # Helper tests for "t_config".
133 # -------------------------------------------------------------------------
134
135 atf_test_case config_unset
136 config_unset_head()
137 {
138     atf_set "descr" "Helper test case for the t_config test program"
139 }
140 config_unset_body()
141 {
142     if atf_config_has 'test'; then
143         atf_fail "Test variable already defined"
144     fi
145 }
146
147 atf_test_case config_empty
148 config_empty_head()
149 {
150     atf_set "descr" "Helper test case for the t_config test program"
151 }
152 config_empty_body()
153 {
154     atf_check_equal "$(atf_config_get 'test')" ""
155 }
156
157 atf_test_case config_value
158 config_value_head()
159 {
160     atf_set "descr" "Helper test case for the t_config test program"
161 }
162 config_value_body()
163 {
164     atf_check_equal "$(atf_config_get 'test')" "foo"
165 }
166
167 atf_test_case config_multi_value
168 config_multi_value_head()
169 {
170     atf_set "descr" "Helper test case for the t_config test program"
171 }
172 config_multi_value_body()
173 {
174     atf_check_equal "$(atf_config_get 'test')" "foo bar"
175 }
176
177 # -------------------------------------------------------------------------
178 # Helper tests for "t_fork".
179 # -------------------------------------------------------------------------
180
181 atf_test_case fork_stop
182 fork_stop_head()
183 {
184     atf_set "descr" "Helper test case for the t_fork test program"
185 }
186 fork_stop_body()
187 {
188     echo ${$} >$(atf_config_get pidfile)
189     echo "Wrote pid file"
190     echo "Waiting for done file"
191     while ! test -f $(atf_config_get donefile); do sleep 1; done
192     echo "Exiting"
193 }
194
195 # -------------------------------------------------------------------------
196 # Helper tests for "t_expect".
197 # -------------------------------------------------------------------------
198
199 atf_test_case expect_pass_and_pass
200 expect_pass_and_pass_body()
201 {
202     atf_expect_pass
203 }
204
205 atf_test_case expect_pass_but_fail_requirement
206 expect_pass_but_fail_requirement_body()
207 {
208     atf_expect_pass
209     atf_fail "Some reason"
210 }
211
212 atf_test_case expect_pass_but_fail_check
213 expect_pass_but_fail_check_body()
214 {
215     atf_fail "Non-fatal failures not implemented"
216 }
217
218 atf_test_case expect_fail_and_fail_requirement
219 expect_fail_and_fail_requirement_body()
220 {
221     atf_expect_fail "Fail reason"
222     atf_fail "The failure"
223     atf_expect_pass
224 }
225
226 atf_test_case expect_fail_and_fail_check
227 expect_fail_and_fail_check_body()
228 {
229     atf_fail "Non-fatal failures not implemented"
230 }
231
232 atf_test_case expect_fail_but_pass
233 expect_fail_but_pass_body()
234 {
235     atf_expect_fail "Fail first"
236     atf_expect_pass
237 }
238
239 atf_test_case expect_exit_any_and_exit
240 expect_exit_any_and_exit_body()
241 {
242     atf_expect_exit -1 "Call will exit"
243     exit 0
244 }
245
246 atf_test_case expect_exit_code_and_exit
247 expect_exit_code_and_exit_body()
248 {
249     atf_expect_exit 123 "Call will exit"
250     exit 123
251 }
252
253 atf_test_case expect_exit_but_pass
254 expect_exit_but_pass_body()
255 {
256     atf_expect_exit -1 "Call won't exit"
257 }
258
259 atf_test_case expect_signal_any_and_signal
260 expect_signal_any_and_signal_body()
261 {
262     atf_expect_signal -1 "Call will signal"
263     kill -9 $$
264 }
265
266 atf_test_case expect_signal_no_and_signal
267 expect_signal_no_and_signal_body()
268 {
269     atf_expect_signal 1 "Call will signal"
270     kill -1 $$
271 }
272
273 atf_test_case expect_signal_but_pass
274 expect_signal_but_pass_body()
275 {
276     atf_expect_signal -1 "Call won't signal"
277 }
278
279 atf_test_case expect_death_and_exit
280 expect_death_and_exit_body()
281 {
282     atf_expect_death "Exit case"
283     exit 123
284 }
285
286 atf_test_case expect_death_and_signal
287 expect_death_and_signal_body()
288 {
289     atf_expect_death "Signal case"
290     kill -9 $$
291 }
292
293 atf_test_case expect_death_but_pass
294 expect_death_but_pass_body()
295 {
296     atf_expect_death "Call won't die"
297 }
298
299 atf_test_case expect_timeout_and_hang
300 expect_timeout_and_hang_head()
301 {
302     atf_set "timeout" "1"
303 }
304 expect_timeout_and_hang_body()
305 {
306     atf_expect_timeout "Will overrun"
307     sleep 5
308 }
309
310 atf_test_case expect_timeout_but_pass
311 expect_timeout_but_pass_head()
312 {
313     atf_set "timeout" "1"
314 }
315 expect_timeout_but_pass_body()
316 {
317     atf_expect_timeout "Will just exit"
318 }
319
320 # -------------------------------------------------------------------------
321 # Helper tests for "t_meta_data".
322 # -------------------------------------------------------------------------
323
324 atf_test_case metadata_no_descr
325 metadata_no_descr_head()
326 {
327     :
328 }
329 metadata_no_descr_body()
330 {
331     :
332 }
333
334 atf_test_case metadata_no_head
335 metadata_no_head_body()
336 {
337     :
338 }
339
340 # -------------------------------------------------------------------------
341 # Helper tests for "t_srcdir".
342 # -------------------------------------------------------------------------
343
344 atf_test_case srcdir_exists
345 srcdir_exists_head()
346 {
347     atf_set "descr" "Helper test case for the t_srcdir test program"
348 }
349 srcdir_exists_body()
350 {
351     [ -f "$(atf_get_srcdir)/datafile" ] || atf_fail "Cannot find datafile"
352 }
353
354 # -------------------------------------------------------------------------
355 # Helper tests for "t_result".
356 # -------------------------------------------------------------------------
357
358 atf_test_case result_pass
359 result_pass_body()
360 {
361     echo "msg"
362 }
363
364 atf_test_case result_fail
365 result_fail_body()
366 {
367     echo "msg"
368     atf_fail "Failure reason"
369 }
370
371 atf_test_case result_skip
372 result_skip_body()
373 {
374     echo "msg"
375     atf_skip "Skipped reason"
376 }
377
378 # -------------------------------------------------------------------------
379 # Main.
380 # -------------------------------------------------------------------------
381
382 atf_init_test_cases()
383 {
384     # Add helper tests for t_cleanup.
385     atf_add_test_case cleanup_pass
386     atf_add_test_case cleanup_fail
387     atf_add_test_case cleanup_skip
388     atf_add_test_case cleanup_curdir
389     atf_add_test_case cleanup_sigterm
390     atf_add_test_case cleanup_fork
391
392     # Add helper tests for t_config.
393     atf_add_test_case config_unset
394     atf_add_test_case config_empty
395     atf_add_test_case config_value
396     atf_add_test_case config_multi_value
397
398     # Add helper tests for t_expect.
399     atf_add_test_case expect_pass_and_pass
400     atf_add_test_case expect_pass_but_fail_requirement
401     atf_add_test_case expect_pass_but_fail_check
402     atf_add_test_case expect_fail_and_fail_requirement
403     atf_add_test_case expect_fail_and_fail_check
404     atf_add_test_case expect_fail_but_pass
405     atf_add_test_case expect_exit_any_and_exit
406     atf_add_test_case expect_exit_code_and_exit
407     atf_add_test_case expect_exit_but_pass
408     atf_add_test_case expect_signal_any_and_signal
409     atf_add_test_case expect_signal_no_and_signal
410     atf_add_test_case expect_signal_but_pass
411     atf_add_test_case expect_death_and_exit
412     atf_add_test_case expect_death_and_signal
413     atf_add_test_case expect_death_but_pass
414     atf_add_test_case expect_timeout_and_hang
415     atf_add_test_case expect_timeout_but_pass
416
417     # Add helper tests for t_fork.
418     atf_add_test_case fork_stop
419
420     # Add helper tests for t_meta_data.
421     atf_add_test_case metadata_no_descr
422     atf_add_test_case metadata_no_head
423
424     # Add helper tests for t_srcdir.
425     atf_add_test_case srcdir_exists
426
427     # Add helper tests for t_result.
428     atf_add_test_case result_pass
429     atf_add_test_case result_fail
430     atf_add_test_case result_skip
431 }
432
433 # vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4