]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - integration/cmd_debug_test.sh
Import the kyua testing framework for infrastructure software
[FreeBSD/FreeBSD.git] / integration / cmd_debug_test.sh
1 # Copyright 2011 The Kyua Authors.
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 are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 #   notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 #   notice, this list of conditions and the following disclaimer in the
12 #   documentation and/or other materials provided with the distribution.
13 # * Neither the name of Google Inc. nor the names of its contributors
14 #   may be used to endorse or promote products derived from this software
15 #   without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30 utils_test_case no_args
31 no_args_body() {
32     cat >Kyuafile <<EOF
33 syntax(2)
34 test_suite("integration")
35 atf_test_program{name="simple_all_pass"}
36 EOF
37     utils_cp_helper simple_all_pass .
38
39     cat >experr <<EOF
40 Usage error for command debug: Not enough arguments.
41 Type 'kyua help debug' for usage information.
42 EOF
43     atf_check -s exit:3 -o empty -e file:experr kyua debug
44 }
45
46
47 utils_test_case many_args
48 many_args_body() {
49     cat >Kyuafile <<EOF
50 syntax(2)
51 test_suite("integration")
52 atf_test_program{name="first"}
53 atf_test_program{name="second"}
54 EOF
55     utils_cp_helper simple_all_pass first
56     utils_cp_helper simple_all_pass second
57
58     cat >experr <<EOF
59 Usage error for command debug: Too many arguments.
60 Type 'kyua help debug' for usage information.
61 EOF
62     atf_check -s exit:3 -o empty -e file:experr kyua debug first:pass \
63         second:pass
64 }
65
66
67 utils_test_case one_arg__ok_pass
68 one_arg__ok_pass_body() {
69     cat >Kyuafile <<EOF
70 syntax(2)
71 test_suite("integration")
72 atf_test_program{name="first"}
73 atf_test_program{name="second"}
74 EOF
75     utils_cp_helper expect_all_pass first
76     utils_cp_helper simple_all_pass second
77
78     cat >expout <<EOF
79 This is the stdout of pass
80 second:pass  ->  passed
81 EOF
82 cat >experr <<EOF
83 This is the stderr of pass
84 EOF
85     atf_check -s exit:0 -o file:expout -e file:experr kyua debug second:pass
86 }
87
88
89 utils_test_case one_arg__ok_fail
90 one_arg__ok_fail_body() {
91     cat >Kyuafile <<EOF
92 syntax(2)
93 test_suite("integration")
94 atf_test_program{name="first"}
95 EOF
96     utils_cp_helper simple_some_fail first
97
98     cat >expout <<EOF
99 This is the stdout of fail
100 first:fail  ->  failed: This fails on purpose
101 EOF
102     cat >experr <<EOF
103 This is the stderr of fail
104 EOF
105     atf_check -s exit:1 -o file:expout -e file:experr kyua debug first:fail
106 }
107
108
109 utils_test_case one_arg__no_match
110 one_arg__no_match_body() {
111     cat >Kyuafile <<EOF
112 syntax(2)
113 test_suite("integration")
114 atf_test_program{name="first"}
115 atf_test_program{name="second"}
116 EOF
117     utils_cp_helper expect_all_pass first
118     utils_cp_helper simple_all_pass second
119
120     cat >experr <<EOF
121 kyua: E: Unknown test case 'second:die'.
122 EOF
123     atf_check -s exit:2 -o empty -e file:experr kyua debug second:die
124 }
125
126
127 utils_test_case one_arg__no_test_case
128 one_arg__no_test_case_body() {
129     # CHECK_STYLE_DISABLE
130     cat >experr <<EOF
131 Usage error for command debug: 'foo' is not a test case identifier (missing ':'?).
132 Type 'kyua help debug' for usage information.
133 EOF
134     # CHECK_STYLE_ENABLE
135     atf_check -s exit:3 -o empty -e file:experr kyua debug foo
136 }
137
138
139 utils_test_case one_arg__bad_filter
140 one_arg__bad_filter_body() {
141     cat >experr <<EOF
142 kyua: E: Test case component in 'foo:' is empty.
143 EOF
144     atf_check -s exit:2 -o empty -e file:experr kyua debug foo:
145 }
146
147
148 utils_test_case body_and_cleanup
149 body_and_cleanup_body() {
150     cat >Kyuafile <<EOF
151 syntax(2)
152 test_suite("integration")
153 atf_test_program{name="single"}
154 EOF
155     utils_cp_helper metadata single
156
157     cat >expout <<EOF
158 single:with_cleanup  ->  passed
159 EOF
160     atf_check -s exit:0 -o file:expout -e empty kyua debug \
161         --stdout=saved.out --stderr=saved.err single:with_cleanup
162
163     cat >expout <<EOF
164 Body message to stdout
165 Cleanup message to stdout
166 EOF
167     atf_check -s exit:0 -o file:expout -e empty cat saved.out
168
169     cat >experr <<EOF
170 Body message to stderr
171 Cleanup message to stderr
172 EOF
173     atf_check -s exit:0 -o file:experr -e empty cat saved.err
174 }
175
176
177 utils_test_case stdout_stderr_flags
178 stdout_stderr_flags_body() {
179     cat >Kyuafile <<EOF
180 syntax(2)
181 test_suite("integration")
182 atf_test_program{name="first"}
183 atf_test_program{name="second"}
184 EOF
185     utils_cp_helper expect_all_pass first
186     utils_cp_helper simple_all_pass second
187
188     cat >expout <<EOF
189 second:pass  ->  passed
190 EOF
191     atf_check -s exit:0 -o file:expout -e empty kyua debug \
192         --stdout=saved.out --stderr=saved.err second:pass
193
194     cat >expout <<EOF
195 This is the stdout of pass
196 EOF
197     cmp -s saved.out expout || atf_fail "--stdout did not redirect the" \
198         "standard output to the desired file"
199
200     cat >experr <<EOF
201 This is the stderr of pass
202 EOF
203     cmp -s saved.err experr || atf_fail "--stderr did not redirect the" \
204         "standard error to the desired file"
205 }
206
207
208 utils_test_case args_are_relative
209 args_are_relative_body() {
210     mkdir root
211     cat >root/Kyuafile <<EOF
212 syntax(2)
213 test_suite("integration")
214 include("subdir/Kyuafile")
215 atf_test_program{name="prog"}
216 EOF
217     utils_cp_helper simple_all_pass root/prog
218
219     mkdir root/subdir
220     cat >root/subdir/Kyuafile <<EOF
221 syntax(2)
222 test_suite("integration")
223 atf_test_program{name="prog"}
224 EOF
225     utils_cp_helper simple_some_fail root/subdir/prog
226
227     cat >expout <<EOF
228 This is the stdout of fail
229 subdir/prog:fail  ->  failed: This fails on purpose
230 EOF
231     cat >experr <<EOF
232 This is the stderr of fail
233 EOF
234     atf_check -s exit:1 -o file:expout -e file:experr kyua debug \
235         -k "$(pwd)/root/Kyuafile" subdir/prog:fail
236 }
237
238
239 utils_test_case only_load_used_test_programs
240 only_load_used_test_programs_body() {
241     cat >Kyuafile <<EOF
242 syntax(2)
243 test_suite("integration")
244 atf_test_program{name="first"}
245 atf_test_program{name="second"}
246 EOF
247     utils_cp_helper simple_all_pass first
248     utils_cp_helper bad_test_program second
249
250     cat >expout <<EOF
251 This is the stdout of pass
252 first:pass  ->  passed
253 EOF
254     cat >experr <<EOF
255 This is the stderr of pass
256 EOF
257     CREATE_COOKIE="$(pwd)/cookie"; export CREATE_COOKIE
258     atf_check -s exit:0 -o file:expout -e file:experr kyua debug first:pass
259     if [ -f "${CREATE_COOKIE}" ]; then
260         atf_fail "An unmatched test case has been executed, which harms" \
261             "performance"
262     fi
263 }
264
265
266 utils_test_case config_behavior
267 config_behavior_body() {
268     cat >"my-config" <<EOF
269 syntax(2)
270 test_suites.suite1["the-variable"] = "value1"
271 test_suites.suite2["the-variable"] = "override me"
272 EOF
273
274     cat >Kyuafile <<EOF
275 syntax(2)
276 atf_test_program{name="config1", test_suite="suite1"}
277 atf_test_program{name="config2", test_suite="suite2"}
278 atf_test_program{name="config3", test_suite="suite3"}
279 EOF
280     utils_cp_helper config config1
281     utils_cp_helper config config2
282     utils_cp_helper config config3
283
284     atf_check -s exit:1 -o match:'failed' -e empty \
285         kyua -c my-config -v test_suites.suite2.the-variable=value2 \
286         debug config1:get_variable
287     atf_check -s exit:0 -o match:'passed' -e empty \
288         kyua -c my-config -v test_suites.suite2.the-variable=value2 \
289         debug config2:get_variable
290     atf_check -s exit:0 -o match:'skipped' -e empty \
291         kyua -c my-config -v test_suites.suite2.the-variable=value2 \
292         debug config3:get_variable
293 }
294
295
296 utils_test_case build_root_flag
297 build_root_flag_body() {
298     cat >Kyuafile <<EOF
299 syntax(2)
300 test_suite("integration")
301 atf_test_program{name="first"}
302 atf_test_program{name="second"}
303 EOF
304     mkdir build
305     utils_cp_helper expect_all_pass build/first
306     utils_cp_helper simple_all_pass build/second
307
308     cat >expout <<EOF
309 This is the stdout of pass
310 second:pass  ->  passed
311 EOF
312 cat >experr <<EOF
313 This is the stderr of pass
314 EOF
315     atf_check -s exit:0 -o file:expout -e file:experr \
316         kyua debug --build-root=build second:pass
317 }
318
319
320 utils_test_case kyuafile_flag__ok
321 kyuafile_flag__ok_body() {
322     cat >Kyuafile <<EOF
323 This file is bogus but it is not loaded.
324 EOF
325
326     cat >myfile <<EOF
327 syntax(2)
328 test_suite("hello-world")
329 atf_test_program{name="sometest"}
330 EOF
331     utils_cp_helper simple_all_pass sometest
332
333     atf_check -s exit:0 -o match:passed -e empty kyua test -k myfile sometest
334     atf_check -s exit:0 -o match:passed -e empty kyua test --kyuafile=myfile \
335         sometest
336 }
337
338
339 utils_test_case missing_kyuafile
340 missing_kyuafile_body() {
341     cat >experr <<EOF
342 kyua: E: Load of 'Kyuafile' failed: File 'Kyuafile' not found.
343 EOF
344     atf_check -s exit:2 -o empty -e file:experr kyua debug foo:bar
345 }
346
347
348 utils_test_case bogus_kyuafile
349 bogus_kyuafile_body() {
350     cat >Kyuafile <<EOF
351 Hello, world.
352 EOF
353     atf_check -s exit:2 -o empty \
354         -e match:"Load of 'Kyuafile' failed: .* Kyuafile:2:" kyua list
355 }
356
357
358 utils_test_case bogus_test_program
359 bogus_test_program_body() {
360     cat >Kyuafile <<EOF
361 syntax(2)
362 test_suite("integration")
363 atf_test_program{name="crash_on_list"}
364 atf_test_program{name="non_executable"}
365 EOF
366     utils_cp_helper bad_test_program crash_on_list
367     echo 'I am not executable' >non_executable
368
369     cat >experr <<EOF
370 kyua: E: Unknown test case 'crash_on_list:a'.
371 EOF
372     atf_check -s exit:2 -o empty -e file:experr kyua debug crash_on_list:a
373
374     cat >experr <<EOF
375 kyua: E: Unknown test case 'non_executable:a'.
376 EOF
377     atf_check -s exit:2 -o empty -e file:experr kyua debug non_executable:a
378
379     # CHECK_STYLE_DISABLE
380     cat >expout <<EOF
381 crash_on_list:__test_cases_list__  ->  broken: Invalid header for test case list; expecting Content-Type for application/X-atf-tp version 1, got ''
382 EOF
383     # CHECK_STYLE_ENABLE
384     atf_check -s exit:1 -o file:expout -e empty kyua debug \
385         crash_on_list:__test_cases_list__
386
387     # CHECK_STYLE_DISABLE
388     cat >expout <<EOF
389 non_executable:__test_cases_list__  ->  broken: Permission denied to run test program
390 EOF
391     # CHECK_STYLE_ENABLE
392     atf_check -s exit:1 -o file:expout -e empty kyua debug \
393         non_executable:__test_cases_list__
394 }
395
396
397 atf_init_test_cases() {
398     atf_add_test_case no_args
399     atf_add_test_case many_args
400     atf_add_test_case one_arg__ok_pass
401     atf_add_test_case one_arg__ok_fail
402     atf_add_test_case one_arg__no_match
403     atf_add_test_case one_arg__no_test_case
404     atf_add_test_case one_arg__bad_filter
405
406     atf_add_test_case body_and_cleanup
407
408     atf_add_test_case stdout_stderr_flags
409
410     atf_add_test_case args_are_relative
411
412     atf_add_test_case only_load_used_test_programs
413
414     atf_add_test_case config_behavior
415
416     atf_add_test_case build_root_flag
417     atf_add_test_case kyuafile_flag__ok
418     atf_add_test_case missing_kyuafile
419     atf_add_test_case bogus_kyuafile
420     atf_add_test_case bogus_test_program
421 }