diff options
| author | Dan Prince <dan.prince@rackspace.com> | 2011-12-14 22:16:25 -0500 |
|---|---|---|
| committer | Dan Prince <dan.prince@rackspace.com> | 2011-12-15 08:08:42 -0500 |
| commit | de815b7f4426134c93ce5910b3401bf036df211d (patch) | |
| tree | 856b4bbf9084b41e2093cc20d60ccb8a50c762f9 | |
| parent | 076317047802d3ed393c5467223abd6f613291ba (diff) | |
Update utils.execute so that check_exit_code handles booleans.
Fixes LP bug #904560.
Change-Id: I5d09b65dade166763981dc6b248034d86ba217f1
| -rw-r--r-- | nova/tests/test_utils.py | 6 | ||||
| -rw-r--r-- | nova/utils.py | 13 |
2 files changed, 15 insertions, 4 deletions
diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index 2c4c02b82..15ac0dc2d 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -79,6 +79,12 @@ exit 1 utils.execute, '/bin/true', this_is_not_a_valid_kwarg=True) + def test_check_exit_code_boolean(self): + utils.execute('/bin/false', check_exit_code=False) + self.assertRaises(exception.ProcessExecutionError, + utils.execute, + '/bin/false', check_exit_code=True) + def test_no_retry_on_success(self): fd, tmpfilename = tempfile.mkstemp() _, tmpfilename2 = tempfile.mkstemp() diff --git a/nova/utils.py b/nova/utils.py index 991efa4f7..1139e101e 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -157,8 +157,8 @@ def execute(*cmd, **kwargs): :cmd Passed to subprocess.Popen. :process_input Send to opened process. - :check_exit_code Single int or list of allowed exit codes. Defaults - to [0]. Raise exception.ProcessExecutionError + :check_exit_code Single bool, int, or list of allowed exit codes. + Defaults to [0]. Raise exception.ProcessExecutionError unless program exits with one of these code. :delay_on_retry True | False. Defaults to True. If set to True, wait a short amount of time before retrying. @@ -173,8 +173,12 @@ def execute(*cmd, **kwargs): process_input = kwargs.pop('process_input', None) check_exit_code = kwargs.pop('check_exit_code', [0]) - if type(check_exit_code) == types.IntType: + ignore_exit_code = False + if type(check_exit_code) == int: check_exit_code = [check_exit_code] + elif type(check_exit_code) == bool: + ignore_exit_code = not check_exit_code + check_exit_code = [0] delay_on_retry = kwargs.pop('delay_on_retry', True) attempts = kwargs.pop('attempts', 1) run_as_root = kwargs.pop('run_as_root', False) @@ -208,7 +212,8 @@ def execute(*cmd, **kwargs): _returncode = obj.returncode # pylint: disable=E1101 if _returncode: LOG.debug(_('Result was %s') % _returncode) - if _returncode not in check_exit_code: + if ignore_exit_code == False \ + and _returncode not in check_exit_code: (stdout, stderr) = result raise exception.ProcessExecutionError( exit_code=_returncode, |
