pgsql_tests.sh.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/bin/sh
  2. # Copyright (C) 2015 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. # If the code is installed, include admin-utils.sh from the destination
  10. # directory. If not, include it from the sources.
  11. prefix=@prefix@
  12. if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
  13. . @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
  14. else
  15. . @abs_top_srcdir@/src/bin/admin/admin-utils.sh
  16. fi
  17. db_user="keatest"
  18. db_password="keatest"
  19. db_name="keatest"
  20. # Set location of the kea-admin.
  21. keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin
  22. # Set location of scripts directory
  23. scripts_dir=@abs_top_srcdir@/src/bin/admin/scripts
  24. # Wipe all tables from the DB:
  25. pgsql_wipe() {
  26. printf "Wiping whole database %s\n" $db_name
  27. export PGPASSWORD=$db_password
  28. # Make a set of drop commands, one for each table owned by keatest
  29. RESULT=`pgsql_execute "SELECT 'drop table if exists '||t.tablename || ' cascade;' as dcmd FROM pg_catalog.pg_tables t WHERE t.tableowner = 'keatest';"`
  30. assert_eq 0 $? "pgsql_wipe select failed, expected exit code: %d, actual: %d"
  31. # Now execute the set of drop commands from the result set returned
  32. RESULT=`pgsql_execute "$RESULT"`
  33. assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d"
  34. }
  35. pgsql_lease_init_test() {
  36. test_start "pgsql.lease-init"
  37. # Let's wipe the whole database
  38. pgsql_wipe
  39. # Create the database
  40. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
  41. assert_eq 0 $? "kea-admin lease-init pgsql failed, expected exit code: %d, actual: %d"
  42. # Verify that all the expected tables exist
  43. # Check schema_version table
  44. RESULT=`pgsql_execute "SELECT version, minor FROM schema_version;"`
  45. assert_eq 0 $? "schema_version table check failed, expected exit code: %d, actual: %d"
  46. # Check lease4 table
  47. RESULT=`pgsql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, state FROM lease4;"`
  48. assert_eq 0 $? "lease4 table check failed, expected exit code: %d, actual: %d"
  49. # Check lease6 table
  50. 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;"`
  51. assert_eq 0 $? "lease6 table check failed, expected exit code: %d, actual: %d"
  52. # Check lease6_types table
  53. RESULT=`pgsql_execute "SELECT lease_type, name FROM lease6_types;"`
  54. assert_eq 0 $? "lease6_types table check failed, expected exit code: %d, actual: %d"
  55. # Check lease_state table
  56. RESULT=`pgsql_execute "SELECT state, name FROM lease_state;"`
  57. assert_eq 0 $? "lease_state table check failed, expected exit code: %d, actual: %d"
  58. # Trying to create it again should fail. This verifies the db present
  59. # check
  60. echo ""
  61. echo "DB created successfully, make sure we aren't allowed to try it again:"
  62. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
  63. assert_eq 2 $? "kea-admin failed to deny lease-init, expected exit code: %d, actual: %d"
  64. # Let's wipe the whole database
  65. pgsql_wipe
  66. test_finish 0
  67. }
  68. pgsql_lease_version_test() {
  69. test_start "pgsql.lease-version"
  70. # Wipe the whole database
  71. pgsql_wipe
  72. # Create the database
  73. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
  74. assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"
  75. # Verfiy that kea-admin lease-version returns the correct version
  76. version=$(${keaadmin} lease-version pgsql -u $db_user -p $db_password -n $db_name)
  77. assert_str_eq "2.0" ${version} "Expected kea-admin to return %s, returned value was %s"
  78. # Let's wipe the whole database
  79. pgsql_wipe
  80. test_finish 0
  81. }
  82. pgsql_upgrade_test() {
  83. test_start "pgsql.upgrade-test"
  84. # Wipe the whole database
  85. pgsql_wipe
  86. # Initialize database to schema 1.0.
  87. pgsql_execute_script @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.pgsql
  88. assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"
  89. ${keaadmin} lease-upgrade pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
  90. assert_eq 0 $? "lease-upgrade failed, expected exit code: %d, actual: %d"
  91. #table: state column added to lease4 (upgrade 1.0 -> 2.0)
  92. output=`pgsql_execute "SELECT state from lease4;"`
  93. ERRCODE=$?
  94. assert_eq 0 $ERRCODE "lease4 is missing state column. (returned status code %d, expected %d)"
  95. #table: state column added to lease6 (upgrade 1.0 -> 2.0)
  96. output=`pgsql_execute "SELECT state from lease6;"`
  97. ERRCODE=$?
  98. assert_eq 0 $ERRCODE "lease6 is missing state column. (returned status code %d, expected %d)"
  99. #table: stored procedures for lease dumps added (upgrade 1.0 -> 2.0)
  100. output=`pgsql_execute "select lease4DumpHeader from lease4DumpHeader();"`
  101. assert_eq 0 $ERRCODE "function lease4DumpHeader() broken or missing. (returned status code %d, expected %d)"
  102. output=`pgsql_execute "select address from lease4DumpData();"`
  103. assert_eq 0 $ERRCODE "function lease4DumpData() broken or missing. (returned status code %d, expected %d)"
  104. output=`pgsql_execute "select lease6DumpHeader from lease6DumpHeader();"`
  105. assert_eq 0 $ERRCODE "function lease6DumpHeader() broken or missing. (returned status code %d, expected %d)"
  106. output=`pgsql_execute "select address from lease6DumpData();"`
  107. assert_eq 0 $ERRCODE "function lease6DumpData() broken or missing. (returned status code %d, expected %d)"
  108. # Let's wipe the whole database
  109. pgsql_wipe
  110. test_finish 0
  111. }
  112. # Given a valid timestamp string, returns a timestamp with timezone string
  113. # for the give time localized by the PostgreSQL server.
  114. get_local_time() {
  115. timestamp="$1"
  116. # Expiration field is a "timestamp with timezone" so we need a reference
  117. # time for the machine/DB this test is running upon.
  118. ref_timestamp=`pgsql_execute "SELECT timestamptz '$1';"`
  119. ERRCODE=$?
  120. assert_eq 0 $ERRCODE "reference time query failed for [$timestamp], expected exit code %d, actual %d"
  121. echo $ref_timestamp
  122. }
  123. # Test verifies the ability to dump lease4 data to CSV file
  124. # The dump output file is compared against a reference file.
  125. # If the dump is successful, the file contents will be the
  126. # same. Note that the expire field in the lease4 table
  127. # is of data type "timestamp with timezone". This means that
  128. # the dumped file content is dependent upon the timezone
  129. # setting the PostgreSQL server is using. To account for
  130. # this the reference data contains a tag, "<timestamp>"
  131. # where the expire column's data would normally be. This
  132. # tag is replaced during text execution with a value
  133. # determined by querying the PostgreSQL server. This
  134. # updated reference data is captured in a temporary file
  135. # which is used for the actual comparison.
  136. pgsql_lease4_dump_test() {
  137. test_start "pgsql.lease4_dump_test"
  138. test_dir="@abs_top_srcdir@/src/bin/admin/tests"
  139. output_dir="@abs_top_builddir@/src/bin/admin/tests"
  140. script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
  141. output_file="$output_dir/data/pgsql.lease4_dump_test.output.csv"
  142. ref_file="$test_dir/data/pgsql.lease4_dump_test.reference.csv"
  143. ref_file_tmp="$output_dir/data/pgsql.lease4_dump_test.reference.csv.tmp"
  144. # wipe out any residuals from prior failed runs
  145. if [ -e $output_file ]
  146. then
  147. rm $output_file
  148. fi
  149. if [ -e $ref_file_tmp ]
  150. then
  151. rm $ref_file_tmp
  152. fi
  153. # Let's wipe the whole database
  154. pgsql_wipe
  155. # Ok, now let's initalize the database
  156. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $script_dir
  157. ERRCODE=$?
  158. assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
  159. timestamp1="2015-01-01 01:15:30"
  160. timestamp2="2015-02-02 02:30:45"
  161. timestamp3="2015-03-03 11:01:07"
  162. # Insert the reference records
  163. insert_sql="\
  164. insert into lease4 values(10,E'\\x20',E'\\x30',40,'$timestamp1',50,'t','t','one.example.com', 0);\
  165. insert into lease4 values(11,'',E'\\x0123',40,'$timestamp2',50,'t','t','', 1);\
  166. insert into lease4 values(12,E'\\x22','',40,'$timestamp3',50,'t','t','three.example.com', 2);"
  167. pgsql_execute "$insert_sql"
  168. ERRCODE=$?
  169. assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
  170. # Dump lease4 to output_file
  171. ${keaadmin} lease-dump pgsql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
  172. ERRCODE=$?
  173. assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed, status code %d"
  174. # Expiration field is a "timestamp with timezone" so we need a localized reference
  175. # times for the machine/DB this test is running upon.
  176. local_timestamp1=`get_local_time "$timestamp1"`
  177. local_timestamp2=`get_local_time "$timestamp2"`
  178. local_timestamp3=`get_local_time "$timestamp3"`
  179. # Create the comparison file by replacing the <timestamp> tags
  180. # with the local reference timestamp
  181. sedstr="\
  182. sed 's/<timestamp1>/$local_timestamp1/g' $ref_file | \
  183. sed 's/<timestamp2>/$local_timestamp2/g' | \
  184. sed 's/<timestamp3>/$local_timestamp3/g' "
  185. eval $sedstr >$ref_file_tmp
  186. ERRCODE=$?
  187. assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
  188. # Compare the dump output to reference file, they should be identical
  189. cmp -s $output_file $ref_file_tmp
  190. ERRCODE=$?
  191. assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
  192. # Remove the output file and temporary reference file
  193. rm $output_file
  194. rm $ref_file_tmp
  195. # Let's wipe the whole database
  196. pgsql_wipe
  197. test_finish 0
  198. }
  199. # Test verifies the ability to dump lease6 data to CSV file
  200. # The dump output file is compared against a reference file.
  201. # If the dump is successful, the file contents will be the
  202. # same. Note that the expire field in the lease6 table
  203. # is of data type "timestamp with timezone". This means that
  204. # the dumped file content is dependent upon the timezone
  205. # setting the PostgreSQL server is using. To account for
  206. # this the reference data contains a tag, "<timestamp>"
  207. # where the expire column's data would normally be. This
  208. # tag is replaced during text execution with a value
  209. # determined by querying the PostgreSQL server. This
  210. # updated reference data is captured in a temporary file
  211. # which is used for the actual comparison.
  212. pgsql_lease6_dump_test() {
  213. test_start "pgsql.lease6_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/pgsql.lease6_dump_test.output.csv"
  218. ref_file="$test_dir/data/pgsql.lease6_dump_test.reference.csv"
  219. ref_file_tmp="$output_dir/data/pgsql.lease6_dump_test.reference.csv.tmp"
  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 $ref_file_tmp ]
  226. then
  227. rm $ref_file_tmp
  228. fi
  229. # Let's wipe the whole database
  230. pgsql_wipe
  231. # Ok, now let's initalize the database
  232. ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $script_dir
  233. ERRCODE=$?
  234. assert_eq 0 $ERRCODE "could not create database, status code %d"
  235. timestamp1="2015-04-04 01:15:30"
  236. timestamp2="2015-02-02 02:30:45"
  237. timestamp3="2015-06-06 11:01:07"
  238. # Insert the reference records
  239. insert_sql="\
  240. insert into lease6 values(10,E'\\x20',30,'$timestamp1',40,50,1,60,70,'t','t','one.example.com', 0);\
  241. insert into lease6 values(11,'',30,'$timestamp2',40,50,1,60,70,'t','t','', 1);\
  242. insert into lease6 values(12,E'\\x21',30,'$timestamp3',40,50,1,60,70,'t','t','three.example.com', 2);"
  243. pgsql_execute "$insert_sql"
  244. ERRCODE=$?
  245. assert_eq 0 $ERRCODE "insert into lease6 failed, status code %d"
  246. # Dump lease6 to output_file
  247. ${keaadmin} lease-dump pgsql -6 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
  248. ERRCODE=$?
  249. assert_eq 0 $ERRCODE "kea-admin lease-dump -6 failed, status code %d"
  250. # Expiration field is a "timestamp with timezone" so we need a localized reference
  251. # times for the machine/DB this test is running upon.
  252. local_timestamp1=`get_local_time "$timestamp1"`
  253. local_timestamp2=`get_local_time "$timestamp2"`
  254. local_timestamp3=`get_local_time "$timestamp3"`
  255. # Create the comparison file by replacing the <timestamp> tags
  256. # with the local reference timestamp
  257. sedstr="\
  258. sed 's/<timestamp1>/$local_timestamp1/g' $ref_file | \
  259. sed 's/<timestamp2>/$local_timestamp2/g' | \
  260. sed 's/<timestamp3>/$local_timestamp3/g' "
  261. eval $sedstr >$ref_file_tmp
  262. ERRCODE=$?
  263. assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
  264. # Compare the dump output to reference file, they should be identical
  265. cmp -s $output_file $ref_file_tmp
  266. ERRCODE=$?
  267. assert_eq 0 $ERRCODE "dump file does not match reference file"
  268. # Remove the output file and temporary reference file
  269. rm $output_file
  270. rm $ref_file_tmp
  271. # Let's wipe the whole database
  272. pgsql_wipe
  273. test_finish 0
  274. }
  275. pgsql_lease_init_test
  276. pgsql_lease_version_test
  277. pgsql_upgrade_test
  278. pgsql_lease4_dump_test
  279. pgsql_lease6_dump_test