def test_watchdog_2(self): """ Tries to catch false positive watchdog invocations """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 thread = Thread(target = executor.run_file, args = ("fake_puppetFile", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS)) thread.start() time.sleep(0.1) subproc_mock.should_finish_event.set() subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, False, "Subprocess should not be terminated before timeout") self.assertEquals(subproc_mock.returncode, 0, "Subprocess should not be terminated before timeout")
def test_watchdog_2(self): """ Tries to catch false positive watchdog invocations """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstrucout = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 callback_method = MagicMock() thread = Thread(target = executor.run_file, args = ("fake_puppetFile", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstrucout, callback_method, "1-1")) thread.start() time.sleep(0.1) subproc_mock.should_finish_event.set() subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, False, "Subprocess should not be terminated before timeout") self.assertEquals(subproc_mock.returncode, 0, "Subprocess should not be terminated before timeout") self.assertTrue(callback_method.called)
def test_watchdog_1(self, kill_process_with_children_mock): """ Tests whether watchdog works """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 0.1 kill_process_with_children_mock.side_effect = lambda pid : subproc_mock.terminate() def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = None thread = Thread(target = executor.run_file, args = ("fake_puppetFile", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS)) thread.start() time.sleep(0.1) subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, True, "Subprocess should be terminated due to timeout")
def test_execution_results(self): subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() tmp_file = tempfile.NamedTemporaryFile() # the structured out file should be preserved across calls to the hooks and script. tmpstructuredoutfile = tmp_file.name tmp_file.close() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() callback_method = MagicMock() result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstructuredoutfile, callback_method, "1-1") self.assertEquals(result, {'exitcode': 0, 'stderr': '', 'stdout': '', 'structuredOut': {}}) self.assertTrue(callback_method.called)
def test_watchdog_1(self, kill_process_with_children_mock): """ Tests whether watchdog works """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstrucout = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 0.1 kill_process_with_children_mock.side_effect = lambda pid : subproc_mock.terminate() def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = None callback_method = MagicMock() thread = Thread(target = executor.run_file, args = ("fake_puppetFile", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstrucout, callback_method, '1')) thread.start() time.sleep(0.1) subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, True, "Subprocess should be terminated due to timeout") self.assertTrue(callback_method.called)
def test_watchdog_1(self, kill_process_with_children_mock): # Test hangs on Windows TODO if IS_WINDOWS: return """ Tests whether watchdog works """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AgentConfig("", ""), self.agentToggleLogger) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstrucout = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 0.1 kill_process_with_children_mock.side_effect = lambda pid : subproc_mock.terminate() def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = None thread = Thread(target = executor.run_file, args = ("fake_puppetFile", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstrucout,"INFO")) thread.start() time.sleep(0.1) subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, True, "Subprocess should be terminated due to timeout")
def test_watchdog_2(self): # Test hangs on Windows TODO if IS_WINDOWS: return """ Tries to catch false positive watchdog invocations """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AgentConfig("", ""), self.agentToggleLogger) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstrucout = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 environment_vars = [("PYTHONPATH", "a:b")] def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 thread = Thread(target = executor.run_file, args = ("fake_puppetFile", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstrucout, "INFO")) thread.start() time.sleep(0.1) subproc_mock.should_finish_event.set() subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, False, "Subprocess should not be terminated before timeout") self.assertEquals(subproc_mock.returncode, 0, "Subprocess should not be terminated before timeout")
def test_execution_results(self): subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstroutfile = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python: python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile, "INFO") self.assertEquals( result, { 'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output', 'structuredOut': {} })
def test_execution_results(self): self.assertEqual.__self__.maxDiff = None subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AgentConfig("", ""), self.agentToggleLogger) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstroutfile = tempfile.mkstemp() if IS_WINDOWS: if os.path.exists(tmpstroutfile): tmpstroutfile = tmpstroutfile + "_t" PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile, "INFO", True, None) self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output', 'structuredOut': {}})
def test_execution_results(self): subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() tmp_file = tempfile.NamedTemporaryFile() # the structured out file should be preserved across calls to the hooks and script. tmpstructuredoutfile = tmp_file.name tmp_file.close() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() callback_method = MagicMock() result = executor.run_file("file", ["arg1", "arg2"], "/fake_tmp_dir", tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstructuredoutfile, "INFO", callback_method, "1-1") self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output', 'structuredOut': {}}) self.assertTrue(callback_method.called)
def test_execution_results(self): self.assertEqual.__self__.maxDiff = None subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AgentConfig("", ""), self.agentToggleLogger) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstroutfile = tempfile.mkstemp() if IS_WINDOWS: if os.path.exists(tmpstroutfile): tmpstroutfile = tmpstroutfile + "_t" PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python: python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile, "INFO", True, None) self.assertEquals( result, { 'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output', 'structuredOut': {} })
def test_execution_results(self): subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstroutfile = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() callback_method = MagicMock() result = executor.run_file("file", ["arg1", "arg2"], "/fake_tmp_dir", tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile, "INFO", callback_method, "1-1") self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output', 'structuredOut': {}}) self.assertTrue(callback_method.called)
def test_watchdog_2(self): # Test hangs on Windows TODO if IS_WINDOWS: return """ Tries to catch false positive watchdog invocations """ subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AgentConfig("", ""), self.agentToggleLogger) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstrucout = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 environment_vars = [("PYTHONPATH", "a:b")] def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python: python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 thread = Thread(target=executor.run_file, args=("fake_puppetFile", ["arg1", "arg2" ], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstrucout, "INFO")) thread.start() time.sleep(0.1) subproc_mock.should_finish_event.set() subproc_mock.finished_event.wait() self.assertEquals( subproc_mock.was_terminated, False, "Subprocess should not be terminated before timeout") self.assertEquals( subproc_mock.returncode, 0, "Subprocess should not be terminated before timeout")
def test_execution_results(self): subproc_mock = self.Subprocess_mockup() executor = PythonExecutor("/tmp", AgentConfig("", "")) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() _, tmpstroutfile = tempfile.mkstemp() PYTHON_TIMEOUT_SECONDS = 5 def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.launch_python_subprocess = launch_python_subprocess_method runShellKillPgrp_method = MagicMock() runShellKillPgrp_method.side_effect = lambda python : python.terminate() executor.runShellKillPgrp = runShellKillPgrp_method subproc_mock.returncode = 0 subproc_mock.should_finish_event.set() result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile) self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output', 'structuredOut': {'msg': 'Unable to read structured output from ' + tmpstroutfile}})