1#
2# Automated Testing Framework (atf)
3#
4# Copyright (c) 2007, 2008, 2009, 2010 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
30all_vars="atf_arch \
31          atf_build_cc \
32          atf_build_cflags \
33          atf_build_cpp \
34          atf_build_cppflags \
35          atf_build_cxx \
36          atf_build_cxxflags \
37          atf_confdir \
38          atf_includedir \
39          atf_libdir \
40          atf_libexecdir \
41          atf_machine \
42          atf_pkgdatadir \
43          atf_shell \
44          atf_workdir"
45all_vars_no=15
46
47atf_test_case list_all
48list_all_head()
49{
50    atf_set "descr" "Tests that at atf-config prints all expected" \
51                    "variables, and not more"
52}
53list_all_body()
54{
55    atf_check -s eq:0 -o save:stdout -e empty atf-config
56    atf_check -s eq:0 -o empty -e empty \
57              test "$(wc -l stdout | awk '{ print $1 }')" = "${all_vars_no}"
58    for v in ${all_vars}; do
59        atf_check -s eq:0 -o ignore -e empty grep "${v}" stdout
60    done
61}
62
63atf_test_case query_one
64query_one_head()
65{
66    atf_set "descr" "Tests that querying a single variable works"
67}
68query_one_body()
69{
70    for v in ${all_vars}; do
71        atf_check -s eq:0 -o save:stdout -o match:"${v}" -e empty \
72            atf-config "${v}"
73        atf_check -s eq:0 -o empty -e empty \
74                  test "$(wc -l stdout | awk '{ print $1 }')" = 1
75    done
76}
77
78atf_test_case query_one_terse
79query_one_terse_head()
80{
81    atf_set "descr" "Tests that querying a single variable in terse mode" \
82                    "works"
83}
84query_one_terse_body()
85{
86    for v in ${all_vars}; do
87        atf_check -s eq:0 -o save:stdout -o match:"${v}" -e empty \
88            atf-config "${v}"
89        atf_check -s eq:0 -o empty -e empty \
90                  test "$(wc -l stdout | awk '{ print $1 }')" = 1
91        atf_check -s eq:0 -o save:stdout -e empty cut -d ' ' -f 3- stdout
92        atf_check -s eq:0 -o empty -e empty mv stdout expout
93        atf_check -s eq:0 -o file:expout -e empty atf-config -t "${v}"
94    done
95}
96
97atf_test_case query_multiple
98query_multiple_head()
99{
100    atf_set "descr" "Tests that querying multiple variables works"
101}
102query_multiple_body()
103{
104    atf_check -s eq:0 -o save:stdout -o match:'atf_libexecdir' \
105        -o match:'atf_shell' -e empty atf-config atf_libexecdir atf_shell
106    atf_check -s eq:0 -o empty -e empty \
107              test "$(wc -l stdout | awk '{ print $1 }')" = 2
108}
109
110atf_test_case query_unknown
111query_unknown_head()
112{
113    atf_set "descr" "Tests that querying an unknown variable delivers" \
114                    "the correct error"
115}
116query_unknown_body()
117{
118    atf_check -s eq:1 -o empty -e match:'Unknown variable.*non_existent' \
119        atf-config non_existent
120}
121
122atf_test_case query_mixture
123query_mixture_head()
124{
125    atf_set "descr" "Tests that querying a known and an unknown variable" \
126                    "delivers the correct error"
127}
128query_mixture_body()
129{
130    for v in ${all_vars}; do
131        atf_check -s eq:1 -o empty -e match:'Unknown variable.*non_existent' \
132                  atf-config "${v}" non_existent
133        atf_check -s eq:1 -o empty -e match:'Unknown variable.*non_existent' \
134                  atf-config non_existent "${v}"
135    done
136}
137
138atf_test_case override_env
139override_env_head()
140{
141    atf_set "descr" "Tests that build-time variables can be overriden" \
142                    "through their corresponding environment variables"
143}
144override_env_body()
145{
146    for v in ${all_vars}; do
147        V=$(echo ${v} | tr '[a-z]' '[A-Z]')
148        atf_check -s eq:0 -o save:stdout -e empty -x "${V}=testval atf-config"
149        atf_check -s eq:0 -o empty -e empty mv stdout all
150
151        atf_check -s eq:0 -o save:stdout -e empty grep "^${v} : " all
152        atf_check -s eq:0 -o empty -e empty mv stdout affected
153        atf_check -s eq:0 -o save:stdout -e empty grep -v "^${v} : " all
154        atf_check -s eq:0 -o empty -e empty mv stdout unaffected
155
156        atf_check -s eq:0 -o empty -e empty \
157                  test "$(wc -l affected | awk '{ print $1 }')" = 1
158        atf_check -s eq:0 -o empty -e empty \
159                  test "$(wc -l unaffected | awk '{ print $1 }')" = \
160                   "$((${all_vars_no} -1))"
161
162        atf_check -s eq:0 -o ignore -e empty grep "^${v} : testval$" affected
163        atf_check -s eq:1 -o empty -e empty grep ' : testval$' unaffected
164    done
165}
166
167# XXX: This does not seem to belong here...
168atf_test_case arch
169arch_head()
170{
171    atf_set "descr" "Tests that the current value of atf_arch is correct" \
172                    "for the corresponding atf_machine"
173}
174arch_body()
175{
176    atf_check -s eq:0 -o save:stdout -e empty atf-config -t atf_arch
177    arch=$(cat stdout)
178    atf_check -s eq:0 -o save:stdout -e empty atf-config -t atf_machine
179    machine=$(cat stdout)
180    echo "Machine type ${machine}, architecture ${arch}"
181
182    case ${machine} in
183        i386|i486|i586|i686)
184            exp_arch=i386
185            ;;
186        x86_64)
187            exp_arch=amd64
188            ;;
189        *)
190            exp_arch=${machine}
191    esac
192    echo "Expected architecture ${exp_arch}"
193
194    atf_check_equal ${arch} ${exp_arch}
195}
196
197atf_init_test_cases()
198{
199    atf_add_test_case list_all
200
201    atf_add_test_case query_one
202    atf_add_test_case query_one_terse
203    atf_add_test_case query_multiple
204    atf_add_test_case query_unknown
205    atf_add_test_case query_mixture
206
207    atf_add_test_case override_env
208
209    atf_add_test_case arch
210}
211
212# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
213