summaryrefslogtreecommitdiffstats
path: root/tests/test_ipalib/test_errors.py
blob: 49bed71048f800970e6d38ec0364be3747135fea (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.

"""
Test the `ipalib.errors` module.
"""

import re
import inspect

from tests.util import assert_equal, raises
from ipalib import errors, text
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, errors.PrivateError)
        assert not issubclass(self.__klass, errors.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, errors.PrivateError)
        assert isinstance(inst, self.klass)
        assert not isinstance(inst, errors.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.errors.PrivateError` exception.
    """
    _klass = errors.PrivateError

    def test_init(self):
        """
        Test the `ipalib.errors.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.errors.SubprocessError` exception.
    """

    _klass = errors.SubprocessError

    def test_init(self):
        """
        Test the `ipalib.errors.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.errors.PluginSubclassError` exception.
    """

    _klass = errors.PluginSubclassError

    def test_init(self):
        """
        Test the `ipalib.errors.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.errors.PluginDuplicateError` exception.
    """

    _klass = errors.PluginDuplicateError

    def test_init(self):
        """
        Test the `ipalib.errors.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.errors.PluginOverrideError` exception.
    """

    _klass = errors.PluginOverrideError

    def test_init(self):
        """
        Test the `ipalib.errors.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.errors.PluginMissingOverrideError` exception.
    """

    _klass = errors.PluginMissingOverrideError

    def test_init(self):
        """
        Test the `ipalib.errors.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, errors.PublicError)
        assert not issubclass(self.__klass, errors.PrivateError)
        assert type(self.__klass.errno) is int
        assert 900 <= self.__klass.errno <= 5999
        return self.__klass
    klass = property(__get_klass)

    def new(self, format=None, message=None, **kw):
        # Test that TypeError is raised if message isn't unicode:
        e = raises(TypeError, self.klass, message='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(format=format, message=message, **kw)
        for required_class in self.required_classes:
            assert isinstance(inst, required_class)
        assert isinstance(inst, self.klass)
        assert not isinstance(inst, errors.PrivateError)
        for (key, value) in kw.iteritems():
            assert getattr(inst, key) is value
        return inst


class test_PublicError(PublicExceptionTester):
    """
    Test the `ipalib.errors.PublicError` exception.
    """
    _klass = errors.PublicError
    required_classes = StandardError, errors.PublicError

    def test_init(self):
        message = u'The translated, interpolated message'
        format = 'key=%(key1)r and key2=%(key2)r'
        uformat = u'Translated key=%(key1)r and key2=%(key2)r'
        val1 = 'Value 1'
        val2 = 'Value 2'
        kw = dict(key1=val1, key2=val2)

        # Test with format=str, message=None
        inst = self.klass(format, **kw)
        assert inst.format is format
        assert_equal(inst.message, format % kw)
        assert inst.forwarded is False
        assert inst.key1 is val1
        assert inst.key2 is val2

        # Test with format=None, message=unicode
        inst = self.klass(message=message, **kw)
        assert inst.format is None
        assert inst.message is message
        assert inst.strerror is message
        assert inst.forwarded is True
        assert inst.key1 is val1
        assert inst.key2 is val2

        # Test with format=None, message=str
        e = raises(TypeError, self.klass, message='the message', **kw)
        assert str(e) == TYPE_ERROR % ('message', unicode, 'the message', str)

        # Test with format=None, message=None
        e = raises(ValueError, self.klass, **kw)
        assert (str(e) == '%s.format is None yet format=None, message=None' %
            self.klass.__name__)


        ######################################
        # Test via PublicExceptionTester.new()

        # Test with format=str, message=None
        inst = self.new(format, **kw)
        assert isinstance(inst, self.klass)
        assert inst.format is format
        assert_equal(inst.message, format % kw)
        assert inst.forwarded is False
        assert inst.key1 is val1
        assert inst.key2 is val2

        # Test with format=None, message=unicode
        inst = self.new(message=message, **kw)
        assert isinstance(inst, self.klass)
        assert inst.format is None
        assert inst.message is message
        assert inst.strerror is message
        assert inst.forwarded is True
        assert inst.key1 is val1
        assert inst.key2 is val2


        ##################
        # Test a subclass:
        class subclass(self.klass):
            format = '%(true)r %(text)r %(number)r'

        uformat = u'Translated %(true)r %(text)r %(number)r'
        kw = dict(true=True, text='Hello!', number=18)

        # Test with format=str, message=None
        e = raises(ValueError, subclass, format, **kw)
        assert str(e) == 'non-generic %r needs format=None; got format=%r' % (
            'subclass', format)

        # Test with format=None, message=None:
        inst = subclass(**kw)
        assert inst.format is subclass.format
        assert_equal(inst.message, subclass.format % kw)
        assert inst.forwarded is False
        assert inst.true is True
        assert inst.text is kw['text']
        assert inst.number is kw['number']

        # Test with format=None, message=unicode:
        inst = subclass(message=message, **kw)
        assert inst.format is subclass.format
        assert inst.message is message
        assert inst.strerror is message
        assert inst.forwarded is True
        assert inst.true is True
        assert inst.text is kw['text']
        assert inst.number is kw['number']

        # Test with instructions:
        # first build up "instructions", then get error and search for
        # lines of instructions appended to the end of the strerror
        # despite the parameter 'instructions' not existing in the format
        instructions = u"The quick brown fox jumps over the lazy dog".split()
        # this expression checks if each word of instructions
        # exists in a string as a separate line, with right order
        regexp = re.compile('(?ims).*' +
                            ''.join(map(lambda x: '(%s).*' % (x),
                                        instructions)) +
                            '$')
        inst = subclass(instructions=instructions, **kw)
        assert inst.format is subclass.format
        assert_equal(inst.instructions, instructions)
        inst_match = regexp.match(inst.strerror).groups()
        assert_equal(list(inst_match),list(instructions))


class BaseMessagesTest(object):
    """Generic test for all of a module's errors or messages
    """
    def test_public_messages(self):
        i = 0
        for klass in self.message_list:
            for required_class in self.required_classes:
                assert issubclass(klass, required_class)
            assert type(klass.errno) is int
            assert klass.errno in self.errno_range
            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 '**ERRNO**' in %s docstring" % klass.__name__
            errno = int(m.group(1))
            assert errno == klass.errno, (
                'docstring=%r but errno=%r in %s' % (errno, klass.errno, klass.__name__)
            )
            self.extratest(klass)

            # Test format
            if klass.format is not None:
                assert klass.format is self.texts[i]
                i += 1

    def extratest(self, cls):
        pass


class test_PublicErrors(object):
    message_list = errors.public_errors
    errno_range = xrange(900, 5999)
    required_classes = (StandardError, errors.PublicError)
    texts = errors._texts

    def extratest(self, cls):
        assert not issubclass(cls, errors.PrivateError)