def cb_timeout(): msg = (_("Time out after waiting" " %(time)s seconds when running proc: %(args)s" " %(kwargs)s") % {'time': time, 'args': args, 'kwargs': kwargs}) LOG.error(msg) raise exception.ProcessExecutionError(msg)
def execute_with_timeout(*args, **kwargs): time = kwargs.pop('timeout', 30) def cb_timeout(): msg = (_("Time out after waiting" " %(time)s seconds when running proc: %(args)s" " %(kwargs)s") % { 'time': time, 'args': args, 'kwargs': kwargs }) LOG.error(msg) raise exception.ProcessExecutionError(msg) timeout = Timeout(time) try: return execute(*args, **kwargs) except Timeout as t: if t is not timeout: LOG.error("Timeout reached but not from our timeout. This is bad!") raise else: msg = (_("Time out after waiting " "%(time)s seconds when running proc: %(args)s" " %(kwargs)s") % { 'time': time, 'args': args, 'kwargs': kwargs }) LOG.error(msg) raise exception.ProcessExecutionError(msg) finally: timeout.cancel()
def test_log_error_when_log_output_on_error_is_true(self): utils.execute = Mock(side_effect=exception.ProcessExecutionError( description='test-desc', exit_code=42, stderr='err', stdout='out', cmd='test')) utils.LOG.error = Mock() with ExpectedException( exception.ProcessExecutionError, "test-desc\nCommand: test\nExit code: 42\n" "Stdout: 'out'\nStderr: 'err'"): utils.execute_with_timeout('/usr/bin/foo', log_output_on_error=True) utils.LOG.error.assert_called_with( u"Command '%(cmd)s' failed. %(description)s Exit code: " u"%(exit_code)s\nstderr: %(stderr)s\nstdout: %(stdout)s", { 'description': 'test-desc', 'stderr': 'err', 'exit_code': 42, 'stdout': 'out', 'cmd': 'test' })
def execute_with_timeout(*args, **kwargs): time = kwargs.pop('timeout', 30) log_output_on_error = kwargs.pop('log_output_on_error', False) timeout = Timeout(time) try: return execute(*args, **kwargs) except exception.ProcessExecutionError as e: if log_output_on_error: LOG.error( _("Command '%(cmd)s' failed. %(description)s " "Exit code: %(exit_code)s\nstderr: %(stderr)s\n" "stdout: %(stdout)s") % {'cmd': e.cmd, 'description': e.description or '', 'exit_code': e.exit_code, 'stderr': e.stderr, 'stdout': e.stdout}) raise except Timeout as t: if t is not timeout: LOG.error(_("Got a timeout but not the one expected.")) raise else: msg = (_("Time out after waiting " "%(time)s seconds when running proc: %(args)s" " %(kwargs)s.") % {'time': time, 'args': args, 'kwargs': kwargs}) LOG.error(msg) raise exception.ProcessExecutionError(msg) finally: timeout.cancel()
def test_restore_failed_due_to_run_restore(self): self.restore_runner.pre_restore = mock.Mock() self.restore_runner._run_restore = mock.Mock( side_effect=exception.ProcessExecutionError('Error')) self.restore_runner.post_restore = mock.Mock() self.assertRaises(exception.ProcessExecutionError, self.restore_runner.restore)
def test__check_format_2(self, mock_logging): self.assertEqual(0, self.mock_exec.call_count) proc_err = exception.ProcessExecutionError() proc_err.stderr = 'Wrong magic number' self.mock_exec.side_effect = proc_err self.assertRaises(exception.GuestError, self.volumeDevice._check_format)
def test_backup_failed_due_to_run_backup(self): self.backup_runner.run = mock.Mock( side_effect=exception.ProcessExecutionError('test')) self.backup_runner._run_pre_backup = mock.Mock() self.backup_runner._run_post_backup = mock.Mock() utils.execute_with_timeout = mock.Mock(return_value=None) self.assertRaises(exception.ProcessExecutionError, self.backup_runner(12345).__enter__)
def test_throws_process_execution_error(self): utils.execute = Mock( side_effect=exception.ProcessExecutionError( description='test-desc', exit_code=42, stderr='err', stdout='out', cmd='test')) with ExpectedException( exception.ProcessExecutionError, "test-desc\nCommand: test\nExit code: 42\n" "Stdout: 'out'\nStderr: 'err'"): utils.execute_with_timeout('/usr/bin/foo')
def test_log_error_when_log_output_on_error_is_true(self): utils.execute = Mock( side_effect=exception.ProcessExecutionError( description='test-desc', exit_code=42, stderr='err', stdout='out', cmd='test')) utils.LOG.error = Mock() with ExpectedException( exception.ProcessExecutionError, "test-desc\nCommand: test\nExit code: 42\n" "Stdout: 'out'\nStderr: 'err'"): utils.execute_with_timeout( '/usr/bin/foo', log_output_on_error=True) utils.LOG.error.assert_called_with( u"Command 'test' failed. test-desc Exit code: 42\n" "stderr: err\nstdout: out")
def execute_change_permission_commands(self, chng_perm_cmd): out, err = utils.execute_with_timeout(chng_perm_cmd, shell=True) if err: raise exception.ProcessExecutionError(cmd=chng_perm_cmd, stderr=err, stdout=out)
def test_restore_failed_due_to_pre_restore(self): self.restore_runner_mocks['pre_restore'].side_effect = ( exception.ProcessExecutionError('Error')) self.assertRaises(exception.ProcessExecutionError, self.restore_runner.restore)