def run_command(self, ctx): """run_command(testcondition) Runs command and tests responses as defined by the TestCondition. """ cmdline = ctx.cmdline stdin = ctx.stdin expectedout = ctx.expectedout expectederr = ctx.expectederr expectedexit = ctx.expectedexit environ = ctx.environ if expectederr: mergeerr = 0 else: mergeerr = 1 self.info("running: %s" % cmdline) p = proctools.spawnpipe(cmdline, env=environ, merge=mergeerr) if stdin: p.write(stdin) if expectedout: output = p.read() if expectederr: errors = p.readerr() p.wait() es = p.exitstatus if int(es) != expectedexit: return self.failed("bad exit value: expected %d, got %d" % (expectedexit, int(es))) if expectedout and (output != expectedout): return self.failed("bad output: %r" % (output)) if expectederr and (errors != expectederr): return self.failed("bad error output: %r" % (errors)) return self.passed("no errors")
def netcat_version(): """netcat_version() Return the version string for the netcat command on this system.""" nc = proctools.spawnpipe("%s -h" % (NETCAT)) ver = nc.readline() nc.read() # discard rest nc.wait() return ver.strip()
def sudo(command, user=None, password=None, extraopts=None, logfile=None): opts = "-S %s" % (IF(user, "-u %s" % (user,), ""),) cmd = "%s %s %s %s" % (SUDO, opts, extraopts or "", command) proc = proctools.spawnpipe(cmd, logfile=logfile, merge=0) if password: proc.readerr(9) # discard password prompt proc.write("%s\r" % (password,)) proc.readerr(1) # discard newline return proc
def test_method(self): ptest = proctools.spawnpipe("cat /etc/hosts | sort") hosts = ptest.read() self.assert_true(hosts) self.assert_false(ptest.readerr()) ptest.close() es = ptest.stat() if es: return self.passed(es) else: return self.failed(es)
def test_method(self): self.info("**** readline test") lspm = proctools.spawnpipe("ls /bin") lines = lspm.readlines() self.assert_true(lines) self.info(lspm.exitstatus) lspm.close() es = lspm.stat() if es: return self.passed(es) else: return self.failed(es)
def redir(lport, cport, laddr=None, caddr=None, extraopts=None): """redir(lport, cport, laddr=None, caddr=None, extraopts=None) Redirect local port to client port, possible to another host is caddr is given. Optionally bind to a specific IP if laddr is also given. """ opts = "--lport=%d --cport=%d" % (lport, cport) if laddr: opts += " --laddr=%s" % (laddr,) if caddr: opts += " --caddr=%s" % (caddr,) cmd = "%s %s %s" % (REDIR, opts, extraopts or "") proc = proctools.spawnpipe(cmd, merge=0) return proc
def test_method(self): self.info("***** ls with errors ") ls = proctools.spawnpipe("ls /usr/binxx", merge=0) self.info("output:") self.info(ls.read()) self.info("errors:") self.info(ls.readerr()) ls.close() ls.wait() es = ls.stat() if es: # should be abnormal exit self.diagnostic(es) return self.failed("normal exit.") else: return self.passed("'ls' exited abnormally as expected.")
def test_method(self): self.info("**** normal 'ls' test") ls = proctools.spawnpipe("ls /usr/bin") self.info(ls.stat()) files = ls.read() self.assert_true(files) self.verboseinfo(repr(files)) self.info("errors:") self.assert_false(ls.readerr()) ls.close() es = ls.stat() if es: # ExitStatus return self.passed(es) else: return self.failed(es)
def sudo_reset(): proc = proctools.spawnpipe("%s -k" % (SUDO,), merge=0) proc.read() proc.wait()
def alsaplayer(session=0, name="alsaplayer", device=DEVICE, extraopts="", logfile=None): opts = "-i daemon -q -n %s -s '%s' -d %s --nosave" % (session, name, device) CMD = "%s %s %s" % (PLAYER, opts, extraopts) aplayer = proctools.spawnpipe(CMD, logfile=logfile) return aplayer
def ssh_version(): """ssh_version() Return the version string for the ssh command on this system.""" ssh = proctools.spawnpipe("ssh -TV") ver = ssh.read() return ver