xref: /freebsd/contrib/kyua/integration/cmd_db_exec_test.sh (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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# Creates a new database file in the store directory.
31#
32# Subsequent invocations of db-exec should just pick this new file up.
33create_empty_store() {
34    cat >Kyuafile <<EOF
35syntax(2)
36EOF
37    atf_check -s exit:0 -o ignore -e empty kyua test
38}
39
40
41utils_test_case one_arg
42one_arg_body() {
43    create_empty_store
44
45    atf_check -s exit:0 -o save:metadata.csv -e empty \
46        kyua db-exec "SELECT * FROM metadata"
47    atf_check -s exit:0 -o ignore -e empty \
48        grep 'schema_version,.*timestamp' metadata.csv
49}
50
51
52utils_test_case many_args
53many_args_body() {
54    create_empty_store
55
56    atf_check -s exit:0 -o save:metadata.csv -e empty \
57        kyua db-exec SELECT "*" FROM metadata
58    atf_check -s exit:0 -o ignore -e empty \
59        grep 'schema_version,.*timestamp' metadata.csv
60}
61
62
63utils_test_case no_args
64no_args_body() {
65    atf_check -s exit:3 -o empty -e match:"Not enough arguments" kyua db-exec
66    test ! -d .kyua/store/ || atf_fail "Database created but it should" \
67        "not have been"
68}
69
70
71utils_test_case invalid_statement
72invalid_statement_body() {
73    create_empty_store
74
75    atf_check -s exit:1 -o empty -e match:"SQLite error.*foo" \
76        kyua db-exec foo
77}
78
79
80utils_test_case no_create_store
81no_create_store_body() {
82    atf_check -s exit:1 -o empty -e match:"No previous results.*not-here" \
83        kyua db-exec --results-file=not-here "SELECT * FROM metadata"
84    if [ -f not-here ]; then
85        atf_fail "Database created but it should not have been"
86    fi
87}
88
89
90utils_test_case results_file__default_home
91results_file__default_home_body() {
92    HOME=home-dir
93    create_empty_store
94
95    atf_check -s exit:0 -o save:metadata.csv -e empty \
96        kyua db-exec "SELECT * FROM metadata"
97    test -f home-dir/.kyua/store/*.db || atf_fail "Database not created in" \
98        "the home directory"
99    atf_check -s exit:0 -o ignore -e empty \
100        grep 'schema_version,.*timestamp' metadata.csv
101}
102
103
104utils_test_case results_file__explicit__ok
105results_file__explicit__ok_body() {
106    create_empty_store
107    mv .kyua/store/*.db custom.db
108    rmdir .kyua/store
109
110    HOME=home-dir
111    atf_check -s exit:0 -o save:metadata.csv -e empty \
112        kyua --logfile=/dev/null db-exec -r custom.db "SELECT * FROM metadata"
113    test ! -d home-dir/.kyua || atf_fail "Home directory created but this" \
114        "should not have happened"
115    atf_check -s exit:0 -o ignore -e empty \
116        grep 'schema_version,.*timestamp' metadata.csv
117}
118
119
120utils_test_case results_file__explicit__fail
121results_file__explicit__fail_head() {
122    atf_set "require.user" "unprivileged"
123}
124results_file__explicit__fail_body() {
125    atf_check -s exit:1 -o empty -e match:"No previous results.*foo.db" \
126        kyua db-exec --results-file=foo.db "SELECT * FROM metadata"
127}
128
129
130utils_test_case no_headers_flag
131no_headers_flag_body() {
132    create_empty_store
133
134    atf_check kyua db-exec "CREATE TABLE data" \
135        "(a INTEGER PRIMARY KEY, b INTEGER, c TEXT)"
136    atf_check kyua db-exec "INSERT INTO data VALUES (65, 43, NULL)"
137    atf_check kyua db-exec "INSERT INTO data VALUES (23, 42, 'foo')"
138
139    cat >expout <<EOF
140a,b,c
14123,42,foo
14265,43,NULL
143EOF
144    atf_check -s exit:0 -o file:expout -e empty \
145        kyua db-exec "SELECT * FROM data ORDER BY a"
146
147    tail -n 2 <expout >expout2
148    atf_check -s exit:0 -o file:expout2 -e empty \
149        kyua db-exec --no-headers "SELECT * FROM data ORDER BY a"
150}
151
152
153atf_init_test_cases() {
154    atf_add_test_case one_arg
155    atf_add_test_case many_args
156    atf_add_test_case no_args
157    atf_add_test_case invalid_statement
158    atf_add_test_case no_create_store
159
160    atf_add_test_case results_file__default_home
161    atf_add_test_case results_file__explicit__ok
162    atf_add_test_case results_file__explicit__fail
163
164    atf_add_test_case no_headers_flag
165}
166