1
from mocker import MockerTestCase
7
from cloudinit import distros
8
from cloudinit import helpers
9
from cloudinit import settings
10
from cloudinit import util
12
from StringIO import StringIO
17
iface lo inet loopback
20
iface eth0 inet static
32
class WriteBuffer(object):
34
self.buffer = StringIO()
38
def write(self, text):
39
self.buffer.write(text)
42
return self.buffer.getvalue()
45
class TestNetCfgDistro(MockerTestCase):
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)
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)
61
exists_mock(mocker.ARGS)
62
self.mocker.count(0, None)
63
self.mocker.result(False)
67
def replace_write(filename, content, mode=0644, omode="wb"):
72
write_bufs[filename] = buf
74
util_mock(mocker.ARGS)
75
self.mocker.call(replace_write)
77
ub_distro.apply_network(BASE_NET_CFG, False)
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)
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():
92
for (k, v) in b2.items():
94
for (k, v) in b1.items():
95
self.assertEquals(v, b2[k])
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)
108
def replace_write(filename, content, mode=0644, omode="wb"):
113
write_bufs[filename] = buf
115
exists_mock(mocker.ARGS)
116
self.mocker.count(0, None)
117
self.mocker.result(False)
119
load_mock(mocker.ARGS)
120
self.mocker.count(0, None)
121
self.mocker.result('')
123
for _i in range(0, 3):
124
write_mock(mocker.ARGS)
125
self.mocker.call(replace_write)
127
write_mock(mocker.ARGS)
128
self.mocker.call(replace_write)
131
rh_distro.apply_network(BASE_NET_CFG, False)
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']
140
self.assertCfgEquals(expected_buf, str(write_buf))
141
self.assertEquals(write_buf.mode, 0644)
143
self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0', write_bufs)
144
write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
148
NETMASK="255.255.255.0"
151
GATEWAY="192.168.1.254"
152
BROADCAST="192.168.1.0"
154
self.assertCfgEquals(expected_buf, str(write_buf))
155
self.assertEquals(write_buf.mode, 0644)
157
self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1', write_bufs)
158
write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
164
self.assertCfgEquals(expected_buf, str(write_buf))
165
self.assertEquals(write_buf.mode, 0644)
167
self.assertIn('/etc/sysconfig/network', write_bufs)
168
write_buf = write_bufs['/etc/sysconfig/network']
170
# Created by cloud-init v. 0.7
173
self.assertCfgEquals(expected_buf, str(write_buf))
174
self.assertEquals(write_buf.mode, 0644)