dhcp_test_lib.sh.in 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. # Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. # PERFORMANCE OF THIS SOFTWARE.
  14. # A list of Kea processes, mainly used by the cleanup functions.
  15. KEA_PROCS="kea-dhcp4 kea-dhcp6 kea-dhcp-ddns"
  16. ### Logging functions ###
  17. # Prints error message.
  18. test_lib_error() {
  19. local s=${1} # Error message.
  20. local no_new_line=${2} # If specified, the message not terminated with
  21. # new line.
  22. printf "ERROR/test_lib: %s" "${s}"
  23. if [ -z ${no_new_line} ]; then
  24. printf "%s" "\n"
  25. fi
  26. }
  27. # Prints info message.
  28. test_lib_info() {
  29. local s=${1} # Info message.
  30. local no_new_line=${2} # If specified, the message is not terminated with
  31. # new line.
  32. printf "INFO/test_lib: %s" "${s}"
  33. if [ -z ${no_new_line} ]; then
  34. printf "%s" "\n"
  35. fi
  36. }
  37. ### Assertions ###
  38. # Assertion that checks if two numbers are equal.
  39. # If numbers are not equal, the mismatched values are presented and the
  40. # detailed error is printed. The detailed error must use the printf
  41. # formatting like this:
  42. # "Expected that some value 1 %d is equal to some other value %d".
  43. assert_eq() {
  44. val1=${1} # Reference value
  45. val2=${2} # Tested value
  46. detailed_err=${3} # Detailed error format string
  47. # If nothing found, present an error an exit.
  48. if [ ${val1} -ne ${val2} ]; then
  49. printf "Assertion failure: ${val1} != ${val2}, for val1=${val1}, val2=${val2}\n"
  50. printf "${detailed_err}\n" ${val1} ${val2}
  51. clean_exit 1
  52. fi
  53. }
  54. # Assertion that checks if two strings are equal.
  55. # If numbers are not equal, the mismatched values are presented and the
  56. # detailed error is printed. The detailed error must use the printf
  57. # formatting like this:
  58. # "Expected that some value 1 %d is equal to some other value %d".
  59. assert_str_eq() {
  60. val1=${1} # Reference value
  61. val2=${2} # Tested value
  62. detailed_err=${3} # Detailed error format string
  63. # If nothing found, present an error an exit.
  64. if [ "${val1}" != "${val2}" ]; then
  65. printf "Assertion failure: ${val1} != ${val2}, for val1=${val1}, val2=${val2}\n"
  66. printf "${detailed_err}\n" ${val1} ${val2}
  67. clean_exit 1
  68. fi
  69. }
  70. # Assertion that checks if one string contains another string.
  71. # If assertion fails, both strings are displayed and the detailed
  72. # error is printed. The detailed error must use the printf formatting
  73. # like this:
  74. # "Expected some string to contain this string: %s".
  75. assert_string_contains() {
  76. pattern="${1}" # Substring or awk pattern
  77. text="${2}" # Text to be searched for substring
  78. detailed_err="${3}" # Detailed error format string
  79. # Search for a pattern
  80. match=$( printf "%s" "${text}" | awk /"${pattern}"/ )
  81. # If nothing found, present an error and exit.
  82. if [ -z "${match}" ]; then
  83. printf "Assertion failure: \n\"%s\"\n\ndoesn't contain pattern:\n
  84. \"%s\"\n\n" "${text}" "${pattern}"
  85. printf "${detailed_err}\n" "\"${pattern}\""
  86. clean_exit 1
  87. fi
  88. }
  89. # Begins a test by prining its name.
  90. test_start() {
  91. TEST_NAME=${1}
  92. if [ -z ${TEST_NAME} ]; then
  93. test_lib_error "test_start requires test name as an argument"
  94. clean_exit 1
  95. fi
  96. printf "\nSTART TEST ${TEST_NAME}\n"
  97. }
  98. # Prints test result an cleans up after the test.
  99. test_finish() {
  100. local exit_code=${1} # Exit code to be returned by the exit function.
  101. if [ ${exit_code} -eq 0 ]; then
  102. cleanup
  103. printf "PASSED ${TEST_NAME}\n\n"
  104. else
  105. # Dump log file for debugging purposes if specified and exists.
  106. # Otherwise the code below would simply call cat.
  107. if [ -n "${LOG_FILE}" -a -s "${LOG_FILE}" ]; then
  108. printf "Log file dump:\n"
  109. cat ${LOG_FILE}
  110. fi
  111. cleanup
  112. printf "FAILED ${TEST_NAME}\n\n"
  113. fi
  114. }
  115. # Stores the configuration specified as a parameter in the configuration
  116. # file which name has been set in the ${CFG_FILE} variable.
  117. create_config() {
  118. local cfg="${1}" # Configuration string.
  119. if [ -z ${CFG_FILE} ]; then
  120. test_lib_error "create_config requires CFG_FILE variable be set"
  121. clean_exit 1
  122. elif [ -z "${cfg}" ]; then
  123. test_lib_error "create_config requires argument holding a configuration"
  124. clean_exit 1
  125. fi
  126. printf "Creating Kea configuration file: %s.\n" ${CFG_FILE}
  127. printf "%b" ${cfg} > ${CFG_FILE}
  128. }
  129. # Stores the keactrl configuration specified as a parameter in the
  130. # configuration file which name has been set in the ${KEACTRL_CFG_FILE}
  131. # variable.
  132. create_keactrl_config() {
  133. local cfg="${1}" # Configuration string.
  134. if [ -z ${KEACTRL_CFG_FILE} ]; then
  135. test_lib_error "create_keactrl_config requires KEACTRL_CFG_FILE \
  136. variable be set"
  137. clean_exit 1
  138. elif [ -z "${cfg}" ]; then
  139. test_lib_error "create_keactrl_config requires argument holding a \
  140. configuration"
  141. clean_exit 1
  142. fi
  143. printf "Creating keactrl configuration file: %s.\n" ${KEACTRL_CFG_FILE}
  144. printf "%b" ${cfg} > ${KEACTRL_CFG_FILE}
  145. }
  146. # Sets Kea logger to write to the file specified by the global value
  147. # ${LOG_FILE}.
  148. set_logger() {
  149. if [ -z ${LOG_FILE} ]; then
  150. test_lib_error "set_logger requies LOG_FILE variable be set"
  151. clean_exit 1
  152. fi
  153. printf "Kea log will be stored in %s.\n" ${LOG_FILE}
  154. export KEA_LOGGER_DESTINATION=${LOG_FILE}
  155. }
  156. # PID file path is by default var/kea, but can be overriden by the
  157. # environmental variable.
  158. PID_FILE_PATH=@localstatedir@/@PACKAGE@/
  159. if [ ! -z ${KEA_PIDFILE_DIR} ]; then
  160. PID_FILE_PATH="${KEA_PIDFILE_DIR}"
  161. fi
  162. # Checks if specified process is running.
  163. #
  164. # This function uses PID file to obtain the PID and then calls
  165. # 'kill -0 <pid>' to check if the process is alive.
  166. # The PID files are expected to be located in the ${PID_FILE_PATH},
  167. # and their names are should follow the follwing pattern:
  168. # <cfg_file_name>.<proc_name>.pid. If the <cfg_file_name> is not
  169. # specified a 'test_config' is used by default.
  170. #
  171. # Return value:
  172. # _GET_PID: holds a PID if process is running
  173. # _GET_PIDS_NUM: holds 1 if process is running, 0 otherwise
  174. get_pid() {
  175. local proc_name=${1} # Process name
  176. local cfg_file_name=${2} # Configuration file name without extension.
  177. # PID file name includes process name. The process name is required.
  178. if [ -z ${proc_name} ]; then
  179. test_lib_error "get_pid requires process name"
  180. clean_exit 1
  181. fi
  182. # PID file name includes server configuration file name. For most of
  183. # the tests it is 'test-config' (excluding .json extension). It is
  184. # possible to specify custom name if required.
  185. if [ -z ${cfg_file_name} ]; then
  186. cfg_file_name="test_config"
  187. fi
  188. # Get the absolute location of the PID file for the specified process
  189. # name.
  190. abs_pidfile_path="${PID_FILE_PATH}/${cfg_file_name}.${proc_name}.pid"
  191. _GET_PID=0
  192. _GET_PIDS_NUM=0
  193. # If the PID file exists, get the PID and see if the process is alive.
  194. if [ -e ${abs_pidfile_path} ]; then
  195. pid=$( cat $abs_pidfile_path )
  196. kill -0 ${pid} > /dev/null 2>&1
  197. if [ $? -eq 0 ]; then
  198. _GET_PID=${pid}
  199. _GET_PIDS_NUM=1
  200. fi
  201. fi
  202. }
  203. # Returns the number of occurrences of the Kea log message in the log file.
  204. # Return value:
  205. # _GET_LOG_MESSAGES: number of log message occurrences.
  206. get_log_messages() {
  207. local msg="${1}" # Message id, e.g. DHCP6_SHUTDOWN
  208. if [ -z ${msg} ]; then
  209. test_lib_error "get_log_messages require message identifier"
  210. clean_exit 1
  211. fi
  212. _GET_LOG_MESSAGES=0
  213. # If log file is not present, the number of occurrences is 0.
  214. if [ -s ${LOG_FILE} ]; then
  215. # Grep log file for the logger message occurrences and remove
  216. # whitespaces, if any.
  217. _GET_LOG_MESSAGES=$( grep -o ${msg} ${LOG_FILE} | wc -w | tr -d " ")
  218. fi
  219. }
  220. # Returns the number of server configurations performed so far. Also
  221. # returns the number of configuration errors.
  222. # Return values:
  223. # _GET_RECONFIGS: number of configurations so far.
  224. # _GET_RECONFIG_ERRORS: number of configuration errors.
  225. get_reconfigs() {
  226. # Grep log file for CONFIG_COMPLETE occurences. There should
  227. # be one occurence per (re)configuration.
  228. _GET_RECONFIGS=$( grep -o CONFIG_COMPLETE ${LOG_FILE} | wc -w )
  229. # Grep log file for CONFIG_LOAD_FAIL to check for configuration
  230. # failures.
  231. _GET_RECONFIG_ERRORS=$( grep -o CONFIG_LOAD_FAIL ${LOG_FILE} | wc -w )
  232. # Remove whitespaces
  233. ${_GET_RECONFIGS##*[! ]}
  234. ${_GET_RECONFIG_ERRORS##*[! ]}
  235. }
  236. # Performs cleanup after test.
  237. # It shuts down running Kea processes and removes temporary files.
  238. # The location of the log file and the configuration files should be set
  239. # in the ${LOG_FILE}, ${CFG_FILE} and ${KEACTRL_CFG_FILE} variables
  240. # recpectively, prior to calling this function.
  241. cleanup() {
  242. # If there is no KEA_PROCS set, just return
  243. if [ -z "${KEA_PROCS}" ]; then
  244. return
  245. fi
  246. # KEA_PROCS holds the name of all Kea processes. Shut down each
  247. # of them if running.
  248. for proc_name in ${KEA_PROCS}
  249. do
  250. get_pid ${proc_name}
  251. # Shut down running Kea process.
  252. if [ ${_GET_PIDS_NUM} -ne 0 ]; then
  253. printf "Shutting down Kea proccess having pid %d.\n" ${_GET_PID}
  254. kill -9 ${_GET_PID}
  255. fi
  256. done
  257. # Remove temporary files.
  258. rm -rf ${LOG_FILE}
  259. # Use asterisk to remove all files starting with the given name,
  260. # in case the LFC has been run. LFC creates files with postfixes
  261. # appended to the lease file name.
  262. if [ ! -z "${LEASE_FILE}" ]; then
  263. rm -rf ${LEASE_FILE}*
  264. fi
  265. rm -rf ${CFG_FILE}
  266. rm -rf ${KEACTRL_CFG_FILE}
  267. }
  268. # Exists the test in the clean way.
  269. # It peformes the cleanup and prints whether the test has passed or failed.
  270. # If a test fails, the Kea log is dumped.
  271. clean_exit() {
  272. exit_code=${1} # Exit code to be returned by the exit function.
  273. case ${exit_code} in
  274. ''|*[!0-9]*)
  275. test_lib_error "argument passed to clean_exit must be a number" ;;
  276. esac
  277. # Print test result and perform a cleanup
  278. test_finish ${exit_code}
  279. exit ${exit_code}
  280. }
  281. # Starts Kea process in background using a configuration file specified
  282. # in the global variable ${CFG_FILE}.
  283. start_kea() {
  284. local bin=${1}
  285. if [ -z ${bin} ]; then
  286. test_lib_error "binary name must be specified for start_kea"
  287. clean_exit 1
  288. fi
  289. printf "Running command %s.\n" "\"${bin} -c ${CFG_FILE}\""
  290. ${bin} -c ${CFG_FILE} &
  291. }
  292. # Waits with timeout for Kea to start.
  293. # This function repeatedly checs if the Kea log file has been created
  294. # and is non-empty. If it is, the function assumes that Kea has started.
  295. # It doesn't check the contents of the log file though.
  296. # If the log file doesn't exist the function sleeps for a second and
  297. # checks again. This is repeated until timeout is reached or non-empty
  298. # log file is found. If timeout is reached, the function reports an
  299. # error.
  300. # Return value:
  301. # _WAIT_FOR_KEA: 0 if Kea hasn't started, 1 otherwise
  302. wait_for_kea() {
  303. local timeout=${1} # Desired timeout in seconds.
  304. case ${timeout} in
  305. ''|*[!0-9]*)
  306. test_lib_error "argument passed to wait_for_kea must be a number"
  307. clean_exit 1 ;;
  308. esac
  309. local loops=0 # Loops counter
  310. _WAIT_FOR_KEA=0
  311. test_lib_info "wait_for_kea " "skip-new-line"
  312. while [ ! -s ${LOG_FILE} ] && [ ${loops} -le ${timeout} ]; do
  313. printf "."
  314. sleep 1
  315. loops=$( expr $loops + 1 )
  316. done
  317. printf "\n"
  318. if [ ${loops} -le ${timeout} ]; then
  319. _WAIT_FOR_KEA=1
  320. fi
  321. }
  322. # Waits for a specific message to occur in the Kea log file.
  323. # This function is called when the test expects specific message
  324. # to show up in the log file as a result of some action that has
  325. # been taken. Typically, the test expects that the message
  326. # is logged when the SIGHUP or SIGTERM signal has been sent to the
  327. # Kea process.
  328. # This function waits a specified number of seconds for the number
  329. # of message occurrences to show up. If the expected number of
  330. # message doesn't occur, the error status is returned.
  331. # Return value:
  332. # _WAIT_FOR_MESSAGE: 0 if the message hasn't occured, 1 otherwise.
  333. wait_for_message() {
  334. local timeout=${1} # Expected timeout value in seconds.
  335. local message="${2}" # Expected message id.
  336. local occurrences=${3} # Number of expected occurrences.
  337. # Validate timeout
  338. case ${timeout} in
  339. ''|*[!0-9]*)
  340. test_lib_error "argument timeout passed to wait_for_message must \
  341. be a number"
  342. clean_exit 1 ;;
  343. esac
  344. # Validate message
  345. if [ -z ${message} ]; then
  346. test_lib_error "message id is a required argument for wait_for_message"
  347. clean_exit 1
  348. fi
  349. # Validate occurrences
  350. case ${occurrences} in
  351. ''|*[!0-9]*)
  352. test_lib_error "argument occurrences passed to wait_for_message \
  353. must be a number"
  354. clean_exit 1 ;;
  355. esac
  356. local loops=0 # Number of loops performed so far.
  357. _WAIT_FOR_MESSAGE=0
  358. test_lib_info "wait_for_message ${message}: " "skip-new-line"
  359. # Check if log file exists and if we reached timeout.
  360. while [ ${loops} -le ${timeout} ]; do
  361. printf "."
  362. # Check if the message has been logged.
  363. get_log_messages ${message}
  364. if [ ${_GET_LOG_MESSAGES} -ge ${occurrences} ]; then
  365. printf "\n"
  366. _WAIT_FOR_MESSAGE=1
  367. return
  368. fi
  369. # Message not recorded. Keep going.
  370. sleep 1
  371. loops=$( expr ${loops} + 1 )
  372. done
  373. printf "\n"
  374. # Timeout.
  375. }
  376. # Waits for server to be down.
  377. # Return value:
  378. # _WAIT_FOR_SERVER_DOWN: 1 if server is down, 0 if timeout occured and the
  379. # server is still running.
  380. wait_for_server_down() {
  381. local timeout=${1} # Timeout specified in seconds.
  382. local proc_name=${2} # Server process name.
  383. case ${timeout} in
  384. ''|*[!0-9]*)
  385. test_lib_error "argument passed to wait_for_server_down must be a number"
  386. clean_exit 1 ;;
  387. esac
  388. local loops=0 # Loops counter
  389. _WAIT_FOR_SERVER_DOWN=0
  390. test_lib_info "wait_for_server_down ${proc_name}: " "skip-new-line"
  391. while [ ${loops} -le ${timeout} ]; do
  392. printf "."
  393. get_pid ${proc_name}
  394. if [ ${_GET_PIDS_NUM} -eq 0 ]; then
  395. printf "\n"
  396. _WAIT_FOR_SERVER_DOWN=1
  397. return
  398. fi
  399. sleep 1
  400. loops=$( expr $loops + 1 )
  401. done
  402. printf "\n"
  403. }
  404. # Sends specified signal to the Kea process.
  405. send_signal() {
  406. local sig=${1} # Signal number.
  407. local proc_name=${2} # Process name
  408. # Validate signal
  409. case ${sig} in
  410. ''|*[!0-9]*)
  411. test_lib_error "signal number passed to send_signal \
  412. must be a number"
  413. clean_exit 1 ;;
  414. esac
  415. # Validate process name
  416. if [ -z ${proc_name} ]; then
  417. test_lib_error "send_signal requires process name be passed as argument"
  418. clean_exit 1
  419. fi
  420. # Get Kea pid.
  421. get_pid ${proc_name}
  422. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  423. printf "ERROR: expected one Kea process to be started.\
  424. Found %d processes started.\n" ${_GET_PIDS_NUM}
  425. clean_exit 1
  426. fi
  427. printf "Sending signal ${sig} to Kea process (pid=%s).\n" ${_GET_PID}
  428. # Actually send a signal.
  429. kill -${sig} ${_GET_PID}
  430. }
  431. # Verifies that a server is up running by its PID file
  432. # The PID file is constructed from the given config file name and
  433. # binary name. If it exists and the PID it contains refers to a
  434. # live process it sets _SERVER_PID_FILE and _SERVER_PID to the
  435. # corresponding values. Otherwise, it emits an error and exits.
  436. verify_server_pid() {
  437. local bin_name="${1}" # binary name of the server
  438. local cfg_file="${2}" # config file name
  439. # We will construct the PID file name based on the server config
  440. # and binary name
  441. if [ -z ${bin_name} ]; then
  442. test_lib_error "verify_server_pid requires binary name"
  443. clean_exit 1
  444. fi
  445. if [ -z ${cfg_file} ]; then
  446. test_lib_error "verify_server_pid requires config file name"
  447. clean_exit 1
  448. fi
  449. # Only the file name portion of the config file is used, try and
  450. # extract it. NOTE if this "algorithm" changes this code will need
  451. # to be updated.
  452. fname=`basename ${cfg_file}`
  453. fname=`echo $fname | cut -f1 -d'.'`
  454. if [ -z ${fname} ]; then
  455. test_lib_error "verify_server_pid could not extract config name"
  456. clean_exit 1
  457. fi
  458. # Now we can build the name:
  459. pid_file="$KEA_PIDFILE_DIR/$fname.$bin_name.pid"
  460. if [ ! -e ${pid_file} ]; then
  461. printf "ERROR: PID file:[%s] does not exist\n" ${pid_file}
  462. clean_exit 1
  463. fi
  464. # File exists, does its PID point to a live process?
  465. pid=`cat ${pid_file}`
  466. kill -0 ${pid}
  467. if [ $? -ne 0 ]; then
  468. printf "ERROR: PID file:[%s] exists but PID:[%d] does not\n" \
  469. ${pid_file} ${pid}
  470. clean_exit 1
  471. fi
  472. # Make the values accessible to the caller
  473. _SERVER_PID="${pid}"
  474. _SERVER_PID_FILE="${pid_file}"
  475. }
  476. # This test verifies that the binary is reporting its version properly.
  477. version_test() {
  478. test_name=${1} # Test name
  479. # Log the start of the test and print test name.
  480. test_start ${test_name}
  481. # Remove dangling Kea instances and remove log files.
  482. cleanup
  483. REPORTED_VERSION="`${bin_path}/${bin} -v`"
  484. if test "${REPORTED_VERSION}" == "${EXPECTED_VERSION}"; then
  485. test_finish 0
  486. else
  487. printf "ERROR: Expected version ${EXPECTED_VERSION}, got ${REPORTED_VERSION}\n"
  488. test_finish 1
  489. fi
  490. }
  491. # This test verifies that the server is using logger variable
  492. # KEA_LOCKFILE_DIR properly (it should be used to point out to the directory,
  493. # where lockfile should be created. Also, "none" value means to not create
  494. # the lockfile at all).
  495. logger_vars_test() {
  496. test_name=${1} # Test name
  497. # Log the start of the test and print test name.
  498. test_start ${test_name}
  499. # Remove dangling Kea instances and remove log files.
  500. cleanup
  501. # Create bogus configuration file. We don't really want the server to start,
  502. # just want it to log something and die. Empty config is an easy way to
  503. # enforce that behavior.
  504. create_config "{ }"
  505. printf "Please ignore any config error messages.\n"
  506. # Remember old KEA_LOCKFILE_DIR
  507. KEA_LOCKFILE_DIR_OLD=${KEA_LOCKFILE_DIR}
  508. # Set lockfile directory to current directory.
  509. KEA_LOCKFILE_DIR=.
  510. # Start Kea.
  511. start_kea ${bin_path}/${bin}
  512. # Wait for Kea to process the invalid configuration and die.
  513. sleep 1
  514. # Check if it is still running. It should have terminated.
  515. get_pid ${bin}
  516. if [ ${_GET_PIDS_NUM} -ne 0 ]; then
  517. printf "ERROR: expected Kea process to not start. Found %d processes"
  518. printf " running.\n" ${_GET_PIDS_NUM}
  519. # Revert to the old KEA_LOCKFILE_DIR value
  520. KEA_LOCKFILE_DIR=${KEA_LOCKFILE_DIR_OLD}
  521. clean_exit 1
  522. fi
  523. if [ ! -f "./logger_lockfile" ]; then
  524. printf "ERROR: Expect ${bin} to create logger_lockfile in the\n"
  525. printf "current directory, but no such file exists.\n"
  526. # Revert to the old KEA_LOCKFILE_DIR value
  527. KEA_LOCKFILE_DIR=${KEA_LOCKFILE_DIR__OLD}
  528. clean_exit 1
  529. fi
  530. # Remove the lock file
  531. rm -f ./logger_lockfile
  532. # Tell Kea to NOT create logfiles at all
  533. KEA_LOCKFILE_DIR="none"
  534. # Start Kea.
  535. start_kea ${bin_path}/${bin}
  536. # Wait for Kea to process the invalid configuration and die.
  537. sleep 1
  538. # Check if it is still running. It should have terminated.
  539. get_pid ${bin}
  540. if [ ${_GET_PIDS_NUM} -ne 0 ]; then
  541. printf "ERROR: expected Kea process to not start. Found %d processes"
  542. printf " running.\n" ${_GET_PIDS_NUM}
  543. # Revert to the old KEA_LOCKFILE_DIR value
  544. KEA_LOCKFILE_DIR=${KEA_LOCKFILE_DIR_OLD}
  545. clean_exit 1
  546. fi
  547. if [ -f "./logger_lockfile" ]; then
  548. printf "ERROR: Expect ${bin} to NOT create logger_lockfile in the\n"
  549. printf "current directory, but the file exists."
  550. # Revert to the old KEA_LOCKFILE_DIR value
  551. KEA_LOCKFILE_DIR=${KEA_LOCKFILE_DIR_OLD}
  552. clean_exit 1
  553. fi
  554. # Revert to the old KEA_LOCKFILE_DIR value
  555. printf "Reverting KEA_LOCKFILE_DIR to ${KEA_LOCKFILE_DIR_OLD}\n"
  556. KEA_LOCKFILE_DIR=${KEA_LOCKFILE_DIR_OLD}
  557. test_finish 0
  558. }
  559. # This test verifies server PID file management
  560. # 1. It verifies that upon startup, the server creates a PID file
  561. # 2. It verifies the an attempt to start a second instance fails
  562. # due to pre-existing PID File/PID detection
  563. server_pid_file_test() {
  564. local server_cfg="${1}"
  565. local log_id="${2}"
  566. # Log the start of the test and print test name.
  567. test_start "${bin}.server_pid_file_test"
  568. # Remove dangling DHCP4 instances and remove log files.
  569. cleanup
  570. # Create new configuration file.
  571. create_config "${CONFIG}"
  572. # Instruct server to log to the specific file.
  573. set_logger
  574. # Start server
  575. start_kea ${bin_path}/${bin}
  576. # Wait up to 20s for server to start.
  577. wait_for_kea 20
  578. if [ ${_WAIT_FOR_KEA} -eq 0 ]; then
  579. printf "ERROR: timeout waiting for %s to start.\n" ${bin}
  580. clean_exit 1
  581. fi
  582. # Verify server is still running
  583. verify_server_pid ${bin} ${CFG_FILE}
  584. printf "PID file is [%s], PID is [%d]" ${_SERVER_PID_FILE} ${_SERVER_PID}
  585. # Now try to start a second one
  586. start_kea ${bin_path}/${bin}
  587. wait_for_message 10 "${log_id}" 1
  588. if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
  589. printf "ERROR: Second %s instance started? PID conflict not reported.\n" ${bin}
  590. clean_exit 1
  591. fi
  592. # Verify server is still running
  593. verify_server_pid ${bin} ${CFG_FILE}
  594. # All ok. Shut down the server and exit.
  595. test_finish 0
  596. }