bind10_control.py 15 KB

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