Esempio n. 1
0
        def mock_popen(program_args, **kwargs):
            """Mock of psutil.Popen that creates processes using os.fork().

            The processes created will always terminate within ~1 minute.

            Args:
                program_args: list(*). Unused program arguments that would
                    otherwise be passed to Popen.
                **kwargs: dict(str: *). Keyword arguments passed to Popen.

            Returns:
                PopenStub. The return value of psutil.Popen.
            """
            popen_calls.append(self.POPEN_CALL(program_args, kwargs))

            pid = 1
            stdout = b''.join(b'%b\n' % o for o in outputs)
            child_procs = [
                scripts_test_utils.PopenStub(pid=i, unresponsive=unresponsive)
                for i in range(pid + 1, pid + 1 + num_children)
            ]
            return scripts_test_utils.PopenStub(pid=pid,
                                                stdout=stdout,
                                                unresponsive=unresponsive,
                                                child_procs=child_procs)
    def test_explicit_attributes(self):
        child = scripts_test_utils.PopenStub()
        popen = scripts_test_utils.PopenStub(pid=123,
                                             name='foo',
                                             stdout='abc',
                                             stderr='def',
                                             reject_signal=True,
                                             reject_terminate=True,
                                             reject_kill=True,
                                             unresponsive=True,
                                             return_code=1,
                                             child_procs=[child])

        self.assertEqual(popen.pid, 123)
        self.assertEqual(popen.stdout.getvalue(), 'abc')
        self.assertEqual(popen.stderr.getvalue(), 'def')
        self.assertEqual(popen.poll_count, 0)
        self.assertEqual(popen.signals_received, [])
        self.assertEqual(popen.terminate_count, 0)
        self.assertEqual(popen.kill_count, 0)
        self.assertTrue(popen.alive)
        self.assertTrue(popen.reject_signal)
        self.assertTrue(popen.reject_terminate)
        self.assertTrue(popen.reject_kill)
        self.assertTrue(popen.unresponsive)
        self.assertEqual(popen.returncode, 1)
        self.assertEqual(popen.children(), [child])
        self.assertEqual(popen.name(), 'foo')
    def test_children(self):
        grandchild = scripts_test_utils.PopenStub()
        child = scripts_test_utils.PopenStub(child_procs=[grandchild])
        popen = scripts_test_utils.PopenStub(child_procs=[child])

        self.assertEqual(popen.children(), [child])
        self.assertEqual(popen.children(recursive=True), [child, grandchild])
Esempio n. 4
0
    def test_start_server_removes_elasticsearch_data(self):
        check_function_calls = {'shutil_rmtree_is_called': False}

        old_os_path_exists = os.path.exists

        def mock_os_remove_files(file_path):  # pylint: disable=unused-argument
            check_function_calls['shutil_rmtree_is_called'] = True

        def mock_os_path_exists(file_path):  # pylint: disable=unused-argument
            if file_path == common.ES_PATH_DATA_DIR:
                return True
            return old_os_path_exists(file_path)

        self.exit_stack.enter_context(self.swap_popen())
        self.exit_stack.enter_context(
            self.swap_to_always_return(subprocess,
                                       'call',
                                       value=scripts_test_utils.PopenStub()))
        self.exit_stack.enter_context(
            self.swap(shutil, 'rmtree', mock_os_remove_files))
        self.exit_stack.enter_context(
            self.swap(os.path, 'exists', mock_os_path_exists))
        self.exit_stack.enter_context(
            self.swap_to_always_return(common, 'wait_for_port_to_be_in_use'))

        self.exit_stack.enter_context(
            servers.managed_elasticsearch_dev_server())
        self.exit_stack.close()

        self.assertTrue(check_function_calls['shutil_rmtree_is_called'])
Esempio n. 5
0
    def test_wait_with_timeout_on_unresponive_popen_raises_timeout_error(
            self) -> None:
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        with self.assertRaisesRegex(psutil.TimeoutExpired,
                                    '10'):  # type: ignore[no-untyped-call]
            popen.wait(timeout=10)
Esempio n. 6
0
    def test_wait_on_unresponsive_popen_raises_runtime_error(self) -> None:
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        with self.assertRaisesRegex(
                RuntimeError,
                'entered an infinite loop'):  # type: ignore[no-untyped-call]
            popen.wait()
Esempio n. 7
0
def mock_managed_process(*unused_args, **unused_kwargs):
    """Mock method for replacing the managed_process() functions.

    Returns:
        Context manager. A context manager that always yields a mock
        process.
    """
    return contextlib2.nullcontext(enter_result=scripts_test_utils.PopenStub(
        alive=False))
    def test_poll(self):
        popen = scripts_test_utils.PopenStub()
        self.assertEqual(popen.poll_count, 0)

        self.assertIsNone(popen.poll())
        self.assertEqual(popen.poll_count, 1)

        popen.terminate()

        self.assertEqual(popen.poll(), 1)
        self.assertEqual(popen.poll_count, 2)
    def test_send_signal(self):
        popen = scripts_test_utils.PopenStub()

        self.assertEqual(popen.signals_received, [])
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.send_signal(signal.SIGINT)

        self.assertEqual(popen.signals_received, [signal.SIGINT])
        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 1)
Esempio n. 10
0
    def test_kill(self) -> None:
        popen = scripts_test_utils.PopenStub()

        self.assertEqual(popen.kill_count, 0)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.kill()

        self.assertEqual(popen.kill_count, 1)
        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 1)
