~ubuntu-branches/ubuntu/quantal/cloud-init/quantal

« back to all changes in this revision

Viewing changes to tests/unittests/test_distros/test_netconfig.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2012-09-30 14:29:04 UTC
  • Revision ID: package-import@ubuntu.com-20120930142904-nq8fkve62i0xytqz
* add CloudStack to DataSources listed by dpkg-reconfigure (LP: #1002155)
* New upstream snapshot.
  * 0440 permissions on /etc/sudoers.d files rather than 0644
  * get host ssh keys to the console (LP: #1055688)
  * MAAS DataSource adjust timestamp in oauth header to one based on the
    timestamp in the response of a 403.  This accounts for a bad local
    clock. (LP: #978127)
  * re-start the salt daemon rather than start to ensure config changes
    are taken.
  * allow for python unicode types in yaml that is loaded.
  * cleanup in how config modules get at users and groups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mocker import MockerTestCase
 
2
 
 
3
import mocker
 
4
 
 
5
import os
 
6
 
 
7
from cloudinit import distros
 
8
from cloudinit import helpers
 
9
from cloudinit import settings
 
10
from cloudinit import util
 
11
 
 
12
from StringIO import StringIO
 
13
 
 
14
 
 
15
BASE_NET_CFG = '''
 
16
auto lo
 
17
iface lo inet loopback
 
18
 
 
19
auto eth0
 
20
iface eth0 inet static
 
21
    address 192.168.1.5
 
22
    netmask 255.255.255.0
 
23
    network 192.168.0.0
 
24
    broadcast 192.168.1.0
 
25
    gateway 192.168.1.254
 
26
 
 
27
auto eth1
 
28
iface eth1 inet dhcp
 
29
'''
 
30
 
 
31
 
 
32
class WriteBuffer(object):
 
33
    def __init__(self):
 
34
        self.buffer = StringIO()
 
35
        self.mode = None
 
36
        self.omode = None
 
37
 
 
38
    def write(self, text):
 
39
        self.buffer.write(text)
 
40
 
 
41
    def __str__(self):
 
42
        return self.buffer.getvalue()
 
43
 
 
44
 
 
45
class TestNetCfgDistro(MockerTestCase):
 
46
 
 
47
    def _get_distro(self, dname):
 
48
        cls = distros.fetch(dname)
 
49
        cfg = settings.CFG_BUILTIN
 
50
        cfg['system_info']['distro'] = dname
 
51
        paths = helpers.Paths({})
 
52
        return cls(dname, cfg, paths)
 
53
 
 
54
    def test_simple_write_ub(self):
 
55
        ub_distro = self._get_distro('ubuntu')
 
56
        util_mock = self.mocker.replace(util.write_file,
 
57
                                        spec=False, passthrough=False)
 
58
        exists_mock = self.mocker.replace(os.path.isfile,
 
59
                                          spec=False, passthrough=False)
 
60
 
 
61
        exists_mock(mocker.ARGS)
 
62
        self.mocker.count(0, None)
 
63
        self.mocker.result(False)
 
64
 
 
65
        write_bufs = {}
 
66
 
 
67
        def replace_write(filename, content, mode=0644, omode="wb"):
 
68
            buf = WriteBuffer()
 
69
            buf.mode = mode
 
70
            buf.omode = omode
 
71
            buf.write(content)
 
72
            write_bufs[filename] = buf
 
73
 
 
74
        util_mock(mocker.ARGS)
 
75
        self.mocker.call(replace_write)
 
76
        self.mocker.replay()
 
77
        ub_distro.apply_network(BASE_NET_CFG, False)
 
78
 
 
79
        self.assertEquals(len(write_bufs), 1)
 
80
        self.assertIn('/etc/network/interfaces', write_bufs)
 
81
        write_buf = write_bufs['/etc/network/interfaces']
 
82
        self.assertEquals(str(write_buf).strip(), BASE_NET_CFG.strip())
 
83
        self.assertEquals(write_buf.mode, 0644)
 
84
 
 
85
    def assertCfgEquals(self, blob1, blob2):
 
86
        cfg_tester = distros.rhel.QuotingConfigObj
 
87
        b1 = dict(cfg_tester(blob1.strip().splitlines()))
 
88
        b2 = dict(cfg_tester(blob2.strip().splitlines()))
 
89
        self.assertEquals(b1, b2)
 
90
        for (k, v) in b1.items():
 
91
            self.assertIn(k, b2)
 
92
        for (k, v) in b2.items():
 
93
            self.assertIn(k, b1)
 
94
        for (k, v) in b1.items():
 
95
            self.assertEquals(v, b2[k])
 
96
 
 
97
    def test_simple_write_rh(self):
 
98
        rh_distro = self._get_distro('rhel')
 
99
        write_mock = self.mocker.replace(util.write_file,
 
100
                                         spec=False, passthrough=False)
 
101
        load_mock = self.mocker.replace(util.load_file,
 
102
                                        spec=False, passthrough=False)
 
103
        exists_mock = self.mocker.replace(os.path.isfile,
 
104
                                          spec=False, passthrough=False)
 
105
 
 
106
        write_bufs = {}
 
107
 
 
108
        def replace_write(filename, content, mode=0644, omode="wb"):
 
109
            buf = WriteBuffer()
 
110
            buf.mode = mode
 
111
            buf.omode = omode
 
112
            buf.write(content)
 
113
            write_bufs[filename] = buf
 
114
 
 
115
        exists_mock(mocker.ARGS)
 
116
        self.mocker.count(0, None)
 
117
        self.mocker.result(False)
 
118
 
 
119
        load_mock(mocker.ARGS)
 
120
        self.mocker.count(0, None)
 
121
        self.mocker.result('')
 
122
 
 
123
        for _i in range(0, 3):
 
124
            write_mock(mocker.ARGS)
 
125
            self.mocker.call(replace_write)
 
126
 
 
127
        write_mock(mocker.ARGS)
 
128
        self.mocker.call(replace_write)
 
129
 
 
130
        self.mocker.replay()
 
131
        rh_distro.apply_network(BASE_NET_CFG, False)
 
132
 
 
133
        self.assertEquals(len(write_bufs), 4)
 
134
        self.assertIn('/etc/sysconfig/network-scripts/ifcfg-lo', write_bufs)
 
135
        write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-lo']
 
136
        expected_buf = '''
 
137
DEVICE="lo"
 
138
ONBOOT=yes
 
139
'''
 
140
        self.assertCfgEquals(expected_buf, str(write_buf))
 
141
        self.assertEquals(write_buf.mode, 0644)
 
142
 
 
143
        self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0', write_bufs)
 
144
        write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
 
145
        expected_buf = '''
 
146
DEVICE="eth0"
 
147
BOOTPROTO="static"
 
148
NETMASK="255.255.255.0"
 
149
IPADDR="192.168.1.5"
 
150
ONBOOT=yes
 
151
GATEWAY="192.168.1.254"
 
152
BROADCAST="192.168.1.0"
 
153
'''
 
154
        self.assertCfgEquals(expected_buf, str(write_buf))
 
155
        self.assertEquals(write_buf.mode, 0644)
 
156
 
 
157
        self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1', write_bufs)
 
158
        write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
 
159
        expected_buf = '''
 
160
DEVICE="eth1"
 
161
BOOTPROTO="dhcp"
 
162
ONBOOT=yes
 
163
'''
 
164
        self.assertCfgEquals(expected_buf, str(write_buf))
 
165
        self.assertEquals(write_buf.mode, 0644)
 
166
 
 
167
        self.assertIn('/etc/sysconfig/network', write_bufs)
 
168
        write_buf = write_bufs['/etc/sysconfig/network']
 
169
        expected_buf = '''
 
170
# Created by cloud-init v. 0.7
 
171
NETWORKING=yes
 
172
'''
 
173
        self.assertCfgEquals(expected_buf, str(write_buf))
 
174
        self.assertEquals(write_buf.mode, 0644)