mysql_tests.sh.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #!/bin/sh
  2. # Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  3. #
  4. # Permission to use, copy, modify, and/or distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. # PERFORMANCE OF THIS SOFTWARE.
  15. # Include common test library.
  16. . @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh
  17. # If the code is installed, include admin-utils.sh from the destination
  18. # directory. If not, include it from the sources.
  19. prefix=@prefix@
  20. if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
  21. . @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
  22. else
  23. . @abs_top_srcdir@/src/bin/admin/admin-utils.sh
  24. fi
  25. db_user="keatest"
  26. db_password="keatest"
  27. db_name="keatest"
  28. # Set location of the kea-admin.
  29. keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin
  30. # Wipe all tables from the DB:
  31. mysql_wipe() {
  32. printf "Wiping whole database %s\n" $db_name
  33. # First we build the list of drop table commands
  34. # We don't bother with "cascade" because as of MySQL
  35. # 5.1 it is only there to ease porting, it doesn't
  36. # actually do anything.
  37. qry="\
  38. SELECT CONCAT('DROP TABLE ', table_schema, '.', table_name, ';') \
  39. FROM information_schema.tables \
  40. WHERE table_schema = 'keatest';"
  41. drop_sql=`mysql_execute "${qry}"`
  42. ERRCODE=$?
  43. assert_eq 0 $ERRCODE "mysql-wipe: table query failed, exit code %d, expected %d"
  44. # We need to turn off referrential integrity checks so tables
  45. # are dropped regardless of whether they are used in foreign keys.
  46. # (This is what cascade would normally do)
  47. mysql_execute "SET SESSION FOREIGN_KEY_CHECKS = 0;$drop_sql"
  48. ERRCODE=$?
  49. assert_eq 0 $ERRCODE "mysql-wipe: drop table sql failed, exit code %d, expected %d"
  50. }
  51. mysql_lease_init_test() {
  52. test_start "mysql.lease-init"
  53. # Let's wipe the whole database
  54. mysql_wipe
  55. # Ok, now let's initalize the database
  56. ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
  57. ERRCODE=$?
  58. assert_eq 0 $ERRCODE "kea-admin lease-init mysql returned non-zero status code %d, expected %d"
  59. # Ok, now let's check if the tables are indeed there.
  60. # First table: schema_version. Should have 2 columns: version and minor.
  61. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  62. SELECT version, minor FROM schema_version;
  63. EOF
  64. ERRCODE=$?
  65. assert_eq 0 $ERRCODE "schema_version table is missing or broken. (returned status code %d, expected %d)"
  66. # Second table: lease4
  67. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  68. SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname FROM lease4;
  69. EOF
  70. ERRCODE=$?
  71. assert_eq 0 $ERRCODE "lease4 table is missing or broken. (returned status code %d, expected %d)"
  72. # Third table: lease6
  73. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  74. 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;
  75. EOF
  76. ERRCODE=$?
  77. assert_eq 0 $ERRCODE "lease6 table is missing or broken. (returned status code %d, expected %d)"
  78. # Fourth table: lease6_types
  79. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  80. SELECT lease_type, name FROM lease6_types;
  81. EOF
  82. ERRCODE=$?
  83. assert_eq 0 $ERRCODE "lease6_types table is missing or broken. (returned status code %d, expected %d)"
  84. # Fifth table: lease_hwaddr_source
  85. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  86. SELECT hwaddr_source, name FROM lease_hwaddr_source;
  87. EOF
  88. ERRCODE=$?
  89. assert_eq 0 $ERRCODE "lease_hwaddr_source table is missing or broken. (returned status code %d, expected %d)"
  90. # Let's wipe the whole database
  91. mysql_wipe
  92. test_finish 0
  93. }
  94. mysql_lease_version_test() {
  95. test_start "mysql.lease-version"
  96. # Let's wipe the whole database
  97. mysql_wipe
  98. # Ok, now let's create a version 1.7
  99. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  100. CREATE TABLE schema_version (
  101. version INT PRIMARY KEY NOT NULL,
  102. minor INT
  103. );
  104. INSERT INTO schema_version VALUES (1, 7);
  105. EOF
  106. version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name)
  107. assert_str_eq "1.7" ${version} "Expected kea-admin to return %s, returned value was %s"
  108. # Let's wipe the whole database
  109. mysql_wipe
  110. test_finish 0
  111. }
  112. mysql_host_reservation_init_test() {
  113. test_start "mysql.host_reservation-init"
  114. # Let's wipe the whole database
  115. mysql_wipe
  116. # Ok, now let's initalize the database
  117. ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
  118. ERRCODE=$?
  119. assert_eq 0 $ERRCODE "kea-admin lease-init mysql returned non-zero status code %d, expected %d"
  120. # Ok, now let's check if the tables are indeed there.
  121. # First table: schema_version. Should have 2 columns: version and minor.
  122. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  123. SELECT version, minor FROM schema_version;
  124. EOF
  125. ERRCODE=$?
  126. assert_eq 0 $ERRCODE "schema_version table is missing or broken. (returned status code %d, expected %d)"
  127. # Second table: hosts
  128. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  129. SELECT host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes FROM hosts;
  130. EOF
  131. ERRCODE=$?
  132. assert_eq 0 $ERRCODE "hosts table is missing or broken. (returned status code %d, expected %d)"
  133. # Third table: ipv6_reservations
  134. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  135. SELECT reservation_id, address, prefix_len, type, dhcp6_iaid, host_id FROM ipv6_reservations;
  136. EOF
  137. ERRCODE=$?
  138. assert_eq 0 $ERRCODE "ipv6_reservations table is missing or broken. (returned status code %d, expected %d)"
  139. # Fourth table: dhcp4_options
  140. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  141. SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp4_subnet_id, host_id FROM dhcp4_options;
  142. EOF
  143. ERRCODE=$?
  144. assert_eq 0 $ERRCODE "dhcp4_options table is missing or broken. (returned status code %d, expected %d)"
  145. # Fifth table: dhcp6_options
  146. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  147. SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp6_subnet_id, host_id FROM dhcp6_options;
  148. EOF
  149. ERRCODE=$?
  150. assert_eq 0 $ERRCODE "dhcp6_options table is missing or broken. (returned status code %d, expected %d)"
  151. # Let's wipe the whole database
  152. mysql_wipe
  153. test_finish 0
  154. }
  155. mysql_upgrade_test() {
  156. test_start "mysql.host_reservation-upgrade"
  157. # Let's wipe the whole database
  158. mysql_wipe
  159. # Initialize database to scheme 1.0.
  160. mysql -u$db_user -p$db_password $db_name < @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.mysql
  161. # Sanity check - verify that it reports version 1.0.
  162. version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts)
  163. assert_str_eq "1.0" ${version} "Expected kea-admin to return %s, returned value was %s"
  164. # Ok, we have a 1.0 database. Let's upgrade it to 3.0
  165. ${keaadmin} lease-upgrade mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
  166. ERRCODE=$?
  167. assert_eq 0 $ERRCODE "kea-admin lease-upgrade mysql returned non-zero status code %d, expected %d"
  168. # Let's check that the new tables are indeed there.
  169. #table: lease6 (upgrade 1.0 -> 2.0)
  170. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  171. SELECT hwaddr, hwtype, hwaddr_source FROM lease6;
  172. EOF
  173. ERRCODE=$?
  174. assert_eq 0 $ERRCODE "lease6 table not upgraded to 2.0 (returned status code %d, expected %d)"
  175. #table: lease_hwaddr_source (upgrade 1.0 -> 2.0)
  176. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  177. SELECT hwaddr_source, name FROM lease_hwaddr_source;
  178. EOF
  179. ERRCODE=$?
  180. assert_eq 0 $ERRCODE "lease_hwaddr_source table is missing or broken. (returned status code %d, expected %d)"
  181. #table: hosts (upgrade 2.0 -> 3.0)
  182. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  183. SELECT host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes FROM hosts;
  184. EOF
  185. ERRCODE=$?
  186. assert_eq 0 $ERRCODE "hosts table is missing or broken. (returned status code %d, expected %d)"
  187. #table: ipv6_reservations (upgrade 2.0 -> 3.0)
  188. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  189. SELECT reservation_id, address, prefix_len, type, dhcp6_iaid, host_id FROM ipv6_reservations;
  190. EOF
  191. ERRCODE=$?
  192. assert_eq 0 $ERRCODE "ipv6_reservations table is missing or broken. (returned status code %d, expected %d)"
  193. #table: dhcp4_options (upgrade 2.0 -> 3.0)
  194. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  195. SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp4_subnet_id, host_id FROM dhcp4_options;
  196. EOF
  197. ERRCODE=$?
  198. assert_eq 0 $ERRCODE "dhcp4_options table is missing or broken. (returned status code %d, expected %d)"
  199. #table: dhcp6_options (upgrade 2.0 -> 3.0)
  200. mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
  201. SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp6_subnet_id, host_id FROM dhcp6_options;
  202. EOF
  203. ERRCODE=$?
  204. assert_eq 0 $ERRCODE "dhcp6_options table is missing or broken. (returned status code %d, expected %d)"
  205. # Verify that it reports version 3.0.
  206. version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name)
  207. assert_str_eq "3.0" ${version} "Expected kea-admin to return %s, returned value was %s"
  208. # Let's wipe the whole database
  209. mysql_wipe
  210. test_finish 0
  211. }
  212. mysql_lease4_dump_test() {
  213. test_start "mysql.lease4_dump_test"
  214. test_dir="@abs_top_srcdir@/src/bin/admin/tests"
  215. output_dir="@abs_top_builddir@/src/bin/admin/tests"
  216. script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
  217. output_file="$output_dir/data/mysql.lease4_dump_test.output.csv"
  218. tmp_file="$output_file.tmp"
  219. ref_file="$test_dir/data/mysql.lease4_dump_test.reference.csv"
  220. # wipe out any residuals from prior failed runs
  221. if [ -e $output_file ]
  222. then
  223. rm $output_file
  224. fi
  225. if [ -e $tmp_file ]
  226. then
  227. rm $tmp_file
  228. fi
  229. # Let's wipe the whole database
  230. mysql_wipe
  231. # Ok, now let's initalize the database
  232. ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
  233. ERRCODE=$?
  234. assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
  235. # Insert the reference record
  236. insert_sql="\
  237. insert into lease4 values(10,20,30,40,\"2015-01-01 01:15:30\",50,1,1,\"one.example.com\", 0);\
  238. insert into lease4 values(11,NULL,123,40,\"2015-02-02 02:30:45\",50,1,1,\"\", 1);\
  239. insert into lease4 values(12,22,NULL,40,\"2015-03-03 11:01:07\",50,1,1,\"three.example.com\", 2);"
  240. mysql_execute "$insert_sql"
  241. ERRCODE=$?
  242. assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
  243. # Dump lease4 to output_file
  244. ${keaadmin} lease-dump mysql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
  245. ERRCODE=$?
  246. assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed, expected exit code %d, actual %d"
  247. # Compare the dump output to reference file, they should be identical
  248. cmp -s $output_file $ref_file
  249. ERRCODE=$?
  250. assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
  251. # remove the output file
  252. rm $output_file
  253. # Let's wipe the whole database
  254. mysql_wipe
  255. test_finish 0
  256. }
  257. mysql_lease6_dump_test() {
  258. test_start "mysql.lease6_dump_test"
  259. test_dir="@abs_top_srcdir@/src/bin/admin/tests"
  260. output_dir="@abs_top_builddir@/src/bin/admin/tests"
  261. script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
  262. output_file="$output_dir/data/mysql.lease6_dump_test.output.csv"
  263. tmp_file="$output_file.tmp"
  264. ref_file="$test_dir/data/mysql.lease6_dump_test.reference.csv"
  265. # wipe out any residuals from prior failed runs
  266. if [ -e $output_file ]
  267. then
  268. rm $output_file
  269. fi
  270. if [ -e $tmp_file ]
  271. then
  272. rm $tmp_file
  273. fi
  274. # Let's wipe the whole database
  275. mysql_wipe
  276. # Ok, now let's initalize the database
  277. ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
  278. ERRCODE=$?
  279. assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
  280. # Insert the reference record
  281. insert_sql="\
  282. insert into lease6 values(10,20,30,\"2015-04-04 01:15:30\",40,50,1,60,70,1,1,\"one.example.com\",80,90,100, 0);\
  283. insert into lease6 values(11,NULL,30,\"2015-05-05 02:30:45\",40,50,1,60,70,1,1,\"\",80,90,100, 1);\
  284. insert into lease6 values(12,21,30,\"2015-06-06 11:01:07\",40,50,1,60,70,1,1,\"three.example.com\",80,90,100, 2);"
  285. mysql_execute "$insert_sql"
  286. ERRCODE=$?
  287. assert_eq 0 $ERRCODE "insert into lease6 failed, expected exit code %d, actual %d"
  288. # Dump lease4 to output_file
  289. ${keaadmin} lease-dump mysql -6 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
  290. ERRCODE=$?
  291. assert_eq 0 $ERRCODE "kea-admin lease-dump -6 failed, status code %d"
  292. # Compare the dump output to reference file, they should be identical
  293. cmp -s $output_file $ref_file
  294. ERRCODE=$?
  295. assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
  296. # remove the output file
  297. rm $output_file
  298. # Let's wipe the whole database
  299. mysql_wipe
  300. test_finish 0
  301. }
  302. mysql_lease_init_test
  303. mysql_host_reservation_init_test
  304. mysql_lease_version_test
  305. mysql_upgrade_test
  306. mysql_lease4_dump_test
  307. mysql_lease6_dump_test