cql_tests.sh.in 9.76 KB
Newer Older
1 2
#!/bin/sh

3
# Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
4 5 6 7 8 9 10 11
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Include common test library.
. @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh

12 13 14 15 16 17 18 19 20 21 22 23 24
# Include admin utilities
. @abs_top_srcdir@/src/bin/admin/admin-utils.sh

# Set path to the production schema scripts
db_scripts_dir=@abs_top_srcdir@/src/share/database/scripts

db_user="keatest"
db_password="keatest"
db_name="keatest"

# Set location of the kea-admin.
keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin

Tomek Mrugalski's avatar
Tomek Mrugalski committed
25
cql_lease_init_test() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
26
    test_start "cql.init"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

    # Create the database
    $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    assert_eq 0 $? "kea-admin lease-init cql failed, expected exit code: %d, actual: %d"

    # Verify that all the expected tables exist

    # Check schema_version table
    cql_execute "SELECT version, minor FROM schema_version;"
    assert_eq 0 $? "schema_version table check failed, expected exit code: %d, actual: %d"

    # Check lease4 table
    cql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, state FROM lease4;"
    assert_eq 0 $? "lease4 table check failed, expected exit code: %d, actual: %d"

    # Check lease6 table
Andrei Pavel's avatar
Andrei Pavel committed
47
    cql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, state FROM lease6;"
48 49 50 51 52 53 54 55 56 57
    assert_eq 0 $? "lease6 table check failed, expected exit code: %d, actual: %d"

    # Check lease6_types table
    cql_execute "SELECT lease_type, name FROM lease6_types;"
    assert_eq 0 $? "lease6_types table check failed, expected exit code: %d, actual: %d"

    # Check lease_state table
    cql_execute "SELECT state, name FROM lease_state;"
    assert_eq 0 $? "lease_state table check failed, expected exit code: %d, actual: %d"

Tomek Mrugalski's avatar
Tomek Mrugalski committed
58
    # Trying to create it again should fail. This verifies the db present
59 60 61 62 63 64 65 66 67
    # check
    echo ""
    echo "Making sure keyspace creation fails the second time..."
    $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    assert_eq 2 $? "kea-admin failed to deny lease-init, expected exit code: %d, actual: %d"

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"
68 69 70 71

    test_finish 0
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
72
cql_lease_version_test() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
73
    test_start "cql.version"
74 75 76 77 78

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

Tomek Mrugalski's avatar
Tomek Mrugalski committed
79
    # Create the database.
80 81 82
    $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    assert_eq 0 $? "kea-admin lease-init cql failed, expected exit code: %d, actual: %d"

Andrei Pavel's avatar
Andrei Pavel committed
83
    # Verify that kea-admin lease-version returns the correct version.
84
    version=$($keaadmin lease-version cql -u $db_user -p $db_password -n $db_name)
Andrei Pavel's avatar
Andrei Pavel committed
85
    assert_str_eq "2.0" $version "Expected kea-admin to return %s, returned value was %s"
86 87 88 89

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"
90 91 92 93

    test_finish 0
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
94 95
cql_upgrade_test() {
    test_start "cql.upgrade"
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

    # Initialize database to schema 1.0.
    cql_execute_script @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.cql
    assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"

    $keaadmin lease-upgrade cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    assert_eq 0 $? "lease-upgrade failed, expected exit code: %d, actual: %d"

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"
111 112 113 114

    test_finish 0
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
cql_lease4_dump_test() {
    test_start "cql.lease4_dump_test"

    test_dir="@abs_top_srcdir@/src/bin/admin/tests"
    output_dir="@abs_top_builddir@/src/bin/admin/tests"
    script_dir="@abs_top_srcdir@/src/bin/admin/scripts"

    output_file="$output_dir/data/cql.lease4_dump_test.output.csv"
    tmp_file="$output_file.tmp"

    ref_file="$test_dir/data/cql.lease4_dump_test.reference.csv"

    # Wipe out any residuals from prior failed runs.
    if [ -e $output_file ]
    then
        rm $output_file
    fi

    if [ -e $tmp_file ]
    then
        rm $tmp_file
    fi

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

    # Create the database
    $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    assert_eq 0 $? "kea-admin lease-init cql failed, expected exit code: %d, actual: %d"

    # Insert the reference record.
    # -1073741302 corresponds to 192.0.2.10
    # -1073741301 corresponds to 192.0.2.11
    # -1073741300 corresponds to 192.0.2.12
    # 1430694930 corresponds to 2015-04-04 01:15:30
    # 1433464245 corresponds to 2015-05-05 02:30:45
    # 1436173267 corresponds to 2015-06-06 11:01:07
Andrei Pavel's avatar
Andrei Pavel committed
153 154
    insert_cql="\
INSERT INTO lease4(address, hwaddr, client_id, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
155
 fqdn_fwd, fqdn_rev, hostname, state)\
Andrei Pavel's avatar
Andrei Pavel committed
156
 VALUES(-1073741302,textAsBlob('20'),textAsBlob('30'),40,1430694930,50,true,true,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
157
 'one.example.com', 0);\
Andrei Pavel's avatar
Andrei Pavel committed
158
INSERT INTO lease4(address, hwaddr, client_id, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
159
 fqdn_fwd, fqdn_rev, hostname, state)\
Andrei Pavel's avatar
Andrei Pavel committed
160 161
 VALUES(-1073741301,NULL,textAsBlob('123'),40,1433464245,50,true,true,'', 1);\
INSERT INTO lease4(address, hwaddr, client_id, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
162
 fqdn_fwd, fqdn_rev, hostname, state)\
Andrei Pavel's avatar
Andrei Pavel committed
163
 VALUES(-1073741300,textAsBlob('22'),NULL,40,1436173267,50,true,true,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
164 165
 'three.example.com', 2);"

Andrei Pavel's avatar
Andrei Pavel committed
166
    cql_execute "$insert_cql"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    assert_eq 0 $? "insert into lease4 failed, expected exit code %d, actual %d"

    # Dump lease4 to output_file.
    $keaadmin lease-dump cql -4 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
    assert_eq 0 $? "kea-admin lease-dump -4 failed, expected exit code %d, actual %d"

    # Compare the dump output to reference file, they should be identical.
    cmp -s $output_file  $ref_file
    assert_eq 0 $? "dump file does not match reference file, expected exit code %d, actual %d"

    # remove the output file.
    rm $output_file

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

    test_finish 0
}

