summaryrefslogtreecommitdiffstats
path: root/contrib/atf/test-programs/sh_helpers.sh
blob: de95882f64722cc5c0bf12569a964028b4d42969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#
# Automated Testing Framework (atf)
#
# Copyright (c) 2007 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# -------------------------------------------------------------------------
# Helper tests for "t_cleanup".
# -------------------------------------------------------------------------

atf_test_case cleanup_pass cleanup
cleanup_pass_head()
{
    atf_set "descr" "Helper test case for the t_cleanup test program"
}
cleanup_pass_body()
{
    touch $(atf_config_get tmpfile)
}
cleanup_pass_cleanup()
{
    if [ $(atf_config_get cleanup no) = yes ]; then
        rm $(atf_config_get tmpfile)
    fi
}

atf_test_case cleanup_fail cleanup
cleanup_fail_head()
{
    atf_set "descr" "Helper test case for the t_cleanup test program"
}
cleanup_fail_body()
{
    touch $(atf_config_get tmpfile)
    atf_fail "On purpose"
}
cleanup_fail_cleanup()
{
    if [ $(atf_config_get cleanup no) = yes ]; then
        rm $(atf_config_get tmpfile)
    fi
}

atf_test_case cleanup_skip cleanup
cleanup_skip_head()
{
    atf_set "descr" "Helper test case for the t_cleanup test program"
}
cleanup_skip_body()
{
    touch $(atf_config_get tmpfile)
    atf_skip "On purpose"
}
cleanup_skip_cleanup()
{
    if [ $(atf_config_get cleanup no) = yes ]; then
        rm $(atf_config_get tmpfile)
    fi
}

atf_test_case cleanup_curdir cleanup
cleanup_curdir_head()
{
    atf_set "descr" "Helper test case for the t_cleanup test program"
}
cleanup_curdir_body()
{
    echo 1234 >oldvalue
}
cleanup_curdir_cleanup()
{
    test -f oldvalue && echo "Old value: $(cat oldvalue)"
}

atf_test_case cleanup_sigterm cleanup
cleanup_sigterm_head()
{
    atf_set "descr" "Helper test case for the t_cleanup test program"
}
cleanup_sigterm_body()
{
    touch $(atf_config_get tmpfile)
    kill $$
    touch $(atf_config_get tmpfile).no
}
cleanup_sigterm_cleanup()
{
    rm $(atf_config_get tmpfile)
}

atf_test_case cleanup_fork cleanup
cleanup_fork_head()
{
    atf_set "descr" "Helper test case for the t_cleanup test program"
}
cleanup_fork_body()
{
    :
}
cleanup_fork_cleanup()
{
    exec 1>out
    exec 2>err
    exec 3>res
    rm -f out err res
}

# -------------------------------------------------------------------------
# Helper tests for "t_config".
# -------------------------------------------------------------------------

atf_test_case config_unset
config_unset_head()
{
    atf_set "descr" "Helper test case for the t_config test program"
}
config_unset_body()
{
    if atf_config_has 'test'; then
        atf_fail "Test variable already defined"
    fi
}

atf_test_case config_empty
config_empty_head()
{
    atf_set "descr" "Helper test case for the t_config test program"
}
config_empty_body()
{
    atf_check_equal "$(atf_config_get 'test')" ""
}

atf_test_case config_value
config_value_head()
{
    atf_set "descr" "Helper test case for the t_config test program"
}
config_value_body()
{
    atf_check_equal "$(atf_config_get 'test')" "foo"
}

atf_test_case config_multi_value
config_multi_value_head()
{
    atf_set "descr" "Helper test case for the t_config test program"
}
config_multi_value_body()
{
    atf_check_equal "$(atf_config_get 'test')" "foo bar"
}

# -------------------------------------------------------------------------
# Helper tests for "t_fork".
# -------------------------------------------------------------------------

atf_test_case fork_stop
fork_stop_head()
{
    atf_set "descr" "Helper test case for the t_fork test program"
}
fork_stop_body()
{
    echo ${$} >$(atf_config_get pidfile)
    echo "Wrote pid file"
    echo "Waiting for done file"
    while ! test -f $(atf_config_get donefile); do sleep 1; done
    echo "Exiting"
}

# -------------------------------------------------------------------------
# Helper tests for "t_expect".
# -------------------------------------------------------------------------

atf_test_case expect_pass_and_pass
expect_pass_and_pass_body()
{
    atf_expect_pass
}

atf_test_case expect_pass_but_fail_requirement
expect_pass_but_fail_requirement_body()
{
    atf_expect_pass
    atf_fail "Some reason"
}

atf_test_case expect_pass_but_fail_check
expect_pass_but_fail_check_body()
{
    atf_fail "Non-fatal failures not implemented"
}

atf_test_case expect_fail_and_fail_requirement
expect_fail_and_fail_requirement_body()
{
    atf_expect_fail "Fail reason"
    atf_fail "The failure"
    atf_expect_pass
}

atf_test_case expect_fail_and_fail_check
expect_fail_and_fail_check_body()
{
    atf_fail "Non-fatal failures not implemented"
}

