def test_wait_for_entire_process_tree_death(self, popen_mock, sleep_mock): process_mock = MagicMock() process_mock.communicate.side_effect = [("1234", None), (None, None), ("4321", None), ("4321", None), (None, None)] returncode_mock = PropertyMock() returncode_mock.side_effect = [0, 1, 0, 0, 1] type(process_mock).returncode = returncode_mock popen_mock.return_value = process_mock process_utils.wait_for_entire_process_tree_death(["1234", "4321"]) expected = [ call(['ps', '-p', '1234', '-o', 'pid', '--no-headers'], stderr=-1, stdout=-1), call(['ps', '-p', '1234', '-o', 'pid', '--no-headers'], stderr=-1, stdout=-1), call(['ps', '-p', '4321', '-o', 'pid', '--no-headers'], stderr=-1, stdout=-1), call(['ps', '-p', '4321', '-o', 'pid', '--no-headers'], stderr=-1, stdout=-1), call(['ps', '-p', '4321', '-o', 'pid', '--no-headers'], stderr=-1, stdout=-1) ] self.assertEquals(popen_mock.call_args_list, expected) expected = [call(0.1), call(0.1), call(0.1)] self.assertEquals(sleep_mock.call_args_list, expected)
def side_effect(*args, **kwargs): process_mock = MagicMock() returncode_mock = PropertyMock() returncode_mock.return_value = 0 type(process_mock).returncode = returncode_mock if args[0][5] in process_tree.keys(): process_mock.communicate.return_value = ( process_tree[args[0][5]], None) else: process_mock.communicate.return_value = ("", None) return process_mock
def side_effect(*args, **kwargs): process_mock = MagicMock() returncode_mock = PropertyMock() if args[0][2] == "4321": returncode_mock.return_value = 0 process_mock.communicate.return_value = ("4321", None) else: returncode_mock.return_value = 1 process_mock.communicate.return_value = (None, None) type(process_mock).returncode = returncode_mock return process_mock
def test_kill(self, popen_mock): process_mock = MagicMock() process_mock.communicate.return_value = (None, None) returncode_mock = PropertyMock() returncode_mock.return_value = 0 type(process_mock).returncode = returncode_mock popen_mock.return_value = process_mock process_utils.kill_pids(["12321113230", "2312415453"], signal.SIGTERM) expected = [ call(['kill', '-15', '12321113230', '2312415453'], stderr=-1, stdout=-1) ] self.assertEquals(popen_mock.call_args_list, expected)
def decorator(old_function): stdin = TemporaryFile("w+t") json_dump(stdin_payload, stdin) stdin.seek(0) patcher = patch("insights.client.client.sys.stdin", new_callable=PropertyMock(return_value=stdin)) return patcher(old_function)
def test_get_children(self, popen_mock): process_mock = MagicMock() process_mock.communicate.return_value = ("123 \n \n 321\n", None) popen_mock.return_value = process_mock returncode_mock = PropertyMock() returncode_mock.return_value = 0 type(process_mock).returncode = returncode_mock result = process_utils.get_children("2312415453") self.assertEquals(result, ["123", "321"]) expected = [ call(['ps', '-o', 'pid', '--no-headers', '--ppid', '2312415453'], stderr=subprocess.PIPE, stdout=subprocess.PIPE) ] self.assertEquals(popen_mock.call_args_list, expected)
def test_is_process_not_running(self, popen_mock): process_mock = MagicMock() process_mock.communicate.return_value = ("", None) returncode_mock = PropertyMock() returncode_mock.return_value = 1 type(process_mock).returncode = returncode_mock popen_mock.return_value = process_mock result = process_utils.is_process_running("2312415453") self.assertEquals(result, False) expected = [ call(['ps', '-p', '2312415453', '-o', 'pid', '--no-headers'], stderr=-1, stdout=-1) ] self.assertEquals(popen_mock.call_args_list, expected)
def test_get_command_by_pid_not_exist(self, popen_mock): process_mock = MagicMock() process_mock.communicate.return_value = ("", None) returncode_mock = PropertyMock() returncode_mock.return_value = 1 type(process_mock).returncode = returncode_mock popen_mock.return_value = process_mock result = process_utils.get_command_by_pid("2312415453") self.assertEquals(result, "NOT_FOUND[2312415453]") expected = [ call(['ps', '-p', '2312415453', '-o', 'command', '--no-headers'], stderr=-1, stdout=-1) ] self.assertEquals(popen_mock.call_args_list, expected)
def boto_session(): role_mock = Mock() type(role_mock).arn = PropertyMock(return_value="role") resource_mock = Mock() resource_mock.Role.return_value = role_mock session_mock = Mock(region_name="us-west-2") session_mock.resource.return_value = resource_mock return session_mock