def initialize(self): super(run_memory_base, self).initialize() docker_containers = DockerContainers(self.parent_subtest) image = DockerImage.full_name_from_defaults(self.config) unit_list = ['', 'b', 'B', 'k', 'K', 'm', 'M', 'g', 'G'] memory_list = [] self.sub_stuff['name'] = [] 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: prefix = self.config['memory_name_prefix'] name = docker_containers.get_unique_name(prefix, length=4) 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['cgroup_memory'] = [] self.sub_stuff['result'] = []
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(run_base, self).cleanup() if self.config['remove_after_test']: dc = DockerContainers(self) dc.clean_all(self.sub_stuff.get("containers")) di = DockerImages(self) di.clean_all(self.sub_stuff.get("images"))
def postprocess(self): self.loginfo("postprocess()") # Raise exception if problems found OutputGood(self.sub_stuff["cmdresult"]) self.failif_ne( self.sub_stuff["cmdresult"].exit_status, 0, "Non-zero commit exit status: %s" % self.sub_stuff["cmdresult"] ) im = self.check_image_exists(self.sub_stuff["new_image_name"]) # Needed for cleanup self.sub_stuff["image_list"] = im self.failif(len(im) < 1, "Failed to look up committed image ") self.check_file_in_image() # Check if is possible start image with default command. dc = DockerCmd( self, "run", ["--rm", self.sub_stuff["image_list"][0].long_id], timeout=self.config["docker_timeout"] ) results = dc.execute() dct = DockerContainers(self) cnts = dct.list_containers() for cont in cnts: if cont.image_name == self.sub_stuff["image_list"][0].full_name: try: dct.kill_container_by_long_id(cont.long_id) except ValueError: pass dc = DockerCmd(self, "rm", ["-f", cont.long_id]) rm_results = dc.execute() self.failif_ne(rm_results.exit_status, 0, "Non-zero commit exit status: %s" % rm_results) self.failif_ne(results.exit_status, 0, "Non-zero commit exit status: %s" % results) self.failif(not self.sub_stuff["rand_data"] in results.stdout, "Unexpected command result: %s" % results.stdout)
def cleanup(self): """ Cleanup the containers defined in self.sub_stuff['containers'] """ if self.config['remove_after_test']: dc = DockerContainers(self) dc.clean_all(self.sub_stuff.get("containers"))
def initialize(self): super(simple, self).initialize() config.none_if_empty(self.config) # Get free name docker_containers = DockerContainers(self) name = docker_containers.get_unique_name() self.sub_stuff['container_name'] = name
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 cleanup(self): """ Cleanup the container """ super(run_user, self).cleanup() if self.config['remove_after_test']: dc = DockerContainers(self) dc.clean_all([self.stuff.get("container")])
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(simple, self).initialize() self._init_stuff() config.none_if_empty(self.config) # Get free name prefix = self.config["container_name_prefix"] docker_containers = DockerContainers(self.parent_subtest) name = docker_containers.get_unique_name(prefix, length=4) self.sub_stuff['container_name'] = name
def cleanup(self): super(run_attach_stdout, self).cleanup() dc = DockerContainers(self.parent_subtest) name = self.sub_stuff["rand_name"] try: dc.kill_container_by_name(name) except ValueError: pass # death was the goal dc.remove_by_name(self.sub_stuff["rand_name"])
def initialize(self): super(flag, self).initialize() self.stuff["containter_name"] = [] self.stuff["subargs"] = [] self.stuff["cmdresult"] = [] docker_containers = DockerContainers(self) self.logdebug("Generating ramdom name will take 1 minute") cname = docker_containers.get_unique_name("docker", "test", 4) self.stuff["containter_name"] = cname
def cleanup(self): super(commit_base, self).cleanup() if self.config["remove_after_test"]: dc = DockerContainers(self) container = self.sub_stuff.get("container") if container: dc.clean_all([container]) di = di = DockerImages(self) images = [img.full_name for img in self.sub_stuff.get("image_list", [])] di.clean_all(images)
def initialize(self): super(diff_base, self).initialize() dc = DockerContainers(self.parent_subtest) scn = self.__class__.__name__ name = self.sub_stuff['name'] = dc.get_unique_name(prefix=scn) fin = DockerImage.full_name_from_defaults(self.config) subargs = ['--name=%s' % (name), fin] subargs = subargs + self.config['command'].split(',') nfdc = NoFailDockerCmd(self, 'run', subargs) nfdc.execute()
def initialize(self): super(run_attach_stdout, self).initialize() dc = DockerContainers(self.parent_subtest) self.sub_stuff["rand_name"] = rand_name = dc.get_unique_name() self.sub_stuff["rand_data"] = utils.generate_random_string(8) self.sub_stuff["subargs"].insert(0, "--name=\"%s\"" % rand_name) attach_options = self.config['attach_options_csv'].split(',') attach_options.append(rand_name) self.sub_stuff['attach_options'] = attach_options self.sub_stuff["rpipe"], self.sub_stuff["wpipe"] = os.pipe()
def cleanup(self): super(rmi_base, self).cleanup() di = DockerImages(self) # Auto-converts "yes/no" to a boolean if self.config['remove_after_test']: dc = DockerContainers(self) dc.clean_all(self.sub_stuff.get("containers")) di = DockerImages(self) imgs = [img.full_name for img in self.sub_stuff.get("image_list", [])] di.clean_all(imgs)
def cleanup(self): super(exec_base, self).cleanup() for ifd in self.sub_stuff['fds']: try: os.close(ifd) except OSError: pass if self.config['remove_after_test']: dc = DockerContainers(self) dc.clean_all(self.sub_stuff.get("containers")) di = DockerImages(self) di.clean_all(self.sub_stuff.get("images"))
def initialize(self): super(syslog, self).initialize() dc = DockerContainers(self) scn = self.__class__.__name__ name = self.stuff["container_name"] = dc.get_unique_name(prefix=scn) self.stuff['name'] = '--name=%s' % name self.stuff['fin'] = DockerImage.full_name_from_defaults(self.config) self.stuff['params'] = '-v /dev/log:/dev/log' now = datetime.datetime.now() time = now.strftime("%Y-%m-%d %H:%M:%S") self.stuff["msg"] = "SYSLOGTEST at time: %s" % time self.stuff['testcmds'] = 'logger %s' % self.stuff["msg"]
def initialize(self): """ Runs one container """ super(kill_bad_base, self).initialize() # Prepare a container docker_containers = DockerContainers(self.parent_subtest) name = docker_containers.get_unique_name() self.sub_stuff['container_name'] = name config.none_if_empty(self.config) self._init_container(name) time.sleep(self.config.get('wait_start', 3))
def try_rm(subtest, cidfilename, cmdresult): docker_containers = DockerContainers(subtest) try: cidfile = open(cidfilename, 'rb') cid = cidfile.read() if len(cid) < 12: raise ValueError() docker_containers.remove_by_id(cid.strip()) subtest.loginfo("Removed container %s", cid[:12]) except ValueError: subtest.logdebug("Container %s not found to rm", cid[:12]) except IOError: subtest.logdebug("Container never ran for %s", cmdresult)
def try_kill(subtest, cidfilename, cmdresult): docker_containers = DockerContainers(subtest) try: cidfile = open(cidfilename, 'rb') cid = cidfile.read() if len(cid) < 12: raise ValueError() else: docker_containers.kill_container_by_long_id(cid.strip()) except ValueError: subtest.logdebug("Container %s not found to kill", cid[:12]) except IOError: subtest.logdebug("Container never ran for %s", cmdresult)
def cleanup(self): super(selinux_base, self).cleanup() for fd in self.sub_stuff.get('fds', []): try: os.close(fd) except OSError: pass # closing was the goal if self.config['remove_after_test']: dc = DockerContainers(self) dc.clean_all(self.sub_stuff.get("containers", [])) for name in self.sub_stuff['volumes']: if os.path.exists(name): shutil.rmtree(name)
def init_use_names(self, use_names=False): if use_names: conts = self.sub_stuff['containers'] containers = DockerContainers(self.parent_subtest) containers = containers.list_containers() cont_ids = [cont['id'] for cont in conts] for cont in containers: if cont.long_id in cont_ids: if use_names is not True and random.choice((True, False)): continue # 50% chance of using id vs. name # replace the id with name cont_idx = cont_ids.index(cont.long_id) conts[cont_idx]['id'] = cont.container_name
def cntnr_ip_map(self): """ Return mapping of container names to IP addresses """ # map container eth0 ifindex's to names dc = DockerContainers(self) names = dc.list_container_names() jsons = [dc.json_by_name(name)[0] for name in names] njs = dict(zip(names, jsons)) result = {} for name in [_ for _ in njs if njs[_]["NetworkSettings"]["IPAddress"] != ""]: result[name] = njs[name]["NetworkSettings"]["IPAddress"] self.logdebug("%s -> %s", name, result[name]) return result
def init_subargs(self): """ Initialize basic arguments that will use for start container Will return a list 'args' """ docker_containers = DockerContainers(self) image = DockerImage.full_name_from_defaults(self.config) name = docker_containers.get_unique_name() self.sub_stuff['name'] = name bash_cmd = self.config['bash_cmd'] args = ["--name=%s" % name] args += get_as_list(self.config['run_args_csv']) args += [image, bash_cmd] return args
def initialize(self): super(syslog, self).initialize() if not os.path.isfile(self.config['syslogfile']): raise DockerTestNAError("Couldn't find system log: %s" % self.config['syslogfile']) dc = DockerContainers(self) name = self.stuff["container_name"] = dc.get_unique_name() self.stuff['name'] = '--name=%s' % name self.stuff['fin'] = DockerImage.full_name_from_defaults(self.config) self.stuff['params'] = '-v /dev/log:/dev/log' now = datetime.datetime.now() time = now.strftime("%Y-%m-%d %H:%M:%S") self.stuff["msg"] = "SYSLOGTEST at time: %s" % time self.stuff['testcmds'] = 'logger %s' % self.stuff["msg"]
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(simple, self).initialize() # Test container setup dc = DockerContainers(self) c_name = dc.get_unique_name() self.sub_stuff["containers"].append(c_name) self.init_test_container(c_name) # export/import command setup di = DockerImages(self) i_name = di.get_unique_name(c_name) # easier debugging self.sub_stuff["images"].append(i_name) subdct = {"container": c_name, "image": i_name} export_cmd_args = self.config['export_cmd_args'].strip() % subdct import_cmd_args = self.config['import_cmd_args'].strip() % subdct self.init_ei_dockercmd(export_cmd_args, import_cmd_args)
def cleanup(self): super(build, self).cleanup() # Auto-converts "yes/no" to a boolean if self.config['try_remove_after_test']: dc = DockerContainers(self) for cid in dc.list_container_ids(): dcmd = DockerCmd(self, 'rm', ['--force', '--volumes', cid]) dcmd.execute() di = DockerImages(self) if self.stuff.get('image_id') is not None: di.remove_image_by_id(self.stuff['image_id']) di.remove_image_by_full_name("empty_base_image") self.loginfo("Successfully removed test images") else: self.loginfo("NOT removing image")
def clean_up(self): super(images_prune, self).clean_up() ctns = DockerContainers(self) ctns.clean_all(['rsyslog'])
def cleanup(self): DockerContainers(self).clean_all(self.sub_stuff['my_containers']) DockerImages(self).clean_all(self.sub_stuff['my_images']) self._restore_docker_auth_file()