dhcp_test_lib.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright (C) 2014 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. # The following two parameters must to be specified in a script
  15. # including this library.
  16. # - BIN - Name of the Kea executable (excluding a path), e.g. b10-dhcp6
  17. # - BIN_PATH - Path to the Kea executable (excluding an executable name),
  18. # e.g. ../
  19. # Begins a test by prining its name.
  20. # It requires the ${TEST_NAME} variable to hold the test name.
  21. test_start() {
  22. printf "\nSTART TEST ${TEST_NAME}\n"
  23. }
  24. # Stores the configuration specified as a parameter in the configuration
  25. # file which name has been set in the ${CFG_FILE} variable.
  26. create_config() {
  27. printf "Creating Kea configuration file: %s.\n" ${CFG_FILE}
  28. printf "%b" ${1} > ${CFG_FILE}
  29. }
  30. # Sets Kea logger to write to the file specified by the global value
  31. # ${LOG_FILE}.
  32. set_logger() {
  33. printf "Kea log will be stored in %s.\n" ${LOG_FILE}
  34. export B10_LOGGER_DESTINATION=${LOG_FILE}
  35. }
  36. # Returns the number of running process pids and the list of pids.
  37. _GET_PIDS= # Return value: holds space separated list of DHCPv6 pids.
  38. _GET_PIDS_NUM= # Return value: holds the number of DHCPv6 server pids.
  39. get_pids() {
  40. _GET_PIDS=`ps axwwo pid,command | grep ${BIN} | grep -v grep | awk '{print $1}'`
  41. _GET_PIDS_NUM=`printf "%s" "${_GET_PIDS}" | wc -w | awk '{print $1}'`
  42. }
  43. # Returns the number of occurrences of the Kea log message in the
  44. # log file.
  45. _GET_LOG_MESSAGES= # Holds the number of log message occurrences.
  46. get_log_messages() {
  47. # Grep log file for the logger message occurrences.
  48. _GET_LOG_MESSAGES=`grep -o ${1} ${LOG_FILE} | wc -w`
  49. # Remove whitespaces.
  50. ${_GET_LOG_MESSAGES##*[! ]}
  51. }
  52. # Returns the number of server configurations performed so far. Also
  53. # returns the number of configuration errors.
  54. _GET_RECONFIGS= # Return value: number of configurations so far.
  55. _GET_RECONFIG_ERRORS= # Return value: number of configuration errors.
  56. get_reconfigs() {
  57. # Grep log file for DHCP6_CONFIG_COMPLETE occurences. There should
  58. # be one occurence per (re)configuration.
  59. _GET_RECONFIGS=`grep -o DHCP6_CONFIG_COMPLETE ${LOG_FILE} | wc -w`
  60. # Grep log file for DHCP6_CONFIG_LOAD_FAIL to check for configuration
  61. # failures.
  62. _GET_RECONFIG_ERRORS=`grep -o DHCP6_CONFIG_LOAD_FAIL ${LOG_FILE} | wc -w`
  63. # Remove whitespaces
  64. ${_GET_RECONFIGS##*[! ]}
  65. ${_GET_RECONFIG_ERRORS##*[! ]}
  66. }
  67. # Performs cleanup for a test.
  68. # It shuts down running Kea processes and removes temporary files.
  69. # The location of the log file and the configuration file should be set
  70. # in the ${LOG_FILE} and ${CFG_FILE} variables recpectively, prior to
  71. # calling this function.
  72. cleanup() {
  73. get_pids
  74. # Shut down running Kea processes.
  75. for pid in ${_GET_PIDS}
  76. do
  77. printf "Shutting down Kea proccess having pid %d.\n" ${pid}
  78. kill -9 ${pid}
  79. done
  80. # Remove temporary files.
  81. rm -rf ${LOG_FILE}
  82. rm -rf ${CFG_FILE}
  83. }
  84. # Exists the test in the clean way.
  85. # It peformes the cleanup and prints whether the test has passed or failed.
  86. # If a test fails, the Kea log is dumped.
  87. clean_exit() {
  88. exit_code=${1} # Exit code to be returned by the exit function.
  89. if [ ${exit_code} -eq 0 ]; then
  90. cleanup
  91. printf "PASSED ${TEST_NAME}\n\n"
  92. else
  93. # Dump log file if exists for debugging purposes.
  94. if [ -s ${LOG_FILE} ]; then
  95. printf "Log file dump:\n"
  96. cat ${LOG_FILE}
  97. fi
  98. cleanup
  99. printf "FAILED ${TEST_NAME}\n\n"
  100. fi
  101. exit ${exit_code}
  102. }
  103. # Starts Kea process in background using a configuration file specified
  104. # in the global variable ${CFG_FILE}
  105. start_kea() {
  106. printf "Running command %s.\n" "\"${BIN_PATH}/${BIN} -c ${CFG_FILE}\""
  107. ${BIN_PATH}/$BIN -c ${CFG_FILE} &
  108. }
  109. # Waits for Kea to startup with timeout.
  110. # This function repeatedly checs if the Kea log file has been created
  111. # and is non-empty. If it is, the function assumes that Kea has started.
  112. # It doesn't check the contents of the log file though.
  113. # If the log file doesn't exist the function sleeps for a second and
  114. # checks again. This is repeated until timeout is reached or non-empty
  115. # log file is found. If timeout is reached, the function reports an
  116. # error.
  117. _WAIT_FOR_KEA=0 # Return value: Holds 0 if Kea hasn't started, 1 otherwise
  118. wait_for_kea() {
  119. timeout=${1} # Desired timeout in seconds.
  120. loops=0 # Loops counter
  121. _WAIT_FOR_KEA=0
  122. while [ ! -s ${LOG_FILE} ] && [ ${loops} -le ${timeout} ]; do
  123. printf "."
  124. sleep 1
  125. loops=`expr $loops + 1`
  126. done
  127. printf "\n"
  128. if [ ${loops} -le ${timeout} ]; then
  129. _WAIT_FOR_KEA=1
  130. fi
  131. }
  132. # Waits for a specific message to occur in the Kea log file.
  133. # This function is called when the test expects specific message
  134. # to show up in the log file as a result of some action that has
  135. # been taken. Typically, the test expects that the message
  136. # is logged when the SIGHUP or SIGTERM signal has been sent to the
  137. # Kea process.
  138. # This function waits a specified number of seconds for the number
  139. # of message occurrences to show up. If the expected number of
  140. # message doesn't occur, the error status is returned.
  141. _WAIT_FOR_MESSAGE=0 # Return value: holds 0 if the message hasn't occured,
  142. # 1 otherwise.
  143. wait_for_message() {
  144. timeout=${1} # Expecte timeout value in seconds.
  145. message=${2} # Expected message id.
  146. occurrences=${3} # Number of expected occurrences.
  147. loops=0 # Number of loops performed so far.
  148. _WAIT_FOR_MESSAGE=0
  149. # Check if log file exists and if we reached timeout.
  150. while [ ! -s {LOG_FILE} ] && [ ${loops} -le ${timeout} ]; do
  151. printf "."
  152. # Check if the message has been logged.
  153. get_log_messages ${message}
  154. if [ ${_GET_LOG_MESSAGES} -eq ${occurrences} ]; then
  155. printf "\n"
  156. _WAIT_FOR_MESSAGE=1
  157. return
  158. fi
  159. # Message not recorded. Keep going.
  160. sleep 1
  161. loops=`expr ${loops} + 1`
  162. done
  163. printf "\n"
  164. # Timeout.
  165. }
  166. # Sends specified signal to the Kea process.
  167. send_signal() {
  168. sig=${1} # Signal number.
  169. # Get Kea pid.
  170. get_pids
  171. if [ ${_GET_PIDS_NUM} -ne 1 ]; then
  172. printf "ERROR: expected one Kea process to be started. Found %d processes started.\n" ${_GET_PIDS_NUM}
  173. clean_exit 1
  174. fi
  175. printf "Sending signal ${sig} to Kea process (pid=%s).\n" ${_GET_PIDS}
  176. # Actually send a signal.
  177. kill -${sig} ${_GET_PIDS}
  178. }