dhcp4_process_tests.sh.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. # Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. # Path to the temporary configuration file.
  7. CFG_FILE=@abs_top_builddir@/src/bin/dhcp4/tests/test_config.json
  8. # Path to the Kea log file.
  9. LOG_FILE=@abs_top_builddir@/src/bin/dhcp4/tests/test.log
  10. # Path to the Kea lease file.
  11. LEASE_FILE=@abs_top_builddir@/src/bin/dhcp4/tests/test_leases.csv
  12. # Path to the Kea LFC application
  13. export KEA_LFC_EXECUTABLE=@abs_top_builddir@/src/bin/lfc/kea-lfc
  14. # Expected version
  15. EXPECTED_VERSION="@PACKAGE_VERSION@"
  16. # Kea configuration to be stored in the configuration file.
  17. CONFIG="{
  18. \"Dhcp4\":
  19. {
  20. \"interfaces-config\": {
  21. \"interfaces\": [ ]
  22. },
  23. \"valid-lifetime\": 4000,
  24. \"renew-timer\": 1000,
  25. \"rebind-timer\": 2000,
  26. \"lease-database\":
  27. {
  28. \"type\": \"memfile\",
  29. \"name\": \"$LEASE_FILE\",
  30. \"persist\": false,
  31. \"lfc-interval\": 0
  32. },
  33. \"subnet4\": [
  34. {
  35. \"subnet\": \"10.0.0.0/8\",
  36. \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]
  37. } ],
  38. \"dhcp-ddns\": {
  39. \"enable-updates\": true,
  40. \"qualifying-suffix\": \"\"
  41. }
  42. },
  43. \"Logging\":
  44. {
  45. \"loggers\": [
  46. {
  47. \"name\": \"kea-dhcp4\",
  48. \"output_options\": [
  49. {
  50. \"output\": \"$LOG_FILE\"
  51. }
  52. ],
  53. \"severity\": \"INFO\"
  54. }
  55. ]
  56. }
  57. }"
  58. # Invalid configuration (syntax error) to check that Kea can check syntax.
  59. CONFIG_BAD_SYNTAX="{
  60. \"Dhcp4\":
  61. {
  62. \"interfaces\": [ ],
  63. \"valid-lifetime\": 4000,
  64. \"renew-timer\": 1000,
  65. \"rebind-timer\": 2000,
  66. \"lease-database\":
  67. {
  68. \"type\": \"memfile\",
  69. \"persist\": false
  70. },
  71. \"subnet4\": [
  72. {
  73. \"subnet\": \"10.0.0.0/8\",
  74. \"pool\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]
  75. } ]
  76. },
  77. \"Logging\":
  78. {
  79. \"loggers\": [
  80. {
  81. \"name\": \"kea-dhcp4\",
  82. \"output_options\": [
  83. {
  84. \"output\": \"$LOG_FILE\"
  85. }
  86. ],
  87. \"severity\": \"INFO\"
  88. }
  89. ]
  90. }
  91. }"
  92. # Invalid configuration (negative valid-lifetime) to check that Kea
  93. # gracefully handles reconfiguration errors.
  94. CONFIG_INVALID="{
  95. \"Dhcp4\":
  96. {
  97. \"interfaces-config\": {
  98. \"interfaces\": [ ]
  99. },
  100. \"valid-lifetime\": -3,
  101. \"renew-timer\": 1000,
  102. \"rebind-timer\": 2000,
  103. \"lease-database\":
  104. {
  105. \"type\": \"memfile\",
  106. \"persist\": false
  107. },
  108. \"subnet4\": [
  109. {
  110. \"subnet\": \"10.0.0.0/8\",
  111. \"pool\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]
  112. } ]
  113. },
  114. \"Logging\":
  115. {
  116. \"loggers\": [
  117. {
  118. \"name\": \"kea-dhcp4\",
  119. \"output_options\": [
  120. {
  121. \"output\": \"$LOG_FILE\"
  122. }
  123. ],
  124. \"severity\": \"INFO\"
  125. }
  126. ]
  127. }
  128. }"
  129. # Set the location of the executable.
  130. bin="kea-dhcp4"
  131. bin_path=@abs_top_builddir@/src/bin/dhcp4
  132. # Import common test library.
  133. . @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh
  134. # This test verifies that syntax check works properly.
  135. syntax_check_test() {
  136. # Log the start of the test and print test name.
  137. test_start "dhcpv4_srv.syntax_check"
  138. # Remove dangling Kea instances and remove log files.
  139. cleanup
  140. # Create correct configuration file.
  141. create_config "${CONFIG}"
  142. # Check it
  143. printf "Running command %s.\n" "\"${bin_path}/${bin} -t -c ${CFG_FILE}\""
  144. ${bin_path}/${bin} -t -c ${CFG_FILE}
  145. exit_code=$?
  146. if [ ${exit_code} -ne 0 ]; then
  147. printf "ERROR: expected exit code 0, got ${exit_code}\n"
  148. clean_exit 1
  149. fi
  150. # Create incorrect configuration file.
  151. create_config "${CONFIG_BAD_SYNTAX}"
  152. # Check it
  153. printf "Running command %s.\n" "\"${bin_path}/${bin} -t -c ${CFG_FILE}\""
  154. printf "A syntax error should be detected\n"
  155. ${bin_path}/${bin} -t -c ${CFG_FILE}
  156. if [ $? -eq 0 ]; then
  157. printf "ERROR: expected exit code not 0, got 0\n"
  158. clean_exit 1
  159. fi
  160. # All ok
  161. clean_exit 0
  162. }
  163. # This test verifies that DHCPv4 can be reconfigured with a SIGHUP signal.
  164. dynamic_reconfiguration_test() {
  165. # Log the start of the test and print test name.
  166. test_start "dhcpv4_srv.dynamic_reconfiguration"
  167. # Remove dangling Kea instances and remove log files.
  168. cleanup
  169. # Create new configuration file.
  170. create_config "${CONFIG}"
  171. # Instruct Kea to log to the specific file.
  172. set_logger
  173. # Start Kea.
  174. start_kea ${bin_path}/${bin}
  175. # Wait up to 20s for Kea to start.
  176. wait_for_kea 20
  177. if [ ${_WAIT_FOR_KEA} -eq 0 ]; then
  178. printf "ERROR: timeout waiting for Kea to start.\n"
  179. clean_exit 1
  180. fi
  181. # Check if it is still running. It could have terminated (e.g. as a result
  182. # of configuration failure).
  183. get_pid ${bin}
  184. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  185. printf "ERROR: expected one Kea process to be started. Found %d processes\
  186. started.\n" ${_GET_PIDS_NUM}
  187. clean_exit 1
  188. fi
  189. # Check in the log file, how many times server has been configured. It should
  190. # be just once on startup.
  191. get_reconfigs
  192. if [ ${_GET_RECONFIGS} -ne 1 ]; then
  193. printf "ERROR: server hasn't been configured.\n"
  194. clean_exit 1
  195. else
  196. printf "Server successfully configured.\n"
  197. fi
  198. # Now use invalid configuration.
  199. create_config "${CONFIG_INVALID}"
  200. # Try to reconfigure by sending SIGHUP
  201. send_signal 1 ${bin}
  202. # The configuration should fail and the error message should be there.
  203. wait_for_message 10 "DHCP4_CONFIG_LOAD_FAIL" 1
  204. # After receiving SIGHUP the server should try to reconfigure itself.
  205. # The configuration provided is invalid so it should result in
  206. # reconfiguration failure but the server should still be running.
  207. get_reconfigs
  208. if [ ${_GET_RECONFIGS} -ne 1 ]; then
  209. printf "ERROR: server has been reconfigured despite bogus configuration.\n"
  210. clean_exit 1
  211. elif [ ${_GET_RECONFIG_ERRORS} -ne 1 ]; then
  212. printf "ERROR: server did not report reconfiguration error despite attempt\
  213. to configure it with invalid configuration.\n"
  214. clean_exit 1
  215. fi
  216. # Make sure the server is still operational.
  217. get_pid ${bin}
  218. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  219. printf "ERROR: Kea process was killed when attempting reconfiguration.\n"
  220. clean_exit 1
  221. fi
  222. # Restore the good configuration.
  223. create_config "${CONFIG}"
  224. # Reconfigure the server with SIGHUP.
  225. send_signal 1 ${bin}
  226. # There should be two occurrences of the DHCP4_CONFIG_COMPLETE messages.
  227. # Wait for it up to 10s.
  228. wait_for_message 10 "DHCP4_CONFIG_COMPLETE" 2
  229. # After receiving SIGHUP the server should get reconfigured and the
  230. # reconfiguration should be noted in the log file. We should now
  231. # have two configurations logged in the log file.
  232. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  233. printf "ERROR: server hasn't been reconfigured.\n"
  234. clean_exit 1
  235. else
  236. printf "Server successfully reconfigured.\n"
  237. fi
  238. # Make sure the server is still operational.
  239. get_pid ${bin}
  240. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  241. printf "ERROR: Kea process was killed when attempting reconfiguration.\n"
  242. clean_exit 1
  243. fi
  244. # When the server receives a signal the call to select() function is
  245. # interrupted. This should not be logged as an error.
  246. get_log_messages "DHCP4_PACKET_RECEIVE_FAIL"
  247. assert_eq 0 ${_GET_LOG_MESSAGES} \
  248. "Expected get_log_messages DHCP4_PACKET_RECEIVE_FAIL return %d, \
  249. returned %d."
  250. # All ok. Shut down Kea and exit.
  251. test_finish 0
  252. }
  253. # This test verifies that DHCPv4 server is shut down gracefully when it
  254. # receives a SIGINT or SIGTERM signal.
  255. shutdown_test() {
  256. test_name=${1} # Test name
  257. signum=${2} # Signal number
  258. # Log the start of the test and print test name.
  259. test_start ${test_name}
  260. # Remove dangling Kea instances and remove log files.
  261. cleanup
  262. # Create new configuration file.
  263. create_config "${CONFIG}"
  264. # Instruct Kea to log to the specific file.
  265. set_logger
  266. # Start Kea.
  267. start_kea ${bin_path}/${bin}
  268. # Wait up to 20s for Kea to start.
  269. wait_for_kea 20
  270. if [ ${_WAIT_FOR_KEA} -eq 0 ]; then
  271. printf "ERROR: timeout waiting for Kea to start.\n"
  272. clean_exit 1
  273. fi
  274. # Check if it is still running. It could have terminated (e.g. as a result
  275. # of configuration failure).
  276. get_pid ${bin}
  277. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  278. printf "ERROR: expected one Kea process to be started. Found %d processes\
  279. started.\n" ${_GET_PIDS_NUM}
  280. clean_exit 1
  281. fi
  282. # Check in the log file, how many times server has been configured. It should
  283. # be just once on startup.
  284. get_reconfigs
  285. if [ ${_GET_RECONFIGS} -ne 1 ]; then
  286. printf "ERROR: server hasn't been configured.\n"
  287. clean_exit 1
  288. else
  289. printf "Server successfully configured.\n"
  290. fi
  291. # Send signal to Kea (SIGTERM, SIGINT etc.)
  292. send_signal ${signum} ${bin}
  293. # Wait up to 10s for the server's graceful shutdown. The graceful shut down
  294. # should be recorded in the log file with the appropriate message.
  295. wait_for_message 10 "DHCP4_SHUTDOWN" 1
  296. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  297. printf "ERROR: Server did not record shutdown in the log.\n"
  298. clean_exit 1
  299. fi
  300. # Make sure the server is down.
  301. wait_for_server_down 5 ${bin}
  302. assert_eq 1 ${_WAIT_FOR_SERVER_DOWN} \
  303. "Expected wait_for_server_down return %d, returned %d"
  304. # When the server receives a signal the call to select() function is
  305. # interrupted. This should not be logged as an error.
  306. get_log_messages "DHCP4_PACKET_RECEIVE_FAIL"
  307. assert_eq 0 ${_GET_LOG_MESSAGES} \
  308. "Expected get_log_messages return %d, returned %d."
  309. test_finish 0
  310. }
  311. # This test verifies that DHCPv4 can be configured to run lease file cleanup
  312. # periodially.
  313. lfc_timer_test() {
  314. # Log the start of the test and print test name.
  315. test_start "dhcpv4_srv.lfc_timer_test"
  316. # Remove dangling Kea instances and remove log files.
  317. cleanup
  318. # Create a configuration with the LFC enabled, by replacing the section
  319. # with the lfc-interval and persist parameters.
  320. LFC_CONFIG=$(printf "${CONFIG}" | sed -e 's/\"lfc-interval\": 0/\"lfc-interval\": 3/g' \
  321. | sed -e 's/\"persist\": false/\"persist\": true/g')
  322. # Create new configuration file.
  323. create_config "${LFC_CONFIG}"
  324. # Instruct Kea to log to the specific file.
  325. set_logger
  326. # Start Kea.
  327. start_kea ${bin_path}/${bin}
  328. # Wait up to 20s for Kea to start.
  329. wait_for_kea 20
  330. if [ ${_WAIT_FOR_KEA} -eq 0 ]; then
  331. printf "ERROR: timeout waiting for Kea to start.\n"
  332. clean_exit 1
  333. fi
  334. # Check if it is still running. It could have terminated (e.g. as a result
  335. # of configuration failure).
  336. get_pid ${bin}
  337. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  338. printf "ERROR: expected one Kea process to be started. Found %d processes\
  339. started.\n" ${_GET_PIDS_NUM}
  340. clean_exit 1
  341. fi
  342. # Check if Kea emits the log message indicating that LFC is started.
  343. wait_for_message 10 "DHCPSRV_MEMFILE_LFC_EXECUTE" 1
  344. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  345. printf "ERROR: Server did not execute LFC.\n"
  346. clean_exit 1
  347. fi
  348. # Give it a short time to run.
  349. sleep 1
  350. # Modify the interval.
  351. LFC_CONFIG=$(printf "${LFC_CONFIG}" | sed -e 's/\"lfc-interval\": 3/\"lfc-interval\": 4/g')
  352. # Create new configuration file.
  353. create_config "${LFC_CONFIG}"
  354. # Reconfigure the server with SIGHUP.
  355. send_signal 1 ${bin}
  356. # There should be two occurrences of the DHCP4_CONFIG_COMPLETE messages.
  357. # Wait for it up to 10s.
  358. wait_for_message 10 "DHCP4_CONFIG_COMPLETE" 2
  359. # After receiving SIGHUP the server should get reconfigured and the
  360. # reconfiguration should be noted in the log file. We should now
  361. # have two configurations logged in the log file.
  362. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  363. printf "ERROR: server hasn't been reconfigured.\n"
  364. clean_exit 1
  365. else
  366. printf "Server successfully reconfigured.\n"
  367. fi
  368. # Make sure the server is still operational.
  369. get_pid ${bin}
  370. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  371. printf "ERROR: Kea process was killed when attempting reconfiguration.\n"
  372. clean_exit 1
  373. fi
  374. # Wait for the LFC to run the second time.
  375. wait_for_message 10 "DHCPSRV_MEMFILE_LFC_EXECUTE" 2
  376. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  377. printf "ERROR: Server did not execute LFC.\n"
  378. clean_exit 1
  379. fi
  380. # Send signal to Kea SIGTERM
  381. send_signal 15 ${bin}
  382. # Wait up to 10s for the server's graceful shutdown. The graceful shut down
  383. # should be recorded in the log file with the appropriate message.
  384. wait_for_message 10 "DHCP4_SHUTDOWN" 1
  385. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  386. printf "ERROR: Server did not record shutdown in the log.\n"
  387. clean_exit 1
  388. fi
  389. # Make sure the server is down.
  390. wait_for_server_down 5 ${bin}
  391. assert_eq 1 ${_WAIT_FOR_SERVER_DOWN} \
  392. "Expected wait_for_server_down return %d, returned %d"
  393. # All ok. Shut down Kea and exit.
  394. test_finish 0
  395. }
  396. server_pid_file_test "${CONFIG}" DHCP4_ALREADY_RUNNING
  397. dynamic_reconfiguration_test
  398. shutdown_test "dhcpv4.sigterm_test" 15
  399. shutdown_test "dhcpv4.sigint_test" 2
  400. version_test "dhcpv4.version"
  401. logger_vars_test "dhcpv4.variables"
  402. lfc_timer_test
  403. syntax_check_test