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() executor.PYTHON_TIMEOUT_SECONDS = 5 def lauch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.lauch_python_subprocess = lauch_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_command", "fake_puppetFile", tmpoutfile, tmperrfile)) 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_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() executor.PYTHON_TIMEOUT_SECONDS = 0.1 kill_process_with_children_mock.side_effect = lambda pid : subproc_mock.terminate() def lauch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.lauch_python_subprocess = lauch_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_command", "fake_puppetFile", tmpoutfile, tmperrfile)) 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() executor.PYTHON_TIMEOUT_SECONDS = 5 def lauch_python_subprocess_method(command, tmpout, tmperr): subproc_mock.tmpout = tmpout subproc_mock.tmperr = tmperr return subproc_mock executor.lauch_python_subprocess = lauch_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("command", "file", tmpoutfile, tmperrfile) self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output'})