def UnexpectedExit_str_encodes_stdout_and_err(self, mock_exit): p = Program() oops = UnexpectedExit( Result( command='meh', exited=54, stdout=six.u('this is not ascii: \u1234'), stderr=six.u('this is also not ascii: \u4321'), encoding='utf-8', hide=('stdout', 'stderr'), )) p.execute = Mock(side_effect=oops) p.run("myapp foo") # NOTE: using explicit binary ASCII here, & accessing raw # getvalue() of the faked sys.stderr (spec.trap auto-decodes it # normally) to have a not-quite-tautological test. otherwise we'd # just be comparing unicode to unicode. shrug? expected = b"""Encountered a bad command exit code! Command: 'meh' Exit code: 54 Stdout: this is not ascii: \xe1\x88\xb4 Stderr: this is also not ascii: \xe4\x8c\xa1 """ got = six.BytesIO.getvalue(sys.stderr) assert got == expected
def shows_UnexpectedExit_str_when_streams_hidden(self, mock_exit): p = Program() oops = UnexpectedExit( Result( command='meh', exited=54, stdout='things!', stderr='ohnoz!', encoding='utf-8', hide=('stdout', 'stderr'), )) p.execute = Mock(side_effect=oops) p.run("myapp foo") # Expect repr() of exception prints to stderr # NOTE: this partially duplicates a test in runners.py; whatever. eq_( sys.stderr.getvalue(), """Encountered a bad command exit code! Command: 'meh' Exit code: 54 Stdout: things! Stderr: ohnoz! """) # And exit with expected code (vs e.g. 1 or 0) mock_exit.assert_called_with(54)
def check_all(results: List[Result]): try: result = next(result for result in results if result.exited != 0) except StopIteration: pass else: raise UnexpectedExit(result)
def test_wait_until_online_retry_on_failure(self): self.host.execute = Mock() self.host.execute.side_effect = [Mock(return_code=1), ConnectionResetError, UnexpectedExit('mock unexpected exit'), CommandTimedOut('mock command timeout', 10), Mock(return_code=0)] self.socket_context.connect_ex.side_effect = [1, 0] self.host.wait_until_online() self.socket_context.connect_ex.assert_called_with(('host1', 22)) self.host.execute.assert_called_with('virsh list')
def test_rename_file(self): self.host.execute = Mock(return_value=Mock(return_code=0)) self.assertTrue(self.host.rename_file('/foo', '/bar')) self.host.execute.assert_called_with('/bin/mv "/foo" "/bar"', True) self.host.execute = Mock(return_value=Mock(return_code=1)) self.assertFalse(self.host.rename_file('/foo', '/bar')) self.host.execute = Mock(side_effect=UnexpectedExit('mock unexpected exit')) self.assertFalse(self.host.rename_file('/foo', '/bar'))
def test_file_exist(self): ls_stdout = '-rw-r--r--. 1 qemu qemu 254279680 Mar 5 2021 /mnt/812ea6a3-7ad0-30f4-9cab-01e3f2985b98/9153b72d-ceac-48ae-b07e-07186ab0c97c\n' self.host.execute = Mock(return_value=(Mock(stdout=ls_stdout))) result = self.host.file_exists('/mnt/812ea6a3-7ad0-30f4-9cab-01e3f2985b98/9153b72d-ceac-48ae-b07e-07186ab0c97c') self.host.execute.assert_called_with( '/bin/ls -la "/mnt/812ea6a3-7ad0-30f4-9cab-01e3f2985b98/9153b72d-ceac-48ae-b07e-07186ab0c97c"', always=True) self.assertEqual(('Mar', '5', '2021'), (result[-4], result[-3], result[-2])) self.host.execute = Mock(side_effect=UnexpectedExit('mock unexpected exit')) self.assertFalse(self.host.file_exists('/mnt/not/there'))
def UnexpectedExit_exits_with_code_when_no_hiding(self, mock_exit): p = Program() oops = UnexpectedExit( Result(command="meh", exited=17, hide=tuple())) p.execute = Mock(side_effect=oops) p.run("myapp foo") # Expect NO repr printed, because stdout/err were not hidden, so we # don't want to add extra annoying verbosity - we want to be more # Make-like here. assert sys.stderr.getvalue() == "" # But we still exit with expected code (vs e.g. 1 or 0) mock_exit.assert_called_with(17)
def run(c, cmd, attach_=False, wait=True, warn=False, raise_error=False, show_ok=True): if attach_: c.run(cmd, pty=True) return if not wait: c.run(cmd, disown=True) spin_wheel(time=4) if show_ok: message("Ok", Level.SUCCESS) return promise = c.run(cmd, asynchronous=True, warn=warn) spin_wheel(promise=promise) if stderr := promise.runner.stderr and raise_error: raise UnexpectedExit(stderr)
def nox_parallel(c): """ Run selected nox test suite sessions in parallel. """ # Run asynchronously and collect promises print(f"Running {len(nox_parallel_sessions)} nox test sessions.") promises = [ c.run( f"nox --session {nox_test} --no-color", asynchronous=True, timeout=360, ) for nox_test in nox_parallel_sessions ] # Join all promises results = [promise.join() for promise in promises] # Check if Result has non-zero exit code (should've already thrown error.) for result in results: if result.exited != 0: raise UnexpectedExit(result) # Report to user of success. print(f"{len(results)} nox sessions ran succesfully.")
def __exit__(self, exc_type, exc_val, exc_tb): if self._failed: raise UnexpectedExit(self._failed[0])