xref: /freebsd/contrib/kyua/integration/cmd_db_migrate_test.sh (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1# Copyright 2013 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# Location of installed schema files.
31: "${KYUA_STOREDIR:=__KYUA_STOREDIR__}"
32
33
34# Location of installed test data files.
35: "${KYUA_STORETESTDATADIR:=__KYUA_STORETESTDATADIR__}"
36
37
38# Creates an empty old-style action database.
39#
40# \param ... Files that contain SQL commands to be run.
41create_historical_db() {
42    mkdir -p "${HOME}/.kyua"
43    cat "${@}" | sqlite3 "${HOME}/.kyua/store.db"
44}
45
46
47# Creates an empty results file.
48#
49# \param ... Files that contain SQL commands to be run.
50create_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}"
54}
55
56
57utils_test_case upgrade__from_v1
58upgrade__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"
65}
66upgrade__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
70    for f in \
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"
74    do
75        [ -f "${HOME}/.kyua/store/${f}" ] || atf_fail "Expected file ${f}" \
76            "was not created"
77    done
78    [ ! -f "${HOME}/.kyua/store.db" ] || atf_fail "Historical database not" \
79        "deleted"
80}
81
82
83utils_test_case upgrade__from_v2
84upgrade__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"
90}
91upgrade__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
95    for f in \
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"
99    do
100        [ -f "${HOME}/.kyua/store/${f}" ] || atf_fail "Expected file ${f}" \
101            "was not created"
102    done
103    [ ! -f "${HOME}/.kyua/store.db" ] || atf_fail "Historical database not" \
104        "deleted"
105}
106
107
108utils_test_case already_up_to_date
109already_up_to_date_head() {
110    atf_set require.files "${KYUA_STOREDIR}/schema_v3.sql"
111    atf_set require.progs "sqlite3"
112}
113already_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" \
116        kyua db-migrate
117}
118
119
120utils_test_case need_upgrade
121need_upgrade_head() {
122    atf_set require.files "${KYUA_STORETESTDATADIR}/schema_v1.sql"
123    atf_set require.progs "sqlite3"
124}
125need_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
129}
130
131
132utils_test_case results_file__ok
133results_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
137}
138
139
140utils_test_case results_file__fail
141results_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
144}
145
146
147utils_test_case too_many_arguments
148too_many_arguments_body() {
149    cat >stderr <<EOF
150Usage error for command db-migrate: Too many arguments.
151Type 'kyua help db-migrate' for usage information.
152EOF
153    atf_check -s exit:3 -o empty -e file:stderr kyua db-migrate abc def
154}
155
156
157atf_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
162
163    atf_add_test_case results_file__ok
164    atf_add_test_case results_file__fail
165
166    atf_add_test_case too_many_arguments
167}
168