1 # Copyright 2013 The Kyua Authors.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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.
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.
30 # Location of installed schema files.
31 : "${KYUA_STOREDIR:=__KYUA_STOREDIR__}"
34 # Location of installed test data files.
35 : "${KYUA_STORETESTDATADIR:=__KYUA_STORETESTDATADIR__}"
38 # Creates an empty old-style action database.
40 # \param ... Files that contain SQL commands to be run.
41 create_historical_db() {
42 mkdir -p "${HOME}/.kyua"
43 cat "${@}" | sqlite3 "${HOME}/.kyua/store.db"
47 # Creates an empty results file.
49 # \param ... Files that contain SQL commands to be run.
50 create_results_file() {
51 mkdir -p "${HOME}/.kyua/store"
52 local dbname="results.$(utils_test_suite_id)-20140718-173200-123456.db"
53 cat "${@}" | sqlite3 "${HOME}/.kyua/store/${dbname}"
57 utils_test_case upgrade__from_v1
58 upgrade__from_v1_head() {
59 atf_set require.files \
60 "${KYUA_STORETESTDATADIR}/schema_v1.sql" \
61 "${KYUA_STORETESTDATADIR}/testdata_v1.sql" \
62 "${KYUA_STOREDIR}/migrate_v1_v2.sql" \
63 "${KYUA_STOREDIR}/migrate_v2_v3.sql"
64 atf_set require.progs "sqlite3"
66 upgrade__from_v1_body() {
67 create_historical_db "${KYUA_STORETESTDATADIR}/schema_v1.sql" \
68 "${KYUA_STORETESTDATADIR}/testdata_v1.sql"
69 atf_check -s exit:0 -o empty -e empty kyua db-migrate
71 "results.test_suite_root.20130108-111331-000000.db" \
72 "results.usr_tests.20130108-123832-000000.db" \
73 "results.usr_tests.20130108-112635-000000.db"
75 [ -f "${HOME}/.kyua/store/${f}" ] || atf_fail "Expected file ${f}" \
78 [ ! -f "${HOME}/.kyua/store.db" ] || atf_fail "Historical database not" \
83 utils_test_case upgrade__from_v2
84 upgrade__from_v2_head() {
85 atf_set require.files \
86 "${KYUA_STORETESTDATADIR}/schema_v2.sql" \
87 "${KYUA_STORETESTDATADIR}/testdata_v2.sql" \
88 "${KYUA_STOREDIR}/migrate_v2_v3.sql"
89 atf_set require.progs "sqlite3"
91 upgrade__from_v2_body() {
92 create_historical_db "${KYUA_STORETESTDATADIR}/schema_v2.sql" \
93 "${KYUA_STORETESTDATADIR}/testdata_v2.sql"
94 atf_check -s exit:0 -o empty -e empty kyua db-migrate
96 "results.test_suite_root.20130108-111331-000000.db" \
97 "results.usr_tests.20130108-123832-000000.db" \
98 "results.usr_tests.20130108-112635-000000.db"
100 [ -f "${HOME}/.kyua/store/${f}" ] || atf_fail "Expected file ${f}" \
103 [ ! -f "${HOME}/.kyua/store.db" ] || atf_fail "Historical database not" \
108 utils_test_case already_up_to_date
109 already_up_to_date_head() {
110 atf_set require.files "${KYUA_STOREDIR}/schema_v3.sql"
111 atf_set require.progs "sqlite3"
113 already_up_to_date_body() {
114 create_results_file "${KYUA_STOREDIR}/schema_v3.sql"
115 atf_check -s exit:1 -o empty -e match:"already at schema version" \
120 utils_test_case need_upgrade
121 need_upgrade_head() {
122 atf_set require.files "${KYUA_STORETESTDATADIR}/schema_v1.sql"
123 atf_set require.progs "sqlite3"
125 need_upgrade_body() {
126 create_results_file "${KYUA_STORETESTDATADIR}/schema_v1.sql"
127 atf_check -s exit:2 -o empty \
128 -e match:"database has schema version 1.*use db-migrate" kyua report
132 utils_test_case results_file__ok
133 results_file__ok_body() {
134 echo "This is not a valid database" >test.db
135 atf_check -s exit:1 -o empty -e match:"Migration failed" \
136 kyua db-migrate --results-file ./test.db
140 utils_test_case results_file__fail
141 results_file__fail_body() {
142 atf_check -s exit:1 -o empty -e match:"No previous results.*test.db" \
143 kyua db-migrate --results-file ./test.db
147 utils_test_case too_many_arguments
148 too_many_arguments_body() {
150 Usage error for command db-migrate: Too many arguments.
151 Type 'kyua help db-migrate' for usage information.
153 atf_check -s exit:3 -o empty -e file:stderr kyua db-migrate abc def
157 atf_init_test_cases() {
158 atf_add_test_case upgrade__from_v1
159 atf_add_test_case upgrade__from_v2
160 atf_add_test_case already_up_to_date
161 atf_add_test_case need_upgrade
163 atf_add_test_case results_file__ok
164 atf_add_test_case results_file__fail
166 atf_add_test_case too_many_arguments