def test_max_seconds(self): if sys.platform.startswith('win'): cmd = 'ping -w 5000 -n 1 1.0.0.0 > NUL' else: cmd = 'sleep 5000' code, out, err = run(cmd, maxSeconds=2, stopOnError=False) self.assertEquals(code, -2)
def test_max_seconds_output(self): if sys.platform.startswith('win'): cmd = 'echo Hello world && ping -w 4000 -n 1 1.0.0.0 > NUL' else: cmd = '%s -n "Hello world"; sleep 5000' % ECHO code, out, err = run(cmd, maxSeconds=2, stopOnError=False) if sys.platform.startswith('win'): out = out.rstrip() self.assertEquals(code, -2) self.assertEquals(out, 'Hello world')
def test_stderr(self): if sys.platform.startswith('win'): cmd = 'echo Hello world >&2' else: cmd = '%s -n "Hello world" > /dev/stderr' % ECHO code, out, err = run(cmd) if sys.platform.startswith('win'): err = err.rstrip() self.assertEquals(code, 0) self.assertEquals(out, '') self.assertEquals(err, 'Hello world')
def test_stdout(self): if sys.platform.startswith('win'): cmd = 'echo Hello world' else: cmd = '%s -n "Hello world"' % ECHO code, out, err = run(cmd) self.assertEquals(code, 0) if sys.platform.startswith('win'): out = out.rstrip() self.assertEquals(out, 'Hello world') self.assertEquals(err, '')
def test_simple(self): if sys.platform.startswith('win'): cmd = 'echo.' else: cmd = '%s -n' % ECHO code, out, err = run(cmd) if sys.platform.startswith('win'): #'echo' on win32 outputs at least a newline out = out.rstrip() self.assertEquals(code, 0) self.assertEquals(out, '') self.assertEquals(err, '')
def test_stdout_multiline(self): if sys.platform.startswith('win'): cmd = 'echo Hello world&& echo Beautiful world' else: cmd = r'%s -e "Hello world\nBeautiful world"' % ECHO code, out, err = run(cmd) if sys.platform.startswith('win'): out = out.replace('\r\n', '\n') self.assertEquals(code, 0) self.assertEquals(out, 'Hello world\nBeautiful world\n') self.assertEquals(err, '')
def _run_exit_test(self, *args, **kwargs): l = list() def exit(code): l.append(code) oldexit = sys.exit sys.exit = exit code, out, err = run(*args, **kwargs) sys.exit = oldexit return l[0]
def _execute_test(self, user=None, group=None): code, out, _ = run('id', stopOnError=False, user=user, group=group) self.assertEquals(code, 0) def get_name(s): return s.split('(')[1].rstrip(')') if user: name = get_name(out.split()[0]) self.assertEquals(name, self.USER) if group: name = get_name(out.split()[1]) self.assertEquals(name, self.GROUP)
def test_stdout_stderr(self): if sys.platform.startswith('win'): cmd = 'echo out && echo err >&2' else: cmd = '%(echo)s -n "out";' \ '%(echo)s -n "err" > /dev/stderr' % \ {'echo': ECHO} code, out, err = run(cmd) if sys.platform.startswith('win'): out = out.rstrip() err = err.rstrip() self.assertEquals(code, 0) self.assertEquals(out, 'out') self.assertEquals(err, 'err')
def test_long_running(self): if sys.platform.startswith('win'): run('ping -w 2000 -n 1 1.0.0.0 > NUL', stopOnError=False) else: run('sleep 2')
def test_run(self): cmd = 'yes | head -n2' output = run(cmd) self.assertEquals(output, (0, 'y\ny\n', ''))
def test_exitcode(self): code, out, err = run('exit 123', stopOnError=False) self.assertEquals(code, 123) self.assertEquals(out, '') self.assertEquals(err, '')
def test_run_python_execute_command(self): for run_ in xrange(20): cmd = '%s -uc "from pylabs.InitBase import q; q.system.process.execute(\'echo %d\', useShell=True)"' \ % (sys.executable, run_) run(cmd)