xref: /netbsd-src/external/bsd/kyua-cli/dist/integration/cmd_db_migrate_test.sh (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1# Copyright 2013 Google 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 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
30utils_test_case upgrade
31upgrade_head() {
32    data=$(atf_get_srcdir)/../store
33
34    atf_set require.files "${data}/schema_v1.sql ${data}/testdata_v1.sql"
35    atf_set require.progs "sqlite3"
36}
37upgrade_body() {
38    data=$(atf_get_srcdir)/../store
39
40    mkdir .kyua
41    cat "${data}/schema_v1.sql" "${data}/testdata_v1.sql" \
42        | sqlite3 .kyua/store.db
43    atf_check -s exit:0 -o empty -e empty kyua db-migrate
44}
45
46
47utils_test_case already_up_to_date
48already_up_to_date_body() {
49    atf_check -s exit:0 -o ignore -e empty \
50        kyua db-exec "SELECT * FROM metadata"  # Create database.
51    atf_check -s exit:1 -o empty -e match:"already at schema version" \
52        kyua db-migrate
53}
54
55
56utils_test_case need_upgrade
57need_upgrade_head() {
58    data=$(atf_get_srcdir)/../store
59
60    atf_set require.files "${data}/schema_v1.sql"
61    atf_set require.progs "sqlite3"
62}
63need_upgrade_body() {
64    data=$(atf_get_srcdir)/../store
65
66    mkdir .kyua
67    sqlite3 .kyua/store.db <"${data}/schema_v1.sql"
68    atf_check -s exit:2 -o empty \
69        -e match:"database has schema version 1.*use db-migrate" kyua report
70}
71
72
73utils_test_case store_flag__ok
74store_flag__ok_body() {
75    echo "This is not a valid database" >test.db
76    atf_check -s exit:1 -o empty -e match:"Migration failed" \
77        kyua db-migrate --store ./test.db
78}
79
80
81utils_test_case store_flag__fail
82store_flag__fail_body() {
83    atf_check -s exit:1 -o empty -e match:"Cannot open.*test.db" \
84        kyua db-migrate --store ./test.db
85}
86
87
88utils_test_case too_many_arguments
89too_many_arguments_body() {
90    cat >stderr <<EOF
91Usage error for command db-migrate: Too many arguments.
92Type 'kyua help db-migrate' for usage information.
93EOF
94    atf_check -s exit:3 -o empty -e file:stderr kyua db-migrate abc def
95}
96
97
98atf_init_test_cases() {
99    atf_add_test_case upgrade
100    atf_add_test_case already_up_to_date
101    atf_add_test_case need_upgrade
102
103    atf_add_test_case store_flag__ok
104    atf_add_test_case store_flag__fail
105
106    atf_add_test_case too_many_arguments
107}
108