def init_container(self, volume=None, volumes_from=None): """ Starts container """ subargs = config.get_as_list(self.config['run_options_csv']) if volume: subargs.append("--volume %s" % volume) if volumes_from: subargs.append("--volumes-from %s" % volumes_from) name = self.sub_stuff['dc'].get_unique_name() self.sub_stuff['containers'].append(name) subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("sh") read_fd, write_fd = os.pipe() self.sub_stuff['fds'].append(write_fd) self.sub_stuff['fds'].append(read_fd) dkrcmd = dockercmd.AsyncDockerCmd(self, 'run', subargs) # TODO: Fix use of dkrcmd.stdin when appropriate class mech. available dkrcmd.execute(read_fd) dkrcmd.stdin = write_fd os.close(read_fd) # no longer needed os.write(write_fd, 'echo "Started"\n') self.failif(not wait_for_output(lambda: dkrcmd.stdout, 'Started', timeout=20), "Error starting container %s: %s" % (name, dkrcmd)) return dkrcmd
def init_subargs(self): dc = self.sub_stuff['dc'] cntr_name = dc.get_unique_name(self.__class__.__name__) self.sub_stuff['cntr_name'] = cntr_name subargs = self.config['run_options_csv'].split(',') self.sub_stuff['subargs'] = subargs subargs.append("--name=%s" % cntr_name) # Need to detect when container is running cidfile = os.path.join(self.tmpdir, cntr_name) self.sub_stuff['cidfile'] = cidfile subargs.append("--cidfile=%s" % cidfile) subargs.append("--volume=%s:/workdir" % self.sub_stuff['volume']) subargs.append("--workdir=/workdir") # FQIN is always last subargs.append(DockerImage.full_name_from_defaults(self.config)) subargs.append('/bin/bash') subargs.append('-c') # Write to a file when signal received # Loop forever until marker-file exists command = ("\"" "echo 'foobar' > stop && " "rm -f stop && trap '/usr/bin/date +%%s> stop' %s && " "while ! [ -f stop ]; do /usr/bin/sleep 0.1s; done" "\"" % self.config['listen_signal']) subargs.append(command)
def _init_container(self): """ Create, store in self.stuff and execute container """ docker_containers = DockerContainers(self) prefix = self.config["container_name_prefix"] name = docker_containers.get_unique_name(prefix, length=4) self.stuff['container_name'] = name if self.config.get('run_options_csv'): subargs = [arg for arg in self.config['run_options_csv'].split(',')] else: subargs = [] subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("bash") container = NoFailDockerCmd(self, 'run', subargs) self.stuff['container_cmd'] = container container.execute() if self.config.get('attach_options_csv'): subargs = [arg for arg in self.config['attach_options_csv'].split(',')] else: subargs = [] subargs.append(name) container = AsyncDockerCmd(self, 'attach', subargs) self.stuff['container_cmd'] = container # overwrites finished cmd stdin = os.pipe() self.stuff['container_stdin'] = stdin[1] container.execute(stdin[0])
def init_subargs(self): self.sub_stuff['subargs'] = self.config['run_options_csv'].split(',') fqin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['fqin'] = fqin self.sub_stuff['subargs'].append(fqin) self.sub_stuff['subargs'] += self.config['bash_cmd'].split(',') self.sub_stuff['subargs'].append(self.config['cmd'])
def initialize(self): super(stop_base, self).initialize() # Prepare a container docker_containers = DockerContainers(self.parent_subtest) prefix = self.config["stop_name_prefix"] name = docker_containers.get_unique_name(prefix, length=4) 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() time.sleep(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 cleanup(self): super(build, self).cleanup() # Auto-converts "yes/no" to a boolean if self.config["remove_after_test"]: # Remove all previously non-existing containers for cid in self.stuff["dc"].list_container_ids(): if cid in self.stuff["existing_containers"]: continue # don't remove previously existing ones self.logdebug("Remoivng container %s", cid) dcmd = DockerCmd(self, "rm", ["--force", "--volumes", cid], verbose=False) dcmd.execute() dimg = self.stuff["di"] base_repo_fqin = DockerImage.full_name_from_defaults(self.config) # Remove all previously non-existing images for img in dimg.list_imgs(): if img in self.stuff["existing_images"]: continue if img.full_name is None or img.full_name is "": thing = img.long_id else: thing = img.full_name # never ever remove base_repo_fqin under any circumstance if thing == base_repo_fqin: continue self.logdebug("Remoivng image %s", img) dcmd = DockerCmd(self, "rmi", ["--force", thing], verbose=False) dcmd.execute()
def initialize(self): super(short_term_app, self).initialize() docker_name = DockerImage.full_name_from_defaults(self.config) # Private to this instance, outside of __init__ prep_changes = DockerCmd(self, "run", ["-d", docker_name, self.config["run_cmd"]], self.config['docker_run_timeout']) results = prep_changes.execute() if results.exit_status: raise error.TestNAError("Problems during initialization of" " test: %s" % results) else: c_id = results.stdout.strip() cont = self.sub_stuff["conts_obj"].list_containers_with_cid(c_id) if cont == []: raise error.TestNAError("Problems during initialization of" " test: Failed to find container with" "id %s." % c_id) self.sub_stuff["container"] = cont[0] self.sub_stuff["containers"].append(self.sub_stuff["container"]) if self.check_if_cmd_finished: self.wait_for_container_death(cont[0])
def cleanup(self): super(run, self).cleanup() # Clean up all containers dc = DockerContainers(self) for cobj in dc.list_containers(): self.logwarning("Found leftover container: %s", cobj.container_name) try: dc.kill_container_by_obj(cobj) except ValueError: pass # already dead else: self.logdebug("Killed container %s, waiting up to %d seconds " "for it to exit", cobj.container_name, dc.timeout) dc.wait_by_obj(cobj) self.logdebug("Removing container %s", cobj.container_name) dc.remove_by_obj(cobj) # Clean up all non-default images fqin = DockerImage.full_name_from_defaults(self.config) di = DockerImages(self) def_img_obj = di.list_imgs_with_full_name(fqin)[0] for img_obj in di.list_imgs(): if img_obj.full_name != def_img_obj.full_name: self.logwarning("Found leftover image: %s", img_obj) di.remove_image_by_image_obj(img_obj) else: self.logdebug("Not removing default image: %s", def_img_obj)
def _init_container(self): """ Create, store in self.stuff and execute container """ try: fin = DockerImage.full_name_from_defaults(self.config) except ValueError: raise DockerTestNAError("Empty test image name configured," "did you set one for this test?") docker_containers = DockerContainers(self) name = docker_containers.get_unique_name() self.stuff['container_name'] = name if self.config.get('run_options_csv'): subargs = [arg for arg in self.config['run_options_csv'].split(',')] else: subargs = [] subargs.append("--name %s" % name) subargs.append(fin) subargs.append("bash") container = NoFailDockerCmd(self, 'run', subargs) self.stuff['container_cmd'] = container container.execute() if self.config.get('attach_options_csv'): subargs = [arg for arg in self.config['attach_options_csv'].split(',')] else: subargs = [] subargs.append(name) container = AsyncDockerCmd(self, 'attach', subargs) self.stuff['container_cmd'] = container # overwrites finished cmd stdin = os.pipe() self.stuff['container_stdin'] = stdin[1] container.execute(stdin[0])
def initialize(self): super(invalid_base, self).initialize() self.sub_stuff['tag'] = [] self.sub_stuff['container_names'] = [] self.sub_stuff['arg_inpars'] = [] self.sub_stuff['arg_invals'] = [] self.sub_stuff['result_inparam'] = False self.sub_stuff['result_invalus'] = False fin = [] arg_inpars = [] arg_invals = [] config.none_if_empty(self.config) invalidparams = self.config['invalid_run_params'] invalidvalues = self.config['invalid_run_values'] for inp in invalidparams.split(','): arg_inpars.append(inp) for inv in invalidvalues.split(','): arg_invals.append(inv) if self.config['input_docker_tag']: fin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['tag'].append(fin) self.sub_stuff['arg_inpars'] = arg_inpars self.sub_stuff['arg_invals'] = arg_invals
def initialize(self): super(icc, self).initialize() fin = DockerImage.full_name_from_defaults(self.config) conts = self.sub_stuff['conts'] self.sub_stuff["cont1_name"] = conts.get_unique_name("server") self.sub_stuff["containers"].append(self.sub_stuff["cont1_name"]) args1 = get_as_list(self.config["docker_cmd1_args"]) args1.append("--name=%s" % (self.sub_stuff["cont1_name"])) args1.append(fin) adcsp = AsyncDockerCmdSpec.PIPE self.sub_stuff["bash1"] = self.dkr_cmd.async("run", args1 + ["sh"], stdin_r=adcsp) self.sub_stuff["cont2_name"] = conts.get_unique_name("client") self.sub_stuff["containers"].append(self.sub_stuff["cont2_name"]) args2 = get_as_list(self.config["docker_cmd2_args"]) args2.append("--name=%s" % (self.sub_stuff["cont2_name"])) args2.append(fin) adcsp = AsyncDockerCmdSpec.PIPE self.sub_stuff["bash2"] = self.dkr_cmd.async("run", args2 + ["sh"], stdin_r=adcsp) ip = self.get_container_ip(self.sub_stuff["cont1_name"]) if ip is None: raise DockerTestNAError("Problems during initialization of" " test: Cannot get container IP addr.") self.sub_stuff["ip1"] = ip
def initialize(self): super(history_base, self).initialize() config.none_if_empty(self.config) dimgs = DockerImages(self.parent_subtest) self.sub_stuff["containers"] = [] self.sub_stuff["images"] = [] new_img_name = dimgs.get_unique_name('1') self.sub_stuff["new_image_name"] = new_img_name new_img_name2 = dimgs.get_unique_name('2') self.sub_stuff["new_image_name2"] = new_img_name2 self.sub_stuff['rand_data'] = utils.generate_random_string(8) cmd_with_rand = (self.config['docker_data_prepare_cmd'] % self.sub_stuff['rand_data']) fqin = DockerImage.full_name_from_defaults(self.config) # create new image in history self.create_image(fqin, new_img_name, cmd_with_rand) self.sub_stuff["images"].append(new_img_name) # create new image in history self.create_image(new_img_name, new_img_name2, cmd_with_rand) self.sub_stuff["images"].append(new_img_name2)
def postprocess(self): super(history_base, self).postprocess() if self.config["docker_expected_result"] == "PASS": # Raise exception if problems found OutputGood(self.sub_stuff['cmdresult']) self.failif(self.sub_stuff['cmdresult'].exit_status != 0, "Non-zero history exit status: %s" % self.sub_stuff['cmdresult']) new_img_name = self.sub_stuff["new_image_name"] new_img_name2 = self.sub_stuff["new_image_name2"] base_img_name = DockerImage.full_name_from_defaults(self.config) self.failif(base_img_name in self.sub_stuff['cmdresult'].stdout, "Unable find image name %s in image history: %s" % (base_img_name, self.sub_stuff['cmdresult'].stdout)) self.failif(new_img_name in self.sub_stuff['cmdresult'].stdout, "Unable find image name %s in image history: %s" % (new_img_name, self.sub_stuff['cmdresult'].stdout)) self.failif(new_img_name2 in self.sub_stuff['cmdresult'].stdout, "Unable find image name %s in image history: %s" % (new_img_name2, self.sub_stuff['cmdresult'].stdout))
def _init_container_attached(self, name): """ Starts detached container and attaches it using docker attach """ if self.config.get("run_options_csv"): subargs = [arg for arg in self.config["run_options_csv"].split(",")] else: subargs = [] if self.tty: subargs.append("--tty=true") else: subargs.append("--tty=false") 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 = DockerCmd(self, "run", subargs, verbose=False) self.sub_stuff["container_cmd"] = container mustpass(container.execute()) if self.config.get("attach_options_csv"): subargs = [arg for arg in self.config["attach_options_csv"].split(",")] else: subargs = [] subargs.append(name) container = AsyncDockerCmd(self, "attach", subargs, verbose=False) self.sub_stuff["container_cmd"] = container # overwrites finished cmd container.execute()
def _init_container_attached(self, name): """ Starts detached container and attaches it using docker attach """ if self.config.get('run_options_csv'): subargs = [arg for arg in self.config['run_options_csv'].split(',')] else: subargs = [] if self.tty: subargs.append('--tty=true') else: subargs.append('--tty=false') 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 = DockerCmd(self, 'run', subargs) self.sub_stuff['container_cmd'] = container mustpass(container.execute()) if self.config.get('attach_options_csv'): subargs = [arg for arg in self.config['attach_options_csv'].split(',')] else: subargs = [] subargs.append(name) container = AsyncDockerCmd(self, 'attach', subargs) self.sub_stuff['container_cmd'] = container # overwrites finished cmd container.execute()
def initialize(self): super(history_base, self).initialize() config.none_if_empty(self.config) self.sub_stuff["containers"] = [] self.sub_stuff["images"] = [] name_prefix = self.config["history_repo_name_prefix"] new_img_name = "%s_%s" % (name_prefix, utils.generate_random_string(8).lower()) self.sub_stuff["new_image_name"] = new_img_name new_img_name2 = "%s_%s" % (name_prefix, utils.generate_random_string(8).lower()) self.sub_stuff["new_image_name2"] = new_img_name2 self.sub_stuff['rand_data'] = utils.generate_random_string(8) cmd_with_rand = (self.config['docker_data_prepare_cmd'] % self.sub_stuff['rand_data']) fqin = DockerImage.full_name_from_defaults(self.config) # create new image in history self.create_image(fqin, new_img_name, cmd_with_rand) self.sub_stuff["images"].append(new_img_name) # create new image in history self.create_image(new_img_name, new_img_name2, cmd_with_rand) self.sub_stuff["images"].append(new_img_name2)
def _init_container_attached(self, name): if self.sub_stuff.get('run_options_csv'): subargs = [arg for arg in self.sub_stuff['run_options_csv'].split(',')] else: subargs = [] 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 = DockerCmd(self, 'run', subargs) self.sub_stuff['container_cmd'] = container mustpass(container.execute()) if self.sub_stuff.get('attach_options_csv'): subargs = [arg for arg in self.sub_stuff['attach_options_csv'].split(',')] else: subargs = [] subargs.append(name) c_attach = AsyncDockerCmd(self, 'attach', subargs) self.sub_stuff['container_cmd'] = c_attach # overwrites finished cmd c_attach.execute()
def run_once(self): super(tls_verify_all_base, self).run_once() # start new docker daemon docker_args = [] docker_args += get_as_list(self.config["docker_daemon_args"]) docker_args.append("-H %s" % self.config["docker_daemon_bind"]) dd = docker_daemon.start(self.config["docker_path"], docker_args) ret = docker_daemon.output_match(dd) if not ret: raise DockerTestNAError("Unable to start docker daemon:" "\n**STDOUT**:\n%s\n**STDERR**:\n%s" % (dd.get_stdout(), dd.get_stderr())) self.sub_stuff["docker_daemon"] = dd if self.sub_stuff["check_container_name"]: self.sub_stuff["cont1_name"] = self.conts.get_unique_name("test") self.sub_stuff["containers"].append(self.sub_stuff["cont1_name"]) else: # Try to generate name without check using docker. rand = utils.generate_random_string(30) self.sub_stuff["cont1_name"] = "test" + rand self.sub_stuff["containers"].append(self.sub_stuff["cont1_name"]) # start docker client command fin = DockerImage.full_name_from_defaults(self.config) args1 = ["--name=%s" % (self.sub_stuff["cont1_name"])] args1.append(fin) args1 += ["ls", "/"] adc_p = AsyncDockerCmdSpec.PIPE self.sub_stuff["bash1"] = self.dkr_cmd.async("run", args1, stdin_r=adc_p) # 1. Run with no options self.sub_stuff["bash1"].wait(120)
def _init_container(self, subargs, cidfile, cmd, check_method=None, custom_dockercmd=None): """ Starts container :warning: When dkrcmd_cls is of Async type, there is no guarantee that it is going to be up&running after return. """ def do_nothing(resutls): return resutls if custom_dockercmd is None: custom_dockercmd = dockercmd.DockerCmd if check_method is None: check_method = do_nothing if not subargs: subargs = [] self.sub_stuff['cidfiles'].add(cidfile) subargs.append('--cidfile %s' % cidfile) name = self.sub_stuff['dc'].get_unique_name() self.sub_stuff['containers'].append(name) subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append(cmd) dkrcmd = custom_dockercmd(self, 'run', subargs) check_method(dkrcmd.execute()) return dkrcmd
def initialize(self): super(tag_base, self).initialize() config.none_if_empty(self.config) di = DockerImages(self.parent_subtest) di.gen_lower_only = self.config['gen_lower_only'] name_prefix = self.config["tag_repo_name_prefix"] new_img_name = di.get_unique_name(name_prefix) while self.check_image_exists(new_img_name): new_img_name = "%s_%s" % (name_prefix, utils.generate_random_string(8)) self.sub_stuff["image"] = new_img_name base_image = DockerImage.full_name_from_defaults(self.config) prep_changes = DockerCmd(self.parent_subtest, "tag", [base_image, self.sub_stuff["image"]], self.config['docker_tag_timeout']) results = prep_changes.execute() if results.exit_status: raise xceptions.DockerTestNAError("Problems during " "initialization of" " test: %s", results) im = self.check_image_exists(self.sub_stuff["image"]) self.sub_stuff['image_list'] = im
def init_container(self, name): subargs = self.get_object_config(name, 'run_options_csv') if subargs: subargs = [arg for arg in self.config['run_options_csv'].split(',')] else: subargs = [] image = DockerImage.full_name_from_defaults(self.config) subargs.append(image) subargs.append("bash") cont = {'result': DockerCmd(self, 'run', subargs, 10)} self.sub_stuff['containers'].append(cont) cont_id = cont['result'].execute().stdout.strip() cont['id'] = cont_id # Cmd must contain one "exit $exit_status" cmd = self.get_object_config(name, 'exec_cmd') cont['exit_status'] = self.re_exit.findall(cmd)[0] sleep = self.re_sleep.findall(cmd) if sleep: sleep = int(sleep[0]) cont['sleep_time'] = sleep else: cont['sleep_time'] = 0 cont['test_cmd'] = AsyncDockerCmd(self, "attach", [cont_id]) cont['test_cmd_stdin'] = cmd
def initialize(self): super(commit_base, self).initialize() config.none_if_empty(self.config) name_prefix = self.config["commit_repo_name_prefix"] new_img_name = "%s_%s" % (name_prefix, utils.generate_random_string(8)) self.sub_stuff["new_image_name"] = new_img_name self.sub_stuff['rand_data'] = utils.generate_random_string(8) cmd_with_rand = (self.config['docker_data_prepare_cmd'] % self.sub_stuff['rand_data']) fqin = DockerImage.full_name_from_defaults(self.config) prep_changes = DockerCmd(self.parent_subtest, "run", ["--detach", fqin, cmd_with_rand], self.config['docker_commit_timeout']) results = prep_changes.execute() if results.exit_status: raise xceptions.DockerTestNAError("Problems during " "initialization of" " test: %s", results) else: self.sub_stuff["container"] = results.stdout.strip()
def postprocess(self): super(history_base, self).postprocess() # Raise exception if problems found expect = self.config["docker_expected_exit_status"] OutputGood(self.sub_stuff['cmdresult'], ignore_error=(expect != 0), skip=['nonprintables_check']) self.failif_ne(self.sub_stuff['cmdresult'].exit_status, expect, "Exit status") if expect == 0: new_img_name = self.sub_stuff["new_image_name"] new_img_name2 = self.sub_stuff["new_image_name2"] base_img_name = DockerImage.full_name_from_defaults(self.config) self.failif(base_img_name in self.sub_stuff['cmdresult'].stdout, "Unable find image name %s in image history: %s" % (base_img_name, self.sub_stuff['cmdresult'].stdout)) self.failif(new_img_name in self.sub_stuff['cmdresult'].stdout, "Unable find image name %s in image history: %s" % (new_img_name, self.sub_stuff['cmdresult'].stdout)) self.failif(new_img_name2 in self.sub_stuff['cmdresult'].stdout, "Unable find image name %s in image history: %s" % (new_img_name2, self.sub_stuff['cmdresult'].stdout))
def run_once(self): super(run_dns, self).run_once() fin = DockerImage.full_name_from_defaults(self.config) self.stuff['subargs'] = ['--privileged', '--rm', fin, "cat /etc/resolv.conf"] self.test_good() self.test_bad()
def init_subargs(self): dc = self.sub_stuff["dc"] cntr_name = dc.get_unique_name() self.sub_stuff["cntr_name"] = cntr_name subargs = self.config["run_options_csv"].split(",") self.sub_stuff["subargs"] = subargs subargs.append("--name=%s" % cntr_name) # Need to detect when container is running cidfile = os.path.join(self.tmpdir, cntr_name) self.sub_stuff["cidfile"] = cidfile subargs.append("--cidfile=%s" % cidfile) subargs.append("--volume=%s:/workdir" % self.sub_stuff["volume"]) subargs.append("--workdir=/workdir") # FQIN is always last subargs.append(DockerImage.full_name_from_defaults(self.config)) subargs.append("/bin/bash") subargs.append("-c") # Write to a file when signal received # Loop forever until marker-file exists command = ( '"' "echo 'foobar' > stop && " "rm -f stop && trap '/usr/bin/date +%%s> stop' %s && " "while ! [ -f stop ]; do /usr/bin/sleep 0.1s; done" '"' % self.config["listen_signal"] ) subargs.append(command)
def cleanup(self): super(build, self).cleanup() # Auto-converts "yes/no" to a boolean if self.config['remove_after_test']: # Remove all previously non-existing containers for cid in self.stuff['dc'].list_container_ids(): if cid in self.stuff['existing_containers']: continue # don't remove previously existing ones self.logdebug("Removing container %s", cid) dcmd = DockerCmd(self, 'rm', ['--force', '--volumes', cid]) dcmd.execute() dimg = self.stuff['di'] base_repo_fqin = DockerImage.full_name_from_defaults(self.config) # Remove all previously non-existing images for img in dimg.list_imgs(): if img in self.stuff['existing_images']: continue if img.full_name is None or img.full_name is '': thing = img.long_id else: thing = img.full_name # never ever remove base_repo_fqin under any circumstance if thing == base_repo_fqin: continue self.logdebug("Removing image %s", img) dcmd = DockerCmd(self, 'rmi', ['--force', thing]) dcmd.execute()
def initialize(self): super(memory_base, self).initialize() docker_containers = self.sub_stuff['docker_containers'] image = DockerImage.full_name_from_defaults(self.config) unit_list = ['', 'b', 'B', 'k', 'K', 'm', 'M', 'g', 'G'] memory_list = [] args = [] if self.config['expect_success'] == "PASS": memory_value = str(self.config['memory_value']) if memory_value is not '0': for unit in unit_list: memory_list.append(memory_value + unit) else: memory_list.append('0') else: memory_list.append(self.config['memory_min_invalid']) memory_list.append(self.config['memory_max_invalid']) memory_list.append(self.config['memory_invalid']) for memory in memory_list: name = docker_containers.get_unique_name() self.sub_stuff['mem_cntnr_name'] = name if self.config['expect_success'] == "PASS": self.sub_stuff['name'].append(name) args.append(self.combine_subargs(name, memory, image, '/bin/bash')) self.sub_stuff['subargs'] = args self.sub_stuff['container_memory'] = memory_list self.sub_stuff['memory_containers'] = [] self.sub_stuff['cgroup_memory'] = []
def create_cntnr(self, command, args='', execute=True, cls=DockerCmd): """ Return possibly executed DockerCmd instance :param name: Name for container :param command: Command argument for container :param args: Complete, space-separated argument list for container :param execute: If true, execute() will be called on instance :param cls: A DockerCmdBase subclass to use for instance :return: A new DockerCmd instance """ fqin = DockerImage.full_name_from_defaults(self.config) name = self.sub_stuff['dc'].get_unique_name() # scrape_names (above) depends on separate name argument from --name subargs = ['--name', name] subargs += get_as_list(self.config['extra_create_args']) subargs += [fqin, command] subargs += args.strip().split() dockercmd = cls(self, 'create', subargs) dockercmd.quiet = True if execute: dockercmd.execute() mustpass(dockercmd.cmdresult) self.sub_stuff['cntnr_names'].append(name) return dockercmd
def complete_docker_command_line(self): super(good_extra_tag, self).complete_docker_command_line() config_copy = self.config.copy() # Remove tag from config docker_repo_tag = config_copy.pop('docker_repo_tag') full_name_wo_tag = DockerImage.full_name_from_defaults(config_copy) return ["--tag=%s" % (docker_repo_tag), full_name_wo_tag]
def initialize(self): super(tag_base, self).initialize() config.none_if_empty(self.config) self.dkrimg.gen_lower_only = self.config['gen_lower_only'] new_img_name = self.dkrimg.get_unique_name() self.sub_stuff["image"] = new_img_name base_image = DockerImage.full_name_from_defaults(self.config) self.prep_image(base_image)
def _init_container(self, name, cmd): """ :return: tuple(container_cmd, container_name) """ if self.config.get('run_options_csv'): subargs = [ arg for arg in self.config['run_options_csv'].split(',') ] else: subargs = [] if name is True: name = self.sub_stuff['cont'].get_unique_name() elif name: name = name if name: subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append(cmd) container = DockerCmd(self, 'run', subargs, verbose=False) return container, name
def init_subargs(self): run_args = self.sub_stuff['run_args'] run_options_csv = self.config["run_options_csv"] if run_options_csv: run_args += get_as_list(run_options_csv) run_args.append('--name') name = self.sub_stuff['cont'].get_unique_name() self.sub_stuff['run_name'] = name run_args.append(name) fqin = DockerImage.full_name_from_defaults(self.config) run_args.append(fqin) bash_cmd = self.config['bash_cmd'] if bash_cmd: run_args += get_as_list(bash_cmd) exec_args = self.sub_stuff['exec_args'] exec_options_csv = self.config["exec_options_csv"] if exec_options_csv: exec_args += get_as_list(exec_options_csv) exec_args.append(name)
def _init_container(self, tty, cmd, cmd_input='\n'): """ Starts container """ name = self.sub_stuff['dc'].get_unique_name() self.sub_stuff['containers'].append(name) subargs = self.sub_stuff['subargs'][:] if tty: subargs.append('--tty=true') else: subargs.append('--tty=false') subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append(cmd) cmd = DockerCmd(self, 'run', subargs, timeout=30) try: cmd.execute(cmd_input) finally: result = cmd.cmdresult return result
def run_once(self): """ Run docker with the given --cgroup-parent; preserve cgroup info. """ super(run_cgroup_parent_base, self).run_once() cidfile = os.path.join(self.tmpdir, 'cidfile') self.sub_stuff['cidfile'] = cidfile subargs = [ '--rm', '--cgroup-parent=%s' % self.sub_stuff['cgroup_parent'], '--cidfile', cidfile, DockerImage.full_name_from_defaults(self.config), '/bin/cat', '/proc/1/cgroup' ] dc = DockerCmd(self, "run", subargs, timeout=self.config['docker_timeout']) self.sub_stuff["cmdresult"] = dc.execute()
def initialize(self): super(volumes_one_source, self).initialize() num_containers = self.config['num_containers'] commands = [] self.sub_stuff['names'] = [] exec_command = self.config['exec_command'] cntr_path = self.config['cntr_path'] host_path = self.tmpdir self.set_selinux_context(self, host_path) vols = ['--volume="%s:%s:z"' % (host_path, cntr_path)] fqin = [DockerImage.full_name_from_defaults(self.config)] for _ in range(num_containers): name = utils.generate_random_string(12) template_keys = { 'write_path': os.path.join(cntr_path, name), 'name': name } self.sub_stuff['names'] += [name] cmd = [exec_command % template_keys] subargs = ['--name=%s' % (name)] + vols + fqin + cmd commands.append(AsyncDockerCmd(self, 'run', subargs)) self.sub_stuff['commands'] = commands
def _init_container_normal(self, name): """ Starts container """ if self.config.get('run_options_csv'): subargs = [arg for arg in self.config['run_options_csv'].split(',')] else: subargs = [] if self.tty: subargs.append('--tty=true') else: subargs.append('--tty=false') 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()
def init_container(self, volume=None, volumes_from=None): """ Starts container """ subargs = config.get_as_list(self.config['run_options_csv']) if volume: subargs.append("--volume %s" % volume) if volumes_from: subargs.append("--volumes-from %s" % volumes_from) dc = self.sub_stuff['dc'] name = dc.get_unique_name() self.sub_stuff['containers'].append(name) subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("sh") read_fd, write_fd = os.pipe() self.sub_stuff['fds'].append(write_fd) self.sub_stuff['fds'].append(read_fd) dkrcmd = dockercmd.AsyncDockerCmd(self, 'run', subargs) dkrcmd.execute(read_fd) dkrcmd.stdin = write_fd os.close(read_fd) # no longer needed # This could take a while if image needs pulling find_name = lambda: str(dc.list_containers_with_name(name)) != [] self.failif_ne(utils.wait_for(find_name, timeout=self.config['docker_timeout']), True, # None == timeout "Container %s not in 'docker ps' output within" " timeout: %s" % (name, dc.list_containers())) # Don't match echo of the 'echo' command itself self.logdebug('Confirming %s is responding', name) os.write(write_fd, "echo -e '%s'\n" % self.OCTAL_READY) dkrcmd.wait_for_ready() self.logdebug('Confirmed') return dkrcmd
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 initialize(self): super(events, self).initialize() dc = self.stuff['dc'] = DockerContainers(self) fullname = dc.get_unique_name() fqin = DockerImage.full_name_from_defaults(self.config) # generic args have spots for some value substitution mapping = {'NAME': fullname, 'IMAGE': fqin} subargs = [] for arg in self.config['run_args'].strip().split(','): tmpl = Template(arg) # Ignores placeholders not in mapping subargs.append(tmpl.safe_substitute(mapping)) # test container executed later self.stuff['nfdc'] = DockerCmd(self, 'run', subargs) self.stuff['nfdc_cid'] = None # docker events command executed later events_cmd = AsyncDockerCmd(self, 'events', ['--since=0']) self.stuff['events_cmd'] = events_cmd self.stuff['events_cmdresult'] = None # These will be removed as expected events for cid are identified leftovers = self.config['expect_events'].strip().split(',') self.stuff['leftovers'] = leftovers for key, value in self.stuff.items(): self.logdebug("init %s = %s", key, value)
def initialize(self): super(commit_base, self).initialize() config.none_if_empty(self.config) di = DockerImages(self) new_img_name = di.get_unique_name() self.sub_stuff["new_image_name"] = new_img_name self.sub_stuff['rand_data'] = utils.generate_random_string(8) cmd_with_rand = (self.config['docker_data_prepare_cmd'] % self.sub_stuff['rand_data']) fqin = DockerImage.full_name_from_defaults(self.config) prep_changes = DockerCmd(self, "run", ["--detach", fqin, cmd_with_rand], self.config['docker_commit_timeout']) results = prep_changes.execute() if results.exit_status: raise xceptions.DockerTestNAError( "Problems during " "initialization of" " test: %s", results) else: self.sub_stuff["container"] = results.stdout.strip()
def init_subargs_imgcmd(self): fqin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['fqin'] = fqin self.sub_stuff['subargs'].append(fqin) self.sub_stuff['subargs'] += get_as_list(self.config['bash_cmd']) self.sub_stuff['subargs'].append(self.config['cmd'])
def init_image(self): fqin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['fqin'] = fqin
def initialize(self): super(inspect_keys, self).initialize() # make a container to check self.create_simple_container(self) image = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['image'] = image