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

3
# Copyright (C) 2014-2018 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

25 26 27 28 29 30
cql_wipe() {
    # 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
31
cql_lease_init_test() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
32
    test_start "cql.init"
33 34

    # Wipe the database.
35
    cql_wipe
36 37 38 39 40 41 42 43 44 45 46 47

    # 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
48
    cql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, state, user_context FROM lease4;"
49 50 51
    assert_eq 0 $? "lease4 table check failed, expected exit code: %d, actual: %d"

    # Check lease6 table
52
    cql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, state, user_context FROM lease6;"
53 54 55 56 57 58 59 60 61 62
    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
63
    # Trying to create it again should fail. This verifies the db present
64 65 66 67 68 69 70
    # 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.
71
    cql_wipe
72 73 74 75

    test_finish 0
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
76
cql_lease_version_test() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
77
    test_start "cql.version"
78 79

    # Wipe the database.
80
    cql_wipe
81

Tomek Mrugalski's avatar
Tomek Mrugalski committed
82
    # Create the database.
83 84 85
    $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
86
    # Verify that kea-admin lease-version returns the correct version.
87
    version=$($keaadmin lease-version cql -u $db_user -p $db_password -n $db_name)
88
    assert_str_eq "3.0" $version "Expected kea-admin to return %s, returned value was %s"
89 90

    # Wipe the database.
91
    cql_wipe
92 93 94 95

    test_finish 0
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
96 97
cql_upgrade_test() {
    test_start "cql.upgrade"
98 99

    # Wipe the database.
100
    cql_wipe
101 102 103 104 105 106 107 108 109

    # 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.
110
    cql_wipe
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
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.
139
    cql_wipe
Tomek Mrugalski's avatar
Tomek Mrugalski committed
140 141 142 143 144 145 146 147 148 149 150 151

    # 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
152 153
    insert_cql="\
INSERT INTO lease4(address, hwaddr, client_id, valid_lifetime, expire, subnet_id,\
154
 fqdn_fwd, fqdn_rev, hostname, state, user_context)\
Andrei Pavel's avatar
Andrei Pavel committed
155
 VALUES(-1073741302,textAsBlob('20'),textAsBlob('30'),40,1430694930,50,true,true,\
156
 'one.example.com', 0, '');\
Andrei Pavel's avatar
Andrei Pavel committed
157
INSERT INTO lease4(address, hwaddr, client_id, valid_lifetime, expire, subnet_id,\
158 159
 fqdn_fwd, fqdn_rev, hostname, state, user_context)\
 VALUES(-1073741301,NULL,textAsBlob('123'),40,1433464245,50,true,true,'', 1, '');\
Andrei Pavel's avatar
Andrei Pavel committed
160
INSERT INTO lease4(address, hwaddr, client_id, valid_lifetime, expire, subnet_id,\
161
 fqdn_fwd, fqdn_rev, hostname, state, user_context)\
Andrei Pavel's avatar
Andrei Pavel committed
162
 VALUES(-1073741300,textAsBlob('22'),NULL,40,1436173267,50,true,true,\
163
 'three.example.com', 2, '');"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
164

Andrei Pavel's avatar
Andrei Pavel committed
165
    cql_execute "$insert_cql"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
    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.
180
    cql_wipe
Tomek Mrugalski's avatar
Tomek Mrugalski committed
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

    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.
209
    cql_wipe
Tomek Mrugalski's avatar
Tomek Mrugalski committed
210 211 212 213 214 215 216 217 218

    # 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
219 220
    insert_cql="\
INSERT INTO lease6(address, duid, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
221
 pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\
222
 hwaddr, hwtype, hwaddr_source, state, user_context)\
Andrei Pavel's avatar
Andrei Pavel committed
223
 VALUES('2001:db8::10',textAsBlob('20'),30,1430694930,40,50,1,60,70,true,true,\
224
 'one.example.com',textAsBlob('80'),90,16,0,'');\
Andrei Pavel's avatar
Andrei Pavel committed
225
INSERT INTO lease6(address, duid, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
226
 pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\
227
 hwaddr, hwtype, hwaddr_source, state, user_context)\
Andrei Pavel's avatar
Andrei Pavel committed
228
 VALUES('2001:db8::11',NULL,30,1433464245,40,50,1,60,70,true,true,\
229
 '',textAsBlob('80'),90,1,1,'');\
Andrei Pavel's avatar
Andrei Pavel committed
230
INSERT INTO lease6(address, duid, valid_lifetime, expire, subnet_id,\
Tomek Mrugalski's avatar
Tomek Mrugalski committed
231
 pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\
232
 hwaddr, hwtype, hwaddr_source, state, user_context)\
Andrei Pavel's avatar
Andrei Pavel committed
233
 VALUES('2001:db8::12',textAsBlob('21'),30,1436173267,40,50,1,60,70,true,true,\
234
 'three.example.com',textAsBlob('80'),90,4,2,'');"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
235

