Beispiel #1
0
 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)
Beispiel #2
0
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()
Beispiel #3
0
    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'
            })
Beispiel #4
0
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()
Beispiel #5
0
 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)
Beispiel #6
0
 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)
Beispiel #7
0
 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__)
Beispiel #8
0
    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')
Beispiel #9
0
    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")
Beispiel #10
0
 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)
Beispiel #11
0
 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)