Commit f42cbfdb authored by Francis Dupont's avatar Francis Dupont

[#1196] Added tests

parent f60a344a
......@@ -50,8 +50,8 @@ Arguments
to be used as a diagnostic tool, so it provides a portable,
human-readable form of the lease data.
**lease-recount**
Recounts leases for MySQL or PostgreSQL database.
**lease-stat-recount**
Recounts lease statistics for MySQL or PostgreSQL database.
``backend``
Specifies the backend type. Currently allowed backends are: memfile,
......
......@@ -151,30 +151,25 @@ cql_version() {
return $error
}
recount4_query() {
# recount IPv4 leases from scratch
query="
START TRANSACTION;
DELETE FROM lease4_stat;
INSERT INTO lease4_stat (subnet_id, state, leases)
SELECT subnet_id, state, COUNT(*)
FROM lease4 WHERE state = 0 OR state = 1
GROUP BY subnet_id, state;
COMMIT;
# recount IPv4 leases from scratch
_RECOUNT4_QUERY=\
"
return $query
}
recount6_query() {
# recount IPv6 leases from scratch
query="
START TRANSACTION;
DELETE FROM lease6_stat;
INSERT INTO lease6_stat (subnet_id, lease_type, state, leases)
SELECT subnet_id, lease_type, state, COUNT(*)
FROM lease6 WHERE state = 0 OR state = 1
GROUP BY subnet_id, lease_type, state;
COMMIT;
START TRANSACTION; \
DELETE FROM lease4_stat; \
INSERT INTO lease4_stat (subnet_id, state, leases) \
SELECT subnet_id, state, COUNT(*) \
FROM lease4 WHERE state = 0 OR state = 1 \
GROUP BY subnet_id, state; \
COMMIT;"
# recount IPv6 leases from scratch
_RECOUNT6_QUERY=\
"
return $query
}
START TRANSACTION; \
DELETE FROM lease6_stat; \
INSERT INTO lease6_stat (subnet_id, lease_type, state, leases) \
SELECT subnet_id, lease_type, state, COUNT(*) \
FROM lease6 WHERE state = 0 OR state = 1 \
GROUP BY subnet_id, lease_type, state; \
COMMIT;"
......@@ -59,8 +59,8 @@ usage() {
printf " - db-version: Checks version of the existing database scheme. Useful\n"
printf " - for checking databaseB version when preparing for an upgrade.\n"
printf " - db-upgrade: Upgrades your database scheme\n"
printf " - lease-dump: Dump current leases to a CSV file\n"
printf " - lease-recount: Recount leases.
printf " - lease-dump: Dumps current leases to a CSV file\n"
printf " - lease-stat-recount: Recounts lease statistics\n"
printf "\n"
printf "BACKEND - one of the supported backends: memfile|mysql|pgsql|cql\n"
printf "\n"
......@@ -624,12 +624,11 @@ cql_dump() {
exit 0
}
### Functions used for recount
### Functions used for recounting statistics
mysql_recount() {
printf "Recount lease statistics from database\n"
printf "Recount leases from database\n"
QUERY4=recount4_query()
RESULT=$(mysql_execute "$QUERY")
RESULT=$(mysql_execute "$_RECOUNT4_QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
......@@ -637,8 +636,7 @@ mysql_recount() {
exit 1
fi
QUERY6=recount6_query()
RESULT=$(mysql_execute "$QUERY")
RESULT=$(mysql_execute "$_RECOUNT6_QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
......@@ -648,10 +646,9 @@ mysql_recount() {
}
pgsql_recount() {
printf "Recount lease statistics from database\n"
printf "Recount leases from database\n"
QUERY4=recount4_query()
RESULT=$(pgsql_execute "$QUERY")
RESULT=$(pgsql_execute "$_RECOUNT4_QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
......@@ -659,8 +656,7 @@ pgsql_recount() {
exit 1
fi
QUERY6=recount6_query()
RESULT=$(pgsql_execute "$QUERY")
RESULT=$(pgsql_execute "$_RECOUNT6_QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
......@@ -685,7 +681,7 @@ if test "${command}" = "-v" || test "${command}" = "--version" ; then
exit 0
fi
is_in_list "${command}" "db-init db-version db-upgrade lease-dump lease-recount"
is_in_list "${command}" "db-init db-version db-upgrade lease-dump lease-stat-recount"
if [ ${_inlist} -eq 0 ]; then
log_error "invalid command: ${command}"
usage
......@@ -864,10 +860,10 @@ case ${command} in
;;
esac
;;
lease-recount)
lease-stat-recount)
case ${backend} in
memfile)
log_info "memfile does not count leases"
log_info "memfile does not keep lease statistics"
;;
mysql)
mysql_recount
......@@ -876,7 +872,7 @@ case ${command} in
pgsql_recount
;;
cql)
log_info "cql does not count leases"
log_info "cql does not keep lease statistics"
;;
esac
;;
......
......@@ -54,8 +54,8 @@ OPTIONS
meant to be used as a diagnostic tool, so it provides a portable,
human-readable form of the lease data.
lease-recount
Recounts leases for MySQL or PostgreSQL database.
lease-stat-recount
Recounts lease statistics for MySQL or PostgreSQL database.
``backend``
Specifies backend type. Currently allowed backends are: memfile,
......
......@@ -736,7 +736,7 @@ EOF
# table: dhcp4_shared_network (should include six new columns)
qry="select ddns_send_updates, ddns_override_no_update, ddns_override_client_update, ddns_replace_client_name, ddns_generated_prefix, ddns_qualifying_suffix from dhcp4_shared_network"
run_statement "dhcp4_shared_network" "$qry"
# table: dhcp6_shared_network (should include six new columns)
qry="select ddns_send_updates, ddns_override_no_update, ddns_override_client_update, ddns_replace_client_name, ddns_generated_prefix, ddns_qualifying_suffix from dhcp6_shared_network"
run_statement "dhcp6_shared_network" "$qry"
......@@ -744,7 +744,7 @@ EOF
# table: dhcp4_subnet (should include six new columns)
qry="select ddns_send_updates, ddns_override_no_update, ddns_override_client_update, ddns_replace_client_name, ddns_generated_prefix, ddns_qualifying_suffix from dhcp4_subnet"
run_statement "dhcp4_subnet" "$qry"
# table: dhcp6_subnet (should include six new columns)
qry="select ddns_send_updates, ddns_override_no_update, ddns_override_client_update, ddns_replace_client_name, ddns_generated_prefix, ddns_qualifying_suffix from dhcp6_subnet"
run_statement "dhcp6_subnet" "$qry"
......@@ -1150,6 +1150,101 @@ mysql_lease_stat_upgrade_test() {
test_finish 0
}
mysql_lease_stat_recount_test() {
test_start "my_sql_lease_stat_recount_test"
# Let's wipe the whole database
mysql_wipe
# Ok, now let's initialize the database
${keaadmin} db-init mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin db-init mysql returned non-zero status code %d, expected %d"
# Now we need insert some leases to "recount"
qry=\
"insert into lease4 (address, subnet_id, state) values (111,10,0);\
insert into lease4 (address, subnet_id, state) values (222,10,0);\
insert into lease4 (address, subnet_id, state) values (333,10,1);\
insert into lease4 (address, subnet_id, state) values (444,10,2);\
insert into lease4 (address, subnet_id, state) values (555,77,0);"
run_statement "insert v4 leases" "$qry"
qry=\
"insert into lease6 (address, lease_type, subnet_id, state) values (111,0,40,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (222,0,40,1);\
insert into lease6 (address, lease_type, subnet_id, state) values (333,1,40,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (444,1,50,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (555,1,50,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (666,1,40,2);"
run_statement "insert v6 leases" "$qry"
# Now we change some counters.
qry=\
"insert into lease4_stat (subnet_id, state, leases) values (20,0,1);\
update lease4_stat set leases = 5 where subnet_id = 10 and state = 0;
delete from lease4_stat where subnet_id = 10 and state = 2;"
run_statement "change v4 stats" "$qry"
qry=\
"insert into lease6_stat (subnet_id, lease_type, state, leases) values (20,1,0,1);
update lease6_stat set leases = 5 where subnet_id = 40 and lease_type = 0 and state = 0;
delete from lease6_stat where subnet_id = 40 and lease_type = 1 and state = 2;"
run_statement "change v6 stats" "$qry"
# Recount all statistics from scratch.
${keaadmin} lease-stat-recount mysql -u $db_user -p $db_password -n $db_name
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-stat-recount mysql returned non-zero status code %d, expected %d"
#
# First we'll verify lease4_stats are correct after recount.
#
# Assigned leases for subnet 10 should be 2
qry="select leases from lease4_stat where subnet_id = 10 and state = 0"
run_statement "#4.1" "$qry" 2
# Assigned leases for subnet 77 should be 1
qry="select leases from lease4_stat where subnet_id = 77 and state = 0"
run_statement "#4.2" "$qry" 1
# Should be no records for EXPIRED
qry="select count(subnet_id) from lease4_stat where state = 2"
run_statement "#4.3" "$qry" 0
#
# Next we'll verify lease6_stats are correct after recount.
#
# Assigned leases for subnet 40 should be 1
qry="select leases from lease6_stat where subnet_id = 40 and lease_type = 0 and state = 0"
run_statement "#6.1" "$qry" 1
# Assigned (PD) leases for subnet 40 should be 1
qry="select leases from lease6_stat where subnet_id = 40 and lease_type = 1 and state = 0"
run_statement "#6.2" "$qry" 1
# Declined leases for subnet 40 should be 1
qry="select leases from lease6_stat where subnet_id = 40 and lease_type = 0 and state = 1"
run_statement "#6.3" "$qry" 1
# Assigned (PD) leases for subnet 50 should be 2
qry="select leases from lease6_stat where subnet_id = 50 and lease_type = 1 and state = 0"
run_statement "#6.4" "$qry" 2
# Should be no records for EXPIRED
qry="select count(subnet_id) from lease4_stat where state = 2"
run_statement "#6.5" "$qry" 0
# Let's wipe the whole database
mysql_wipe
test_finish 0
}
# Verifies that you can upgrade from an earlier version and
# that unused subnet ID values in hosts and options tables are
# converted to NULL.
......@@ -1244,4 +1339,5 @@ mysql_lease6_dump_test
mysql_lease4_stat_test
mysql_lease6_stat_test
mysql_lease_stat_upgrade_test
mysql_lease_stat_recount_test
mysql_unused_subnet_id_test
......@@ -791,6 +791,101 @@ pgsql_lease_stat_upgrade_test() {
pgsql_wipe
}
pgsql_lease_stat_recount_test() {
test_start "pgsql_lease_stat_recount_test"
# Let's wipe the whole database
pgsql_wipe
# Ok, now let's initialize the database
${keaadmin} db-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin db-init pgsql returned non-zero status code %d, expected %d"
# Now we need insert some leases to "recount"
qry=\
"insert into lease4 (address, subnet_id, state) values (111,10,0);\
insert into lease4 (address, subnet_id, state) values (222,10,0);\
insert into lease4 (address, subnet_id, state) values (333,10,1);\
insert into lease4 (address, subnet_id, state) values (444,10,2);\
insert into lease4 (address, subnet_id, state) values (555,77,0);"
run_statement "insert v4 leases" "$qry"
qry=\
"insert into lease6 (address, lease_type, subnet_id, state) values (111,0,40,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (222,0,40,1);\
insert into lease6 (address, lease_type, subnet_id, state) values (333,1,40,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (444,1,50,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (555,1,50,0);\
insert into lease6 (address, lease_type, subnet_id, state) values (666,1,40,2);"
run_statement "insert v6 leases" "$qry"
# Now we change some counters.
qry=\
"insert into lease4_stat (subnet_id, state, leases) values (20,0,1);\
update lease4_stat set leases = 5 where subnet_id = 10 and state = 0;
delete from lease4_stat where subnet_id = 10 and state = 2;"
run_statement "change v4 stats" "$qry"
qry=\
"insert into lease6_stat (subnet_id, lease_type, state, leases) values (20,1,0,1);
update lease6_stat set leases = 5 where subnet_id = 40 and lease_type = 0 and state = 0;
delete from lease6_stat where subnet_id = 40 and lease_type = 1 and state = 2;"
run_statement "change v6 stats" "$qry"
# Recount all statistics from scratch.
${keaadmin} lease-stat-recount pgsql -u $db_user -p $db_password -n $db_name
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-stat-recount pgsql returned non-zero status code %d, expected %d"
#
# First we'll verify lease4_stats are correct after recount.
#
# Assigned leases for subnet 10 should be 2
qry="select leases from lease4_stat where subnet_id = 10 and state = 0"
run_statement "#4.1" "$qry" 2
# Assigned leases for subnet 77 should be 1
qry="select leases from lease4_stat where subnet_id = 77 and state = 0"
run_statement "#4.2" "$qry" 1
# Should be no records for EXPIRED
qry="select count(subnet_id) from lease4_stat where state = 2"
run_statement "#4.3" "$qry" 0
#
# Next we'll verify lease6_stats are correct after recount.
#
# Assigned leases for subnet 40 should be 1
qry="select leases from lease6_stat where subnet_id = 40 and lease_type = 0 and state = 0"
run_statement "#6.1" "$qry" 1
# Assigned (PD) leases for subnet 40 should be 1
qry="select leases from lease6_stat where subnet_id = 40 and lease_type = 1 and state = 0"
run_statement "#6.2" "$qry" 1
# Declined leases for subnet 40 should be 1
qry="select leases from lease6_stat where subnet_id = 40 and lease_type = 0 and state = 1"
run_statement "#6.3" "$qry" 1
# Assigned (PD) leases for subnet 50 should be 2
qry="select leases from lease6_stat where subnet_id = 50 and lease_type = 1 and state = 0"
run_statement "#6.4" "$qry" 2
# Should be no records for EXPIRED
qry="select count(subnet_id) from lease4_stat where state = 2"
run_statement "#6.5" "$qry" 0
# Let's wipe the whole database
pgsql_wipe
test_finish 0
}
# Verifies that you can upgrade from earlier version and
# that unused subnet ID values in hosts and options tables are
# converted to NULL.
......@@ -885,4 +980,5 @@ pgsql_lease6_dump_test
pgsql_lease4_stat_test
pgsql_lease6_stat_test
pgsql_lease_stat_upgrade_test
pgsql_lease_stat_recount_test
pgsql_unused_subnet_id_test
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment