def test_work_dir(): os.chdir("/") orig_cwd = os.getcwd() assert tempfile.gettempdir() != orig_cwd cmd = Command(['/bin/ls', '/etc/passwd'], work_dir=tempfile.gettempdir()) cmd.execute() assert os.getcwd() == orig_cwd
def test_work_dir(): os.chdir("/") orig_cwd = os.getcwd() assert tempfile.gettempdir() != orig_cwd cmd = Command(['/bin/ls', '/etc/passwd'], work_dir=tempfile.gettempdir()) cmd.execute() assert os.getcwd() == orig_cwd
def test_work_dir(self): os.chdir("/") orig_cwd = os.getcwd() self.assertNotEqual(orig_cwd, tempfile.gettempdir()) cmd = Command(['/bin/ls', '/etc/passwd'], work_dir=tempfile.gettempdir()) cmd.execute() self.assertEqual(orig_cwd, os.getcwd())
def test_resource_limits(): """ Simple smoke test for setting resource limits. """ resource_limits = {"RLIMIT_NOFILE": 1024} cmd = Command(['/bin/cat', '/etc/passwd'], resource_limits=resource_limits) cmd.set_resource_limits(resource_limits) cmd.execute()
def test_retcode(true_binary, false_binary): cmd = Command([false_binary]) cmd.execute() assert cmd.getretcode() != 0 assert cmd.getstate() == Command.FINISHED cmd = Command([true_binary]) cmd.execute() assert cmd.getretcode() == 0 assert cmd.getstate() == Command.FINISHED
def test_long_output(): """ Test that output thread in the Command class captures all of the output. (and also it does not hang the command by filling up the pipe) By default stderr is redirected to stdout. """ # in bytes, should be enough to fill a pipe num_lines = 5000 line_length = 1000 num_bytes = num_lines * (line_length + 1) with tempfile.NamedTemporaryFile() as file: for _ in range(num_lines): file.write(b'A' * line_length) file.write(b'\n') file.flush() assert os.path.getsize(file.name) == num_bytes cmd = Command(["/bin/cat", file.name]) cmd.execute() assert cmd.getstate() == Command.FINISHED assert cmd.getretcode() == 0 assert cmd.geterroutput() is None assert len("".join(cmd.getoutput())) == num_bytes
def test_stderr(): cmd = Command(["/bin/cat", "/foo/bar", "/etc/passwd"], redirect_stderr=False) cmd.execute() assert cmd.getstate() == Command.FINISHED assert cmd.getretcode() != 0 # The error could contain localized output strings so check just # for the path itself. assert '/foo/bar' in "\n".join(cmd.geterroutput()) assert '/foo/bar' not in "\n".join(cmd.getoutput()) assert 'root' in "\n".join(cmd.getoutput())
def test_stderr(self): cmd = Command(["/bin/cat", "/foo/bar", "/etc/passwd"], redirect_stderr=False) cmd.execute() self.assertEqual(Command.FINISHED, cmd.getstate()) self.assertNotEqual(0, cmd.getretcode()) # The error could contain localized output strings so check just # for the path itself. self.assertTrue("/foo/bar" in "\n".join(cmd.geterroutput())) self.assertFalse("/foo/bar" in "\n".join(cmd.getoutput())) self.assertTrue("root" in "\n".join(cmd.getoutput()))
def test_subst_multiple(): """ Test multiple substitutions within one argument. """ cmd = Command(['foo', 'aaa%ARG%bbb%XYZ%ccc', 'bar'], args_subst={"%ARG%": "blah", "%XYZ%": "xyz"}) assert cmd.cmd == ['foo', 'aaablahbbbxyzccc', 'bar']
def test_stderr(): cmd = Command(["/bin/cat", "/foo/bar", "/etc/passwd"], redirect_stderr=False) cmd.execute() assert cmd.getstate() == Command.FINISHED assert cmd.getretcode() != 0 # The error could contain localized output strings so check just # for the path itself. assert '/foo/bar' in "\n".join(cmd.geterroutput()) assert '/foo/bar' not in "\n".join(cmd.getoutput()) assert 'root' in "\n".join(cmd.getoutput())
def test_timeout(self): timeout = 30 cmd = Command(["/bin/sleep", str(timeout)], timeout=3) start_time = time.time() cmd.execute() # Check the process is no longer around. self.assertIsNotNone(cmd.getpid()) self.assertRaises(ProcessLookupError, os.kill, cmd.getpid(), 0) elapsed_time = time.time() - start_time self.assertTrue(elapsed_time < timeout) self.assertEqual(Command.TIMEDOUT, cmd.getstate()) self.assertEqual(None, cmd.getretcode())
def test_subst_append_exclsubst_nosubst(): """ Exclusive substituation is on however no substitution was performed, therefore arguments can be appended. """ cmd = Command(['foo', 'bar'], args_subst={"ARG": "blah"}, args_append=["1", "2"], excl_subst=True) assert cmd.cmd == ['foo', 'bar', '1', '2']
def test_subst_append_exclsubst(): """ Exclusive substitution is on and was performed, therefore no arguments should be appended. """ cmd = Command(['foo', 'ARG', 'bar'], args_subst={"ARG": "blah"}, args_append=["1", "2"], excl_subst=True) assert cmd.cmd == ['foo', 'blah', 'bar']
def test_opengrok_binary(command): """ Test that installed command is able to run :param command: the command name :return: """ print('DEBUG: {}'.format(command)) cmd = Command([command, '--help']) cmd.execute() print('DEBUG: out = {}'.format(cmd.getoutputstr())) assert cmd.getretcode() == 0 assert cmd.getstate() == Command.FINISHED assert len(cmd.getoutputstr()) > 1
def test_command_timeout(sleep_binary): timeout = 30 cmd = Command([sleep_binary, str(timeout)], timeout=3) start_time = time.time() cmd.execute() # Check the process is no longer around. assert cmd.getpid() is not None with pytest.raises(ProcessLookupError): os.kill(cmd.getpid(), 0) elapsed_time = time.time() - start_time assert elapsed_time < timeout assert cmd.getstate() == Command.TIMEDOUT assert cmd.getretcode() is None
def test_opengrok_version(command): """ Test that installed command has the version option :param command: the command name :return: """ cmd = Command([command, '--version']) cmd.execute() assert cmd.getretcode() == 0 assert cmd.getstate() == Command.FINISHED assert version in cmd.getoutputstr()
def test_resource_limits(): """ Simple smoke test for setting resource limits. """ resource_limits = {"RLIMIT_NOFILE": 1024} cmd = Command(['/bin/cat', '/etc/passwd'], resource_limits=resource_limits) cmd.set_resource_limits(resource_limits) cmd.execute()
def test_command_timeout(): timeout = 30 cmd = Command(["/bin/sleep", str(timeout)], timeout=3) start_time = time.time() cmd.execute() # Check the process is no longer around. assert cmd.getpid() is not None with pytest.raises(ProcessLookupError): os.kill(cmd.getpid(), 0) elapsed_time = time.time() - start_time assert elapsed_time < timeout assert cmd.getstate() == Command.TIMEDOUT assert cmd.getretcode() is None
def test_opengrok_binary(command): """ Test that installed command is able to run :param command: the command name :return: """ cmd = Command([command, '--help']) cmd.execute() assert cmd.getretcode() == 0 assert cmd.getstate() == Command.FINISHED assert len(cmd.getoutputstr()) > 1
def test_command_notimeout(): cmd_timeout = 30 cmd = Command(["/bin/sleep", "3"], timeout=cmd_timeout) cmd.execute() assert cmd.getstate() == Command.FINISHED assert cmd.getretcode() == 0
def test_command_notimeout(sleep_binary): cmd_timeout = 30 cmd = Command([sleep_binary, "3"], timeout=cmd_timeout) cmd.execute() assert cmd.getstate() == Command.FINISHED assert cmd.getretcode() == 0
def test_retcode_usr(self): cmd = Command(["/usr/bin/false"]) cmd.execute() self.assertNotEqual(0, cmd.getretcode()) self.assertEqual(Command.FINISHED, cmd.getstate()) cmd = Command(["/usr/bin/true"]) cmd.execute() self.assertEqual(0, cmd.getretcode()) self.assertEqual(Command.FINISHED, cmd.getstate())
def test_command_to_str(): cmd = Command(["foo", "bar"]) assert str(cmd) == "foo bar"
def test_retcode(true_binary, false_binary): cmd = Command([false_binary]) cmd.execute() assert cmd.getretcode() != 0 assert cmd.getstate() == Command.FINISHED cmd = Command([true_binary]) cmd.execute() assert cmd.getretcode() == 0 assert cmd.getstate() == Command.FINISHED
def test_execute_nonexistent(): cmd = Command(['/baaah', '/etc/passwd']) cmd.execute() assert cmd.getretcode() is None assert cmd.getstate() == Command.ERRORED
def test_env(): cmd = Command(['/usr/bin/env'], env_vars={'FOO': 'BAR', 'A': 'B'}) cmd.execute() assert "FOO=BAR\n" in cmd.getoutput()
def test_str(self): cmd = Command(["foo", "bar"]) self.assertEqual("foo bar", str(cmd))
def test_notimeout(self): cmd_timeout = 30 cmd = Command(["/bin/sleep", "3"], timeout=cmd_timeout) cmd.execute() self.assertEqual(Command.FINISHED, cmd.getstate()) self.assertEqual(0, cmd.getretcode())
def test_getoutput(): cmd = Command(['/bin/ls', '/etc/passwd']) cmd.execute() assert cmd.getoutput() == ['/etc/passwd\n']
def test_getoutput(self): cmd = Command(['/bin/ls', '/etc/passwd']) cmd.execute() self.assertEqual(['/etc/passwd\n'], cmd.getoutput())
def test_subst_append_default(): cmd = Command(['foo', '=ARG=', 33, 'bar'], args_subst={"ARG": "blah"}, args_append=["1", "2"]) assert cmd.cmd == ['foo', '=blah=', '33', 'bar', '1', '2']
def test_env(env_binary): cmd = Command([env_binary], env_vars={'FOO': 'BAR', 'A': 'B'}) cmd.execute() assert "FOO=BAR\n" in cmd.getoutput()
def test_env(): cmd = Command(['/usr/bin/env'], env_vars={'FOO': 'BAR', 'A': 'B'}) cmd.execute() assert "FOO=BAR\n" in cmd.getoutput()
def test_subst_append_default(self): cmd = Command(['foo', '=ARG=', 'bar'], args_subst={"ARG": "blah"}, args_append=["1", "2"]) self.assertEqual(['foo', '=blah=', 'bar', '1', '2'], cmd.cmd)
def test_execute_nonexistent(): cmd = Command(['/baaah', '/etc/passwd']) cmd.execute() assert cmd.getretcode() is None assert cmd.getstate() == Command.ERRORED
def test_getoutput(): cmd = Command(['/bin/ls', '/etc/passwd']) cmd.execute() assert cmd.getoutput() == ['/etc/passwd\n']
def test_execute_nonexistent(self): cmd = Command(['/baaah', '/etc/passwd']) cmd.execute() self.assertEqual(None, cmd.getretcode()) self.assertEqual(Command.ERRORED, cmd.getstate())