def run_once(self): sig = getattr(signal, self.config['listen_signal']) dkrcmd = AsyncDockerCmd(self, 'run', self.sub_stuff['subargs'], timeout=self.config['docker_timeout']) self.logdebug("Starting background docker command, " "timeout %s seconds: " "%s", self.config['docker_timeout'], dkrcmd.command) dkrcmd.verbose = True # Runs in background self.sub_stuff['dkrcmd'] = dkrcmd dkrcmd.execute() pid = dkrcmd.process_id ss = self.config['secret_sauce'] while True: stdout = dkrcmd.stdout if stdout.count(ss) >= 1: break time.sleep(0.1) wait_start = self.config['wait_start'] self.loginfo("Container running, waiting %d seconds to send signal" % wait_start) # Allow noticable time difference for date command, # and eat into dkrcmd timeout after receiving signal. time.sleep(wait_start) self.failif(not utils.pid_is_alive(pid), "Pid %s not running after wait: %s" % (pid, dkrcmd.exit_status)) self.loginfo("Signaling pid %d with signal %s", pid, self.config['listen_signal']) utils.signal_pid(pid, sig) self.loginfo("Waiting up to %d seconds for exit", dkrcmd.timeout) # Throw exception if takes > docker_timeout to exit dkrcmd.wait()
def run_once(self): self.loginfo("Starting background docker command, timeout %s seconds", self.config['docker_timeout']) in_pipe_r, in_pipe_w = os.pipe() dkrcmd = AsyncDockerCmd(self, 'run', self.sub_stuff['subargs'], timeout=self.config['docker_timeout']) dkrcmd.verbose = True dkrcmd.timeout = 10 # Runs in background dkrcmd.execute(in_pipe_r) self.sub_stuff['dkrcmd'] = dkrcmd wait = self.config['wait_interactive_cmd'] icmd = self.config['interactive_cmd'] + "\n" # Allow noticable time difference for date command, # and eat into dkrcmd timeout after receiving signal. time.sleep(wait) os.write(in_pipe_w, icmd) self.loginfo("Waiting up to %d seconds for exit", dkrcmd.timeout) # Throw exception if takes > docker_timeout to exit self.loginfo("Container running, waiting %d seconds to finish.", wait) self.logdebug("interactive cmds %s", icmd) time.sleep(wait) os.close(in_pipe_w) dkrcmd.wait()
def run_once(self): super(start_base, self).run_once() # 1. Run with no options dkrcmd = AsyncDockerCmd(self, 'start', self.complete_docker_command_line(), self.config['docker_start_timeout']) self.loginfo("Starting container...") self.loginfo("Executing background command: %s" % dkrcmd.command) dkrcmd.execute() dkrcmd.wait(60) self.sub_stuff["dkrcmd"] = dkrcmd
def run_once(self): super(tag_base, self).run_once() dkrcmd = AsyncDockerCmd(self, 'tag', self.complete_docker_command_line()) self.loginfo("Executing background command: %s" % dkrcmd) dkrcmd.execute() while not dkrcmd.done: self.loginfo("tagging...") time.sleep(3) self.sub_stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): super(history_base, self).run_once() dkrcmd = AsyncDockerCmd(self, 'history', self.complete_history_cmd(), self.config['docker_history_timeout']) self.loginfo("Executing background command: %s" % dkrcmd) dkrcmd.execute() while not dkrcmd.done: self.loginfo("historyting...") time.sleep(3) self.sub_stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): super(commit_base, self).run_once() dkrcmd = AsyncDockerCmd(self.parent_subtest, 'commit', self.complete_docker_command_line(), self.config['docker_commit_timeout']) self.loginfo("Executing background command: %s" % dkrcmd) dkrcmd.execute() while not dkrcmd.done: self.loginfo("Commiting...") time.sleep(3) self.sub_stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): super(rmi_base, self).run_once() # 1. Run with no options dkrcmd = AsyncDockerCmd(self, 'rmi', self.complete_docker_command_line(), self.config['docker_rmi_timeout'], True) self.loginfo("Executing background command: %s" % dkrcmd) dkrcmd.execute() while not dkrcmd.done: self.loginfo("Deleting image...") time.sleep(3) self.sub_stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): super(start_base, self).run_once() # 1. Run with no options dkrcmd = AsyncDockerCmd(self.parent_subtest, 'start', self.complete_docker_command_line(), self.config['docker_start_timeout']) self.loginfo("Executing background command: %s" % dkrcmd) dkrcmd.execute() while not dkrcmd.done: self.loginfo("Starting container...") time.sleep(3) self.sub_stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): self.logdebug("Starting background docker commands") runcmd = AsyncDockerCmd(self.parent_subtest, 'run', self.sub_stuff['subargs'], timeout=self.config['docker_timeout']) self.logdebug("Run Command: %s", runcmd.command) runcmd.execute(self.sub_stuff["rpipe"]) ss = self.config['secret_sauce'] while True: stdout = runcmd.stdout if stdout.count(ss) >= 1: break time.sleep(0.1) self.loginfo("Container running, waiting for %s seconds.", self.config['wait_interactive_cmd']) time.sleep(self.config['wait_interactive_cmd']) # Not needed by this process anymore os.close(self.sub_stuff["rpipe"]) self.logdebug("Starting attach command") attachcmd = AsyncDockerCmd(self.parent_subtest, 'attach', self.sub_stuff['attach_options'], timeout=self.config['docker_timeout']) self.logdebug("Attach Command: %s", runcmd.command) attachcmd.execute() self.loginfo("Waiting for %s seconds for attach", self.config['wait_interactive_cmd']) time.sleep(self.config['wait_interactive_cmd']) rand_data = self.sub_stuff["rand_data"] self.logdebug("Sending test data: %s", rand_data) os.write(self.sub_stuff["wpipe"], rand_data) # line buffered # send EOF to container os.close(self.sub_stuff["wpipe"]) self.logdebug("Waiting for processes to exit") self.sub_stuff['run_cmdresult'] = runcmd.wait() self.sub_stuff['cmdresult'] = attachcmd.wait()
def run_once(self): super(run_base, self).run_once() # Prints out basic info sig = getattr(signal, self.config['listen_signal']) dkrcmd = AsyncDockerCmd(self.parent_subtest, 'run', self.sub_stuff['subargs'], timeout=self.config['docker_timeout']) # Runs in background self.sub_stuff['cmdresult'] = dkrcmd.execute() pid = dkrcmd.process_id self.loginfo("Container running, waiting %d seconds to send signal" % self.config['wait_start']) # Allow noticable time difference for date command, # and eat into dkrcmd timeout after receiving signal. time.sleep(self.config['wait_start']) self.loginfo("Signaling pid %d with signal %s", pid, self.config['listen_signal']) utils.signal_pid(pid, sig) self.loginfo("Waiting up to %d seconds for exit", dkrcmd.timeout) # Throw exception if takes > docker_timeout to exit dkrcmd.wait()
def run_once(self): super(build, self).run_once() subargs = [self.config['docker_build_options'], "-t", self.stuff['image_name_tag'], self.srcdir] # Don't really need async here, just exercizing class dkrcmd = AsyncDockerCmd(self, 'build', subargs, self.config['build_timeout_seconds']) self.loginfo("Executing background command: %s" % dkrcmd) dkrcmd.execute() while not dkrcmd.done: self.loginfo("Building...") time.sleep(3) self.stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): super(build, self).run_once() subargs = [self.config['docker_build_options'], "-t", self.stuff['image_name_tag'], self.srcdir] # Don't really need async here, just exercizing class dkrcmd = AsyncDockerCmd(self, 'build', subargs, self.config['build_timeout_seconds'], verbose=True) dkrcmd.execute() nss = NotSeenString(getattr, dkrcmd, 'stdout') while not dkrcmd.done: if nss.has_new_line(): self.loginfo("Building: %s" % nss) else: time.sleep(3) self.stuff["cmdresult"] = dkrcmd.wait()
def run_once(self): def wait_for_output(check, output, stderr=False): """ Wait until check in the new output """ idx = output.idx if stderr: output_matches = lambda: check in output.geterr(idx) else: output_matches = lambda: check in output.get(idx) if utils.wait_for(output_matches, 10, step=0.01) is None: return -1 return 0 def error_msg(log_us): """ Format a nice string from dictionary """ out = ["%s\n%s" % (key, value) for key, value in log_us.iteritems()] return "\n\n".join(out) def _output_matches(cmd1, cmd2): """ Compares the output of stdout&stderr """ out1 = cmd1.stdout.splitlines() + cmd1.stderr.splitlines() out1 = set((_ for _ in out1 if not _.startswith('[debug]'))) out2 = cmd2.stdout.splitlines() + cmd2.stderr.splitlines() out2 = set((_ for _ in out2 if not _.startswith('[debug]'))) return out1 == out2 super(simple_base, self).run_once() log_us = {} # Create container dkrcmd, name = self._init_container(self.sub_stuff['subargs'], 'bash') log_us['container'] = dkrcmd dkrcmd.execute() self.wait_exists(name) # Create docker logs --follow log1 = AsyncDockerCmd(self, 'logs', ['--follow', name], verbose=False) self.sub_stuff['async_processes'].append(log1) log_us['log1'] = log1 log1.execute() log1_out = Output(log1) # Generate output to stdout for _ in xrange(5): prefix = utils.generate_random_string(5) dkrcmd.stdin("PREFIX='%s'\n" % prefix) line = utils.generate_random_string(10) dkrcmd.stdin("echo $PREFIX: %s\n" % line) line = "%s: %s" % (prefix, line) self.failif(wait_for_output(line, log1_out), "Stdout '%s' did not occur in log1 output in 10s:\n%s" % (line, error_msg(log_us))) # Start docker logs without follow and compare output log2 = DockerCmd(self, 'logs', [name], verbose=False) log_us['log2'] = log2 log2.execute() match = lambda: _output_matches(log1, log2) self.failif(not utils.wait_for(match, 5), "Outputs of log1 and " "log2 are not the same:\n%s" % error_msg(log_us)) # Generate output to stderr for _ in xrange(5): prefix = utils.generate_random_string(5) dkrcmd.stdin("PREFIX='%s'\n" % prefix) line = utils.generate_random_string(10) dkrcmd.stdin(">&2 echo $PREFIX: %s\n" % line) line = "%s: %s" % (prefix, line) self.failif(wait_for_output(line, log1_out, self.sub_stuff['stderr']), "Output '%s' did not occur in log1 output in 10s:\n%s" % (line, error_msg(log_us))) self.failif(_output_matches(log1, log2), 'Outputs log1 and log2 are ' "the same even thought new input was generated and log2 " "was executed without --follow:\n%s" % error_msg(log_us)) # Stop the container dkrcmd.stdin('exit\n') dkrcmd.close() # Wait for docker logs exit log1.wait(10) # Start docker logs without follow and compare output log3 = DockerCmd(self, 'logs', [name], verbose=False) log_us['log3'] = log3 log3.execute() match = lambda: _output_matches(log1, log3) self.failif(not utils.wait_for(match, 5), "Outputs of log1 and " "log3 are not the same:\n%s" % error_msg(log_us))