1.1.11
by Andres Rodriguez
Import upstream version 0.1+bzr462+dfsg |
1 |
# Copyright 2012 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
3 |
||
4 |
"""Test maasserver components module."""
|
|
5 |
||
6 |
from __future__ import ( |
|
7 |
print_function, |
|
8 |
unicode_literals, |
|
9 |
)
|
|
10 |
||
11 |
__metaclass__ = type |
|
12 |
__all__ = [] |
|
13 |
||
14 |
||
15 |
import random |
|
16 |
||
17 |
from maasserver import components |
|
18 |
from maasserver.components import ( |
|
19 |
COMPONENT, |
|
20 |
discard_persistent_error, |
|
21 |
get_persistent_errors, |
|
22 |
register_persistent_error, |
|
23 |
)
|
|
24 |
from maasserver.testing.enum import map_enum |
|
25 |
from maasserver.testing.factory import factory |
|
26 |
from maasserver.testing.testcase import TestCase |
|
27 |
||
28 |
||
29 |
def simple_error_display(error): |
|
30 |
return str(error) |
|
31 |
||
32 |
||
33 |
def get_random_component(): |
|
34 |
random.choice(map_enum(COMPONENT).values()) |
|
35 |
||
36 |
||
37 |
class PersistentErrorsUtilitiesTest(TestCase): |
|
38 |
||
39 |
def setUp(self): |
|
40 |
super(PersistentErrorsUtilitiesTest, self).setUp() |
|
41 |
self._PERSISTENT_ERRORS = {} |
|
42 |
self.patch(components, '_PERSISTENT_ERRORS', self._PERSISTENT_ERRORS) |
|
43 |
||
44 |
def test_register_persistent_error_registers_error(self): |
|
45 |
error_message = factory.getRandomString() |
|
46 |
component = get_random_component() |
|
47 |
register_persistent_error(component, error_message) |
|
48 |
self.assertItemsEqual( |
|
49 |
{component: error_message}, self._PERSISTENT_ERRORS) |
|
50 |
||
51 |
def test_register_persistent_error_stores_last_error(self): |
|
52 |
error_message = factory.getRandomString() |
|
53 |
error_message2 = factory.getRandomString() |
|
54 |
component = get_random_component() |
|
55 |
register_persistent_error(component, error_message) |
|
56 |
register_persistent_error(component, error_message2) |
|
57 |
self.assertItemsEqual( |
|
58 |
{component: error_message2}, self._PERSISTENT_ERRORS) |
|
59 |
||
60 |
def test_discard_persistent_error_discards_error(self): |
|
61 |
error_message = factory.getRandomString() |
|
62 |
component = get_random_component() |
|
63 |
register_persistent_error(component, error_message) |
|
64 |
discard_persistent_error(component) |
|
65 |
self.assertItemsEqual({}, self._PERSISTENT_ERRORS) |
|
66 |
||
67 |
def test_discard_persistent_error_can_be_called_many_times(self): |
|
68 |
error_message = factory.getRandomString() |
|
69 |
component = get_random_component() |
|
70 |
register_persistent_error(component, error_message) |
|
71 |
discard_persistent_error(component) |
|
72 |
discard_persistent_error(component) |
|
73 |
self.assertItemsEqual({}, self._PERSISTENT_ERRORS) |
|
74 |
||
75 |
def get_persistent_errors_returns_text_for_error_codes(self): |
|
76 |
errors, components = [], [] |
|
77 |
for i in range(3): |
|
78 |
error_message = factory.getRandomString() |
|
79 |
component = get_random_component() |
|
80 |
register_persistent_error(component, error_message) |
|
81 |
errors.append(error_message) |
|
82 |
components.append(component) |
|
83 |
self.assertItemsEqual(errors, get_persistent_errors()) |