def initialize(self): super(only_tag, self).initialize() name_prefix = self.config["rmi_repo_tag_name_prefix"] rand_data = utils.generate_random_string(5).lower() self.sub_stuff["rand_data"] = rand_data im_name = "%s_%s" % (name_prefix, rand_data) im = self.check_image_exists(im_name) while im != []: rand_data = utils.generate_random_string(5).lower() self.sub_stuff["rand_data"] = rand_data im_name = "%s_%s" % (name_prefix, rand_data) im = self.check_image_exists(im_name) self.sub_stuff["image_name"] = im_name # Private to this instance, outside of __init__ prep_changes = DockerCmd(self, "tag", [self.parent_subtest.stuff["base_image"], self.sub_stuff["image_name"]], 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(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(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 initialize(self): super(port_base, self).initialize() self.sub_stuff['server_env'] = utils.generate_random_string(8) params = {'port': random.randrange(4000, 5000), 'container': 'SERVER', 'data': ('Testing data %s' % utils.generate_random_string(12))} self.sub_stuff['params'] = params self.sub_stuff['client_env'] = utils.generate_random_string(6)
def _setup(self, cg_parent): """ Set up test parameters: the value of the --cgroup--parent option. String may include {rand1} and/or {rand2}; we replace those with two pseudorandom strings. """ DockerVersion().require_client("1.10") self.sub_stuff['rand1'] = utils.generate_random_string(8) self.sub_stuff['rand2'] = utils.generate_random_string(8) self.sub_stuff['cgroup_parent'] = cg_parent.format(**self.sub_stuff)
def initialize(self): super(port_base, self).initialize() self.sub_stuff['server_env'] = utils.generate_random_string(8) params = { 'port': random.randrange(4000, 5000), 'container': 'SERVER', 'data': ('Testing data %s' % utils.generate_random_string(12)) } self.sub_stuff['params'] = params self.sub_stuff['client_env'] = utils.generate_random_string(6)
def _setup(self, cg_parent): """ Set up test parameters: the value of the --cgroup--parent option. String may include {rand1} and/or {rand2}; we replace those with two pseudorandom strings. """ DockerVersion().require_client("1.10") self.sub_stuff['rand1'] = utils.generate_random_string(8) self.sub_stuff['rand2'] = utils.generate_random_string(8) self.sub_stuff['cgroup_parent'] = cg_parent.format(**self.sub_stuff)
def make_test_files(host_path): # Symlink can't be mountpoint (e.g. for NFS, SMB, etc.) read_fn = utils.generate_random_string(24) write_fn = utils.generate_random_string(24) read_data = utils.generate_random_string(24) read_hash = hashlib.md5(read_data).hexdigest() tr_file = open(os.path.join(host_path, read_fn), 'wb') tr_file.write(read_data) tr_file.close() return (read_fn, write_fn, read_data, read_hash)
def make_test_files(host_path): # Symlink can't be mountpoint (e.g. for NFS, SMB, etc.) read_fn = utils.generate_random_string(24) write_fn = utils.generate_random_string(24) read_data = utils.generate_random_string(24) read_hash = hashlib.md5(read_data).hexdigest() tr_file = open(os.path.join(host_path, read_fn), 'wb') tr_file.write(read_data) tr_file.close() return (read_fn, write_fn, read_data, read_hash)
def make_test_files(host_path): # Symlink can't be mountpoint (e.g. for NFS, SMB, etc.) if (not os.path.isdir(host_path) or os.path.islink(host_path)): raise DockerTestNAError('Configured path "%s" is a symlink ' 'or not a directory' % host_path) read_fn = utils.generate_random_string(24) write_fn = utils.generate_random_string(24) read_data = utils.generate_random_string(24) read_hash = hashlib.md5(read_data).hexdigest() tr_file = open(os.path.join(host_path, read_fn), 'wb') tr_file.write(read_data) tr_file.close() return (read_fn, write_fn, read_data, read_hash)
def initialize(self): super(cp_symlink, self).initialize() # Create a randomly-named file with random content; we will # copy this into the container, into a path pointed to by # symlinks, then make sure it copied correctly. self.sub_stuff['xfer_file'] = 'cp_symlink_' + \ utils.generate_random_string(15) self.sub_stuff['xfer_content'] = utils.generate_random_string(30) xfer_file_local = os.path.join(self.tmpdir, self.sub_stuff['xfer_file']) with open(xfer_file_local, 'w') as xfer_file_fh: xfer_file_fh.write(self.sub_stuff['xfer_content']) self.sub_stuff['xfer_file_local'] = xfer_file_local
def make_test_files(host_path): # Symlink can't be mountpoint (e.g. for NFS, SMB, etc.) if (not os.path.isdir(host_path) or os.path.islink(host_path)): raise DockerTestNAError('Configured path "%s" is a symlink ' 'or not a directory' % host_path) read_fn = utils.generate_random_string(24) write_fn = utils.generate_random_string(24) read_data = utils.generate_random_string(24) read_hash = hashlib.md5(read_data).hexdigest() tr_file = open(os.path.join(host_path, read_fn), 'wb') tr_file.write(read_data) tr_file.close() return (read_fn, write_fn, read_data, read_hash)
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): """ Execute 6 variants of the same 'docker run -a' command and store the results. variants: - tty - nontty variants: - stdin (execute bash, put 'ls /\n exit\n' on stdin) - stdout (execute ls /) - stderr (execute ls /nonexisting/directory/...) """ super(run_attach_base, self).initialize() # Prepare a container config.none_if_empty(self.config) self.sub_stuff['dc'] = DockerContainers(self.parent_subtest) self.sub_stuff['containers'] = [] self._init_test_depenent() for tty in (True, False): # generate matrix of tested variants # interactive container cont = self._init_container(tty, 'bash', 'ls /\nexit\n') self.sub_stuff['res_stdin_%s' % tty] = cont # stdout container cont = self._init_container(tty, 'ls /') self.sub_stuff['res_stdout_%s' % tty] = cont # stderr container cont = self._init_container(tty, 'ls /I/hope/this/does/not/exist/%s' % utils.generate_random_string(6)) self.sub_stuff['res_stderr_%s' % tty] = cont
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(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 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 initialize(self): super(simple, self).initialize() subargs = ['--name=%s' % self.sub_stuff["container_name"]] subargs.append(self.sub_stuff['fqin']) subargs.append('/bin/bash') subargs.append('-c') contents = utils.generate_random_string(12) self.sub_stuff['file_contents'] = contents # /tmp file inside container cpfile = utils.generate_random_string(8) self.sub_stuff['cpfile'] = cpfile cmd = '\'echo "%s" > %s && md5sum %s\'' % (contents, cpfile, cpfile) subargs.append(cmd) nfdc = DockerCmd(self, 'run', subargs) cmdresult = mustpass(nfdc.execute()) self.sub_stuff['cpfile_md5'] = cmdresult.stdout.split()[0]
def initialize(self): super(simple, self).initialize() subargs = ["--name=%s" % self.sub_stuff["container_name"]] subargs.append(self.sub_stuff["fqin"]) subargs.append("/bin/bash") subargs.append("-c") contents = utils.generate_random_string(12) self.sub_stuff["file_contents"] = contents # /tmp file inside container cpfile = utils.generate_random_string(8) self.sub_stuff["cpfile"] = cpfile cmd = "'echo \"%s\" > %s && md5sum %s'" % (contents, cpfile, cpfile) subargs.append(cmd) nfdc = DockerCmd(self, "run", subargs) cmdresult = mustpass(nfdc.execute()) self.sub_stuff["cpfile_md5"] = cmdresult.stdout.split()[0]
def initialize(self): super(volume_mount, self).initialize() vol = "v_" + utils.generate_random_string(8) subargs = ["create", "--name", vol] mustpass(DockerCmd(self, "volume", subargs).execute()) self.sub_stuff["volume_name"] = vol # First container: bind-mounts new volume, then unpacks two # tarballs into it. I don't know why it has to be two tarballs # and not one, but the tar thing (vs plain copy) has to do # with pivot_root. Whatever that is. c1 = DockerContainers(self).get_unique_name(prefix="c1_") # Path is not configurable because it's hardcoded in the tarballs vol_binding = vol + ":/.imagebuilder-transient-mount" subargs = ["--name", c1, "-v", vol_binding, "busybox"] mustpass(DockerCmd(self, "create", subargs).execute()) self.sub_stuff["container1"] = c1 for ab in ["a", "b"]: tar_file = "cp_volume_mount_data_%s.tar" % ab tar_path = os.path.join(self.parent_subtest.bindir, tar_file) # Failure mode only happens if cp is via redirected stdin. docker_path = self.config["docker_path"] utils.run("%s cp - %s:/ < %s" % (docker_path, c1, tar_path)) # Second container also bind-mounts the volume, but directly # through the host filesystem. subargs = ["inspect", "-f", '"{{ .Mountpoint }}"', vol] inspect_cmd = mustpass(DockerCmd(self, "volume", subargs).execute()) mp = inspect_cmd.stdout.strip() c2 = DockerContainers(self).get_unique_name(prefix="c2_") subargs = ["--name", c2, "-v", mp + "/0:/mountdir", "-v", mp + "/1:/mountfile", "busybox", "cat", "/mountfile"] mustpass(DockerCmd(self, "create", subargs).execute()) self.sub_stuff["container2"] = c2
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 initialize(self): super(simple, self).initialize() subargs = ['--name=%s' % self.sub_stuff["container_name"]] subargs.append(self.sub_stuff['fqin']) subargs.append('/bin/bash') subargs.append('-c') contents = utils.generate_random_string(12) self.sub_stuff['file_contents'] = contents # /tmp file inside container cpfile = utils.generate_random_string(8) self.sub_stuff['cpfile'] = cpfile cmd = '\'echo "%s" > %s && md5sum %s\'' % (contents, cpfile, cpfile) subargs.append(cmd) nfdc = DockerCmd(self, 'run', subargs) cmdresult = mustpass(nfdc.execute()) self.sub_stuff['cpfile_md5'] = cmdresult.stdout.split()[0]
def initialize(self): super(delete_wrong_name, self).initialize() name_prefix = self.config["rmi_repo_tag_name_prefix"] rand_data = utils.generate_random_string(5).lower() self.sub_stuff["rand_data"] = rand_data im_name = "%s_%s" % (name_prefix, rand_data) im = self.check_image_exists(im_name) while im != []: rand_data = utils.generate_random_string(5) self.sub_stuff["rand_data"] = rand_data im_name = "%s_%s" % (name_prefix, rand_data) im = self.check_image_exists(im_name) self.sub_stuff["image_name"] = im_name
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 initialize(self): super(simple_base, self).initialize() rand_name = utils.generate_random_string(8) self.sub_stuff["rand_name"] = rand_name self.sub_stuff["subargs"].insert(0, "--name=\"%s\"" % rand_name) run_in_pipe_r, run_in_pipe_w = os.pipe() self.sub_stuff['file_desc'].append(run_in_pipe_r) self.sub_stuff['file_desc'].append(run_in_pipe_w) self.sub_stuff["run_in_pipe_w"] = run_in_pipe_w dkrcmd = AsyncDockerCmd(self.parent_subtest, 'run', self.sub_stuff['subargs'], verbose=True) # Runs in background self.sub_stuff['cmdresult'] = dkrcmd.execute(run_in_pipe_r) self.sub_stuff['cmd_run'] = dkrcmd self.wait_interactive_cmd() dkrcmd.update_result() self.logdebug("Detail after waiting: %s", dkrcmd) attach_options = self.config['attach_options_csv'].split(',') self.sub_stuff['subargs_a'] = attach_options c_name = self.sub_stuff["rand_name"] self.sub_stuff["containers"].append(c_name) cid = self.sub_stuff["cont"].list_containers_with_name(c_name) self.failif(cid == [], "Unable to search container with name %s" % (c_name))
def initialize(self): """ Execute 6 variants of the same 'docker run -a' command and store the results. variants: - tty - nontty variants: - stdin (execute bash, put 'ls /\n exit\n' on stdin) - stdout (execute ls /) - stderr (execute ls /nonexisting/directory/...) """ super(run_attach_base, self).initialize() # Prepare a container config.none_if_empty(self.config) self.sub_stuff['dc'] = DockerContainers(self) self.sub_stuff['containers'] = [] self._init_test_dependent() # TODO: Test w/ tty=True when some DockerCmd class supports pty # for tty in (True, False): # generate matrix of tested variants tty = False # interactive container cont = self._init_container(tty, 'bash', 'ls /\nexit\n') self.sub_stuff['res_stdin_%s' % tty] = cont # stdout container cont = self._init_container(tty, 'ls /') self.sub_stuff['res_stdout_%s' % tty] = cont # stderr container cont = self._init_container( tty, 'ls /I/hope/this/does/not/exist/%s' % utils.generate_random_string(6)) self.sub_stuff['res_stderr_%s' % tty] = cont
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(delete_wrong_name, self).initialize() rand_data = utils.generate_random_string(5).lower() self.sub_stuff["rand_data"] = rand_data im_name = DockerImages(self).get_unique_name() self.sub_stuff["image_name"] = im_name
def initialize(self): super(cp, self).initialize() name = self.stuff['container_name'] = utils.generate_random_string(12) subargs = ['--name=%s' % name] fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append('/bin/bash') subargs.append('-c') contents = utils.generate_random_string(12) self.stuff['file_contents'] = contents cpfile = '/tmp/' + utils.generate_random_string(8) self.stuff['cpfile'] = cpfile cmd = '\'echo "%s" > %s && md5sum %s\'' % (contents, cpfile, cpfile) subargs.append(cmd) nfdc = NoFailDockerCmd(self, 'run', subargs) cmdresult = nfdc.execute() self.stuff['cpfile_md5'] = cmdresult.stdout.split()[0]
def initialize(self): super(delete_wrong_name, self).initialize() rand_data = utils.generate_random_string(5).lower() self.sub_stuff["rand_data"] = rand_data im_name = DockerImages(self).get_unique_name() self.sub_stuff["image_name"] = im_name
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 generate_search(self, prefix, mask=None): """ Generate string not present in mask """ for _ in xrange(1000): search = prefix + utils.generate_random_string(6) if mask and search in itertools.chain.from_iterable(mask): continue break else: self.failif(True, "Unable to generate search in 1000 iterations. " "(%s, %s)" % (prefix, mask)) return search
def generate_special_name(self): img = self.sub_stuff['image_list'][0] tag = "%s_%s" % (img.tag, utils.generate_random_string(8)) repo = img.repo registry = img.repo_addr registry_user = img.user new_img_name = DockerImage.full_name_from_component(repo, tag, registry, registry_user) return new_img_name
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): 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 initialize(self): super(empty, self).initialize() # FIXME: Need a standard way to do this image_name_tag = ("%s%s%s" % (self.parent_subtest.config['image_name_prefix'], utils.generate_random_string(4), self.parent_subtest.config['image_name_postfix'])) image_name, image_tag = image_name_tag.split(':', 1) self.sub_stuff['image_name_tag'] = image_name_tag self.sub_stuff['image_name'] = image_name self.sub_stuff['image_tag'] = image_tag
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 generate_special_name(self): name_prefix = self.config["tag_repo_name_prefix"] img = self.sub_stuff['image_list'][0] tag = img.tag repo = img.repo registry = img.repo_addr reg_user = "******" % (name_prefix, utils.generate_random_string(8)) new_img_name = DockerImage.full_name_from_component(repo, tag, registry, reg_user) return new_img_name
def generate_search(self, prefix, mask=None): """ Generate string not present in mask """ for _ in xrange(1000): search = prefix + utils.generate_random_string(6) if mask and search in itertools.chain.from_iterable(mask): continue break else: self.failif( True, "Unable to generate search in 1000 iterations. " "(%s, %s)" % (prefix, mask)) return search
def initialize(self): super(run_twice, self).initialize() name = self.stuff['container_name'] = utils.generate_random_string(12) subargs = ['--name=%s' % name] fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append('/bin/bash') subargs.append('-c') cmd = '\'echo test\'' subargs.append(cmd) self.stuff['subargs'] = subargs self.stuff['cmdresults'] = [] self.stuff['2nd_cmdresults'] = []
def run_once(self): super(insert, self).run_once() fin = DockerImage.full_name_from_defaults(self.config) file_path = "/tmp/" + utils.generate_random_string(8) self.stuff['file_path'] = file_path subargs = [fin, self.config['file_url'], file_path] nfdc = NoFailDockerCmd(self, "insert", subargs, timeout=self.config['docker_timeout']) cmdresult = nfdc.execute() inserted_image = cmdresult.stdout.split()[-1].strip() self.stuff['inserted_image'] = inserted_image
def initialize(self): super(run_twice, self).initialize() name = self.stuff["container_name"] = utils.generate_random_string(12) subargs = ["--name=%s" % name] fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append("/bin/bash") subargs.append("-c") cmd = "'echo test'" subargs.append(cmd) self.stuff["subargs"] = subargs self.stuff["cmdresults"] = [] self.stuff["2nd_cmdresults"] = []
def initialize(self): super(run_twice, self).initialize() name = self.stuff['container_name'] = utils.generate_random_string(12) subargs = ['--name=%s' % name] fin = DockerImage.full_name_from_defaults(self.config) subargs.append(fin) subargs.append('/bin/bash') subargs.append('-c') cmd = '\'echo test\'' subargs.append(cmd) self.stuff['subargs'] = subargs self.stuff['cmdresults'] = [] self.stuff['2nd_cmdresults'] = []
def generate_special_name(self): """ keep the name, only get unique tag """ img = self.sub_stuff['image_list'][0] _tag = "%s_%s" % (img.tag, utils.generate_random_string(8)) if self.config['gen_lower_only']: _tag = _tag.lower() else: _tag += '_UP' # guarantee some upper-case repo = img.repo registry = img.repo_addr registry_user = img.user new_img_name = DockerImage.full_name_from_component( repo, _tag, registry, registry_user) return new_img_name
def generate_special_name(self): name_prefix = self.config["tag_repo_name_prefix"] img = self.sub_stuff['image_list'][0] tag = img.tag repo = "%s_%s" % (name_prefix, utils.generate_random_string(8)) if self.config['gen_lower_only']: repo = repo.lower() else: repo += '_UP' # guarantee some upper-case registry = img.repo_addr registry_user = img.user new_img_name = DockerImage.full_name_from_component( repo, tag, registry, registry_user) return new_img_name
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) self.sub_stuff["image_name"] = di.get_unique_name(":tag") 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']) dnamsg = ("Problems during initialization of" " test: %s") prep_changes.execute() if prep_changes.cmdresult.exit_status: raise DockerTestNAError(dnamsg % prep_changes.cmdresult) else: self.sub_stuff["container"] = prep_changes.cmdresult.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']) dkrcmd.execute() if dkrcmd.cmdresult.exit_status: raise DockerTestNAError(dnamsg % dkrcmd.cmdresult) args = ["-d", self.sub_stuff["image_name"], cmd_with_rand] prep_changes = DockerCmd(self, "run", args, self.config['docker_commit_timeout']) prep_changes.execute() if prep_changes.cmdresult.exit_status: raise DockerTestNAError(dnamsg % dkrcmd.cmdresult) else: prep_stdout = prep_changes.cmdresult.stdout.strip() self.sub_stuff["containers"].append(prep_stdout) im = self.check_image_exists(self.sub_stuff["image_name"]) self.sub_stuff['image_list'] = im
def _create_htpasswd(self): """ Write an htpasswd file in our current directory, for use by the registry container (via bind mount). Use the registry image itself to run htpasswd, with -n so output goes to stdout, then capture that stdout and write it to a file in our current directory. """ self.sub_stuff['user'] = '******' self.sub_stuff['passwd'] = utils.generate_random_string(12) registry_fqin = self.config['registry_fqin'] subargs = [ '--rm', '--entrypoint', 'htpasswd', registry_fqin, '-Bbn', self.sub_stuff['user'], self.sub_stuff['passwd'] ] htpasswd_cmdresult = DockerCmd(self, 'run', subargs).execute() with open('htpasswd', 'wb') as htpasswd_fh: htpasswd_fh.write(htpasswd_cmdresult.stdout)
def initialize(self): super(only_tag, self).initialize() rand_data = utils.generate_random_string(5).lower() self.sub_stuff["rand_data"] = rand_data im_name = DockerImages(self).get_unique_name() self.sub_stuff["image_name"] = im_name # Private to this instance, outside of __init__ prep_changes = DockerCmd(self, "tag", [ self.parent_subtest.stuff["base_image"], self.sub_stuff["image_name"] ], 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(volumes_one_source, self).initialize() num_containers = self.config['num_containers'] commands = [] self.sub_stuff['names'] = [] exec_command = self.config['exec_command'] cntr_path = self.config['cntr_path'] host_path = self.tmpdir self.set_selinux_context(self, host_path) vols = ['--volume="%s:%s:z"' % (host_path, cntr_path)] fqin = [DockerImage.full_name_from_defaults(self.config)] for _ in range(num_containers): name = utils.generate_random_string(12) template_keys = {'write_path': os.path.join(cntr_path, name), 'name': name} self.sub_stuff['names'] += [name] cmd = [exec_command % template_keys] subargs = ['--name=%s' % (name)] + vols + fqin + cmd commands.append(AsyncDockerCmd(self, 'run', subargs)) self.sub_stuff['commands'] = commands
def initialize(self): super(volume_mount, self).initialize() vol = 'v_' + utils.generate_random_string(8) subargs = ['create', '--name', vol] mustpass(DockerCmd(self, 'volume', subargs).execute()) self.sub_stuff['volume_name'] = vol # First container: bind-mounts new volume, then unpacks two # tarballs into it. I don't know why it has to be two tarballs # and not one, but the tar thing (vs plain copy) has to do # with pivot_root. Whatever that is. c1 = DockerContainers(self).get_unique_name(prefix='c1_') # Path is not configurable because it's hardcoded in the tarballs vol_binding = vol + ':/.imagebuilder-transient-mount' self.base_image = self.config['docker_registry_host'] + '/' + self.config['base_image'] subargs = ['--name', c1, '-v', vol_binding, self.base_image] mustpass(DockerCmd(self, 'create', subargs).execute()) self.sub_stuff['container1'] = c1 for ab in ['a', 'b']: tar_file = 'cp_volume_mount_data_%s.tar' % ab tar_path = os.path.join(self.parent_subtest.bindir, tar_file) # Failure mode only happens if cp is via redirected stdin. docker_path = self.config['docker_path'] utils.run("%s cp - %s:/ < %s" % (docker_path, c1, tar_path)) # Second container also bind-mounts the volume, but directly # through the host filesystem. subargs = ['inspect', '-f', '"{{ .Mountpoint }}"', vol] inspect_cmd = mustpass(DockerCmd(self, 'volume', subargs).execute()) mp = inspect_cmd.stdout.strip() c2 = DockerContainers(self).get_unique_name(prefix='c2_') subargs = ['--name', c2, '-v', mp + '/0:/mountdir:Z', '-v', mp + '/1:/mountfile:Z', self.base_image, 'cat', '/mountfile'] mustpass(DockerCmd(self, 'create', subargs).execute()) self.sub_stuff['container2'] = c2
def get_unique_name(self, prefix="", suffix="", length=4): """ Get unique name for a new image :param prefix: Name prefix :param suffix: Name suffix :param length: Length of random string (greater than 1) :return: Image name guaranteed to not be in-use. """ assert length > 1 if prefix: _name = "%s_%s_%%s" % (self.subtest.__class__.__name__, prefix) else: _name = "%s_%%s" % self.subtest.__class__.__name__ all_images = self.list_imgs_full_name() if suffix: _name += suffix for _ in xrange(1000): name = _name % utils.generate_random_string(length) if self.gen_lower_only: name = name.lower() if name not in all_images: return name
def add_repo(self, url): """ Adds package repository located on [url]. @param url: Universal Resource Locator of the repository. """ # Check if we URL is already set for section in self.cfgparser.sections(): for option, value in self.cfgparser.items(section): if option == 'url' and value == url: return True # Didn't find it, let's set it up while True: section_name = 'software_manager' + '_' section_name += utils.generate_random_string(4) if not self.cfgparser.has_section(section_name): break self.cfgparser.add_section(section_name) self.cfgparser.set(section_name, 'name', 'Autotest managed repository') self.cfgparser.set(section_name, 'url', url) self.cfgparser.set(section_name, 'enabled', 1) self.cfgparser.set(section_name, 'gpgcheck', 0) self.cfgparser.write(open(self.repo_file_path, "w"))
def initialize(self): super(commit_base, self).initialize() config.none_if_empty(self.config) di = DockerImages(self) new_img_name = di.get_unique_name() self.sub_stuff["new_image_name"] = new_img_name self.sub_stuff['rand_data'] = utils.generate_random_string(8) cmd_with_rand = (self.config['docker_data_prepare_cmd'] % self.sub_stuff['rand_data']) fqin = DockerImage.full_name_from_defaults(self.config) prep_changes = DockerCmd(self, "run", ["--detach", fqin, cmd_with_rand], self.config['docker_commit_timeout']) results = prep_changes.execute() if results.exit_status: raise xceptions.DockerTestNAError( "Problems during " "initialization of" " test: %s", results) else: self.sub_stuff["container"] = results.stdout.strip()
def migrate_wait(self, vms_name, srchost, dsthost, start_work=None, check_work=None, mig_mode="exec", params_append=None): vms_count = len(vms_name) mig_ports = [] host_offline_migration = self.params.get("host_mig_offline") sync = SyncData(self.master_id(), self.hostid, self.params.get("hosts"), {'src': srchost, 'dst': dsthost, 'port': "ports"}, self.sync_server) mig_params = {} if host_offline_migration != "yes": if self.params.get("hostid") == dsthost: last_port = 5199 for _ in range(vms_count): last_port = utils_misc.find_free_port(last_port + 1, 6000) mig_ports.append(last_port) mig_ports = sync.sync(mig_ports, timeout=120) mig_ports = mig_ports[dsthost] logging.debug("Migration port %s" % (mig_ports)) mig_cmds = {} for mig_port, vm_name in zip(mig_ports, vms_name): mig_dst_cmd = "nc -l %s %s" % (dsthost, mig_port) mig_src_cmd = "nc %s %s" % (dsthost, mig_port) mig_params["migration_exec_cmd_src_%s" % (vm_name)] = mig_src_cmd mig_params["migration_exec_cmd_dst_%s" % (vm_name)] = mig_dst_cmd else: # Generate filenames for migration. mig_fnam = {} for vm_name in vms_name: while True: fnam = ("mig_" + utils.generate_random_string(6) + "." + vm_name) fpath = os.path.join(self.test.tmpdir, fnam) if (fnam not in mig_fnam.values() and not os.path.exists(fnam)): mig_fnam[vm_name] = fpath break mig_fs = sync.sync(mig_fnam, timeout=120) mig_cmds = {} # Prepare cmd and files. if self.params.get("hostid") == srchost: mig_src_cmd = "gzip -c > %s" for vm_name in vms_name: mig_params["migration_sfiles_path_%s" % (vm_name)] = ( mig_fs[srchost][vm_name]) mig_params["migration_dfiles_path_%s" % (vm_name)] = ( mig_fs[dsthost][vm_name]) mig_params["migration_exec_cmd_src_%s" % (vm_name)] = ( mig_src_cmd % mig_fs[srchost][vm_name]) if self.params.get("hostid") == dsthost: mig_dst_cmd = "gzip -c -d %s" for vm_name in vms_name: mig_params["migration_exec_cmd_dst_%s" % (vm_name)] = ( mig_dst_cmd % mig_fs[dsthost][vm_name]) logging.debug("Exec commands %s", mig_cmds) super_cls = super(MultihostMigrationExec, self) super_cls.migrate_wait(vms_name, srchost, dsthost, start_work=start_work, mig_mode=mig_mode, params_append=mig_params)