xref: /freebsd/contrib/atf/atf-sh/atf_check_test.sh (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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.
25
26# TODO: Bring in the checks in the bootstrap testsuite for atf_check.
27
28atf_test_case info_ok
29info_ok_head()
30{
31    atf_set "descr" "Verifies that atf_check prints an informative" \
32                    "message even when the command is successful"
33}
34info_ok_body()
35{
36    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
37
38    atf_check -s eq:0 -o save:stdout -e save:stderr -x \
39              "${h} atf_check_info_ok"
40    grep 'Executing command.*true' stdout >/dev/null || \
41        atf_fail "atf_check does not print an informative message"
42
43    atf_check -s eq:0 -o save:stdout -e save:stderr -x \
44              "${h} atf_check_info_fail"
45    grep 'Executing command.*false' stdout >/dev/null || \
46        atf_fail "atf_check does not print an informative message"
47}
48
49atf_test_case expout_mismatch
50expout_mismatch_head()
51{
52    atf_set "descr" "Verifies that atf_check prints a diff of the" \
53                    "stdout and the expected stdout if the two do not" \
54                    "match"
55}
56expout_mismatch_body()
57{
58    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
59
60    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
61              "${h} atf_check_expout_mismatch"
62    grep 'Executing command.*echo bar' stdout >/dev/null || \
63        atf_fail "atf_check does not print an informative message"
64    grep 'stdout does not match golden output' stderr >/dev/null || \
65        atf_fail "atf_check does not print the stdout header"
66    grep 'stderr' stderr >/dev/null && \
67        atf_fail "atf_check prints the stderr header"
68    grep '^-foo' stderr >/dev/null || \
69        atf_fail "atf_check does not print the stdout's diff"
70    grep '^+bar' stderr >/dev/null || \
71        atf_fail "atf_check does not print the stdout's diff"
72}
73
74atf_test_case experr_mismatch
75experr_mismatch_head()
76{
77    atf_set "descr" "Verifies that atf_check prints a diff of the" \
78                    "stderr and the expected stderr if the two do not" \
79                    "match"
80}
81experr_mismatch_body()
82{
83    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
84
85    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
86              "${h} atf_check_experr_mismatch"
87    grep 'Executing command.*echo bar' stdout >/dev/null || \
88        atf_fail "atf_check does not print an informative message"
89    grep 'stdout' stderr >/dev/null && \
90        atf_fail "atf_check prints the stdout header"
91    grep 'stderr does not match golden output' stderr >/dev/null || \
92        atf_fail "atf_check does not print the stderr header"
93    grep '^-foo' stderr >/dev/null || \
94        atf_fail "atf_check does not print the stderr's diff"
95    grep '^+bar' stderr >/dev/null || \
96        atf_fail "atf_check does not print the stderr's diff"
97}
98
99atf_test_case null_stdout
100null_stdout_head()
101{
102    atf_set "descr" "Verifies that atf_check prints a the stdout it got" \
103                    "when it was supposed to be null"
104}
105null_stdout_body()
106{
107    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
108
109    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
110              "${h} atf_check_null_stdout"
111    grep 'Executing command.*echo.*These.*contents' stdout >/dev/null || \
112        atf_fail "atf_check does not print an informative message"
113    grep 'stdout not empty' stderr >/dev/null || \
114        atf_fail "atf_check does not print the stdout header"
115    grep 'stderr' stderr >/dev/null && \
116        atf_fail "atf_check prints the stderr header"
117    grep 'These are the contents' stderr >/dev/null || \
118        atf_fail "atf_check does not print stdout's contents"
119}
120
121atf_test_case null_stderr
122null_stderr_head()
123{
124    atf_set "descr" "Verifies that atf_check prints a the stderr it got" \
125                    "when it was supposed to be null"
126}
127null_stderr_body()
128{
129    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
130
131    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
132              "${h} atf_check_null_stderr"
133    grep 'Executing command.*echo.*These.*contents' stdout >/dev/null || \
134        atf_fail "atf_check does not print an informative message"
135    grep 'stdout' stderr >/dev/null && \
136        atf_fail "atf_check prints the stdout header"
137    grep 'stderr not empty' stderr >/dev/null || \
138        atf_fail "atf_check does not print the stderr header"
139    grep 'These are the contents' stderr >/dev/null || \
140        atf_fail "atf_check does not print stderr's contents"
141}
142
143atf_test_case equal
144equal_head()
145{
146    atf_set "descr" "Verifies that atf_check_equal works"
147}
148equal_body()
149{
150    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
151
152    atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_equal_ok"
153
154    atf_check -s eq:1 -o ignore -e ignore -x \
155        "${h} -r resfile atf_check_equal_fail"
156    atf_check -s eq:0 -o ignore -e empty grep '^failed: a != b (a != b)$' \
157        resfile
158
159    atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_equal_eval_ok"
160
161    atf_check -s eq:1 -o ignore -e ignore -x \
162        "${h} -r resfile atf_check_equal_eval_fail"
163    atf_check -s eq:0 -o ignore -e empty \
164        grep '^failed: \${x} != \${y} (a != b)$' resfile
165}
166
167atf_test_case not_equal
168not_equal_head()
169{
170    atf_set "descr" "Verifies that atf_check_not_equal works"
171}
172not_equal_body()
173{
174    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
175
176    atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_not_equal_ok"
177
178    atf_check -s eq:1 -o ignore -e ignore -x \
179        "${h} -r resfile atf_check_not_equal_fail"
180    atf_check -s eq:0 -o ignore -e empty grep '^failed: a == b (a == b)$' \
181        resfile
182
183    atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_not_equal_eval_ok"
184
185    atf_check -s eq:1 -o ignore -e ignore -x \
186        "${h} -r resfile atf_check_not_equal_eval_fail"
187    atf_check -s eq:0 -o ignore -e empty \
188        grep '^failed: \${x} == \${y} (a == b)$' resfile
189}
190
191atf_test_case flush_stdout_on_death
192flush_stdout_on_death_body()
193{
194    CONTROL_FILE="$(pwd)/done" "$(atf_get_srcdir)/misc_helpers" \
195        -s "$(atf_get_srcdir)" atf_check_flush_stdout >out 2>err &
196    pid="${!}"
197    while [ ! -f ./done ]; do
198        echo "Still waiting for helper to create control file"
199        ls
200        sleep 1
201    done
202    kill -9 "${pid}"
203
204    grep 'Executing command.*true' out \
205        || atf_fail 'First command not in output'
206    grep 'Executing command.*false' out \
207        || atf_fail 'Second command not in output'
208}
209
210atf_init_test_cases()
211{
212    atf_add_test_case info_ok
213    atf_add_test_case expout_mismatch
214    atf_add_test_case experr_mismatch
215    atf_add_test_case null_stdout
216    atf_add_test_case null_stderr
217    atf_add_test_case equal
218    atf_add_test_case flush_stdout_on_death
219}
220
221# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
222