def _test_process(self, run_as_root): test_pid = str(os.getppid()) cmd = ['bash', '-c', '(sleep 10)'] proc = async_process.AsyncProcess(cmd, run_as_root=run_as_root) proc.start() self.addCleanup(self._stop_process, proc) common_utils.wait_until_true(lambda: proc._process.pid, sleep=0.5, timeout=10) bash_pid = utils.find_parent_pid(proc._process.pid) testcase_pid = utils.find_parent_pid(bash_pid) self.assertEqual(test_pid, testcase_pid)
def test_raises_unknown_exception(self): with testtools.ExpectedException(RuntimeError): self.m_execute.side_effect = RuntimeError() utils.find_parent_pid(-1)
def test_raises_exception_returncode_0(self): with testtools.ExpectedException(exceptions.ProcessExecutionError): self.m_execute.side_effect = \ exceptions.ProcessExecutionError('', returncode=0) utils.find_parent_pid(-1)
def test_returns_parent_id_for_good_ouput(self): self.m_execute.return_value = '123 \n' self.assertEqual(utils.find_parent_pid(-1), '123')
def test_returns_none_for_no_valid_pid(self): self.m_execute.side_effect = exceptions.ProcessExecutionError( '', returncode=1) self.assertIsNone(utils.find_parent_pid(-1))