def run_once(self): super(icc, self).run_once() # 1. Run with no options # Try to connect containers using python. self.run_cmd(self.sub_stuff["bash1"].stdin, "python -c 'import socket; s = socket.socket();" " s.bind((\"0.0.0.0\", 8081)); w = s.listen(10);" " w,_ = s.accept(); w.sendall(\"PING 64 bytes\");" " w.close(); s.close()'\n") time.sleep(1) self.run_cmd(self.sub_stuff["bash2"].stdin, "python -c 'print \"PING\"; import socket;" "s = socket.socket(); s.connect((\"%s\", 8081));" " print \"Recv:\" + s.recv(100); s.close();'\n" % self.sub_stuff["ip1"]) # Wait for possible ping passing. out_fn = lambda: self.sub_stuff["bash2"].stdout wait_for_output(out_fn, "64 bytes", 10) if "python: not found" in out_fn(): self.logdebug("Fall back to command ping") self.run_cmd(self.sub_stuff["bash2"].stdin, "ping %s\n" % self.sub_stuff["ip1"]) wait_for_output(out_fn, "64 bytes", 10) self.sub_stuff["bash1"].wait(1) self.sub_stuff["bash2"].wait(1)
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 touch_and_check(self, cont, volume, filename, context_pre, should_fail, context_eq): """ Touch file $filename using $cont, check it passed/fail and then verify context is (not) the same as $context_pre. Also verify all files have the same context. :param cont: dkrcmd instance :param volume: Path to shared volume (on host) :param filename: filename to touch on guest (relative to shared volume) :param context_pre: Reference context :param should_fail: Should the file creation fail? :param context_eq: Should the context be equal to reference one? :return: new context """ self.logdebug("Volume: %s Context: %s", volume, get_selinux_context(volume)) self.logdebug("Touching /tmp/test/%s in container" % filename) os.write(cont.stdin, "touch /tmp/test/%s\necho RET: $?\n" % filename) match = wait_for_output(lambda: cont.stdout, r'RET:\s+0$', timeout=10) if should_fail: self.failif( match, "File creation passed unexpectedly:" "\n%s" % cont.stdout) else: self.failif(not match, "Unable to create file:\n%s" % cont.stdout) context_post = get_selinux_context(volume) if context_eq: self.failif_ne(context_post, context_pre, "Selinux context") else: self.failif( context_pre == context_post, "Selinux context had not " "change (%s)." % context_post) self.check_context_recursive(volume, context_post) return context_post
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 start_test_container(self, read_fd, write_fd): dc = self.sub_stuff["dc"] name = dc.get_unique_name() subargs = ["--interactive", "--publish-all", "--name", name, self.sub_stuff["builds"][-1].image_name, "sh"] async_dkrcmd = AsyncDockerCmd(self, "run", subargs) self.sub_stuff["async_dkrcmd"] = async_dkrcmd async_dkrcmd.execute(read_fd) os.close(read_fd) os.write(write_fd, 'echo "StArTeD!"\n') self.failif(not wait_for_output(lambda: async_dkrcmd.stdout, "StArTeD!", timeout=10), str(async_dkrcmd)) return name
def start_base_container(self): reader, writer = os.pipe() # Exception could occur before os.close(reader) below self.sub_stuff['fds'].append(reader) self.sub_stuff['fds'].append(writer) self.sub_stuff['dkrcmd_stdin'] = writer dkrcmd = AsyncDockerCmd(self, 'run', self.sub_stuff['run_args']) dkrcmd.execute(reader) self.sub_stuff['containers'].append(self.sub_stuff['run_name']) os.close(reader) # not needed anymore self.sub_stuff['dkrcmd'] = dkrcmd os.write(writer, 'echo "Started"\n') if not wait_for_output(lambda: dkrcmd.stdout, "Started"): raise DockerTestFail("Unable to start base container:\n %s" % (dkrcmd))
def start_test_container(self, read_fd, write_fd): dc = self.sub_stuff['dc'] name = dc.get_unique_name() subargs = ['--interactive', '--publish-all', '--name', name, self.sub_stuff['builds'][-1].image_name, 'sh'] async_dkrcmd = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['async_dkrcmd'] = async_dkrcmd async_dkrcmd.execute(read_fd) os.close(read_fd) os.write(write_fd, 'echo "StArTeD!"\n') self.failif(not wait_for_output(lambda: async_dkrcmd.stdout, "StArTeD!", timeout=10), str(async_dkrcmd)) return name
def start_test_container(self, read_fd, write_fd): dc = self.sub_stuff['dc'] name = dc.get_unique_name() subargs = [ '--interactive', '--publish-all', '--name', name, self.sub_stuff['builds'][-1].image_name, 'sh' ] async_dkrcmd = AsyncDockerCmd(self, 'run', subargs) self.sub_stuff['async_dkrcmd'] = async_dkrcmd async_dkrcmd.execute(read_fd) os.close(read_fd) os.write(write_fd, 'echo "StArTeD!"\n') self.failif( not wait_for_output( lambda: async_dkrcmd.stdout, "StArTeD!", timeout=10), str(async_dkrcmd)) return name
def init_subcntrs(self): # Called from init_substitutions cntr = dockercmd.AsyncDockerCmd( self, 'run', ['--detach', self.sub_stuff['FQIN'], 'sleep 5m']) cntr.execute() # cntr.stdout is a property if output.wait_for_output(lambda: cntr.stdout, r'^\w{64}$'): self.sub_stuff['RUNCNTR'] = cntr.stdout.splitlines()[-1].strip() else: raise xceptions.DockerTestNAError("Failed to initialize %s" % self.config_section) # Throw away cntr, all we need is the CID result = mustpass( dockercmd.DockerCmd( self, 'run', ['--detach', self.sub_stuff['FQIN'], 'true']).execute()) # Only the CID is needed self.sub_stuff['STPCNTR'] = result.stdout.splitlines()[-1].strip()
def touch_and_check(self, cont, volume, filename, context_pre, should_fail, context_eq): """ Touch file $filename using $cont, check it passed/fail and then verify context is (not) the same as $context_pre. Also verify all files have the same context. :param cont: dkrcmd instance :param volume: Path to shared volume (on host) :param filename: filename to touch on guest (relative to shared volume) :param context_pre: Reference context :param should_fail: Should the file creation fail? :param context_eq: Should the context be equal to reference one? :return: new context """ self.logdebug("Volume: %s Context: %s", volume, get_selinux_context(volume)) self.logdebug("Touching /tmp/test/%s in container" % filename) # Some filesystems don't synchronize directory cache flushes if # pagecache for a file isn't also dirty. Always updating # content is easier that checking filesystem from inside a container. os.write(cont.stdin, "date > /tmp/test/%s\necho RET: $?\n" % filename) match = wait_for_output(lambda: cont.stdout, r'RET:\s+0$', timeout=10) if should_fail: self.failif(match, "File creation passed unexpectedly:" "\n%s" % cont.stdout) else: self.failif(not match, "Unable to create file:\n%s" % cont.stdout) context_post = get_selinux_context(volume) if context_eq: self.failif_ne(context_post, context_pre, "Selinux context") else: self.failif(context_pre == context_post, "Selinux context had not " "change (%s)." % context_post) self.check_context_recursive(volume, context_post) return context_post
def wait_for(self, dkrcmd, what, fail_msg, negative=False, stderr=False): if stderr: func = lambda: dkrcmd.stderr else: func = lambda: dkrcmd.stdout self.failif(negative == wait_for_output(func, what), fail_msg)