atf_test_case expect_fail_but_pass
expect_fail_but_pass_body()
{
    atf_expect_fail "Fail first"
    atf_expect_pass
}

atf_test_case expect_exit_any_and_exit
expect_exit_any_and_exit_body()
{
    atf_expect_exit -1 "Call will exit"
    exit 0
}

atf_test_case expect_exit_code_and_exit
expect_exit_code_and_exit_body()
{
    atf_expect_exit 123 "Call will exit"
    exit 123
}

atf_test_case expect_exit_but_pass
expect_exit_but_pass_body()
{
    atf_expect_exit -1 "Call won't exit"
}

atf_test_case expect_signal_any_and_signal
expect_signal_any_and_signal_body()
{
    atf_expect_signal -1 "Call will signal"
    kill -9 $$
}

atf_test_case expect_signal_no_and_signal
expect_signal_no_and_signal_body()
{
    atf_expect_signal 1 "Call will signal"
    kill -1 $$
}

atf_test_case expect_signal_but_pass
expect_signal_but_pass_body()
{
    atf_expect_signal -1 "Call won't signal"
}

atf_test_case expect_death_and_exit
expect_death_and_exit_body()
{
    atf_expect_death "Exit case"
    exit 123
}

atf_test_case expect_death_and_signal
expect_death_and_signal_body()
{
    atf_expect_death "Signal case"
    kill -9 $$
}

atf_test_case expect_death_but_pass
expect_death_but_pass_body()
{
    atf_expect_death "Call won't die"
}

atf_test_case expect_timeout_and_hang
expect_timeout_and_hang_head()
{
    atf_set "timeout" "1"
}
expect_timeout_and_hang_body()
{
    atf_expect_timeout "Will overrun"
    sleep 5
}

atf_test_case expect_timeout_but_pass
expect_timeout_but_pass_head()
{
    atf_set "timeout" "1"
}
expect_timeout_but_pass_body()
{
    atf_expect_timeout "Will just exit"
}

# -------------------------------------------------------------------------
# Helper tests for "t_meta_data".
# -------------------------------------------------------------------------

atf_test_case metadata_no_descr
metadata_no_descr_head()
{
    :
}
metadata_no_descr_body()
{
    :
}

atf_test_case metadata_no_head
metadata_no_head_body()
{
    :
}

# -------------------------------------------------------------------------
# Helper tests for "t_srcdir".
# -------------------------------------------------------------------------

atf_test_case srcdir_exists
srcdir_exists_head()
{
    atf_set "descr" "Helper test case for the t_srcdir test program"
}
srcdir_exists_body()
{
    [ -f "$(atf_get_srcdir)/datafile" ] || atf_fail "Cannot find datafile"
}

# -------------------------------------------------------------------------
# Helper tests for "t_result".
# -------------------------------------------------------------------------

atf_test_case result_pass
result_pass_body()
{
    echo "msg"
}

atf_test_case result_fail
result_fail_body()
{
    echo "msg"
    atf_fail "Failure reason"
}

atf_test_case result_skip
result_skip_body()
{
    echo "msg"
    atf_skip "Skipped reason"
}

# -------------------------------------------------------------------------
# Main.
# -------------------------------------------------------------------------

atf_init_test_cases()
{
    # Add helper tests for t_cleanup.
    atf_add_test_case cleanup_pass
    atf_add_test_case cleanup_fail
    atf_add_test_case cleanup_skip
    atf_add_test_case cleanup_curdir
    atf_add_test_case cleanup_sigterm
    atf_add_test_case cleanup_fork

    # Add helper tests for t_config.
    atf_add_test_case config_unset
    atf_add_test_case config_empty
    atf_add_test_case config_value
    atf_add_test_case config_multi_value

    # Add helper tests for t_expect.
    atf_add_test_case expect_pass_and_pass
    atf_add_test_case expect_pass_but_fail_requirement
    atf_add_test_case expect_pass_but_fail_check
    atf_add_test_case expect_fail_and_fail_requirement
    atf_add_test_case expect_fail_and_fail_check
    atf_add_test_case expect_fail_but_pass
    atf_add_test_case expect_exit_any_and_exit
    atf_add_test_case expect_exit_code_and_exit
    atf_add_test_case expect_exit_but_pass
    atf_add_test_case expect_signal_any_and_signal
    atf_add_test_case expect_signal_no_and_signal
    atf_add_test_case expect_signal_but_pass
    atf_add_test_case expect_death_and_exit
    atf_add_test_case expect_death_and_signal
    atf_add_test_case expect_death_but_pass
    atf_add_test_case expect_timeout_and_hang
    atf_add_test_case expect_timeout_but_pass

    # Add helper tests for t_fork.
    atf_add_test_case fork_stop

    # Add helper tests for t_meta_data.
    atf_add_test_case metadata_no_descr
    atf_add_test_case metadata_no_head

    # Add helper tests for t_srcdir.
    atf_add_test_case srcdir_exists

    # Add helper tests for t_result.
    atf_add_test_case result_pass
    atf_add_test_case result_fail
    atf_add_test_case result_skip
}

# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
OpenPOWER on IntegriCloud