def initialize(self): super(stop_base, self).initialize() # Prepare a container docker_containers = DockerContainers(self) name = docker_containers.get_unique_name() self.sub_stuff['container_name'] = name config.none_if_empty(self.config) if self.config.get('run_options_csv'): subargs = [ arg for arg in self.config['run_options_csv'].split(',') ] subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("bash") subargs.append("-c") subargs.append(self.config['exec_cmd']) container = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['container_cmd'] = container container.execute() container.wait_for_ready() # Prepare the "stop" command if self.config.get('stop_options_csv'): subargs = [ arg for arg in self.config['stop_options_csv'].split(',') ] subargs.append(name) self.sub_stuff['stop_cmd'] = DockerCmd(self, 'stop', subargs)
def initialize(self): super(simple_base, self).initialize() rand_name = self.sub_stuff['cont'].get_unique_name() self.sub_stuff["rand_name"] = rand_name self.sub_stuff["subargs"].insert(0, "--name=\"%s\"" % rand_name) run_in_pipe_r, run_in_pipe_w = os.pipe() self.sub_stuff['file_desc'].append(run_in_pipe_r) self.sub_stuff['file_desc'].append(run_in_pipe_w) self.sub_stuff["run_in_pipe_w"] = run_in_pipe_w dkrcmd = AsyncDockerCmd(self, 'run', self.sub_stuff['subargs']) # Runs in background self.sub_stuff['cmdresult'] = dkrcmd.execute(run_in_pipe_r) self.sub_stuff['cmd_run'] = dkrcmd dkrcmd.wait_for_ready() self.logdebug("Detail after waiting: %s", dkrcmd.cmdresult) attach_options = self.config['attach_options_csv'].split(',') self.sub_stuff['subargs_a'] = attach_options c_name = self.sub_stuff["rand_name"] self.sub_stuff["containers"].append(c_name) cid = self.sub_stuff["cont"].list_containers_with_name(c_name) self.failif(cid == [], "Unable to search container with name %s" % (c_name))
def initialize(self): super(stop_base, self).initialize() # Prepare a container docker_containers = DockerContainers(self) name = docker_containers.get_unique_name() self.sub_stuff['container_name'] = name config.none_if_empty(self.config) if self.config.get('run_options_csv'): subargs = [arg for arg in self.config['run_options_csv'].split(',')] subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("bash") subargs.append("-c") subargs.append(self.config['exec_cmd']) container = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['container_cmd'] = container container.execute() container.wait_for_ready(timeout=self.config['wait_start']) # Prepare the "stop" command if self.config.get('stop_options_csv'): subargs = [arg for arg in self.config['stop_options_csv'].split(',')] subargs.append(name) self.sub_stuff['stop_cmd'] = DockerCmd(self, 'stop', subargs)
def start_test_container(self, read_fd, write_fd): dc = self.sub_stuff['dc'] name = dc.get_unique_name() subargs = [ '--interactive', '--publish-all', '--name', name, self.sub_stuff['builds'][-1].image_name, 'sh' ] async_dkrcmd = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['async_dkrcmd'] = async_dkrcmd async_dkrcmd.execute(read_fd) os.close(read_fd) os.write(write_fd, b'\necho "R""EADY"\n') async_dkrcmd.wait_for_ready() return name
def _start_container(self): """ Start a container. All it does is emit output to stdout, one line per second. We don't care what the output is, we just care that the number of output lines increases over time. """ c_name = self.stuff['dc'].get_unique_name() fin = DockerImage.full_name_from_defaults(self.config) subargs = ['--detach', '--name=' + c_name, fin, 'bash -c "echo READY;' ' while :; do date +%s; sleep 1;done"'] dkrcmd = AsyncDockerCmd(self, 'run', subargs) dkrcmd.execute() dkrcmd.wait_for_ready(c_name) self.stuff['container_name'] = c_name
def _start_idle_container(self): """ Start a container. We only need it briefly, until we (the test, running in host space) can cd into its root filesystem. Container will spin idly until a semaphore file is removed. """ c_name = self.stuff['dc'].get_unique_name() fin = DockerImage.full_name_from_defaults(self.config) self.stuff['trigger_file'] = trigger_file = 'DELETE-ME' subargs = ['--rm', '--name=' + c_name, fin, 'bash -c "echo READY;touch /%s;' ' while [ -e /%s ]; do sleep 0.1; done"' % (trigger_file, trigger_file)] dkrcmd = AsyncDockerCmd(self, 'run', subargs) dkrcmd.execute() dkrcmd.wait_for_ready(c_name) self.stuff['container_name'] = c_name
def _start_idle_container(self): """ Start a container. We only need it briefly, until we (the test, running in host space) can cd into its root filesystem. Container will spin idly until a semaphore file is removed. """ c_name = self.stuff['dc'].get_unique_name() fin = DockerImage.full_name_from_defaults(self.config) self.stuff['trigger_file'] = trigger_file = 'DELETE-ME' subargs = [ '--rm', '--name=' + c_name, fin, 'bash -c "echo READY;touch /%s;' ' while [ -e /%s ]; do sleep 0.1; done"' % (trigger_file, trigger_file) ] dkrcmd = AsyncDockerCmd(self, 'run', subargs) dkrcmd.execute() dkrcmd.wait_for_ready(c_name) self.stuff['container_name'] = c_name
def run_once(self): """ Run a background container with --rm; while that's running, clone another process sharing the same mount namespace. That process will signal the container to finish, and when docker cleans it up (--rm) we expect no errors. """ super(rmdir_mount, self).run_once() fin = DockerImage.full_name_from_defaults(self.config) name = DockerContainers(self).get_unique_name() # Create a state file; container will exit once this is deleted waitfile = "state_file_for_signaling_the_container_to_exit" waitfile_local = os.path.join(self.tmpdir, waitfile) open(waitfile_local, "a").close() # Basically: mount self.tmpdir as /tmp in container, constantly # check the state file, exit when it's deleted, then (--rm) have # docker clean it up. subargs = [ "--rm", "-v", "%s:/tmp:z" % self.tmpdir, "--name", name, fin, "bash", "-c", "'echo READY; while [ -e /tmp/%s ]; do sleep 0.1; done'" % waitfile, ] dkrcmd = AsyncDockerCmd(self, "run", subargs) self.sub_stuff["dkrcmd"] = dkrcmd dkrcmd.execute() dkrcmd.wait_for_ready(name, timeout=5) # Container is running. Now, in parallel: clone, delete state file, # and wait for container to terminate. in_n = "rm -f %s;" " while :;do" " docker inspect %s 2>/dev/null || exit 0;" " done" % (waitfile_local, name) self.sub_stuff["unshare"] = utils.run("unshare -m bash -c '%s'" % in_n)
def run_once(self): """ Run a background container with --rm; while that's running, clone another process sharing the same mount namespace. That process will signal the container to finish, and when docker cleans it up (--rm) we expect no errors. """ super(rmdir_mount, self).run_once() fin = DockerImage.full_name_from_defaults(self.config) name = DockerContainers(self).get_unique_name() # Create a state file; container will exit once this is deleted waitfile = 'state_file_for_signaling_the_container_to_exit' waitfile_local = os.path.join(self.tmpdir, waitfile) open(waitfile_local, 'a').close() # Basically: mount self.tmpdir as /tmp in container, constantly # check the state file, exit when it's deleted, then (--rm) have # docker clean it up. subargs = [ '--rm', '-v', '%s:/tmp:z' % self.tmpdir, '--name', name, fin, 'bash', '-c', "'echo READY; while [ -e /tmp/%s ]; do sleep 0.1; done'" % waitfile ] dkrcmd = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['dkrcmd'] = dkrcmd dkrcmd.execute() dkrcmd.wait_for_ready(name, timeout=5) # Container is running. Now, in parallel: clone, delete state file, # and wait for container to terminate. in_n = ("rm -f %s;" " while :;do" " docker inspect %s 2>/dev/null || exit 0;" " done" % (waitfile_local, name)) self.sub_stuff['unshare'] = utils.run("unshare -m bash -c '%s'" % in_n)