summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-01-03 15:35:54 -0700
committerJason Gerard DeRose <jderose@redhat.com>2009-01-03 15:35:54 -0700
commitd1517b95ca14773773647434fb589c8224307328 (patch)
tree6e9064d09405795856385a8927e8832f00c41fb2
parent0d3ddef93b0a72b824297e5504e435a4427f14bd (diff)
downloadfreeipa-d1517b95ca14773773647434fb589c8224307328.tar.gz
freeipa-d1517b95ca14773773647434fb589c8224307328.tar.xz
freeipa-d1517b95ca14773773647434fb589c8224307328.zip
Ported errors.SubprocessError to errors2
-rw-r--r--ipalib/errors2.py31
-rw-r--r--tests/test_ipalib/test_error2.py79
2 files changed, 110 insertions, 0 deletions
diff --git a/ipalib/errors2.py b/ipalib/errors2.py
index f49c10ef9..676dd79f3 100644
--- a/ipalib/errors2.py
+++ b/ipalib/errors2.py
@@ -46,6 +46,37 @@ class PrivateError(StandardError):
Base class for exceptions that are *never* returned in an RPC response.
"""
+ format = ''
+
+ def __init__(self, **kw):
+ self.message = self.format % kw
+ for (key, value) in kw.iteritems():
+ assert not hasattr(self, key), 'conflicting kwarg %s.%s = %r' % (
+ self.__class__.__name__, key, value,
+ )
+ setattr(self, key, value)
+ StandardError.__init__(self, self.message)
+
+
+class SubprocessError(PrivateError):
+ """
+ Raised when ``subprocess.call()`` returns a non-zero exit status.
+
+ This custom exception is needed because Python 2.4 doesn't have the
+ ``subprocess.CalledProcessError`` exception (which was added in Python 2.5).
+
+ For example:
+
+ >>> e = SubprocessError(returncode=1, argv=('/bin/false',))
+ >>> e.returncode
+ 1
+ >>> e.argv
+ ('/bin/false',)
+ >>> str(e)
+ "return code 1 from ('/bin/false',)"
+ """
+ format = 'return code %(returncode)d from %(argv)r'
+
class PublicError(StandardError):
"""
diff --git a/tests/test_ipalib/test_error2.py b/tests/test_ipalib/test_error2.py
index d70e49bee..f8b1a314d 100644
--- a/tests/test_ipalib/test_error2.py
+++ b/tests/test_ipalib/test_error2.py
@@ -26,11 +26,90 @@ import inspect
from ipalib import errors2
+class PrivateExceptionTester(object):
+ _klass = None
+ __klass = None
+
+ def __get_klass(self):
+ if self.__klass is None:
+ self.__klass = self._klass
+ assert issubclass(self.__klass, StandardError)
+ assert issubclass(self.__klass, errors2.PrivateError)
+ assert not issubclass(self.__klass, errors2.PublicError)
+ return self.__klass
+ klass = property(__get_klass)
+
+ def new(self, **kw):
+ for (key, value) in kw.iteritems():
+ assert not hasattr(self.klass, key), key
+ inst = self.klass(**kw)
+ assert isinstance(inst, StandardError)
+ assert isinstance(inst, errors2.PrivateError)
+ assert isinstance(inst, self.klass)
+ assert not isinstance(inst, errors2.PublicError)
+ for (key, value) in kw.iteritems():
+ assert getattr(inst, key) is value
+ assert str(inst) == self.klass.format % kw
+ assert inst.message == str(inst)
+ return inst
+
+
+class test_PrivateError(PrivateExceptionTester):
+ """
+ Test the `ipalib.errors2.PrivateError` exception.
+ """
+ _klass = errors2.PrivateError
+
+ def test_init(self):
+ """
+ Test the `ipalib.errors2.PrivateError.__init__` method.
+ """
+ inst = self.klass(key1='Value 1', key2='Value 2')
+ assert inst.key1 == 'Value 1'
+ assert inst.key2 == 'Value 2'
+ assert str(inst) == ''
+
+ # Test subclass and use of format:
+ class subclass(self.klass):
+ format = '%(true)r %(text)r %(number)r'
+
+ kw = dict(true=True, text='Hello!', number=18)
+ inst = subclass(**kw)
+ assert inst.true is True
+ assert inst.text is kw['text']
+ assert inst.number is kw['number']
+ assert str(inst) == subclass.format % kw
+
+ # Test via PrivateExceptionTester.new()
+ inst = self.new(**kw)
+ assert isinstance(inst, self.klass)
+ assert inst.true is True
+ assert inst.text is kw['text']
+ assert inst.number is kw['number']
+
+
+class test_SubprocessError(PrivateExceptionTester):
+ """
+ Test the `ipalib.errors2.SubprocessError` exception.
+ """
+ _klass = errors2.SubprocessError
+
+ def test_init(self):
+ """
+ Test the `ipalib.errors2.SubprocessError.__init__` method.
+ """
+ inst = self.klass(returncode=1, argv=('/bin/false',))
+ assert inst.returncode == 1
+ assert inst.argv == ('/bin/false',)
+ assert str(inst) == "return code 1 from ('/bin/false',)"
+
+
def test_public_errors():
"""
Test the `ipalib.errors2.public_errors` module variable.
"""
for klass in errors2.public_errors:
+ assert issubclass(klass, StandardError)
assert issubclass(klass, errors2.PublicError)
assert not issubclass(klass, errors2.PrivateError)
assert type(klass.code) is int