summaryrefslogtreecommitdiffstats
path: root/tests/test_ipalib/test_error2.py
blob: a5b1e7c5802de9607156ecf1429d300ec3d99160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Authors:
#   Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Test the `ipalib.error2` module.
"""

import re
import inspect
from tests.util import assert_equal, raises, DummyUGettext
from ipalib import errors2
from ipalib.constants import TYPE_ERROR


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.new(returncode=1, argv=('/bin/false',))
        assert inst.returncode == 1
        assert inst.argv == ('/bin/false',)
        assert str(inst) == "return code 1 from ('/bin/false',)"
        assert inst.message == str(inst)


class test_PluginSubclassError(PrivateExceptionTester):
    """
    Test the `ipalib.errors2.PluginSubclassError` exception.
    """

    _klass = errors2.PluginSubclassError

    def test_init(self):
        """
        Test the `ipalib.errors2.PluginSubclassError.__init__` method.
        """
        inst = self.new(plugin='bad', bases=('base1', 'base2'))
        assert inst.plugin == 'bad'
        assert inst.bases == ('base1', 'base2')
        assert str(inst) == \
            "'bad' not subclass of any base in ('base1', 'base2')"
        assert inst.message == str(inst)


class test_PluginDuplicateError(PrivateExceptionTester):
    """
    Test the `ipalib.errors2.PluginDuplicateError` exception.
    """

    _klass = errors2.PluginDuplicateError

    def test_init(self):
        """
        Test the `ipalib.errors2.PluginDuplicateError.__init__` method.
        """
        inst = self.new(plugin='my_plugin')
        assert inst.plugin == 'my_plugin'
        assert str(inst) == "'my_plugin' was already registered"
        assert inst.message == str(inst)


class test_PluginOverrideError(PrivateExceptionTester):
    """
    Test the `ipalib.errors2.PluginOverrideError` exception.
    """

    _klass = errors2.PluginOverrideError

    def test_init(self):
        """
        Test the `ipalib.errors2.PluginOverrideError.__init__` method.
        """
        inst = self.new(base='Base', name='cmd', plugin='my_cmd')
        assert inst.base == 'Base'
        assert inst.name == 'cmd'
        assert inst.plugin == 'my_cmd'
        assert str(inst) == "unexpected override of Base.cmd with 'my_cmd'"
        assert inst.message == str(inst)


class test_PluginMissingOverrideError(PrivateExceptionTester):
    """
    Test the `ipalib.errors2.PluginMissingOverrideError` exception.
    """

    _klass = errors2.PluginMissingOverrideError

    def test_init(self):
        """
        Test the `ipalib.errors2.PluginMissingOverrideError.__init__` method.
        """
        inst = self.new(base='Base', name='cmd', plugin='my_cmd')
        assert inst.base == 'Base'
        assert inst.name == 'cmd'
        assert inst.plugin == 'my_cmd'
        assert str(inst) == "Base.cmd not registered, cannot override with 'my_cmd'"
        assert inst.message == str(inst)



##############################################################################
# Unit tests for public errors:

class PublicExceptionTester(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.PublicError)
        assert not issubclass(self.__klass, errors2.PrivateError)
        assert type(self.__klass.code) is int
        assert 900 <= self.__klass.code <= 5999
        return self.__klass
    klass = property(__get_klass)

    def new(self, message=None, **kw):
        # Test that TypeError is raised if message isn't unicode:
        e = raises(TypeError, self.klass, 'The message')
        assert str(e) == TYPE_ERROR % ('message', unicode, 'The message', str)

        # Test the instance:
        for (key, value) in kw.iteritems():
            assert not hasattr(self.klass, key), key
        inst = self.klass(message=message, **kw)
        assert isinstance(inst, StandardError)
        assert isinstance(inst, errors2.PublicError)
        assert isinstance(inst, self.klass)
        assert not isinstance(inst, errors2.PrivateError)
        for (key, value) in kw.iteritems():
            assert getattr(inst, key) is value
        assert str(inst) == inst.get_format(lambda m: m) % kw
        assert inst.message == str(inst)
        return inst


class test_PublicError(PublicExceptionTester):
    """
    Test the `ipalib.errors2.PublicError` exception.
    """
    _klass = errors2.PublicError

    def test_init(self):
        """
        Test the `ipalib.errors2.PublicError.__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 message, get_format():
        class subclass(self.klass):
            def get_format(self, _):
                return _('%(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_equal(inst.message, u'%(true)r %(text)r %(number)r' % kw)

        # Test via PublicExceptionTester.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']


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
        assert 900 <= klass.code <= 5999
        doc = inspect.getdoc(klass)
        assert doc is not None, 'need class docstring for %s' % klass.__name__
        m = re.match(r'^\*{2}(\d+)\*{2} ', doc)
        assert m is not None, "need '**CODE**' in %s docstring" % klass.__name__
        code = int(m.group(1))
        assert code == klass.code, (
            'docstring=%r but code=%r in %s' % (code, klass.code, klass.__name__)
        )