Andrei Pavel's avatar
Andrei Pavel committed
236
    cql_execute "$insert_cql"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250
    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.
251
    cql_wipe
Tomek Mrugalski's avatar
Tomek Mrugalski committed
252 253 254 255

    test_finish 0
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
# Upgrades an existing schema to a target newer version
# param target_version - desired schema version as "major.minor"
cql_upgrade_schema_to_version() {
    target_version=$1

    # Check if the scripts directory exists at all.
    if [ ! -d ${db_scripts_dir}/cql ]; then
        log_error "Invalid scripts directory: ${db_scripts_dir}/mysql"
        exit 1
    fi

    # Check if there are any files in it
    num_files=$(find ${db_scripts_dir}/cql/upgrade*.sh -type f | wc -l)
    if [ $num_files -eq 0 ]; then
        log_error "No scripts in ${db_scripts_dir}/cql?"
        exit 1
    fi

    version=`cql_version`
    for script in ${db_scripts_dir}/cql/upgrade*.sh
    do
        if [ $version = "$target_version" ]
        then
            break;
        fi

        echo "Processing $script file..."
        sh ${script} -u ${db_user} -p ${db_password} -k ${db_name}
        version=`cql_version`
    done

    echo "Schema upgraded to $version"
}

# Verifies that you can upgrade from an earlier version and
# that unused subnet ID values in hosts and options tables are
292
# converted to -1
293 294 295 296 297 298 299 300 301
cql_unused_subnet_id_test() {
    test_start "cql.unused_subnet_id_test"

    # Let's wipe the whole database
    cql_wipe

    # We need to create an older database with lease data so we can
    # verify the upgrade mechanisms which convert subnet id values
    #
302
    # Initialize database to schema 1.0.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    cql_execute_script @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.cql

    # Now upgrade to schema 2.0, the version just before global HRs
    cql_upgrade_schema_to_version 2.0

    # Now we need insert some hosts to "migrate" for both v4 and v6
    qry=\
"insert into host_reservations (id, host_identifier_type, host_identifier, host_ipv4_subnet_id, host_ipv6_subnet_id, option_subnet_id)\
   values (1, 0, textAsBlob('0123456'), 0, 0, 0);\
 insert into host_reservations (id, host_identifier_type, host_identifier, host_ipv4_subnet_id, host_ipv6_subnet_id, option_subnet_id)\
   values (2, 0, textAsBlob('1123456'), 4, 0, 4);\
 insert into host_reservations (id, host_identifier_type, host_identifier, host_ipv4_subnet_id, host_ipv6_subnet_id, option_subnet_id)\
   values (3, 0, textAsBlob('2123456'), 0, 6, 6);\
 insert into host_reservations (id, host_identifier_type, host_identifier, host_ipv4_subnet_id, host_ipv6_subnet_id, option_subnet_id)\
   values (4, 0, textAsBlob('3123456'), 4, 6, 0);\
 insert into host_reservations (id, host_identifier_type, host_identifier, host_ipv4_subnet_id, host_ipv6_subnet_id, option_subnet_id)\
   values (5, 0, textAsBlob('3123456'), -1, 6, 6);"

    cql_execute "$qry"
    assert_eq 0 $? "insert hosts failed, expected exit code: %d, actual: %d"

    # Ok, we have a 2.0 database with hosts and options. Let's upgrade it.
    ${keaadmin} lease-upgrade cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
    ERRCODE=$?

    # Upgrade should succeed
    assert_eq 0 $ERRCODE "upgrade failed"

    test_dir="@abs_top_srcdir@/src/bin/admin/tests"
    ref_file="$test_dir/data/cql.subnet_id_test.reference.csv"

    output_dir="@abs_top_builddir@/src/bin/admin/tests"
    export_file="$output_dir/data/cql.subnet_id_test.csv"

    # Fetch host_reservation data for comparison
    echo "Exporting host_reservation data to $export_file ..."

    qry=\
"select id, host_ipv4_subnet_id, host_ipv6_subnet_id, option_subnet_id from\
 host_reservations where id in(1,2,3,4,5);"

    cql_execute "$qry" >$export_file
345
    assert_eq 0 $? "insert hosts failed, expected exit code: %d, actual: %d"
346 347 348 349 350 351 352 353 354
    # Compare the dump output to reference file, they should be identical.
    cmp -s $export_file  $ref_file
    assert_eq 0 $? "export file does not match reference file, expected exit code %d, actual %d"

    # remove the output file.
    rm $export_file

    # Wipe the database.
    cql_wipe
355 356 357

    # Report test success.
    test_finish 0
358 359 360
}


Tomek Mrugalski's avatar
Tomek Mrugalski committed
361 362 363
# Run tests.
cql_lease_init_test
cql_lease_version_test
Tomek Mrugalski's avatar
Tomek Mrugalski committed
364
cql_upgrade_test
Tomek Mrugalski's avatar
Tomek Mrugalski committed
365 366
cql_lease4_dump_test
cql_lease6_dump_test
367
cql_unused_subnet_id_test