~evilnick/maas/1.2-docs-tidyup+preseed

« back to all changes in this revision

Viewing changes to src/maasserver/tests/test_node.py

[r=rvb][bug=1058998][author=rvb] Backport of maas r 1330

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
__all__ = []
14
14
 
15
15
from datetime import timedelta
 
16
import random
16
17
 
17
18
from django.conf import settings
18
19
from django.core.exceptions import (
36
37
    Node,
37
38
    node as node_module,
38
39
    )
39
 
from maasserver.models.node import NODE_TRANSITIONS
 
40
from maasserver.models.node import (
 
41
    generate_hostname,
 
42
    NODE_TRANSITIONS,
 
43
    )
40
44
from maasserver.models.user import create_auth_token
41
45
from maasserver.testing import reload_object
42
46
from maasserver.testing.factory import factory
45
49
    ignore_unused,
46
50
    map_enum,
47
51
    )
 
52
from maastesting.testcase import TestCase as DjangoLessTestCase
48
53
from metadataserver.models import (
49
54
    NodeCommissionResult,
50
55
    NodeUserData,
52
57
from provisioningserver.enum import POWER_TYPE
53
58
from provisioningserver.power.poweraction import PowerAction
54
59
from testtools.matchers import (
 
60
    AllMatch,
 
61
    Contains,
55
62
    Equals,
56
63
    FileContains,
 
64
    MatchesAll,
57
65
    MatchesListwise,
 
66
    Not,
58
67
    )
59
68
 
60
69
 
 
70
class UtilitiesTest(DjangoLessTestCase):
 
71
 
 
72
    def test_generate_hostname_does_not_contain_ambiguous_chars(self):
 
73
        ambiguous_chars = 'ilousvz1250'
 
74
        hostnames = [generate_hostname(5) for i in range(200)]
 
75
        does_not_contain_chars_matcher = (
 
76
            MatchesAll(*[Not(Contains(char)) for char in ambiguous_chars]))
 
77
        self.assertThat(
 
78
            hostnames, AllMatch(does_not_contain_chars_matcher))
 
79
 
 
80
    def test_generate_hostname_uses_size(self):
 
81
        sizes = [
 
82
            random.randint(1, 10), random.randint(1, 10),
 
83
            random.randint(1, 10)]
 
84
        hostnames = [generate_hostname(size) for size in sizes]
 
85
        self.assertEqual(sizes, [len(hostname) for hostname in hostnames])
 
86
 
 
87
 
61
88
class NodeTest(TestCase):
62
89
 
63
90
    def test_system_id(self):
182
209
        node.delete()
183
210
        self.assertEqual(2, mocked_apply_async.call_count)
184
211
 
185
 
    def test_set_mac_based_hostname_default_enlistment_domain(self):
186
 
        # The enlistment domain defaults to `local`.
187
 
        node = factory.make_node()
188
 
        node.set_mac_based_hostname('AA:BB:CC:DD:EE:FF')
189
 
        hostname = 'node-aabbccddeeff.local'
190
 
        self.assertEqual(hostname, node.hostname)
191
 
 
192
 
    def test_set_mac_based_hostname_alt_enlistment_domain(self):
193
 
        # A non-default enlistment domain can be specified.
194
 
        Config.objects.set_config("enlistment_domain", "example.com")
195
 
        node = factory.make_node()
196
 
        node.set_mac_based_hostname('AA:BB:CC:DD:EE:FF')
197
 
        hostname = 'node-aabbccddeeff.example.com'
198
 
        self.assertEqual(hostname, node.hostname)
199
 
 
200
 
    def test_set_mac_based_hostname_cleaning_enlistment_domain(self):
201
 
        # Leading and trailing dots and whitespace are cleaned from the
202
 
        # configured enlistment domain before it's joined to the hostname.
203
 
        Config.objects.set_config("enlistment_domain", " .example.com. ")
204
 
        node = factory.make_node()
205
 
        node.set_mac_based_hostname('AA:BB:CC:DD:EE:FF')
206
 
        hostname = 'node-aabbccddeeff.example.com'
207
 
        self.assertEqual(hostname, node.hostname)
208
 
 
209
 
    def test_set_mac_based_hostname_no_enlistment_domain(self):
210
 
        # The enlistment domain can be set to the empty string and
211
 
        # set_mac_based_hostname sets a hostname with no domain.
212
 
        Config.objects.set_config("enlistment_domain", "")
213
 
        node = factory.make_node()
214
 
        node.set_mac_based_hostname('AA:BB:CC:DD:EE:FF')
215
 
        hostname = 'node-aabbccddeeff'
216
 
        self.assertEqual(hostname, node.hostname)
 
212
    def test_set_random_hostname_set_hostname(self):
 
213
        # Blank out enlistment_domain.
 
214
        Config.objects.set_config("enlistment_domain", '')
 
215
        node = factory.make_node('test' * 10)
 
216
        node.set_random_hostname()
 
217
        self.assertEqual(5, len(node.hostname))
 
218
 
 
219
    def test_set_random_hostname_checks_hostname_existence(self):
 
220
        Config.objects.set_config("enlistment_domain", '')
 
221
        existing_node = factory.make_node(hostname='hostname')
 
222
 
 
223
        hostnames = [existing_node.hostname, "new_hostname"]
 
224
        self.patch(
 
225
            node_module, "generate_hostname",
 
226
            lambda size: hostnames.pop(0))
 
227
 
 
228
        node = factory.make_node()
 
229
        node.set_random_hostname()
 
230
        self.assertEqual('new_hostname', node.hostname)
217
231
 
218
232
    def test_get_effective_power_type_defaults_to_config(self):
219
233
        power_types = list(map_enum(POWER_TYPE).values())