Пример #1
0
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
Пример #2
0
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
Пример #3
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())
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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()
Пример #7
0
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
Пример #8
0
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())
Пример #9
0
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())
Пример #10
0
 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()))
Пример #11
0
 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())
Пример #12
0
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
Пример #13
0
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
Пример #14
0
def test_execute_nonexistent():
    cmd = Command(['/baaah', '/etc/passwd'])
    cmd.execute()
    assert cmd.getretcode() is None
    assert cmd.getstate() == Command.ERRORED
Пример #15
0
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
Пример #16
0
def test_execute_nonexistent():
    cmd = Command(['/baaah', '/etc/passwd'])
    cmd.execute()
    assert cmd.getretcode() is None
    assert cmd.getstate() == Command.ERRORED
Пример #17
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
Пример #18
0
 def test_execute_nonexistent(self):
     cmd = Command(['/baaah', '/etc/passwd'])
     cmd.execute()
     self.assertEqual(None, cmd.getretcode())
     self.assertEqual(Command.ERRORED, cmd.getstate())
Пример #19
0
 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())