Line 0
Link Here
|
|
|
1 |
--- tests/unittests/test_distros/test_netconfig.py.orig 2019-07-17 16:30:07 UTC |
2 |
+++ tests/unittests/test_distros/test_netconfig.py |
3 |
@@ -1,5 +1,6 @@ |
4 |
# This file is part of cloud-init. See LICENSE file for license information. |
5 |
|
6 |
+import copy |
7 |
import os |
8 |
from six import StringIO |
9 |
from textwrap import dedent |
10 |
@@ -14,7 +15,7 @@ from cloudinit.distros.parsers.sys_conf import SysConf |
11 |
from cloudinit import helpers |
12 |
from cloudinit import settings |
13 |
from cloudinit.tests.helpers import ( |
14 |
- FilesystemMockingTestCase, dir2dict, populate_dir) |
15 |
+ FilesystemMockingTestCase, dir2dict) |
16 |
from cloudinit import util |
17 |
|
18 |
|
19 |
@@ -213,128 +214,95 @@ class TestNetCfgDistroBase(FilesystemMockingTestCase): |
20 |
self.assertEqual(v, b2[k]) |
21 |
|
22 |
|
23 |
-class TestNetCfgDistroFreebsd(TestNetCfgDistroBase): |
24 |
+class TestNetCfgDistroFreeBSD(TestNetCfgDistroBase): |
25 |
|
26 |
- frbsd_ifout = """\ |
27 |
-hn0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500 |
28 |
- options=51b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,TSO4,LRO> |
29 |
- ether 00:15:5d:4c:73:00 |
30 |
- inet6 fe80::215:5dff:fe4c:7300%hn0 prefixlen 64 scopeid 0x2 |
31 |
- inet 10.156.76.127 netmask 0xfffffc00 broadcast 10.156.79.255 |
32 |
- nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL> |
33 |
- media: Ethernet autoselect (10Gbase-T <full-duplex>) |
34 |
- status: active |
35 |
-""" |
36 |
+ def setUp(self): |
37 |
+ super(TestNetCfgDistroFreeBSD, self).setUp() |
38 |
+ self.distro = self._get_distro('freebsd', renderers=['freebsd']) |
39 |
|
40 |
- @mock.patch('cloudinit.distros.freebsd.Distro.get_ifconfig_list') |
41 |
- @mock.patch('cloudinit.distros.freebsd.Distro.get_ifconfig_ifname_out') |
42 |
- def test_get_ip_nic_freebsd(self, ifname_out, iflist): |
43 |
- frbsd_distro = self._get_distro('freebsd') |
44 |
- iflist.return_value = "lo0 hn0" |
45 |
- ifname_out.return_value = self.frbsd_ifout |
46 |
- res = frbsd_distro.get_ipv4() |
47 |
- self.assertEqual(res, ['lo0', 'hn0']) |
48 |
- res = frbsd_distro.get_ipv6() |
49 |
- self.assertEqual(res, []) |
50 |
+ def _apply_and_verify_freebsd(self, apply_fn, config, expected_cfgs=None, |
51 |
+ bringup=False): |
52 |
+ if not expected_cfgs: |
53 |
+ raise ValueError('expected_cfg must not be None') |
54 |
|
55 |
- @mock.patch('cloudinit.distros.freebsd.Distro.get_ifconfig_ether') |
56 |
- @mock.patch('cloudinit.distros.freebsd.Distro.get_ifconfig_ifname_out') |
57 |
- @mock.patch('cloudinit.distros.freebsd.Distro.get_interface_mac') |
58 |
- def test_generate_fallback_config_freebsd(self, mac, ifname_out, if_ether): |
59 |
- frbsd_distro = self._get_distro('freebsd') |
60 |
+ tmpd = None |
61 |
+ with mock.patch('cloudinit.net.freebsd.available') as m_avail: |
62 |
+ m_avail.return_value = True |
63 |
+ with self.reRooted(tmpd) as tmpd: |
64 |
+ util.ensure_dir('/etc') |
65 |
+ util.ensure_file('/etc/rc.conf') |
66 |
+ util.ensure_file('/etc/resolv.conf') |
67 |
+ apply_fn(config, bringup) |
68 |
|
69 |
- if_ether.return_value = 'hn0' |
70 |
- ifname_out.return_value = self.frbsd_ifout |
71 |
- mac.return_value = '00:15:5d:4c:73:00' |
72 |
- res = frbsd_distro.generate_fallback_config() |
73 |
- self.assertIsNotNone(res) |
74 |
+ results = dir2dict(tmpd) |
75 |
+ for cfgpath, expected in expected_cfgs.items(): |
76 |
+ print("----------") |
77 |
+ print(expected) |
78 |
+ print("^^^^ expected | rendered VVVVVVV") |
79 |
+ print(results[cfgpath]) |
80 |
+ print("----------") |
81 |
+ self.assertEqual( |
82 |
+ set(expected.split('\n')), |
83 |
+ set(results[cfgpath].split('\n'))) |
84 |
+ self.assertEqual(0o644, get_mode(cfgpath, tmpd)) |
85 |
|
86 |
- def test_simple_write_freebsd(self): |
87 |
- fbsd_distro = self._get_distro('freebsd') |
88 |
+ @mock.patch('cloudinit.net.get_interfaces_by_mac') |
89 |
+ def test_apply_network_config_freebsd_standard(self, ifaces_mac): |
90 |
+ ifaces_mac.return_value = { |
91 |
+ '00:15:5d:4c:73:00': 'eth0', |
92 |
+ } |
93 |
+ rc_conf_expected = """\ |
94 |
+defaultrouter=192.168.1.254 |
95 |
+ifconfig_eth0='192.168.1.5 netmask 255.255.255.0' |
96 |
+ifconfig_eth1=DHCP |
97 |
+""" |
98 |
|
99 |
- rc_conf = '/etc/rc.conf' |
100 |
- read_bufs = { |
101 |
- rc_conf: 'initial-rc-conf-not-validated', |
102 |
- '/etc/resolv.conf': 'initial-resolv-conf-not-validated', |
103 |
+ expected_cfgs = { |
104 |
+ '/etc/rc.conf': rc_conf_expected, |
105 |
+ '/etc/resolv.conf': '' |
106 |
} |
107 |
+ self._apply_and_verify_freebsd(self.distro.apply_network_config, |
108 |
+ V1_NET_CFG, |
109 |
+ expected_cfgs=expected_cfgs.copy()) |
110 |
|
111 |
- tmpd = self.tmp_dir() |
112 |
- populate_dir(tmpd, read_bufs) |
113 |
- with self.reRooted(tmpd): |
114 |
- with mock.patch("cloudinit.distros.freebsd.util.subp", |
115 |
- return_value=('vtnet0', '')): |
116 |
- fbsd_distro.apply_network(BASE_NET_CFG, False) |
117 |
- results = dir2dict(tmpd) |
118 |
+ @mock.patch('cloudinit.net.get_interfaces_by_mac') |
119 |
+ def test_apply_network_config_freebsd_ifrename(self, ifaces_mac): |
120 |
+ ifaces_mac.return_value = { |
121 |
+ '00:15:5d:4c:73:00': 'vtnet0', |
122 |
+ } |
123 |
+ rc_conf_expected = """\ |
124 |
+ifconfig_vtnet0_name=eth0 |
125 |
+defaultrouter=192.168.1.254 |
126 |
+ifconfig_eth0='192.168.1.5 netmask 255.255.255.0' |
127 |
+ifconfig_eth1=DHCP |
128 |
+""" |
129 |
|
130 |
- self.assertIn(rc_conf, results) |
131 |
- self.assertCfgEquals( |
132 |
- dedent('''\ |
133 |
- ifconfig_vtnet0="192.168.1.5 netmask 255.255.255.0" |
134 |
- ifconfig_vtnet1="DHCP" |
135 |
- defaultrouter="192.168.1.254" |
136 |
- '''), results[rc_conf]) |
137 |
- self.assertEqual(0o644, get_mode(rc_conf, tmpd)) |
138 |
+ V1_NET_CFG_RENAME = copy.deepcopy(V1_NET_CFG) |
139 |
+ V1_NET_CFG_RENAME['config'][0]['mac_address'] = '00:15:5d:4c:73:00' |
140 |
|
141 |
- def test_simple_write_freebsd_from_v2eni(self): |
142 |
- fbsd_distro = self._get_distro('freebsd') |
143 |
- |
144 |
- rc_conf = '/etc/rc.conf' |
145 |
- read_bufs = { |
146 |
- rc_conf: 'initial-rc-conf-not-validated', |
147 |
- '/etc/resolv.conf': 'initial-resolv-conf-not-validated', |
148 |
+ expected_cfgs = { |
149 |
+ '/etc/rc.conf': rc_conf_expected, |
150 |
+ '/etc/resolv.conf': '' |
151 |
} |
152 |
+ self._apply_and_verify_freebsd(self.distro.apply_network_config, |
153 |
+ V1_NET_CFG_RENAME, |
154 |
+ expected_cfgs=expected_cfgs.copy()) |
155 |
|
156 |
- tmpd = self.tmp_dir() |
157 |
- populate_dir(tmpd, read_bufs) |
158 |
- with self.reRooted(tmpd): |
159 |
- with mock.patch("cloudinit.distros.freebsd.util.subp", |
160 |
- return_value=('vtnet0', '')): |
161 |
- fbsd_distro.apply_network(BASE_NET_CFG_FROM_V2, False) |
162 |
- results = dir2dict(tmpd) |
163 |
- |
164 |
- self.assertIn(rc_conf, results) |
165 |
- self.assertCfgEquals( |
166 |
- dedent('''\ |
167 |
- ifconfig_vtnet0="192.168.1.5 netmask 255.255.255.0" |
168 |
- ifconfig_vtnet1="DHCP" |
169 |
- defaultrouter="192.168.1.254" |
170 |
- '''), results[rc_conf]) |
171 |
- self.assertEqual(0o644, get_mode(rc_conf, tmpd)) |
172 |
- |
173 |
- def test_apply_network_config_fallback_freebsd(self): |
174 |
- fbsd_distro = self._get_distro('freebsd') |
175 |
- |
176 |
- # a weak attempt to verify that we don't have an implementation |
177 |
- # of _write_network_config or apply_network_config in fbsd now, |
178 |
- # which would make this test not actually test the fallback. |
179 |
- self.assertRaises( |
180 |
- NotImplementedError, fbsd_distro._write_network_config, |
181 |
- BASE_NET_CFG) |
182 |
- |
183 |
- # now run |
184 |
- mynetcfg = { |
185 |
- 'config': [{"type": "physical", "name": "eth0", |
186 |
- "mac_address": "c0:d6:9f:2c:e8:80", |
187 |
- "subnets": [{"type": "dhcp"}]}], |
188 |
- 'version': 1} |
189 |
- |
190 |
- rc_conf = '/etc/rc.conf' |
191 |
- read_bufs = { |
192 |
- rc_conf: 'initial-rc-conf-not-validated', |
193 |
- '/etc/resolv.conf': 'initial-resolv-conf-not-validated', |
194 |
+ @mock.patch('cloudinit.net.get_interfaces_by_mac') |
195 |
+ def test_apply_network_config_freebsd_nameserver(self, ifaces_mac): |
196 |
+ ifaces_mac.return_value = { |
197 |
+ '00:15:5d:4c:73:00': 'eth0', |
198 |
} |
199 |
|
200 |
- tmpd = self.tmp_dir() |
201 |
- populate_dir(tmpd, read_bufs) |
202 |
- with self.reRooted(tmpd): |
203 |
- with mock.patch("cloudinit.distros.freebsd.util.subp", |
204 |
- return_value=('vtnet0', '')): |
205 |
- fbsd_distro.apply_network_config(mynetcfg, bring_up=False) |
206 |
- results = dir2dict(tmpd) |
207 |
- |
208 |
- self.assertIn(rc_conf, results) |
209 |
- self.assertCfgEquals('ifconfig_vtnet0="DHCP"', results[rc_conf]) |
210 |
- self.assertEqual(0o644, get_mode(rc_conf, tmpd)) |
211 |
+ V1_NET_CFG_DNS = copy.deepcopy(V1_NET_CFG) |
212 |
+ ns = ['1.2.3.4'] |
213 |
+ V1_NET_CFG_DNS['config'][0]['subnets'][0]['dns_nameservers'] = ns |
214 |
+ expected_cfgs = { |
215 |
+ '/etc/resolv.conf': 'nameserver 1.2.3.4\n' |
216 |
+ } |
217 |
+ self._apply_and_verify_freebsd(self.distro.apply_network_config, |
218 |
+ V1_NET_CFG_DNS, |
219 |
+ expected_cfgs=expected_cfgs.copy()) |
220 |
|
221 |
|
222 |
class TestNetCfgDistroUbuntuEni(TestNetCfgDistroBase): |