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_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 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 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 run_once(self): super(cp_symlink, self).run_once() self.sub_stuff['cname'] = DockerContainers(self).get_unique_name() # Run a container. It will wait for the presence of a signal file, # then check for presence of the xfer file and compare its contents # to the expected value. Different exit codes (tested in postprocess) # mean different errors. destdir = self.config['destdir'] command = ('\'ln -s /tmp /mylink; echo READY; while [ ! -f /stop ]; do sleep 0.2s; done; xfer_file=%s/%s; test -f $xfer_file || exit 4; actual=$(< $xfer_file); expect="%s"; test "$actual" = "$expect" && exit 0; echo $xfer_file: bad content "$actual" - expected "$expect";exit 5\'' % (destdir, self.sub_stuff['xfer_file'], self.sub_stuff['xfer_content'])) subargs = [ '--name=%s' % self.sub_stuff['cname'], DockerImage.full_name_from_defaults(self.config), '/bin/bash', '-c', command] self.sub_stuff['dkrcmd'] = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['dkrcmd'].execute() self.sub_stuff['dkrcmd'].wait_for_ready() # First: copy the desired destination file cp_dest = self.config['cp_dest'] if '%' in cp_dest: cp_dest = cp_dest % self.sub_stuff['xfer_file'] subargs = [ self.sub_stuff['xfer_file_local'], "%s:%s" % (self.sub_stuff['cname'], cp_dest) ] mustpass(DockerCmd(self, 'cp', subargs).execute()) # Second: create the signal file that tells the container to stop. # We don't use the content file as signal, first, because we # don't know if docker-cp is atomic, i.e. if we can have a race # condition where the destfile exists but does not have content; # and second, because there are situations in which docker-cp # to / (root) works but /tmp or /mylink->/tmp does not. subargs[1] = '%s:/stop' % self.sub_stuff['cname'] mustpass(DockerCmd(self, 'cp', subargs).execute())
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) container = AsyncDockerCmd(self, 'attach', subargs) self.sub_stuff['container_cmd'] = container # overwrites finished cmd container.execute()
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 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(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 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 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 _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 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 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 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 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 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(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 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 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(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 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 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 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(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): """ 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) c_attach = AsyncDockerCmd(self, 'attach', subargs) self.sub_stuff['container_cmd'] = c_attach # overwrites finished cmd c_attach.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, 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 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 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 initialize(self): super(short_term_app, self).initialize() docker_name = DockerImage.full_name_from_component( self.config["docker_repo_name"], self.config["docker_repo_tag"]) # 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 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 _init_container(self, subargs_in, 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_in: subargs_in = [] subargs = subargs_in[:] # Do not modify caller's copy 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 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 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 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 initialize(self): super(history_base, self).initialize() config.none_if_empty(self.config) dimgs = DockerImages(self) 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 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 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 init_dkrcmds(subtest, path_info, dockercmds): run_template = subtest.config['run_template'] cmd_tmplate = subtest.config['cmd_template'] fqin = DockerImage.full_name_from_defaults(subtest.config) for test_dict in path_info: dockercmds.append( subtest.make_dockercmd(subtest, DockerCmd, fqin, run_template, cmd_tmplate, test_dict))
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 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 lookup_image_id(self, image_name, image_tag): di = DockerImages(self) fqin = DockerImage.full_name_from_component(image_name, image_tag) imglst = di.list_imgs_with_full_name(fqin) try: # Don't care about duplicate ID's return imglst[0].long_id except IndexError: return None # expected by some sub-subtests
def initialize(self): super(import_export_base, self).initialize() self.sub_stuff['subargs'] = self.config['run_options_csv'].split(',') fin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['subargs'].append(fin) self.sub_stuff['subargs'].append(self.config['docker_data_prep_cmd']) self.sub_stuff["containers"] = [] self.sub_stuff["images"] = [] self.sub_stuff["cont"] = DockerContainers(self)
def initialize(self): super(diff_base, self).initialize() dc = DockerContainers(self) name = self.sub_stuff['name'] = dc.get_unique_name() fin = DockerImage.full_name_from_defaults(self.config) subargs = ['--name=%s' % (name), fin] subargs = subargs + self.config['command'].split(',') nfdc = DockerCmd(self, 'run', subargs) mustpass(nfdc.execute())
def initialize(self): super(CpBase, self).initialize() set_selinux_context(self.tmpdir) dc = self.sub_stuff['dc'] = DockerContainers(self) self.sub_stuff['di'] = DockerImages(self) container_name = dc.get_unique_name() self.sub_stuff['container_name'] = container_name fqin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['fqin'] = fqin
def run_once(self): super(oci_umount_bz1472121, self).run_once() fqin = DockerImage.full_name_from_defaults(self.config) bindmount = '/var/lib/docker/devicemapper' dc = DockerCmd(self, 'run', ['--rm', '-v', '%s:%s' % (bindmount, bindmount), fqin, 'true']) # On a system with faulty oci-umount, e.g. 1.12.6-47.git0fdc778.el7, # this will fail with "oci runtime error: ...error running hook: # signal: segmentation fault (core dumped)" OutputGood(dc.execute())
def run_once(self): super(flag, self).run_once() args = ["run"] args.append("--name=%s" % self.stuff["containter_name"]) fin = DockerImage.full_name_from_defaults(self.config) args.append(fin) args.append("/bin/bash") args.append("-c") args.append("\"echo negative test for docker flags\"") dc = DockerCmd(self, self.config["flag_args"], args) self.stuff["cmdresult"] = mustfail(dc.execute())
def run_once(self): super(run_dns, self).run_once() fin = DockerImage.full_name_from_defaults(self.config) privileged = ['--privileged'] if dockertest.docker_daemon.user_namespaces_enabled(): privileged.append('--userns=host') self.stuff['subargs'] = privileged + [ '--rm', fin, "cat /etc/resolv.conf" ] self.test_good() self.test_bad()
def initialize(self): super(workdir, self).initialize() self.stuff['fin'] = DockerImage.full_name_from_defaults(self.config) _path = utils.generate_random_string(12) self.stuff['good_dirs'] = {} self.stuff['good_dirs']['exist_dir'] = '/var/log' self.stuff['good_dirs']['nonexist_dir'] = '/tmp/%s' % _path self.stuff['bad_dirs'] = {} self.stuff['bad_dirs']['invalid_dir'] = 'tmp/%s' % _path self.stuff['bad_dirs']['file_as_dir'] = '/etc/hosts' self.stuff['cmdresults'] = {}
def create_simple_container(subtest): fin = DockerImage.full_name_from_defaults(subtest.config) name = utils.generate_random_string(12) subargs = ["--name=%s" % (name), fin, "/bin/bash", "-c", "'/bin/true'"] nfdc = DockerCmd(subtest, 'run', subargs) mustpass(nfdc.execute()) if not subtest.sub_stuff or not subtest.sub_stuff['containers']: subtest.sub_stuff['containers'] = [name] else: subtest.sub_stuff['containers'] += [name] return name
def initialize(self): self.sub_stuff['dc'] = DockerContainers(self) fqin = DockerImage.full_name_from_defaults(self.config) self.sub_stuff['fqin'] = fqin self.sub_stuff['run_options'] = (get_as_list( self.config['run_options_csv'])) self.sub_stuff['run_options'] += ['--name', self.get_run_name()] self.sub_stuff['top_options'] = (get_as_list( self.config['top_options_csv'])) self.sub_stuff['containers'] = [] self.sub_stuff['run_dkrcmd'] = self.init_run_dkrcmd() self.sub_stuff['top_dkrcmd'] = self.init_top_dkrcmd()
def initialize(self): super(attach_base, self).initialize() self.sub_stuff['subargs'] = self.config['run_options_csv'].split(',') fin = DockerImage.full_name_from_defaults(self.config) if self.image_exist(fin) == []: self.pull_image(fin) self.sub_stuff['subargs'].append(fin) self.sub_stuff['subargs'] += ['bash', '-c', self.config['cmd']] self.sub_stuff["containers"] = [] self.sub_stuff["images"] = [] self.sub_stuff["cont"] = DockerContainers(self) self.sub_stuff["file_desc"] = []
def _init_container(self, subargs, cmd): """ Prepares dkrcmd and stores the name in self.sub_stuff['containers'] :return: dkrcmd """ name = self.sub_stuff['dc'].get_unique_name() subargs.append("--name %s" % name) self.sub_stuff['containers'].append(name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append(cmd) dkrcmd = DockerCmd(self, 'run', subargs) return dkrcmd
def _init_container(self, subargs, cmd): """ Starts container """ name = self.sub_stuff['dc'].get_unique_name() self.sub_stuff['container'] = name subargs.append("--name %s" % name) fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("bash") subargs.append("-c") subargs.append(cmd) self.sub_stuff['cmd'] = DockerCmd(self, 'run', subargs, verbose=False)