def testReturncode(self, cmd1, cmd2, returncode): p1 = v2v._simple_exec_cmd([cmd1], stdout=subprocess.PIPE) with terminating(p1): p2 = v2v._simple_exec_cmd([cmd2], stdin=p1.stdout, stdout=subprocess.PIPE) with terminating(p2): p = v2v.PipelineProc(p1, p2) p.wait(self.PROC_WAIT_TIMEOUT) self.assertEqual(p.returncode, returncode)
def testWait(self, cmd1, cmd2, waitRet): p1 = v2v._simple_exec_cmd(cmd1, stdout=subprocess.PIPE) with terminating(p1): p2 = v2v._simple_exec_cmd(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE) with terminating(p2): p = v2v.PipelineProc(p1, p2) ret = p.wait(2) p.kill() self.assertEqual(ret, waitRet)
def testWait(self, cmd1, cmd2, waitRet): if six.PY3 and waitRet: raise SkipTest('broken on Python 3') p1 = v2v._simple_exec_cmd(cmd1, stdout=subprocess.PIPE) with terminating(p1): p2 = v2v._simple_exec_cmd(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE) with terminating(p2): p = v2v.PipelineProc(p1, p2) ret = p.wait(2 * SHORT_SLEEP) p.kill() self.assertEqual(ret, waitRet)
def check_failure(self): with self.assertRaises(utils.TerminatingFailure) as e: with utils.terminating(self.proc): self.assertIsNone(self.proc_poll()) self.assertEqual(e.exception.pid, self.proc.pid) self.assertEqual(type(e.exception.error), ExpectedFailure)
def testRun(self): msg = 'foo\nbar' p1 = v2v._simple_exec_cmd(['echo', '-n', msg], stdout=subprocess.PIPE) with terminating(p1): p2 = v2v._simple_exec_cmd(['cat'], stdin=p1.stdout, stdout=subprocess.PIPE) with terminating(p2): p = v2v.PipelineProc(p1, p2) self.assertEqual(p.pids, [p1.pid, p2.pid]) ret = p.wait(self.PROC_WAIT_TIMEOUT) self.assertEqual(ret, True) out = p.stdout.read() self.assertEqual(out, msg)
def test_terminating_with_infected_kill(self): with MonkeyPatchScope([(zombiereaper, 'autoReapPID', self.reaped.add) ]): self.proc.kill = lambda: None with utils.terminating(self.proc): self.assertTrue(os.path.exists(self.proc_path)) self.assertTrue(self.proc.pid in self.reaped)
def test_terminating_with_infected_kill(self): with MonkeyPatchScope([(zombiereaper, 'autoReapPID', self.reaped.add )]): self.proc.kill = lambda: None with utils.terminating(self.proc): self.assertTrue(os.path.exists(self.proc_path)) self.assertTrue(self.proc.pid in self.reaped)
def test_process_terminated(self): self.proc.terminate() self.proc.wait() def fail(): raise RuntimeError("Attempt to kill a terminated process") self.proc.kill = fail with utils.terminating(self.proc): pass self.assertEqual(self.proc.returncode, -signal.SIGTERM)
def test_process_zombie(self): self.proc.terminate() wait_for_zombie(self.proc, 1) def fail(): raise RuntimeError("Attempt to kill a zombie process") self.proc.kill = fail with utils.terminating(self.proc): pass self.assertEqual(self.proc.returncode, -signal.SIGTERM)
def test_terminating_with_kill_exception(self): class FakeKillError(Exception): pass def fake_kill(): raise FakeKillError("fake kill exception") self.proc.kill = fake_kill with utils.terminating(self.proc): self.assertIsNone(self.proc.poll()) self.assertIsNone(self.proc.returncode)
def test_terminating_with_kill_exception(self): class FakeKillError(Exception): pass def fake_kill(): raise FakeKillError("fake kill exception") with MonkeyPatchScope([(zombiereaper, 'autoReapPID', self.reaped.add) ]): self.proc.kill = fake_kill with utils.terminating(self.proc): self.assertTrue(os.path.exists(self.proc_path)) self.assertTrue(self.proc.pid not in self.reaped)
def copyFromImage(dstImgPath, methodArgs): fileObj = methodArgs['fileObj'] bytes_left = total_size = methodArgs['length'] cmd = [ constants.EXT_DD, "if=%s" % dstImgPath, "bs=%s" % constants.MEGAB, "count=%s" % (total_size / constants.MEGAB + 1) ] p = commands.execCmd(cmd, sync=False) p.blocking = True with utils.terminating(p): _copyData(p.stdout, fileObj, bytes_left)
def test_terminating_with_kill_exception(self): class FakeKillError(Exception): pass def fake_kill(): raise FakeKillError("fake kill exception") self.proc.kill = fake_kill with self.assertRaises(utils.TerminatingFailure) as e: with utils.terminating(self.proc): self.assertIsNone(self.proc.poll()) self.assertEqual(e.exception.pid, self.proc.pid) self.assertEqual(type(e.exception.error), FakeKillError) self.assertIsNone(self.proc.returncode)
def test_terminating_with_kill_exception(self): class FakeKillError(Exception): pass def fake_kill(): raise FakeKillError("fake kill exception") with MonkeyPatchScope([(zombiereaper, 'autoReapPID', self.reaped.add )]): self.proc.kill = fake_kill with utils.terminating(self.proc): self.assertTrue(os.path.exists(self.proc_path)) self.assertTrue(self.proc.pid not in self.reaped)
def copyToImage(dstImgPath, methodArgs): totalSize = getLengthFromArgs(methodArgs) fileObj = methodArgs['fileObj'] cmd = [constants.EXT_DD, "of=%s" % dstImgPath, "bs=%s" % constants.MEGAB] p = commands.execCmd(cmd, sync=False) with utils.terminating(p): _copyData(fileObj, p.stdin, totalSize) p.stdin.close() if not p.wait(WAIT_TIMEOUT): log.error("timeout waiting for dd process") raise se.StorageException() if p.returncode != 0: log.error("dd error - code %s, stderr %s", p.returncode, p.stderr.read(1000)) raise se.MiscFileWriteException()
def _query_v2v_caps(self): self._v2v_caps = frozenset() p = _simple_exec_cmd([_VIRT_V2V.cmd, '--machine-readable'], env=os.environ.copy(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) with terminating(p): try: out, err = p.communicate() except Exception: logging.exception('Terminating virt-v2v process after error') raise if p.returncode != 0: raise V2VProcessError('virt-v2v exited with code: %d, stderr: %r' % (p.returncode, err)) self._v2v_caps = frozenset(out.splitlines()) logging.debug("Detected virt-v2v capabilities: %r", self._v2v_caps)
def _query_v2v_caps(self): self._v2v_caps = frozenset() p = _simple_exec_cmd([_VIRT_V2V.cmd, '--machine-readable'], env=os.environ.copy(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) with terminating(p): try: out, err = p.communicate() except Exception: logging.exception('Terminating virt-v2v process after error') raise if p.returncode != 0: raise V2VProcessError( 'virt-v2v exited with code: %d, stderr: %r' % (p.returncode, err)) self._v2v_caps = frozenset(out.splitlines()) logging.debug("Detected virt-v2v capabilities: %r", self._v2v_caps)
def test_terminating(self): with utils.terminating(self.proc): self.assertIsNone(self.proc.poll()) proc_path = "/proc/%d" % self.proc.pid self.assertTrue(wait_for_removal(proc_path, timeout=1))
def test_terminating(self): with utils.terminating(self.proc): self.assertIsNone(self.proc.poll()) self.assertEqual(self.proc.returncode, -signal.SIGKILL)
def test_terminating(self): with utils.terminating(self.proc): self.assertTrue(os.path.exists(self.proc_path)) self.assertTrue(wait_for_removal(self.proc_path, timeout=1))
def test_process_running(self): with utils.terminating(self.proc): self.assertIsNone(self.proc_poll()) self.assertEqual(self.proc.returncode, -signal.SIGKILL)