pgsql_tests.sh.in 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #!/bin/sh
  2. # Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. # Include common test library.
  8. . @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh
  9. # Include admin utilities
  10. . @abs_top_srcdir@/src/bin/admin/admin-utils.sh
  11. # Set path to the production schema scripts
  12. db_scripts_dir=@abs_top_srcdir@/src/share/database/scripts
  13. db_user="keatest"
  14. db_password="keatest"
  15. db_name="keatest"
  16. db_host="localhost"
  17. # Set location of the kea-admin.
  18. keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin
  19. # Wipe all tables from the DB:
  20. pgsql_wipe() {
  21. printf "Wiping whole database %s\n" $db_name
  22. export PGPASSWORD=$db_password
  23. cat $db_scripts_dir/pgsql/dhcpdb_drop.pgsql | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U keatest -d keatest >/dev/null 2>&1
  24. assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d"
  25. }
  26. pgsql_lease_init_test() {
  27. test_start "pgsql.lease-init"
  28. # Let's wipe the whole database
  29. pgsql_wipe
  30. # Create the database
  31. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
  32. assert_eq 0 $? "kea-admin lease-init pgsql failed, expected exit code: %d, actual: %d"
  33. # Verify that all the expected tables exist
  34. # Check schema_version table
  35. RESULT=`pgsql_execute "SELECT version, minor FROM schema_version;"`
  36. assert_eq 0 $? "schema_version table check failed, expected exit code: %d, actual: %d"
  37. # Check lease4 table
  38. RESULT=`pgsql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, state FROM lease4;"`
  39. assert_eq 0 $? "lease4 table check failed, expected exit code: %d, actual: %d"
  40. # Check lease6 table
  41. RESULT=`pgsql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, state FROM lease6;"`
  42. assert_eq 0 $? "lease6 table check failed, expected exit code: %d, actual: %d"
  43. # Check lease6_types table
  44. RESULT=`pgsql_execute "SELECT lease_type, name FROM lease6_types;"`
  45. assert_eq 0 $? "lease6_types table check failed, expected exit code: %d, actual: %d"
  46. # Check lease_state table
  47. RESULT=`pgsql_execute "SELECT state, name FROM lease_state;"`
  48. assert_eq 0 $? "lease_state table check failed, expected exit code: %d, actual: %d"
  49. # Trying to create it again should fail. This verifies the db present
  50. # check
  51. echo ""
  52. echo "DB created successfully, make sure we aren't allowed to try it again:"
  53. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
  54. assert_eq 2 $? "kea-admin failed to deny lease-init, expected exit code: %d, actual: %d"
  55. # Let's wipe the whole database
  56. pgsql_wipe
  57. test_finish 0
  58. }
  59. pgsql_lease_version_test() {
  60. test_start "pgsql.lease-version"
  61. # Wipe the whole database
  62. pgsql_wipe
  63. # Create the database
  64. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
  65. assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"
  66. # Verify that kea-admin lease-version returns the correct version
  67. version=$(${keaadmin} lease-version pgsql -u $db_user -p $db_password -n $db_name)
  68. assert_str_eq "3.1" ${version} "Expected kea-admin to return %s, returned value was %s"
  69. # Let's wipe the whole database
  70. pgsql_wipe
  71. test_finish 0
  72. }
  73. pgsql_upgrade_1_0_to_2_0() {
  74. # Added state column to lease4
  75. output=`pgsql_execute "select state from lease4;"`
  76. ERRCODE=$?
  77. assert_eq 0 $ERRCODE "lease4 is missing state column. (expected status code %d, returned %d)"
  78. # Added state column to lease6
  79. output=`pgsql_execute "select state from lease6;"`
  80. ERRCODE=$?
  81. assert_eq 0 $ERRCODE "lease6 is missing state column. (expected status code %d, returned %d)"
  82. # Added stored procedures for lease dumps
  83. output=`pgsql_execute "select lease4DumpHeader from lease4DumpHeader();"`
  84. assert_eq 0 $ERRCODE "function lease4DumpHeader() broken or missing. (expected status code %d, returned %d)"
  85. output=`pgsql_execute "select address from lease4DumpData();"`
  86. assert_eq 0 $ERRCODE "function lease4DumpData() broken or missing. (expected status code %d, returned %d)"
  87. output=`pgsql_execute "select lease6DumpHeader from lease6DumpHeader();"`
  88. assert_eq 0 $ERRCODE "function lease6DumpHeader() broken or missing. (expected status code %d, returned %d)"
  89. output=`pgsql_execute "select address from lease6DumpData();"`
  90. assert_eq 0 $ERRCODE "function lease6DumpData() broken or missing. (expected status code %d, returned %d)"
  91. }
  92. pgsql_upgrade_2_0_to_3_0() {
  93. # Added hwaddr, hwtype, and hwaddr_source columns to lease6 table
  94. output=`pgsql_execute "select hwaddr, hwtype, hwaddr_source from lease6;"`
  95. ERRCODE=$?
  96. assert_eq 0 $ERRCODE "lease6 table not upgraded to 3.0 (expected status code %d, returned %d)"
  97. # Added lease_hwaddr_source table
  98. output=`pgsql_execute "select hwaddr_source, name from lease_hwaddr_source;"`
  99. ERRCODE=$?
  100. assert_eq 0 $ERRCODE "lease_hwaddr_source table is missing or broken. (expected status code %d, returned %d)"
  101. # Added hosts table
  102. output=`pgsql_execute "select host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes, dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name from hosts;"`
  103. ERRCODE=$?
  104. assert_eq 0 $ERRCODE "hosts table is missing or broken. (expected status code %d, returned %d)"
  105. # Added ipv6_reservations table
  106. output=`pgsql_execute "select reservation_id, address, prefix_len, type, dhcp6_iaid, host_id from ipv6_reservations;"`
  107. ERRCODE=$?
  108. assert_eq 0 $ERRCODE "ipv6_reservations table is missing or broken. (expected status code %d, returned %d)"
  109. # Added dhcp4_options table
  110. output=`pgsql_execute "select option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp4_subnet_id, host_id, scope_id from dhcp4_options;"`
  111. ERRCODE=$?
  112. assert_eq 0 $ERRCODE "dhcp4_options table is missing or broken. (expected status code %d, returned %d)"
  113. # Added dhcp6_options table
  114. output=`pgsql_execute "select option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp6_subnet_id, host_id,scope_id from dhcp6_options;"`
  115. ERRCODE=$?
  116. assert_eq 0 $ERRCODE "dhcp6_options table is missing or broken. (expected status code %d, returned %d)"
  117. # Added host_identifier_type table
  118. output=`pgsql_execute "select type, name from host_identifier_type;"`
  119. ERRCODE=$?
  120. assert_eq 0 $ERRCODE "host_identifier_type table is missing or broken. (expected status code %d, returned %d)"
  121. # Added dhcp_option_scope table
  122. output=`pgsql_execute "select scope_id, scope_name from dhcp_option_scope;"`
  123. ERRCODE=$?
  124. assert_eq 0 $ERRCODE "dhcp_option_scope table is missing or broken. (expected status code %d, returned %d)"
  125. # Added dhcp6_options table
  126. output=`pgsql_execute "select option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp6_subnet_id, host_id,scope_id from dhcp6_options;"`
  127. ERRCODE=$?
  128. assert_eq 0 $ERRCODE "dhcp6_options table is missing or broken. (expected status code %d, returned %d)"
  129. # Added order by clause to lease4DumpData
  130. output=`pgsql_execute "select address from lease4DumpData();"`
  131. assert_eq 0 $ERRCODE "function lease4DumpData() broken or missing. (expected status code %d, returned %d)"
  132. output=`pgsql_execute "\sf lease4DumpData"`
  133. assert_eq 0 $ERRCODE "\sf of lease4DumpData failed. (expected status code %d, returned %d)"
  134. count=`echo $output | grep -ic "order by l\.address"`
  135. assert_eq 1 $count "lease4DumpData is missing order by clause"
  136. # Added hwaddr columns to lease6DumpHeader
  137. output=`pgsql_execute "select lease6DumpHeader from lease6DumpHeader();"`
  138. assert_eq 0 $ERRCODE "function lease6DumpHeader() broken or missing. (expected status code %d, returned %d)"
  139. count=`echo $output | grep -ic "hwaddr,hwtype,hwaddr_source"`
  140. assert_eq 1 $count "lease6DumpHeader is missing hwaddr columns"
  141. # Added hwaddr columns to lease6DumpData
  142. output=`pgsql_execute "select hwaddr,hwtype,hwaddr_source from lease6DumpData();"`
  143. assert_eq 0 $ERRCODE "function lease6DumpData() broken or missing. (expected status code %d, returned %d)"
  144. # Added order by clause to lease6DumpData
  145. output=`pgsql_execute "\sf lease4DumpData"`
  146. assert_eq 0 $ERRCODE "\sf of lease4DumpData failed. (expected status code %d, returned %d)"
  147. count=`echo $output | grep -ic "order by l\.address"`
  148. assert_eq 1 $count "lease4DumpData is missing order by clause"
  149. # lease_hardware_source should have row for source = 0
  150. output=`pgsql_execute "select count(hwaddr_source) from lease_hwaddr_source where hwaddr_source = 0 and name='HWADDR_SOURCE_UNKNOWN';"`
  151. ERRCODE=$?
  152. assert_eq 0 $ERRCODE "select from lease_hwaddr_source failed. (expected status code %d, returned %d)"
  153. assert_eq 1 "$output" "lease_hwaddr_source does not contain entry for HWADDR_SOURCE_UNKNOWN. (record count %d, expected %d)"
  154. }
  155. pgsql_upgrade_3_0_to_3_1() {
  156. # Verify upgraded schema reports version 3.1.
  157. version=$(${keaadmin} lease-version pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir)
  158. assert_str_eq "3.1" ${version} "Expected kea-admin to return %s, returned value was %s"
  159. }
  160. pgsql_upgrade_test() {
  161. test_start "pgsql.upgrade-test"
  162. # Wipe the whole database
  163. pgsql_wipe
  164. # Initialize database to schema 1.0.
  165. pgsql_execute_script @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.pgsql
  166. assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"
  167. ${keaadmin} lease-upgrade pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
  168. assert_eq 0 $? "lease-upgrade failed, expected exit code: %d, actual: %d"
  169. # Check 1.0 to 2.0 upgrade
  170. pgsql_upgrade_1_0_to_2_0
  171. # Check 2.0 to 3.0 upgrade
  172. pgsql_upgrade_2_0_to_3_0
  173. # Check 3.0 to 3.1 upgrade
  174. pgsql_upgrade_3_0_to_3_1
  175. # Let's wipe the whole database
  176. pgsql_wipe
  177. test_finish 0
  178. }
  179. # Given a valid timestamp string, returns a timestamp with timezone string
  180. # for the give time localized by the PostgreSQL server.
  181. get_local_time() {
  182. timestamp="$1"
  183. # Expiration field is a "timestamp with timezone" so we need a reference
  184. # time for the machine/DB this test is running upon.
  185. ref_timestamp=`pgsql_execute "select timestamptz '$1';"`
  186. ERRCODE=$?
  187. assert_eq 0 $ERRCODE "reference time query failed for [$timestamp], expected exit code %d, actual %d"
  188. echo $ref_timestamp
  189. }
  190. # Test verifies the ability to dump lease4 data to CSV file
  191. # The dump output file is compared against a reference file.
  192. # If the dump is successful, the file contents will be the
  193. # same. Note that the expire field in the lease4 table
  194. # is of data type "timestamp with timezone". This means that
  195. # the dumped file content is dependent upon the timezone
  196. # setting the PostgreSQL server is using. To account for
  197. # this the reference data contains a tag, "<timestamp>"
  198. # where the expire column's data would normally be. This
  199. # tag is replaced during text execution with a value
  200. # determined by querying the PostgreSQL server. This
  201. # updated reference data is captured in a temporary file
  202. # which is used for the actual comparison.
  203. pgsql_lease4_dump_test() {
  204. test_start "pgsql.lease4_dump_test"
  205. test_dir="@abs_top_srcdir@/src/bin/admin/tests"
  206. output_dir="@abs_top_builddir@/src/bin/admin/tests"
  207. script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
  208. output_file="$output_dir/data/pgsql.lease4_dump_test.output.csv"
  209. ref_file="$test_dir/data/pgsql.lease4_dump_test.reference.csv"
  210. ref_file_tmp="$output_dir/data/pgsql.lease4_dump_test.reference.csv.tmp"
  211. # wipe out any residuals from prior failed runs
  212. if [ -e $output_file ]
  213. then
  214. rm $output_file
  215. fi
  216. if [ -e $ref_file_tmp ]
  217. then
  218. rm $ref_file_tmp
  219. fi
  220. # Let's wipe the whole database
  221. pgsql_wipe
  222. # Ok, now let's initialize the database
  223. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
  224. ERRCODE=$?
  225. assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
  226. timestamp1="2015-01-01 01:15:30"
  227. timestamp2="2015-02-02 02:30:45"
  228. timestamp3="2015-03-03 11:01:07"
  229. # Insert the reference records
  230. insert_sql="\
  231. insert into lease4 values(10,E'\\x20',E'\\x30',40,'$timestamp1',50,'t','t','one.example.com', 0);\
  232. insert into lease4 values(11,'',E'\\x0123',40,'$timestamp2',50,'t','t','', 1);\
  233. insert into lease4 values(12,E'\\x22','',40,'$timestamp3',50,'t','t','three.example.com', 2);"
  234. pgsql_execute "$insert_sql"
  235. ERRCODE=$?
  236. assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
  237. # Dump lease4 to output_file
  238. ${keaadmin} lease-dump pgsql -4 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
  239. ERRCODE=$?
  240. assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed, status code %d"
  241. # Expiration field is a "timestamp with timezone" so we need a localized reference
  242. # times for the machine/DB this test is running upon.
  243. local_timestamp1=`get_local_time "$timestamp1"`
  244. local_timestamp2=`get_local_time "$timestamp2"`
  245. local_timestamp3=`get_local_time "$timestamp3"`
  246. # Create the comparison file by replacing the <timestamp> tags
  247. # with the local reference timestamp
  248. sedstr="\
  249. sed 's/<timestamp1>/$local_timestamp1/g' $ref_file | \
  250. sed 's/<timestamp2>/$local_timestamp2/g' | \
  251. sed 's/<timestamp3>/$local_timestamp3/g' "
  252. eval $sedstr >$ref_file_tmp
  253. ERRCODE=$?
  254. assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
  255. # Compare the dump output to reference file, they should be identical
  256. cmp -s $output_file $ref_file_tmp
  257. ERRCODE=$?
  258. assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
  259. # Remove the output file and temporary reference file
  260. rm $output_file
  261. rm $ref_file_tmp
  262. # Let's wipe the whole database
  263. pgsql_wipe
  264. test_finish 0
  265. }
  266. # Test verifies the ability to dump lease6 data to CSV file
  267. # The dump output file is compared against a reference file.
  268. # If the dump is successful, the file contents will be the
  269. # same. Note that the expire field in the lease6 table
  270. # is of data type "timestamp with timezone". This means that
  271. # the dumped file content is dependent upon the timezone
  272. # setting the PostgreSQL server is using. To account for
  273. # this the reference data contains a tag, "<timestamp>"
  274. # where the expire column's data would normally be. This
  275. # tag is replaced during text execution with a value
  276. # determined by querying the PostgreSQL server. This
  277. # updated reference data is captured in a temporary file
  278. # which is used for the actual comparison.
  279. pgsql_lease6_dump_test() {
  280. test_start "pgsql.lease6_dump_test"
  281. test_dir="@abs_top_srcdir@/src/bin/admin/tests"
  282. output_dir="@abs_top_builddir@/src/bin/admin/tests"
  283. script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
  284. output_file="$output_dir/data/pgsql.lease6_dump_test.output.csv"
  285. ref_file="$test_dir/data/pgsql.lease6_dump_test.reference.csv"
  286. ref_file_tmp="$output_dir/data/pgsql.lease6_dump_test.reference.csv.tmp"
  287. # wipe out any residuals from prior failed runs
  288. if [ -e $output_file ]
  289. then
  290. rm $output_file
  291. fi
  292. if [ -e $ref_file_tmp ]
  293. then
  294. rm $ref_file_tmp
  295. fi
  296. # Let's wipe the whole database
  297. pgsql_wipe
  298. # Ok, now let's initialize the database
  299. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
  300. ERRCODE=$?
  301. assert_eq 0 $ERRCODE "could not create database, status code %d"
  302. timestamp1="2015-04-04 01:15:30"
  303. timestamp2="2015-02-02 02:30:45"
  304. timestamp3="2015-06-06 11:01:07"
  305. # Insert the reference records
  306. insert_sql="\
  307. insert into lease6 values(10,E'\\x20',30,'$timestamp1',40,50,1,60,70,'t','t','one.example.com', 0,decode('80','hex'),90,16);\
  308. insert into lease6 values(11,'',30,'$timestamp2',40,50,1,60,70,'t','t','', 1,decode('80','hex'),90,1);\
  309. insert into lease6 values(12,E'\\x21',30,'$timestamp3',40,50,1,60,70,'t','t','three.example.com', 2,decode('80','hex'),90,4);"
  310. pgsql_execute "$insert_sql"
  311. ERRCODE=$?
  312. assert_eq 0 $ERRCODE "insert into lease6 failed, status code %d"
  313. # Dump lease6 to output_file
  314. ${keaadmin} lease-dump pgsql -6 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
  315. ERRCODE=$?
  316. assert_eq 0 $ERRCODE "kea-admin lease-dump -6 failed, status code %d"
  317. # Expiration field is a "timestamp with timezone" so we need a localized reference
  318. # times for the machine/DB this test is running upon.
  319. local_timestamp1=`get_local_time "$timestamp1"`
  320. local_timestamp2=`get_local_time "$timestamp2"`
  321. local_timestamp3=`get_local_time "$timestamp3"`
  322. # Create the comparison file by replacing the <timestamp> tags
  323. # with the local reference timestamp
  324. sedstr="\
  325. sed 's/<timestamp1>/$local_timestamp1/g' $ref_file | \
  326. sed 's/<timestamp2>/$local_timestamp2/g' | \
  327. sed 's/<timestamp3>/$local_timestamp3/g' "
  328. eval $sedstr >$ref_file_tmp
  329. ERRCODE=$?
  330. assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
  331. # Compare the dump output to reference file, they should be identical
  332. cmp -s $output_file $ref_file_tmp
  333. ERRCODE=$?
  334. assert_eq 0 $ERRCODE "dump file does not match reference file"
  335. # Remove the output file and temporary reference file
  336. rm $output_file
  337. rm $ref_file_tmp
  338. # Let's wipe the whole database
  339. pgsql_wipe
  340. test_finish 0
  341. }
  342. pgsql_lease_init_test
  343. pgsql_lease_version_test
  344. pgsql_upgrade_test
  345. pgsql_lease4_dump_test
  346. pgsql_lease6_dump_test