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