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(simple, self).run_once() # Prints out basic info self.loginfo("Starting docker command, timeout %s seconds", self.config['docker_timeout']) # Save image save_cmd = self.config['save_cmd'] self.sub_stuff['save_ar'] = (save_cmd % {"image": self.sub_stuff["rand_name"], "tmpdir": self.tmpdir}) dkrcmd = DockerCmd(self, 'save', [self.sub_stuff['save_ar']], verbose=True) self.sub_stuff['cmdresult_save'] = dkrcmd.execute() # Delete image dkrcmd = DockerCmd(self, 'rmi', [self.sub_stuff["rand_name"]], verbose=True) self.sub_stuff['cmdresult_del'] = dkrcmd.execute() # Load image load_cmd = self.config['load_cmd'] self.sub_stuff['load_ar'] = (load_cmd % {"image": self.sub_stuff["rand_name"], "tmpdir": self.tmpdir}) dkrcmd = DockerCmd(self, 'load', [self.sub_stuff['load_ar']], verbose=True) dkrcmd.verbose = True self.sub_stuff['cmdresult_load'] = dkrcmd.execute()
def filedir_contents_postprocess(self, build_def, command, parameter): bad_command = [command.find('file_exist') < 0, command.find('dir_exist') < 0, command.find('rx_file') < 0] if all(bad_command): raise DockerTestError('Command error: %s' % command) positive = command[0] != '!' # Need a character unlikely in file name params = get_as_list(parameter, ':') path = params[0] try: regex = re.compile(''.join(params[1:])) except IndexError: # parameter not specified regex = None # Only cmd differs between all commands (file, dir, rx). if command.find('file') > -1: cmd = 'cat "%s"' % path else: cmd = 'ls -la "%s"' % path subargs = ['--rm', '--attach', 'stdout', build_def.image_name, cmd] dkrcmd = DockerCmd(self, 'run', subargs, verbose=False) dkrcmd.quiet = True dkrcmd.execute() exists = dkrcmd.exit_status == 0 self.logdebug('%s(%s) exists: %s', command, path, exists) if command.find('exist') > -1: return positive == exists if not exists: return False # guaranteed failure, don't bother searching contents = dkrcmd.stdout.strip() mobj = regex.search(contents) self.logdebug('%s(%s) matches: %s', command, regex.pattern, bool(mobj)) return positive == bool(mobj)
def initialize(self): """ 1) Generate list of random $names and $repeat_count number of `docker load` commands associated to them 2) Run container for each name which saves the $name to a file 3) Commit the container to $name image 4) Remove the container """ super(stressed_load, self).initialize() # Generate $count random unique nonexisting image names self.sub_stuff['load_cmds'] = [] # tuple(name, dkrcmd) self.sub_stuff['cmdresults_save'] = [] # results of dkr save self.sub_stuff['cmdresults_del'] = [] # resutls of dkr rmi self.sub_stuff['cmdresults_load'] = [] # tuple(name, dkr load result) rand_names = [] while len(rand_names) < self.config['image_count']: name = utils.generate_random_string(8).lower() if (name not in rand_names and not os.path.exists(os.path.join(self.tmpdir, name))): rand_names.append(name) subargs = [self.config['load_cmd'] % {"image": name, "tmpdir": self.tmpdir}] for _ in xrange(self.config['repeat_count']): load_cmd = DockerCmd(self, 'load', subargs) self.sub_stuff['load_cmds'].append((name, load_cmd)) random.shuffle(self.sub_stuff['load_cmds']) # randomize the order self.sub_stuff['rand_names'] = rand_names for rand_name in rand_names: self.sub_stuff['containers'].append(rand_name) self.sub_stuff["images"].append(rand_name) cmd = "sh -c 'echo TEST: %s > /test_file'" % rand_name dkrcmd, name = self._init_container(rand_name, cmd) self.sub_stuff['containers'].append(name) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) cid = self.sub_stuff["cont"].list_containers_with_name(rand_name) self.failif(cid == [], "Unable to search container with name %s: details :%s" % (rand_name, cmdresult)) dkrcmd = DockerCmd(self, 'commit', [rand_name, rand_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) dkrcmd = DockerCmd(self, 'rm', [rand_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Failed to cleanup env for test: %s" % (cmdresult))
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 run_once(self): super(memory_base, self).run_once() memory_containers = self.sub_stuff['memory_containers'] for subargs in self.sub_stuff['subargs']: dkrcmd = DockerCmd(self, 'run -d -i', subargs) dkrcmd.execute() memory_containers.append(dkrcmd)
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 run_once(self): super(simple, self).run_once() # Prints out basic info self.loginfo("Starting docker command, timeout %s seconds", self.config['docker_timeout']) # Save image save_cmd = self.config['save_cmd'] self.sub_stuff['save_ar'] = (save_cmd % { "image": self.sub_stuff["rand_name"], "tmpdir": self.tmpdir }) dkrcmd = DockerCmd(self, 'save', [self.sub_stuff['save_ar']], verbose=True) self.sub_stuff['cmdresult_save'] = dkrcmd.execute() # Delete image dkrcmd = DockerCmd(self, 'rmi', [self.sub_stuff["rand_name"]], verbose=True) self.sub_stuff['cmdresult_del'] = dkrcmd.execute() # Load image load_cmd = self.config['load_cmd'] self.sub_stuff['load_ar'] = (load_cmd % { "image": self.sub_stuff["rand_name"], "tmpdir": self.tmpdir }) dkrcmd = DockerCmd(self, 'load', [self.sub_stuff['load_ar']], verbose=True) dkrcmd.verbose = True self.sub_stuff['cmdresult_load'] = dkrcmd.execute()
def initialize(self): super(simple, self).initialize() rand_name = utils.generate_random_string(8).lower() self.sub_stuff["rand_name"] = rand_name self.sub_stuff['containers'].append(rand_name) self.sub_stuff["images"].append(rand_name) dkrcmd = self._init_container(rand_name, self.config['docker_data_prep_cmd'])[0] cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: raise error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) rand_name = self.sub_stuff["rand_name"] cid = self.sub_stuff["cont"].list_containers_with_name(rand_name) self.failif( cid == [], "Unable to search container with name %s: details :%s" % (rand_name, cmdresult)) dkrcmd = DockerCmd(self, 'commit', [rand_name, rand_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: raise error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) dkrcmd = DockerCmd(self, 'rm', [rand_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: raise error.TestNAError("Failed to cleanup env for test: %s" % (cmdresult))
def initialize(self): super(simple, self).initialize() rand_name = utils.generate_random_string(8).lower() self.sub_stuff["rand_name"] = rand_name self.sub_stuff["subargs"].insert(0, "--name=\"%s\"" % rand_name) dkrcmd = DockerCmd(self, 'run', self.sub_stuff['subargs']) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) c_name = self.sub_stuff["rand_name"] self.sub_stuff['containers'].append(c_name) self.sub_stuff["images"].append(c_name) cid = self.sub_stuff["cont"].list_containers_with_name(c_name) self.failif(cid == [], "Unable to search container with name %s: details :%s" % (c_name, cmdresult)) dkrcmd = DockerCmd(self, 'commit', [c_name, c_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) dkrcmd = DockerCmd(self, 'rm', [c_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Failed to cleanup env for test: %s" % (cmdresult))
def cleanup(self): super(inspect_base, self).cleanup() if self.config['remove_after_test'] and self.sub_stuff['containers']: dkrcmd = DockerCmd(self.parent_subtest, 'rm', self.sub_stuff['containers']) dkrcmd.execute()
def cleanup(self): super(diff_base, self).cleanup() if self.config['remove_after_test']: dkrcmd = DockerCmd(self.parent_subtest, 'rm', [self.sub_stuff['name']]) dkrcmd.execute()
def create_image(self, old_name, new_name, cmd): prep_changes = DockerCmd(self.parent_subtest, "run", ["--name=%s" % new_name, old_name, cmd], self.config['docker_history_timeout']) results = prep_changes.execute() if results.exit_status: raise xceptions.DockerTestNAError("Problems during " "initialization of" " test: %s", results) else: self.sub_stuff["containers"].append(new_name) prep_changes = DockerCmd(self.parent_subtest, "commit", [new_name, new_name], self.config['docker_history_timeout']) results = prep_changes.execute() if results.exit_status: raise xceptions.DockerTestNAError("Problems during " "initialization of" " test: %s", results)
def cleanup(self): super(run_memory_base, self).cleanup() if self.config['remove_after_test']: for name in self.sub_stuff.get('name', []): self.logdebug("Cleaning up %s", name) dcmd = DockerCmd(self, 'rm', ['--force', name]) dcmd.execute()
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 _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 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_command(self, cmd, subargs, cmd_name): dkrcmd = DockerCmd(self, cmd, subargs, timeout=self.config['docker_timeout']) self.loginfo(dkrcmd.command) dkrcmd.execute() self.sub_stuff[cmd_name] = dkrcmd self.logdebug("Command %s: %s" % (cmd_name, dkrcmd.cmdresult))
def filedir_contents_postprocess(self, build_def, command, parameter): bad_command = [command.find('file_exist') < 0, command.find('dir_exist') < 0, command.find('rx_file') < 0] if all(bad_command): raise DockerTestError('Command error: %s' % command) positive = command[0] != '!' # Need a character unlikely in file name params = get_as_list(parameter, ':') path = params[0] try: regex = re.compile(''.join(params[1:])) except IndexError: # parameter not specified regex = None # Only cmd differs between all commands (file, dir, rx). if command.find('file') > -1: cmd = 'cat "%s"' % path else: cmd = 'ls -la "%s"' % path subargs = ['--rm', '--attach', 'stdout', build_def.image_name, cmd] dkrcmd = DockerCmd(self, 'run', subargs) dkrcmd.quiet = True dkrcmd.execute() exists = dkrcmd.exit_status == 0 self.logdebug('%s(%s) exists: %s', command, path, exists) if command.find('exist') > -1: return positive == exists if not exists: return False # guaranteed failure, don't bother searching contents = dkrcmd.stdout.strip() mobj = regex.search(contents) self.logdebug('%s(%s) matches: %s', command, regex.pattern, bool(mobj)) return positive == bool(mobj)
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): """ 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 initialize(self): super(simple, self).initialize() rand_name = utils.generate_random_string(8).lower() self.sub_stuff["rand_name"] = rand_name self.sub_stuff['containers'].append(rand_name) self.sub_stuff["images"].append(rand_name) dkrcmd = self._init_container(rand_name, self.config['docker_data_prep_cmd'])[0] cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) rand_name = self.sub_stuff["rand_name"] cid = self.sub_stuff["cont"].list_containers_with_name(rand_name) self.failif(cid == [], "Unable to search container with name %s: details :%s" % (rand_name, cmdresult)) dkrcmd = DockerCmd(self, 'commit', [rand_name, rand_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Unable to prepare env for test: %s" % (cmdresult)) dkrcmd = DockerCmd(self, 'rm', [rand_name]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: error.TestNAError("Failed to cleanup env for test: %s" % (cmdresult))
def cleanup(self): super(workdir, self).cleanup() if self.config['remove_after_test']: containers = [name for name in self.stuff['good_dirs'].keys()] dkrcmd = DockerCmd(self, 'rm', containers) dkrcmd.execute() containers = [name for name in self.stuff['bad_dirs'].keys()] dkrcmd = DockerCmd(self, 'rm', containers) dkrcmd.execute()
def cleanup(self): super(run_memory_base, self).cleanup() if self.config['expect_success'] == "PASS": if self.config['remove_after_test']: for name in self.sub_stuff['name']: dcmd = DockerCmd(self.parent_subtest, 'rm', ['--force', name]) dcmd.execute()
def run_once(self): super(exec_pid_count, self).run_once() subargs = self.sub_stuff['exec_args'] subargs.append("find /proc -maxdepth 1 -a -type d -a " "-regextype posix-extended -a " r"-regex '/proc/[0-9]+'") dkrcmd_exec = DockerCmd(self, 'exec', subargs) self.sub_stuff['dkrcmd_exec'] = dkrcmd_exec dkrcmd_exec.execute()
def run_once(self): super(memory_base, self).run_once() memory_containers = self.sub_stuff['memory_containers'] for subargs in self.sub_stuff['subargs']: dkrcmd = DockerCmd(self, 'run -d -i', subargs) dkrcmd.execute() if self.config['expect_success'] == 'PASS': mustpass(dkrcmd) else: mustfail(dkrcmd, 125) memory_containers.append(dkrcmd)
def cleanup(self): super(events, self).cleanup() if self.config['try_remove_after_test']: cid = self.stuff['nfdc_cid'] dc = self.stuff['dc'] try: dc.kill_container_by_long_id(cid) except ValueError: pass # container isn't running, this is fine. dcmd = DockerCmd(self, 'rm', ['--force', '--volumes', cid]) dcmd.execute() # don't care if this fails
def initialize(self): super(with_blocking_container_by_tag, self).initialize() rand_data = utils.generate_random_string(5) self.sub_stuff["rand_data"] = rand_data di = DockerImages(self.parent_subtest) name_prefix = self.config["rmi_repo_tag_name_prefix"] self.sub_stuff["image_name"] = di.get_unique_name(name_prefix) cmd_with_rand = self.config['docker_data_prepare_cmd'] % (rand_data) prep_changes = DockerCmd(self, "run", ["-d", self.parent_subtest.stuff['base_image'], cmd_with_rand], self.config['docker_commit_timeout']) results = prep_changes.execute() dnamsg = ("Problems during initialization of" " test: %s", results) if results.exit_status: raise DockerTestNAError(dnamsg) else: self.sub_stuff["container"] = results.stdout.strip() self.sub_stuff["containers"].append(self.sub_stuff["container"]) # Private to this instance, outside of __init__ dkrcmd = DockerCmd(self, 'commit', self.complete_commit_command_line(), self.config['docker_commit_timeout']) results = dkrcmd.execute() if results.exit_status: raise DockerTestNAError(dnamsg) prep_changes = DockerCmd(self, "run", ["-d", self.sub_stuff["image_name"], cmd_with_rand], self.config['docker_commit_timeout']) results = prep_changes.execute() if results.exit_status: raise DockerTestNAError(dnamsg) else: self.sub_stuff["containers"].append(results.stdout.strip()) im = self.check_image_exists(self.sub_stuff["image_name"]) self.sub_stuff['image_list'] = im
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 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 wait_for_container_death(self, container_obj): cont_id = container_obj.long_id prep_changes = DockerCmd(self, "wait", [cont_id], self.config['docker_run_timeout']) results = prep_changes.execute() return results.exit_status
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 run_once(self): super(every_last, self).run_once() total = len(self.sub_stuff['lastfiles']) self.sub_stuff['expected_total'] = total self.failif(total < self.config['max_files'], "Max files number expected : %d," "exceeds container total has : %d" % (self.config['max_files'], total)) self.loginfo("Testing copy of %d files from container" % total) self.sub_stuff['results'] = {} # cont_path -> cmdresult nfdc = DockerCmd(self, 'cp') nfdc.quiet = True nfiles = 0 for index, srcfile in enumerate(self.sub_stuff['lastfiles']): if index % 100 == 0: self.loginfo("Copied %d of %d", nfiles, total) cont_path = "%s:%s" % (self.sub_stuff['container_name'], srcfile) host_path = self.tmpdir host_fullpath = os.path.join(host_path, os.path.basename(srcfile)) nfdc.subargs = [cont_path, host_path] mustpass(nfdc.execute()) self.failif(not os.path.isfile(host_fullpath), "Not a file: '%s'" % host_fullpath) nfiles += 1 self.sub_stuff['nfiles'] = nfiles if nfiles >= self.config['max_files']: self.loginfo("Configuration max %d, Copied %d of %d" % (self.config['max_files'], nfiles, total)) break
def run_once(self): super(events, self).run_once() dc = self.stuff['dc'] # Start listening self.stuff['events_cmd'].execute() # Do something to make new events cmdresult = mustpass(self.stuff['nfdc'].execute()) cid = self.stuff['nfdc_cid'] = cmdresult.stdout.strip() while True: _json = dc.json_by_long_id(cid) if len(_json) > 0 and _json[0]["State"]["Running"]: self.loginfo("Waiting for test container to exit...") time.sleep(3) else: break if self.config['rm_after_run']: self.loginfo("Removing test container...") try: dc.kill_container_by_long_id(cid) except ValueError: pass # container isn't running, this is fine. dcmd = DockerCmd(self, 'rm', ['--force', '--volumes', cid]) mustpass(dcmd.execute()) # No way to know how long async events take to pass through :S self.loginfo("Sleeping %s seconds for events to catch up", self.config['wait_stop']) time.sleep(self.config['wait_stop']) # Kill off docker events after 1 second events_cmd = self.stuff['events_cmd'] self.stuff['events_cmdresult'] = events_cmd.wait(timeout=1)
def run_once(self): super(workdir, self).run_once() for name, _dir in self.stuff['good_dirs'].items(): subargs = ['--workdir=%s' % _dir] subargs.append('--name=%s' % name) subargs.append(self.stuff['fin']) subargs.append('pwd') nfdc = DockerCmd(self, 'run', subargs) self.stuff['cmdresults'][name] = mustpass(nfdc.execute()) for name, _dir in self.stuff['bad_dirs'].items(): subargs = ['--workdir=%s' % _dir] subargs.append('--name=%s' % name) subargs.append(self.stuff['fin']) subargs.append('pwd') dc = DockerCmd(self, 'run', subargs) self.stuff['cmdresults'][name] = dc.execute()
def initialize(self): super(systemd, self).initialize() unit_file_srcpath = '{}{}'.format(self.bindir, '/p4321.service') shutil.copyfile(unit_file_srcpath, self.config['sysd_unitf_dest']) dkrcmd = DockerCmd(self, 'build', ['--force-rm -t', self.config['name'], self.bindir]) mustpass(dkrcmd.execute())
def cleanup(self): super(empty, self).cleanup() if self.parent_subtest.config['try_remove_after_test']: dkrcmd = DockerCmd(self, 'rmi', [self.sub_stuff['image_name_tag']]) cmdresult = dkrcmd.execute() if cmdresult.exit_status != 0: self.logwarning("Cleanup command failed: %s" % cmdresult)
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 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 run_once(self): super(inspect_container_simple, self).run_once() subargs = [self.sub_stuff['name']] nfdc = DockerCmd(self, "inspect", subargs) self.sub_stuff['cmdresult'] = mustpass(nfdc.execute()) # Log details when command is successful self.logdebug(nfdc.cmdresult)
def run_once(self): super(every_last, self).run_once() total = len(self.sub_stuff["lastfiles"]) self.sub_stuff["expected_total"] = total self.failif( total < self.config["max_files"], "Max files number expected : %d," "exceeds container total has : %d" % (self.config["max_files"], total), ) self.loginfo("Testing copy of %d files from container" % total) self.sub_stuff["results"] = {} # cont_path -> cmdresult nfdc = DockerCmd(self, "cp") nfdc.quiet = True nfiles = 0 for index, srcfile in enumerate(self.sub_stuff["lastfiles"]): if index % 100 == 0: self.loginfo("Copied %d of %d", nfiles, total) cont_path = "%s:%s" % (self.sub_stuff["container_name"], srcfile) host_path = self.tmpdir host_fullpath = os.path.join(host_path, os.path.basename(srcfile)) nfdc.subargs = [cont_path, host_path] mustpass(nfdc.execute()) self.failif(not os.path.isfile(host_fullpath), "Not a file: '%s'" % host_fullpath) nfiles += 1 self.sub_stuff["nfiles"] = nfiles if nfiles >= self.config["max_files"]: self.loginfo("Configuration max %d, Copied %d of %d" % (self.config["max_files"], nfiles, total)) break
def cleanup(self): # Call empty parent's cleanup, not empty's. super(empty, self).cleanup() # pylint: disable=E1003 # Fail test if **successful** image_name = self.sub_stuff['image_name'] # assume id lookup failed if self.parent_subtest.config['remove_after_test']: dkrcmd = DockerCmd(self, 'rmi', [image_name]) mustfail(dkrcmd.execute(), 1)
def rm_container(self, what): # Can't use containers module b/c "rm" is the test-subject subargs = self.config['rm_options_csv'].strip().split(',') subargs.append(what) rm_cmd = DockerCmd(self, 'rm', subargs) self.sub_stuff['rm_cmdresult'] = rm_cmd.execute() wait_rm = self.config['wait_rm'] self.loginfo("Sleeping %d seconds after rm", wait_rm) time.sleep(wait_rm)
def run_once(self): super(syslog, self).run_once() subargs = [self.stuff['name'], self.stuff['params'], self.stuff['fin'], self.stuff['testcmds']] nfdc = DockerCmd(self, "run", subargs) self.stuff['cmdresults'] = mustpass(nfdc.execute())
def remove_lock_container(self): prep_changes = DockerCmd(self, "rm", [self.sub_stuff["container"]], self.config['docker_rmi_timeout']) results = prep_changes.execute() if results.exit_status: raise DockerTestNAError( "Problems during initialization of" " test: %s", results)
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 docker_push(self, img_name, tag_name): """ Run docker tag, then docker push against our local registry. Tag should always succeed but make no assumptions about push. """ push_image = self.config['push_image'] dc = DockerCmd(self, 'pull', [push_image]) mustpass(dc.execute()) self.sub_stuff['my_images'] += [push_image] self.sub_stuff['img_name'] = img_name self.sub_stuff['tag_name'] = tag_name pushed_name = ('{server}/{img_name}:{tag_name}'.format( **self.sub_stuff)) dc = DockerCmd(self, 'tag', [push_image, pushed_name]) mustpass(dc.execute()) self.sub_stuff['my_images'] += [pushed_name] return DockerCmd(self, 'push', [pushed_name]).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(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 initialize(self): super(long_term_app, self).initialize() kill_cmd = DockerCmd(self, "kill", [self.sub_stuff["container"].long_id], self.config['docker_run_timeout']) results = kill_cmd.execute() if results.exit_status: raise error.TestNAError( "Problems during initialization of" " test: %s", results)
def run_once(self): super(create_signal, self).run_once() cont = self.sub_stuff['cont'] sig = getattr(signal, self.config['listen_signal']) cont.kill_signal = sig # Should not fail sigdkrcmd = DockerCmd( self, 'kill', ['--signal', str(sig), self.get_cid()]) sigdkrcmd = mustpass(sigdkrcmd.execute()) self.sub_stuff['sigdkrcmd'] = sigdkrcmd
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 cntnr_veth_map(self): """ Return mapping of container names to veth* devices """ # map ifindex's to ``veth*`` names on host tmp_cmd = 'brctl show' cmd_result = utils.run(tmp_cmd, verbose=False) veths = re.findall(r'veth\w+', cmd_result.stdout) ifindex = [ int(open('/sys/class/net/%s/ifindex' % veth, 'r').read()) for veth in veths ] index_host = dict(zip(ifindex, veths)) self.logdebug("Host index to veth: %s", index_host) # 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 [ _name for (_name, json) in njs.iteritems() if json["NetworkSettings"]["MacAddress"] != "" ]: subargs = [name, 'cat', '/sys/class/net/eth0/ifindex'] dkrcmd = DockerCmd(self, 'exec', subargs, verbose=False) dkrcmd.execute() if dkrcmd.exit_status == 0: # Host's ifindex always one greater than container's ifindex = int(dkrcmd.stdout) + 1 # State could have changed during looping if ifindex in index_host: result[name] = index_host[ifindex] else: self.logdebug("Host veth %s dissapeared while mapping %s", ifindex, name) else: self.logdebug("Can't examine eth0 for container %s", name) self.logdebug("Container names to veth: %s", result) return result
def run_once(self): super(simple, self).run_once() # build arg list and execute command subargs = ["%s:%s" % (self.sub_stuff['container_name'], self.sub_stuff['cpfile'])] subargs.append(self.tmpdir) nfdc = DockerCmd(self, "cp", subargs, timeout=self.config['docker_timeout']) mustpass(nfdc.execute()) copied_path = "%s/%s" % (self.tmpdir, self.sub_stuff['cpfile'].split('/')[-1]) self.sub_stuff['copied_path'] = copied_path
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