bind10_control.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # Copyright (C) 2011 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from lettuce import *
  16. import subprocess
  17. import re
  18. import json
  19. @step('start bind10(?: with configuration (\S+))?' +\
  20. '(?: with cmdctl port (\d+))?' +\
  21. '(?: with msgq socket file (\S+))?' +\
  22. '(?: as (\S+))?')
  23. def start_bind10(step, config_file, cmdctl_port, msgq_sockfile, process_name):
  24. """
  25. Start BIND 10 with the given optional config file, cmdctl port, and
  26. store the running process in world with the given process name.
  27. Parameters:
  28. config_file ('with configuration <file>', optional): this configuration
  29. will be used. The path is relative to the base lettuce
  30. directory.
  31. cmdctl_port ('with cmdctl port <portnr>', optional): The port on which
  32. b10-cmdctl listens for bindctl commands. Defaults to 47805.
  33. msgq_sockfile ('with msgq socket file', optional): The msgq socket file
  34. that will be used for internal communication
  35. process_name ('as <name>', optional). This is the name that can be used
  36. in the following steps of the scenario to refer to this
  37. BIND 10 instance. Defaults to 'bind10'.
  38. This call will block until BIND10_STARTUP_COMPLETE or BIND10_STARTUP_ERROR
  39. is logged. In the case of the latter, or if it times out, the step (and
  40. scenario) will fail.
  41. It will also fail if there is a running process with the given process_name
  42. already.
  43. """
  44. args = [ 'bind10', '-v' ]
  45. if config_file is not None:
  46. args.append('-p')
  47. args.append("configurations/")
  48. args.append('-c')
  49. args.append(config_file)
  50. if cmdctl_port is None:
  51. args.append('--cmdctl-port=47805')
  52. else:
  53. args.append('--cmdctl-port=' + cmdctl_port)
  54. if process_name is None:
  55. process_name = "bind10"
  56. else:
  57. args.append('-m')
  58. args.append(process_name + '_msgq.socket')
  59. world.processes.add_process(step, process_name, args)
  60. # check output to know when startup has been completed
  61. (message, line) = world.processes.wait_for_stderr_str(process_name,
  62. ["BIND10_STARTUP_COMPLETE",
  63. "BIND10_STARTUP_ERROR"])
  64. assert message == "BIND10_STARTUP_COMPLETE", "Got: " + str(line)
  65. @step('wait for bind10 auth (?:of (\w+) )?to start')
  66. def wait_for_auth(step, process_name):
  67. """Wait for b10-auth to run. This is done by blocking until the message
  68. AUTH_SERVER_STARTED is logged.
  69. Parameters:
  70. process_name ('of <name', optional): The name of the BIND 10 instance
  71. to wait for. Defaults to 'bind10'.
  72. """
  73. if process_name is None:
  74. process_name = "bind10"
  75. world.processes.wait_for_stderr_str(process_name, ['AUTH_SERVER_STARTED'],
  76. False)
  77. @step('wait for bind10 xfrout (?:of (\w+) )?to start')
  78. def wait_for_xfrout(step, process_name):
  79. """Wait for b10-xfrout to run. This is done by blocking until the message
  80. XFROUT_NEW_CONFIG_DONE is logged.
  81. Parameters:
  82. process_name ('of <name', optional): The name of the BIND 10 instance
  83. to wait for. Defaults to 'bind10'.
  84. """
  85. if process_name is None:
  86. process_name = "bind10"
  87. world.processes.wait_for_stderr_str(process_name,
  88. ['XFROUT_NEW_CONFIG_DONE'],
  89. False)
  90. @step('have bind10 running(?: with configuration ([\S]+))?' +\
  91. '(?: with cmdctl port (\d+))?' +\
  92. '(?: as ([\S]+))?')
  93. def have_bind10_running(step, config_file, cmdctl_port, process_name):
  94. """
  95. Compound convenience step for running bind10, which consists of
  96. start_bind10.
  97. Currently only supports the 'with configuration' option.
  98. """
  99. start_step = 'start bind10 with configuration ' + config_file
  100. if cmdctl_port is not None:
  101. start_step += ' with cmdctl port ' + str(cmdctl_port)
  102. if process_name is not None:
  103. start_step += ' as ' + process_name
  104. step.given(start_step)
  105. # function to send lines to bindctl, and store the result
  106. def run_bindctl(commands, cmdctl_port=None):
  107. """Run bindctl.
  108. Parameters:
  109. commands: a sequence of strings which will be sent.
  110. cmdctl_port: a port number on which cmdctl is listening, is converted
  111. to string if necessary. If not provided, or None, defaults
  112. to 47805
  113. bindctl's stdout and stderr streams are stored (as one multiline string
  114. in world.last_bindctl_stdout/stderr.
  115. Fails if the return code is not 0
  116. """
  117. if cmdctl_port is None:
  118. cmdctl_port = 47805
  119. args = ['bindctl', '-p', str(cmdctl_port)]
  120. bindctl = subprocess.Popen(args, 1, None, subprocess.PIPE,
  121. subprocess.PIPE, None)
  122. for line in commands:
  123. bindctl.stdin.write(line + "\n")
  124. (stdout, stderr) = bindctl.communicate()
  125. result = bindctl.returncode
  126. world.last_bindctl_stdout = stdout
  127. world.last_bindctl_stderr = stderr
  128. assert result == 0, "bindctl exit code: " + str(result) +\
  129. "\nstdout:\n" + str(stdout) +\
  130. "stderr:\n" + str(stderr)
  131. @step('last bindctl( stderr)? output should( not)? contain (\S+)')
  132. def check_bindctl_output(step, stderr, notv, string):
  133. """Checks the stdout (or stderr) stream of the last run of bindctl,
  134. fails if the given string is not found in it (or fails if 'not' was
  135. set and it is found
  136. Parameters:
  137. stderr ('stderr'): Check stderr instead of stdout output
  138. notv ('not'): reverse the check (fail if string is found)
  139. string ('contain <string>') string to look for
  140. """
  141. if stderr is None:
  142. output = world.last_bindctl_stdout
  143. else:
  144. output = world.last_bindctl_stderr
  145. found = False
  146. if string in output:
  147. found = True
  148. if notv is None:
  149. assert found == True, "'" + string +\
  150. "' was not found in bindctl output:\n" +\
  151. output
  152. else:
  153. assert not found, "'" + string +\
  154. "' was found in bindctl output:\n" +\
  155. output
  156. def parse_bindctl_output_as_data_structure():
  157. """Helper function for data-related command tests: evaluates the
  158. last output of bindctl as a data structure that can then be
  159. inspected.
  160. If the bindctl output is not valid (json) data, this call will
  161. fail with an assertion failure.
  162. If it is valid, it is parsed and returned as whatever data
  163. structure it represented.
  164. """
  165. # strip any extra output after a charater that commonly terminates a valid
  166. # JSON expression, i.e., ']', '}' and '"'. (The extra output would
  167. # contain 'Exit from bindctl' message, and depending on environment some
  168. # other control-like characters...but why is this message even there?)
  169. # Note that this filter is not perfect. For example, it cannot recognize
  170. # a simple expression of true/false/null.
  171. output = re.sub("(.*)([^]}\"]*$)", r"\1", world.last_bindctl_stdout)
  172. try:
  173. return json.loads(output)
  174. except ValueError as ve:
  175. assert False, "Last bindctl output does not appear to be a " +\
  176. "parseable data structure: '" + output + "': " + str(ve)
  177. def find_process_pid(step, process_name):
  178. """Helper function to request the running processes from Boss, and
  179. return the pid of the process with the given process_name.
  180. Fails with an assert if the response from boss is not valid JSON,
  181. or if the process with the given name is not found.
  182. """
  183. # show_processes output is a list of lists, where the inner lists
  184. # are of the form [ pid, "name" ]
  185. # Not checking data form; errors will show anyway (if these turn
  186. # out to be too vague, we can change this)
  187. step.given('send bind10 the command Boss show_processes')
  188. running_processes = parse_bindctl_output_as_data_structure()
  189. for process in running_processes:
  190. if process[1] == process_name:
  191. return process[0]
  192. assert False, "Process named " + process_name +\
  193. " not found in output of Boss show_processes";
  194. @step("remember the pid of process ([\S]+)")
  195. def remember_pid(step, process_name):
  196. """Stores the PID of the process with the given name as returned by
  197. Boss show_processes command.
  198. Fails if the process with the given name does not appear to exist.
  199. Stores the component_name->pid value in the dict world.process_pids.
  200. This should only be used by the related step
  201. 'the pid of process <name> should (not) have changed'
  202. Arguments:
  203. process name ('process <name>') the name of the component to store
  204. the pid of.
  205. """
  206. if world.process_pids is None:
  207. world.process_pids = {}
  208. world.process_pids[process_name] = find_process_pid(step, process_name)
  209. @step('pid of process ([\S]+) should not have changed')
  210. def check_pid(step, process_name):
  211. """Checks the PID of the process with the given name as returned by
  212. Boss show_processes command.
  213. Fails if the process with the given name does not appear to exist.
  214. Fails if the process with the given name exists, but has a different
  215. pid than it had when the step 'remember the pid of process' was
  216. called.
  217. Fails if that step has not been called (since world.process_pids
  218. does not exist).
  219. """
  220. assert world.process_pids is not None, "No process pids stored"
  221. assert process_name in world.process_pids, "Process named " +\
  222. process_name +\
  223. " was not stored"
  224. pid = find_process_pid(step, process_name)
  225. assert world.process_pids[process_name] == pid,\
  226. "Expected pid: " + str(world.process_pids[process_name]) +\
  227. " Got pid: " + str(pid)
  228. @step('set bind10 configuration (\S+) to (.*)(?: with cmdctl port (\d+))?')
  229. def config_set_command(step, name, value, cmdctl_port):
  230. """
  231. Run bindctl, set the given configuration to the given value, and commit it.
  232. Parameters:
  233. name ('configuration <name>'): Identifier of the configuration to set
  234. value ('to <value>'): value to set it to.
  235. cmdctl_port ('with cmdctl port <portnr>', optional): cmdctl port to send
  236. the command to. Defaults to 47805.
  237. Fails if cmdctl does not exit with status code 0.
  238. """
  239. commands = ["config set " + name + " " + value,
  240. "config commit",
  241. "quit"]
  242. run_bindctl(commands, cmdctl_port)
  243. @step('send bind10 the following commands(?: with cmdctl port (\d+))?')
  244. def send_multiple_commands(step, cmdctl_port):
  245. """
  246. Run bindctl, and send it the given multiline set of commands.
  247. A quit command is always appended.
  248. cmdctl_port ('with cmdctl port <portnr>', optional): cmdctl port to send
  249. the command to. Defaults to 47805.
  250. Fails if cmdctl does not exit with status code 0.
  251. """
  252. commands = step.multiline.split("\n")
  253. # Always add quit
  254. commands.append("quit")
  255. run_bindctl(commands, cmdctl_port)
  256. @step('remove bind10 configuration (\S+)(?: value (\S+))?(?: with cmdctl port (\d+))?')
  257. def config_remove_command(step, name, value, cmdctl_port):
  258. """
  259. Run bindctl, remove the given configuration item, and commit it.
  260. Parameters:
  261. name ('configuration <name>'): Identifier of the configuration to remove
  262. value ('value <value>'): if name is a named set, use value to identify
  263. item to remove
  264. cmdctl_port ('with cmdctl port <portnr>', optional): cmdctl port to send
  265. the command to. Defaults to 47805.
  266. Fails if cmdctl does not exit with status code 0.
  267. """
  268. cmd = "config remove " + name
  269. if value is not None:
  270. cmd = cmd + " " + value
  271. commands = [cmd,
  272. "config commit",
  273. "quit"]
  274. run_bindctl(commands, cmdctl_port)
  275. @step('send bind10 the command (.+)(?: with cmdctl port (\d+))?')
  276. def send_command(step, command, cmdctl_port):
  277. """
  278. Run bindctl, send the given command, and exit bindctl.
  279. Parameters:
  280. command ('the command <command>'): The command to send.
  281. cmdctl_port ('with cmdctl port <portnr>', optional): cmdctl port to send
  282. the command to. Defaults to 47805.
  283. Fails if cmdctl does not exit with status code 0.
  284. """
  285. commands = [command,
  286. "quit"]
  287. run_bindctl(commands, cmdctl_port)
  288. @step('bind10 module (\S+) should( not)? be running')
  289. def module_is_running(step, name, not_str):
  290. """
  291. Convenience step to check if a module is running; can only work with
  292. default cmdctl port; sends a 'help' command with bindctl, then
  293. checks if the output contains the given name.
  294. Parameters:
  295. name ('module <name>'): The name of the module (case sensitive!)
  296. not ('not'): Reverse the check (fail if it is running)
  297. """
  298. if not_str is None:
  299. not_str = ""
  300. step.given('send bind10 the command help')
  301. step.given('last bindctl output should' + not_str + ' contain ' + name)