~ubuntu-branches/ubuntu/raring/maas/raring-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-17 08:28:36 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20120717082836-ucb2vou8tqg353eq
Tags: 0.1+bzr777+dfsg-0ubuntu1
* New upstream release
* Only run 'maas' command as root. (LP: #974046)
  - debian/extras/maas: Check id.
  - debian/maas.install: Install in 'sbin'.
* debian/maas.postinst:
  - restart apache2 after everything gets processed.
  - Update version to handle upgrades.
* debian/extras/maas-provision: Add wrapper to access 'maasprovisiong'
  command line.
* debian/patches/99_temporary_fix_path.patch: Dropped. No longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
"""Tests for the config_master_dhcp command."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
from optparse import OptionValueError
 
16
 
 
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
 
23
 
 
24
 
 
25
def make_master_constants():
 
26
    """Return the standard, unchanging config for the master nodegroup."""
 
27
    return {
 
28
        'name': 'master',
 
29
        'worker_ip': '127.0.0.1',
 
30
    }
 
31
 
 
32
 
 
33
def make_dhcp_settings():
 
34
    """Return an arbitrary dict of DHCP settings."""
 
35
    return {
 
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',
 
41
    }
 
42
 
 
43
 
 
44
def make_cleared_dhcp_settings():
 
45
    """Return dict of cleared DHCP settings."""
 
46
    return {
 
47
        setting: None
 
48
        for setting in make_dhcp_settings().keys()}
 
49
 
 
50
 
 
51
class TestConfigMasterDHCP(TestCase):
 
52
 
 
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')
 
57
        self.assertThat(
 
58
            master,
 
59
            MatchesStructure.fromExample(make_master_constants()))
 
60
        self.assertThat(master, MatchesStructure.fromExample(settings))
 
61
 
 
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)
 
66
        master.save()
 
67
        call_command('config_master_dhcp', clear=True)
 
68
        self.assertThat(
 
69
            master,
 
70
            MatchesStructure.fromExample(make_master_constants()))
 
71
        self.assertThat(
 
72
            master,
 
73
            MatchesStructure.fromExample(make_cleared_dhcp_settings()))
 
74
 
 
75
    def test_does_not_accept_partial_dhcp_settings(self):
 
76
        settings = make_dhcp_settings()
 
77
        del settings['subnet_mask']
 
78
        self.assertRaises(
 
79
            OptionValueError,
 
80
            call_command, 'config_master_dhcp', **settings)
 
81
 
 
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)
 
88
        self.assertThat(
 
89
            NodeGroup.objects.get(name='master'),
 
90
            MatchesStructure.fromExample(make_cleared_dhcp_settings()))
 
91
 
 
92
    def test_clear_conflicts_with_ensure(self):
 
93
        self.assertRaises(
 
94
            OptionValueError,
 
95
            call_command, 'config_master_dhcp', clear=True, ensure=True)
 
96
 
 
97
    def test_ensure_creates_master_nodegroup_without_dhcp_settings(self):
 
98
        call_command('config_master_dhcp', ensure=True)
 
99
        self.assertThat(
 
100
            NodeGroup.objects.get(name='master'),
 
101
            MatchesStructure.fromExample(make_cleared_dhcp_settings()))
 
102
 
 
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)
 
106
        self.assertThat(
 
107
            NodeGroup.objects.get(name='master'),
 
108
            MatchesStructure.fromExample(make_cleared_dhcp_settings()))
 
109
 
 
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)
 
114
        self.assertThat(
 
115
            NodeGroup.objects.get(name='master'),
 
116
            MatchesStructure.fromExample(settings))
 
117
 
 
118
    def test_name_option_turns_dhcp_setting_name_into_option(self):
 
119
        self.assertEqual('--subnet-mask', name_option('subnet_mask'))