cql_lease6_dump_test() {
    test_start "cql.lease6_dump_test"

    test_dir="@abs_top_srcdir@/src/bin/admin/tests"
    output_dir="@abs_top_builddir@/src/bin/admin/tests"
    script_dir="@abs_top_srcdir@/src/bin/admin/scripts"

    output_file="$output_dir/data/cql.lease6_dump_test.output.csv"
    tmp_file="$output_file.tmp"

    ref_file="$test_dir/data/cql.lease6_dump_test.reference.csv"

    # Wipe out any residuals from prior failed runs.
    if [ -e $output_file ]
    then
        rm $output_file
    fi

    if [ -e $tmp_file ]
    then
        rm $tmp_file
    fi

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

    # Create the database.
    $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    assert_eq 0 $? "could not create database, expected exit code %d, actual %d"

    # Insert the reference record.
    # 1430694930 corresponds to 2015-04-04 01:15:30
    # 1433464245 corresponds to 2015-05-05 02:30:45
    # 1436173267 corresponds to 2015-06-06 11:01:07
Andrei Pavel's avatar
Andrei Pavel committed
222 223
    insert_cql="\
INSERT INTO lease6(address, duid, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
224 225
 pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\
 hwaddr, hwtype, hwaddr_source, state)\
Andrei Pavel's avatar
Andrei Pavel committed
226
 VALUES('2001:db8::10',textAsBlob('20'),30,1430694930,40,50,1,60,70,true,true,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
227
 'one.example.com',textAsBlob('80'),90,16,0);\
Andrei Pavel's avatar
Andrei Pavel committed
228
INSERT INTO lease6(address, duid, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
229 230
 pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\
 hwaddr, hwtype, hwaddr_source, state)\
Andrei Pavel's avatar
Andrei Pavel committed
231
 VALUES('2001:db8::11',NULL,30,1433464245,40,50,1,60,70,true,true,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
232
 '',textAsBlob('80'),90,1,1);\
Andrei Pavel's avatar
Andrei Pavel committed
233
INSERT INTO lease6(address, duid, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
234 235
 pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\
 hwaddr, hwtype, hwaddr_source, state)\
Andrei Pavel's avatar
Andrei Pavel committed
236
 VALUES('2001:db8::12',textAsBlob('21'),30,1436173267,40,50,1,60,70,true,true,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
237 238
 'three.example.com',textAsBlob('80'),90,4,2);"

Andrei Pavel's avatar
Andrei Pavel committed
239
    cql_execute "$insert_cql"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    assert_eq 0 $? "insert into lease6 failed, expected exit code %d, actual %d"

    # Dump lease4 to output_file.
    $keaadmin lease-dump cql -6 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
    assert_eq 0 $? "kea-admin lease-dump -6 failed, status code %d"

    # Compare the dump output to reference file, they should be identical.
    cmp -s $output_file  $ref_file
    assert_eq 0 $? "dump file does not match reference file, expected exit code %d, actual %d"

    # remove the output file.
    rm $output_file

    # Wipe the database.
    cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql
    assert_eq 0 $? "drop table query failed, exit code %d, expected %d"

    test_finish 0
}

# Run tests.
cql_lease_init_test
cql_lease_version_test
Tomek Mrugalski's avatar
Tomek Mrugalski committed
263
cql_upgrade_test
Tomek Mrugalski's avatar
Tomek Mrugalski committed
264 265
cql_lease4_dump_test
cql_lease6_dump_test