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
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
114atf_test_case cleanup_fork cleanup
115cleanup_fork_head()
116{
117    atf_set "descr" "Helper test case for the t_cleanup test program"
118}
119cleanup_fork_body()
120{
121    :
122}
123cleanup_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
135atf_test_case config_unset
136config_unset_head()
137{
138    atf_set "descr" "Helper test case for the t_config test program"
139}
140config_unset_body()
141{
142    if atf_config_has 'test'; then
143        atf_fail "Test variable already defined"
144    fi
145}
146
147atf_test_case config_empty
148config_empty_head()
149{
150    atf_set "descr" "Helper test case for the t_config test program"
151}
152config_empty_body()
153{
154    atf_check_equal "$(atf_config_get 'test')" ""
155}
156
157atf_test_case config_value
158config_value_head()
159{
160    atf_set "descr" "Helper test case for the t_config test program"
161}
162config_value_body()
163{
164    atf_check_equal "$(atf_config_get 'test')" "foo"
165}
166
167atf_test_case config_multi_value
168config_multi_value_head()
169{
170    atf_set "descr" "Helper test case for the t_config test program"
171}
172config_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
181atf_test_case fork_stop
182fork_stop_head()
183{
184    atf_set "descr" "Helper test case for the t_fork test program"
185}
186fork_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
199atf_test_case expect_pass_and_pass
200expect_pass_and_pass_body()
201{
202    atf_expect_pass
203}
204
205atf_test_case expect_pass_but_fail_requirement
206expect_pass_but_fail_requirement_body()
207{
208    atf_expect_pass
209    atf_fail "Some reason"
210}
211
212atf_test_case expect_pass_but_fail_check
213expect_pass_but_fail_check_body()
214{
215    atf_fail "Non-fatal failures not implemented"
216}
217
218atf_test_case expect_fail_and_fail_requirement
219expect_fail_and_fail_requirement_body()
220{
221    atf_expect_fail "Fail reason"
222    atf_fail "The failure"
223    atf_expect_pass
224}
225
226atf_test_case expect_fail_and_fail_check
227expect_fail_and_fail_check_body()
228{
229    atf_fail "Non-fatal failures not implemented"
230}
231
232atf_test_case expect_fail_but_pass
233expect_fail_but_pass_body()
234{
235    atf_expect_fail "Fail first"
236    atf_expect_pass
237}
238
239atf_test_case expect_exit_any_and_exit
240expect_exit_any_and_exit_body()
241{
242    atf_expect_exit -1 "Call will exit"
243    exit 0
244}
245
246atf_test_case expect_exit_code_and_exit
247expect_exit_code_and_exit_body()
248{
249    atf_expect_exit 123 "Call will exit"
250    exit 123
251}
252
253atf_test_case expect_exit_but_pass
254expect_exit_but_pass_body()
255{
256    atf_expect_exit -1 "Call won't exit"
257}
258
259atf_test_case expect_signal_any_and_signal
260expect_signal_any_and_signal_body()
261{
262    atf_expect_signal -1 "Call will signal"
263    kill -9 $$
264}
265
266atf_test_case expect_signal_no_and_signal
267expect_signal_no_and_signal_body()
268{
269    atf_expect_signal 1 "Call will signal"
270    kill -1 $$
271}
272
273atf_test_case expect_signal_but_pass
274expect_signal_but_pass_body()
275{
276    atf_expect_signal -1 "Call won't signal"
277}
278
279atf_test_case expect_death_and_exit
280expect_death_and_exit_body()
281{
282    atf_expect_death "Exit case"
283    exit 123
284}
285
286atf_test_case expect_death_and_signal
287expect_death_and_signal_body()
288{
289    atf_expect_death "Signal case"
290    kill -9 $$
291}
292
293atf_test_case expect_death_but_pass
294expect_death_but_pass_body()
295{
296    atf_expect_death "Call won't die"
297}
298
299atf_test_case expect_timeout_and_hang
300expect_timeout_and_hang_head()
301{
302    atf_set "timeout" "1"
303}
304expect_timeout_and_hang_body()
305{
306    atf_expect_timeout "Will overrun"
307    sleep 5
308}
309
310atf_test_case expect_timeout_but_pass
311expect_timeout_but_pass_head()
312{
313    atf_set "timeout" "1"
314}
315expect_timeout_but_pass_body()
316{
317    atf_expect_timeout "Will just exit"
318}
319
320# -------------------------------------------------------------------------
321# Helper tests for "t_meta_data".
322# -------------------------------------------------------------------------
323
324atf_test_case metadata_no_descr
325metadata_no_descr_head()
326{
327    :
328}
329metadata_no_descr_body()
330{
331    :
332}
333
334atf_test_case metadata_no_head
335metadata_no_head_body()
336{
337    :
338}
339
340# -------------------------------------------------------------------------
341# Helper tests for "t_srcdir".
342# -------------------------------------------------------------------------
343
344atf_test_case srcdir_exists
345srcdir_exists_head()
346{
347    atf_set "descr" "Helper test case for the t_srcdir test program"
348}
349srcdir_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
358atf_test_case result_pass
359result_pass_body()
360{
361    echo "msg"
362}
363
364atf_test_case result_fail
365result_fail_body()
366{
367    echo "msg"
368    atf_fail "Failure reason"
369}
370
371atf_test_case result_skip
372result_skip_body()
373{
374    echo "msg"
375    atf_skip "Skipped reason"
376}
377
378# -------------------------------------------------------------------------
379# Main.
380# -------------------------------------------------------------------------
381
382atf_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
434