1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the config_master_dhcp command."""
6
from __future__ import (
15
from optparse import OptionValueError
17
from django.core.management import call_command
18
from maasserver.management.commands.config_master_dhcp import name_option
19
from maasserver.models import NodeGroup
20
from maasserver.testing.factory import factory
21
from maasserver.testing.testcase import TestCase
22
from testtools.matchers import MatchesStructure
25
def make_master_constants():
26
"""Return the standard, unchanging config for the master nodegroup."""
29
'worker_ip': '127.0.0.1',
33
def make_dhcp_settings():
34
"""Return an arbitrary dict of DHCP settings."""
36
'subnet_mask': '255.255.0.0',
37
'broadcast_ip': '10.111.255.255',
38
'router_ip': factory.getRandomIPAddress(),
39
'ip_range_low': '10.111.123.9',
40
'ip_range_high': '10.111.244.18',
44
def make_cleared_dhcp_settings():
45
"""Return dict of cleared DHCP settings."""
48
for setting in make_dhcp_settings().keys()}
51
class TestConfigMasterDHCP(TestCase):
53
def test_configures_dhcp_for_master_nodegroup(self):
54
settings = make_dhcp_settings()
55
call_command('config_master_dhcp', **settings)
56
master = NodeGroup.objects.get(name='master')
59
MatchesStructure.fromExample(make_master_constants()))
60
self.assertThat(master, MatchesStructure.fromExample(settings))
62
def test_clears_dhcp_settings(self):
63
master = NodeGroup.objects.ensure_master()
64
for attribute, value in make_dhcp_settings().items():
65
setattr(master, attribute, value)
67
call_command('config_master_dhcp', clear=True)
70
MatchesStructure.fromExample(make_master_constants()))
73
MatchesStructure.fromExample(make_cleared_dhcp_settings()))
75
def test_does_not_accept_partial_dhcp_settings(self):
76
settings = make_dhcp_settings()
77
del settings['subnet_mask']
80
call_command, 'config_master_dhcp', **settings)
82
def test_ignores_nonsense_settings_when_clear_is_passed(self):
83
settings = make_dhcp_settings()
84
call_command('config_master_dhcp', **settings)
85
settings['subnet_mask'] = '@%$^&'
86
settings['broadcast_ip'] = ''
87
call_command('config_master_dhcp', clear=True, **settings)
89
NodeGroup.objects.get(name='master'),
90
MatchesStructure.fromExample(make_cleared_dhcp_settings()))
92
def test_clear_conflicts_with_ensure(self):
95
call_command, 'config_master_dhcp', clear=True, ensure=True)
97
def test_ensure_creates_master_nodegroup_without_dhcp_settings(self):
98
call_command('config_master_dhcp', ensure=True)
100
NodeGroup.objects.get(name='master'),
101
MatchesStructure.fromExample(make_cleared_dhcp_settings()))
103
def test_ensure_leaves_cleared_settings_cleared(self):
104
call_command('config_master_dhcp', clear=True)
105
call_command('config_master_dhcp', ensure=True)
107
NodeGroup.objects.get(name='master'),
108
MatchesStructure.fromExample(make_cleared_dhcp_settings()))
110
def test_ensure_leaves_dhcp_settings_intact(self):
111
settings = make_dhcp_settings()
112
call_command('config_master_dhcp', **settings)
113
call_command('config_master_dhcp', ensure=True)
115
NodeGroup.objects.get(name='master'),
116
MatchesStructure.fromExample(settings))
118
def test_name_option_turns_dhcp_setting_name_into_option(self):
119
self.assertEqual('--subnet-mask', name_option('subnet_mask'))