Browse Source

[3599] Remaining unit-tests implemented.

Tomek Mrugalski 10 years ago
parent
commit
81f75cb84a

+ 1 - 1
src/bin/admin/kea-admin.in

@@ -132,7 +132,7 @@ mysql_init() {
         exit 1
     fi
 
-    printf "Initializing database."
+    printf "Initializing database using script %s\n" $scripts_dir/mysql/dhcpdb_create.mysql
     mysql -B --user=$db_user --password=$db_password $db_name < $scripts_dir/mysql/dhcpdb_create.mysql
     ERRCODE=$?
 

+ 141 - 0
src/bin/admin/tests/dhcpdb_create_1.0.mysql

@@ -0,0 +1,141 @@
+# Copyright (C) 2012-2013  Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+# This is the BIND 10 DHCP schema specification for MySQL.
+#
+# The schema is reasonably portable (with the exception of the engine
+# specification, which is MySQL-specific).  Minor changes might be needed for
+# other databases.
+
+# To create the schema, either type the command:
+#
+# mysql -u <user> -p <password> <database> < dhcpdb_create.mysql
+#
+# ... at the command prompt, or log in to the MySQL database and at the "mysql>"
+# prompt, issue the command:
+#
+# source dhcpdb_create.mysql
+
+
+# Holds the IPv4 leases.
+CREATE TABLE lease4 (
+    address INT UNSIGNED PRIMARY KEY NOT NULL,  # IPv4 address
+    hwaddr VARBINARY(20),                       # Hardware address
+    client_id VARBINARY(128),                   # Client ID
+    valid_lifetime INT UNSIGNED,                # Length of the lease (seconds)
+    expire TIMESTAMP,                           # Expiration time of the lease
+    subnet_id INT UNSIGNED,                     # Subnet identification
+    fqdn_fwd BOOL,                              # Has forward DNS update been performed by a server
+    fqdn_rev BOOL,                              # Has reverse DNS update been performed by a server
+    hostname VARCHAR(255)                       # The FQDN of the client
+    ) ENGINE = INNODB;
+
+
+# Create search indexes for lease4 table 
+# index by hwaddr and subnet_id
+CREATE INDEX lease4_by_hwaddr_subnet_id ON lease4 (hwaddr, subnet_id);
+
+# index by client_id and subnet_id
+CREATE INDEX lease4_by_client_id_subnet_id ON lease4 (client_id, subnet_id);
+
+# Holds the IPv6 leases.
+# N.B. The use of a VARCHAR for the address is temporary for development:
+# it will eventually be replaced by BINARY(16).
+CREATE TABLE lease6 (
+    address VARCHAR(39) PRIMARY KEY NOT NULL,   # IPv6 address
+    duid VARBINARY(128),                        # DUID
+    valid_lifetime INT UNSIGNED,                # Length of the lease (seconds)
+    expire TIMESTAMP,                           # Expiration time of the lease
+    subnet_id INT UNSIGNED,                     # Subnet identification
+    pref_lifetime INT UNSIGNED,                 # Preferred lifetime
+    lease_type TINYINT,                         # Lease type (see lease6_types
+                                                #    table for possible values)
+    iaid INT UNSIGNED,                          # See Section 10 of RFC 3315
+    prefix_len TINYINT UNSIGNED,                # For IA_PD only
+    fqdn_fwd BOOL,                              # Has forward DNS update been performed by a server
+    fqdn_rev BOOL,                              # Has reverse DNS update been performed by a server
+    hostname VARCHAR(255)                       # The FQDN of the client
+
+    ) ENGINE = INNODB;
+
+# Create search indexes for lease4 table 
+# index by iaid, subnet_id, and duid 
+CREATE INDEX lease6_by_iaid_subnet_id_duid ON lease6 (iaid, subnet_id, duid);
+
+# ... and a definition of lease6 types.  This table is a convenience for
+# users of the database - if they want to view the lease table and use the
+# type names, they can join this table with the lease6 table.
+# Make sure those values match Lease6::LeaseType enum (see src/bin/dhcpsrv/
+# lease_mgr.h)
+CREATE TABLE lease6_types (
+    lease_type TINYINT PRIMARY KEY NOT NULL,    # Lease type code.
+    name VARCHAR(5)                             # Name of the lease type
+    );
+START TRANSACTION;
+INSERT INTO lease6_types VALUES (0, "IA_NA");   # Non-temporary v6 addresses
+INSERT INTO lease6_types VALUES (1, "IA_TA");   # Temporary v6 addresses
+INSERT INTO lease6_types VALUES (2, "IA_PD");   # Prefix delegations
+COMMIT;
+
+# Finally, the version of the schema.  We start at 0.1 during development.
+# This table is only modified during schema upgrades.  For historical reasons
+# (related to the names of the columns in the BIND 10 DNS database file), the
+# first column is called "version" and not "major".
+#
+# NOTE: this MUST be kept in step with src/lib/dhcpsrv/tests/schema_copy.h,
+#       which defines the schema for the unit tests.  If you are updating
+#       the version number, the schema has changed: please ensure that
+#       schema_copy.h has been updated as well.
+CREATE TABLE schema_version (
+    version INT PRIMARY KEY NOT NULL,       # Major version number
+    minor INT                               # Minor version number
+    );
+START TRANSACTION;
+INSERT INTO schema_version VALUES (1, 0);
+COMMIT;
+
+# Notes:
+#
+# Indexes
+# =======
+# It is likely that additional indexes will be needed.  However, the
+# increase in lookup performance from these will come at the expense
+# of a decrease in performance during insert operations due to the need
+# to update the indexes.  For this reason, the need for additional indexes
+# will be determined by experiment during performance tests.
+#
+# The most likely additional indexes will cover the following columns:
+#
+# expire
+# To speed up the deletion of expired leases from the database.
+#
+# hwaddr and client_id
+# For lease stability: if a client requests a new lease, try to find an
+# existing or recently expired lease for it so that it can keep using the
+# same IP address.
+#
+# Field Sizes
+# ===========
+# If any of the VARxxx field sizes are altered, the lengths in the MySQL
+# backend source file (mysql_lease_mgr.cc) must be correspondingly changed.
+#
+# Portability
+# ===========
+# The "ENGINE = INNODB" on some tables is not portablea to another database
+# and will need to be removed.
+#
+# Some columns contain binary data so are stored as VARBINARY instead of
+# VARCHAR.  This may be non-portable between databases: in this case, the
+# definition should be changed to VARCHAR.

+ 98 - 12
src/bin/admin/tests/mysql_tests.sh.in

@@ -32,7 +32,7 @@ db_pass="keatest"
 db_name="keatest"
 
 # Set location of the kea-admin.
-keactrl=@abs_top_builddir@/src/bin/admin/kea-admin
+keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin
 
 # Wipe all tables from the DB:
 mysql_wipe() {
@@ -56,20 +56,50 @@ mysql_init_test() {
     # Let's wipe the whole database
     mysql_wipe
 
-    # Ok, now let's create a version 1.7
+    # Ok, now let's initalize the database
+    ${keaadmin} init mysql -u $db_user -p $db_pass -n $db_name
+    ERRCODE=$?
+
+    assert_eq 0 $ERRCODE "kea-admin init mysql returned non-zero status code %d, expected %d"
+
+    # Ok, now let's check if the tables are indeed there.
+    # First table: schema_version. Should have 2 columns: version and minor.
     mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
-CREATE TABLE schema_version (
-    version INT PRIMARY KEY NOT NULL,
-    minor INT
-    );
-INSERT INTO schema_version VALUES (1, 7);
+    SELECT version, minor FROM schema_version;
 EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "schema_version table is missing or broken. (returned status code %d, expected %d)"
 
-    version=$(${keactrl} version mysql -u $db_user -p $db_pass -n $db_name)
+    # Second table: lease4
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+    SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname FROM lease4;
+EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "lease4 table is missing or broken. (returned status code %d, expected %d)"
 
-    printf "Reported version: %s, expected 1.7\n" $version
+    # Third table: lease6
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+    SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, hwaddr, hwtype, hwaddr_source FROM lease6;
+EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "lease6 table is missing or broken. (returned status code %d, expected %d)"
 
-    assert_str_eq "1.7" ${version} "Expected kea-admin to return %s, returned value was %s"
+    # Fourth table: lease6_types
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+    SELECT lease_type, name FROM lease6_types;
+EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "lease6_types table is missing or broken. (returned status code %d, expected %d)"
+
+    # Fifth table: lease6_hwaddr_source
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+    SELECT hwaddr_source, name FROM lease6_hwaddr_source;
+EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "lease6_hwaddr_source table is missing or broken. (returned status code %d, expected %d)"
+
+    # Let's wipe the whole database
+    mysql_wipe
 
     test_finish 0
 }
@@ -77,7 +107,24 @@ EOF
 mysql_version_test() {
     test_start "mysql.version"
 
-    # @todo: Implement this
+    # Let's wipe the whole database
+    mysql_wipe
+
+    # Ok, now let's create a version 1.7
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+CREATE TABLE schema_version (
+    version INT PRIMARY KEY NOT NULL,
+    minor INT
+    );
+INSERT INTO schema_version VALUES (1, 7);
+EOF
+
+    version=$(${keaadmin} version mysql -u $db_user -p $db_pass -n $db_name)
+
+    assert_str_eq "1.7" ${version} "Expected kea-admin to return %s, returned value was %s"
+
+    # Let's wipe the whole database
+    mysql_wipe
 
     test_finish 0
 }
@@ -85,7 +132,46 @@ mysql_version_test() {
 mysql_upgrade_test() {
     test_start "mysql.upgrade"
 
-    # @todo: Implement this
+    # Let's wipe the whole database
+    mysql_wipe
+
+    # Initialize database to scheme 1.0.
+    mysql -u$db_user -p$db_pass $db_name < @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.mysql
+
+    # Sanity check - verify that it reports version 1.0.
+    version=$(${keaadmin} version mysql -u $db_user -p $db_pass -n $db_name)
+
+    assert_str_eq "1.0" ${version} "Expected kea-admin to return %s, returned value was %s"
+
+    # Ok, we have a 1.0 database. Let's upgrade it to 2.0
+    ${keaadmin} upgrade mysql -u $db_user -p $db_pass -n $db_name
+    ERRCODE=$?
+
+    assert_eq 0 $ERRCODE "kea-admin upgrade mysql returned non-zero status code %d, expected %d"
+
+    # Let's check that the new tables are indeed there.
+
+    # Third table: lease6
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+    SELECT hwaddr, hwtype, hwaddr_source FROM lease6;
+EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "lease6 table not upgraded to 2.0 (returned status code %d, expected %d)"
+
+    # Fifth table: lease6_hwaddr_source
+    mysql -u$db_user -p$db_pass $db_name &>/dev/null <<EOF
+    SELECT hwaddr_source, name FROM lease6_hwaddr_source;
+EOF
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "lease6_hwaddr_source table is missing or broken. (returned status code %d, expected %d)"
+
+    # Verify that it reports version 2.0.
+    version=$(${keaadmin} version mysql -u $db_user -p $db_pass -n $db_name)
+
+    assert_str_eq "2.0" ${version} "Expected kea-admin to return %s, returned value was %s"
+
+    # Let's wipe the whole database
+    mysql_wipe
 
     test_finish 0
 }

+ 10 - 2
src/lib/testutils/dhcp_test_lib.sh.in

@@ -113,8 +113,9 @@ test_finish() {
         cleanup
         printf "PASSED ${TEST_NAME}\n\n"
     else
-        # Dump log file if exists for debugging purposes.
-        if [ -s ${LOG_FILE} ]; then
+        # Dump log file for debugging purposes if specified and exists.
+        # Otherwise the code below would simply call cat.
+        if [ -n "${LOG_FILE}" -a -s "${LOG_FILE}" ]; then
             printf "Log file dump:\n"
             cat ${LOG_FILE}
         fi
@@ -226,6 +227,12 @@ get_reconfigs() {
 # in the ${LOG_FILE}, ${CFG_FILE} and ${KEACTRL_CFG_FILE} variables
 # recpectively, prior to calling this function.
 cleanup() {
+
+    # If there is no KEA_PROCS set, just return
+    if [ -z "${KEA_PROCS}" ]; then
+        return
+    fi
+
     # KEA_PROCS holds the name of all Kea processes. Shut down each
     # of them if running.
     for proc_name in ${KEA_PROCS}
@@ -238,6 +245,7 @@ cleanup() {
             kill -9 ${pid}
         done
     done
+
     # Remove temporary files.
     rm -rf ${LOG_FILE}
     rm -rf ${CFG_FILE}