bind10_control.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. @step('start bind10(?: with configuration (\S+))?' +\
  19. '(?: with cmdctl port (\d+))?' +\
  20. '(?: with msgq socket file (\S+))?' +\
  21. '(?: as (\S+))?')
  22. def start_bind10(step, config_file, cmdctl_port, msgq_sockfile, process_name):
  23. """
  24. Start BIND 10 with the given optional config file, cmdctl port, and
  25. store the running process in world with the given process name.
  26. Parameters:
  27. config_file ('with configuration <file>', optional): this configuration
  28. will be used. The path is relative to the base lettuce
  29. directory.
  30. cmdctl_port ('with cmdctl port <portnr>', optional): The port on which
  31. b10-cmdctl listens for bindctl commands. Defaults to 47805.
  32. msgq_sockfile ('with msgq socket file', optional): The msgq socket file
  33. that will be used for internal communication
  34. process_name ('as <name>', optional). This is the name that can be used
  35. in the following steps of the scenario to refer to this
  36. BIND 10 instance. Defaults to 'bind10'.
  37. This call will block until BIND10_STARTUP_COMPLETE or BIND10_STARTUP_ERROR
  38. is logged. In the case of the latter, or if it times out, the step (and
  39. scenario) will fail.
  40. It will also fail if there is a running process with the given process_name
  41. already.
  42. """
  43. args = [ 'bind10', '-v' ]
  44. if config_file is not None:
  45. args.append('-p')
  46. args.append("configurations/")
  47. args.append('-c')
  48. args.append(config_file)
  49. if cmdctl_port is None:
  50. args.append('--cmdctl-port=47805')
  51. else:
  52. args.append('--cmdctl-port=' + cmdctl_port)
  53. if process_name is None:
  54. process_name = "bind10"
  55. else:
  56. args.append('-m')
  57. args.append(process_name + '_msgq.socket')
  58. world.processes.add_process(step, process_name, args)
  59. # check output to know when startup has been completed
  60. (message, line) = world.processes.wait_for_stderr_str(process_name,
  61. ["BIND10_STARTUP_COMPLETE",
  62. "BIND10_STARTUP_ERROR"])
  63. assert message == "BIND10_STARTUP_COMPLETE", "Got: " + str(line)
  64. @step('wait for bind10 auth (?:of (\w+) )?to start')
  65. def wait_for_auth(step, process_name):
  66. """Wait for b10-auth to run. This is done by blocking until the message
  67. AUTH_SERVER_STARTED is logged.
  68. Parameters:
  69. process_name ('of <name', optional): The name of the BIND 10 instance
  70. to wait for. Defaults to 'bind10'.
  71. """
  72. if process_name is None:
  73. process_name = "bind10"
  74. world.processes.wait_for_stderr_str(process_name, ['AUTH_SERVER_STARTED'],
  75. False)
  76. @step('have bind10 running(?: with configuration ([\S]+))?' +\
  77. '(?: with cmdctl port (\d+))?' +\
  78. '(?: as ([\S]+))?')
  79. def have_bind10_running(step, config_file, cmdctl_port, process_name):
  80. """
  81. Compound convenience step for running bind10, which consists of
  82. start_bind10 and wait_for_auth.
  83. Currently only supports the 'with configuration' option.
  84. """
  85. start_step = 'start bind10 with configuration ' + config_file
  86. wait_step = 'wait for bind10 auth to start'
  87. if cmdctl_port is not None:
  88. start_step += ' with cmdctl port ' + str(cmdctl_port)
  89. if process_name is not None:
  90. start_step += ' as ' + process_name
  91. wait_step = 'wait for bind10 auth of ' + process_name + ' to start'
  92. step.given(start_step)
  93. step.given(wait_step)
  94. @step('set bind10 configuration (\S+) to (.*)(?: with cmdctl port (\d+))?')
  95. def set_config_command(step, name, value, cmdctl_port):
  96. """
  97. Run bindctl, set the given configuration to the given value, and commit it.
  98. Parameters:
  99. name ('configuration <name>'): Identifier of the configuration to set
  100. value ('to <value>'): value to set it to.
  101. cmdctl_port ('with cmdctl port <portnr>', optional): cmdctl port to send
  102. the command to. Defaults to 47805.
  103. Fails if cmdctl does not exit with status code 0.
  104. """
  105. if cmdctl_port is None:
  106. cmdctl_port = '47805'
  107. args = ['bindctl', '-p', cmdctl_port]
  108. bindctl = subprocess.Popen(args, 1, None, subprocess.PIPE,
  109. subprocess.PIPE, None)
  110. bindctl.stdin.write("config set " + name + " " + value + "\n")
  111. bindctl.stdin.write("config commit\n")
  112. bindctl.stdin.write("quit\n")
  113. result = bindctl.wait()
  114. assert result == 0, "bindctl exit code: " + str(result)
  115. @step('send bind10 the command (.+)(?: with cmdctl port (\d+))?')
  116. def send_command(step, command, cmdctl_port):
  117. """
  118. Run bindctl, send the given command, and exit bindctl.
  119. Parameters:
  120. command ('the command <command>'): The command to send.
  121. cmdctl_port ('with cmdctl port <portnr>', optional): cmdctl port to send
  122. the command to. Defaults to 47805.
  123. Fails if cmdctl does not exit with status code 0.
  124. """
  125. if cmdctl_port is None:
  126. cmdctl_port = '47805'
  127. args = ['bindctl', '-p', cmdctl_port]
  128. bindctl = subprocess.Popen(args, 1, None, subprocess.PIPE,
  129. subprocess.PIPE, None)
  130. bindctl.stdin.write(command + "\n")
  131. bindctl.stdin.write("quit\n")
  132. result = bindctl.wait()
  133. assert result == 0, "bindctl exit code: " + str(result)