pgsql_tests.sh.in 18 KB

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