Esempio n. 11
0
    def test_terminate(self):
        popen = scripts_test_utils.PopenStub()

        self.assertEqual(popen.terminate_count, 0)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.terminate()

        self.assertEqual(popen.terminate_count, 1)
        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 1)
Esempio n. 12
0
    def test_terminate_on_unresponsive_popen_does_nothing(self):
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        self.assertEqual(popen.terminate_count, 0)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.terminate()

        self.assertEqual(popen.terminate_count, 1)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 13
0
    def test_reject_signal(self):
        popen = scripts_test_utils.PopenStub(reject_signal=True)

        self.assertEqual(popen.signals_received, [])
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        with self.assertRaisesRegexp(OSError, 'rejected'):
            popen.send_signal(signal.SIGINT)

        self.assertEqual(popen.signals_received, [signal.SIGINT])
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 14
0
    def test_kill_on_unresponsive_popen_ends_process(self):
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        self.assertEqual(popen.kill_count, 0)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.kill()

        self.assertEqual(popen.kill_count, 1)
        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 1)
Esempio n. 15
0
    def test_reject_kill(self):
        popen = scripts_test_utils.PopenStub(reject_kill=True)

        self.assertEqual(popen.kill_count, 0)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        with self.assertRaisesRegexp(OSError, 'rejected'):
            popen.kill()

        self.assertEqual(popen.kill_count, 1)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 16
0
    def test_send_signal_on_unresponsive_popen_does_nothing(self):
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        self.assertEqual(popen.signals_received, [])
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.send_signal(signal.SIGINT)

        self.assertEqual(popen.signals_received, [signal.SIGINT])
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 17
0
    def test_reject_kill(self) -> None:
        popen = scripts_test_utils.PopenStub(reject_kill=True)

        self.assertEqual(popen.kill_count, 0)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        with self.assertRaisesRegex(
                OSError, 'rejected'):  # type: ignore[no-untyped-call]
            popen.kill()

        self.assertEqual(popen.kill_count, 1)
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 18
0
    def test_wait_with_timeout(self):
        popen = scripts_test_utils.PopenStub()

        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.wait(timeout=10)

        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.wait(timeout=10)

        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 19
0
    def test_wait(self) -> None:
        popen = scripts_test_utils.PopenStub()

        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.wait()

        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        popen.wait()

        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 20
0
    def test_communicate(self):
        popen = scripts_test_utils.PopenStub(stdout='abc', stderr='def')

        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        self.assertEqual(popen.communicate(), ('abc', 'def'))

        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        self.assertEqual(popen.communicate(), ('abc', 'def'))

        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 21
0
    def test_communicate_with_input(self):
        popen = scripts_test_utils.PopenStub(stdout='abc', stderr='def')

        self.assertEqual(popen.stdin.getvalue(), '')
        self.assertTrue(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        self.assertEqual(popen.communicate(input='ghi'), ('abc', 'def'))

        self.assertEqual(popen.stdin.getvalue(), 'ghi')
        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)

        self.assertEqual(popen.communicate(input='ghi'), ('abc', 'def'))

        self.assertEqual(popen.stdin.getvalue(), 'ghi')
        self.assertFalse(popen.is_running())
        self.assertEqual(popen.returncode, 0)
Esempio n. 22
0
    def test_default_attributes(self):
        popen = scripts_test_utils.PopenStub()

        self.assertEqual(popen.pid, 1)
        self.assertEqual(popen.stdout.getvalue(), '')
        self.assertEqual(popen.stderr.getvalue(), '')
        self.assertEqual(popen.poll_count, 0)
        self.assertEqual(popen.signals_received, [])
        self.assertEqual(popen.terminate_count, 0)
        self.assertEqual(popen.kill_count, 0)
        self.assertTrue(popen.alive)
        self.assertFalse(popen.reject_signal)
        self.assertFalse(popen.reject_terminate)
        self.assertFalse(popen.reject_kill)
        self.assertFalse(popen.unresponsive)
        self.assertEqual(popen.returncode, 0)
        self.assertEqual(popen.name(), 'process')
        self.assertEqual(popen.children(), [])
Esempio n. 23
0
 def mock_managed_protractor_server(**unused_kwargs):  # pylint: disable=unused-argument
     return contextlib2.nullcontext(
         enter_result=scripts_test_utils.PopenStub(
             stdout='sample\n✓\noutput\n', alive=False))
 def mock_check_call(cmd_tokens, **unsued_kwargs):  # pylint: disable=unused-argument
     self.cmd_token_list.append(cmd_tokens[2:])
     return scripts_test_utils.PopenStub()
Esempio n. 25
0
 def mock_popen(unused_commands, stdout, stderr):  # pylint: disable=unused-argument
     return scripts_test_utils.PopenStub(stdout=b'True', stderr=b'True')
Esempio n. 26
0
    def test_reassign_returncode(self):
        popen = scripts_test_utils.PopenStub(return_code=1)
        self.assertEqual(popen.returncode, 1)

        popen.returncode = 2
        self.assertEqual(popen.returncode, 2)
Esempio n. 27
0
    def test_communicate_on_unresponsive_popen_raises_runtime_error(self):
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        with self.assertRaisesRegexp(RuntimeError, 'entered an infinite loop'):
            popen.communicate()
Esempio n. 28
0
    def test_wait_with_timeout_on_unresponive_popen_raises_timeout_error(self):
        popen = scripts_test_utils.PopenStub(unresponsive=True)
        self.assertTrue(popen.unresponsive)

        with self.assertRaisesRegexp(psutil.TimeoutExpired, '10'):
            popen.wait(timeout=10)