summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2016-07-14 14:25:09 -0400
committerScott Moser <smoser@ubuntu.com>2016-07-14 14:25:09 -0400
commit74e4dff3774075b4e471df0258c8584feeb352bb (patch)
tree5815cfa2ec4f9035753e904f30116fdd5b872553 /tests/unittests
parent660e04e658381ee52c9dc5f877745614cee15c42 (diff)
parent9a0189ccda40e366517c0690e0ea2833e533bf1a (diff)
downloadvyos-cloud-init-74e4dff3774075b4e471df0258c8584feeb352bb.tar.gz
vyos-cloud-init-74e4dff3774075b4e471df0258c8584feeb352bb.zip
ConfigDrive: fix writing of 'injected' files and legacy networking
Previous commit inadvertently disabled the consumption of 'injected' files in configdrive (openstack server boot --file=/target/file=local-file) unless the datasource was in 'pass' mode. The default mode is 'net' so that was not likely to happen. Also here are: a.) some comments to apply_network_config b.) add backwards compatibility for distros that do not yet implement apply_network_config by converting the network config into ENI format and calling apply_network. This is required because prior to the previous commit, those distros would have had 'apply_network' called with the openstack provided ENI file. But after this change they will have apply_network_config called by cloudinit's main. c.) add network_state_to_eni for converting net config to eni it supports the not-actually-correct 'hwaddress' field in ENI LP: #1602373
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/test_distros/test_netconfig.py60
-rw-r--r--tests/unittests/test_net.py31
2 files changed, 91 insertions, 0 deletions
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index 9172e3aa..36eae2dc 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -319,3 +319,63 @@ defaultrouter="192.168.1.254"
'''
self.assertCfgEquals(expected_buf, str(write_buf))
self.assertEqual(write_buf.mode, 0o644)
+
+ def test_apply_network_config_fallback(self):
+ fbsd_distro = self._get_distro('freebsd')
+
+ # a weak attempt to verify that we don't have an implementation
+ # of _write_network_config or apply_network_config in fbsd now,
+ # which would make this test not actually test the fallback.
+ self.assertRaises(
+ NotImplementedError, fbsd_distro._write_network_config,
+ BASE_NET_CFG)
+
+ # now run
+ mynetcfg = {
+ 'config': [{"type": "physical", "name": "eth0",
+ "mac_address": "c0:d6:9f:2c:e8:80",
+ "subnets": [{"type": "dhcp"}]}],
+ 'version': 1}
+
+ write_bufs = {}
+ read_bufs = {
+ '/etc/rc.conf': '',
+ '/etc/resolv.conf': '',
+ }
+
+ def replace_write(filename, content, mode=0o644, omode="wb"):
+ buf = WriteBuffer()
+ buf.mode = mode
+ buf.omode = omode
+ buf.write(content)
+ write_bufs[filename] = buf
+
+ def replace_read(fname, read_cb=None, quiet=False):
+ if fname not in read_bufs:
+ if fname in write_bufs:
+ return str(write_bufs[fname])
+ raise IOError("%s not found" % fname)
+ else:
+ if fname in write_bufs:
+ return str(write_bufs[fname])
+ return read_bufs[fname]
+
+ with ExitStack() as mocks:
+ mocks.enter_context(
+ mock.patch.object(util, 'subp', return_value=('vtnet0', '')))
+ mocks.enter_context(
+ mock.patch.object(os.path, 'exists', return_value=False))
+ mocks.enter_context(
+ mock.patch.object(util, 'write_file', replace_write))
+ mocks.enter_context(
+ mock.patch.object(util, 'load_file', replace_read))
+
+ fbsd_distro.apply_network_config(mynetcfg, bring_up=False)
+
+ self.assertIn('/etc/rc.conf', write_bufs)
+ write_buf = write_bufs['/etc/rc.conf']
+ expected_buf = '''
+ifconfig_vtnet0="DHCP"
+'''
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEqual(write_buf.mode, 0o644)
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index 3ae00fc6..6f4dad13 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -269,6 +269,37 @@ iface eth1000 inet dhcp
self.assertEqual(expected.lstrip(), contents.lstrip())
+class TestEniNetworkStateToEni(TestCase):
+ mycfg = {
+ 'config': [{"type": "physical", "name": "eth0",
+ "mac_address": "c0:d6:9f:2c:e8:80",
+ "subnets": [{"type": "dhcp"}]}],
+ 'version': 1}
+ my_mac = 'c0:d6:9f:2c:e8:80'
+
+ def test_no_header(self):
+ rendered = eni.network_state_to_eni(
+ network_state=network_state.parse_net_config_data(self.mycfg),
+ render_hwaddress=True)
+ self.assertIn(self.my_mac, rendered)
+ self.assertIn("hwaddress", rendered)
+
+ def test_with_header(self):
+ header = "# hello world\n"
+ rendered = eni.network_state_to_eni(
+ network_state=network_state.parse_net_config_data(self.mycfg),
+ header=header, render_hwaddress=True)
+ self.assertIn(header, rendered)
+ self.assertIn(self.my_mac, rendered)
+
+ def test_no_hwaddress(self):
+ rendered = eni.network_state_to_eni(
+ network_state=network_state.parse_net_config_data(self.mycfg),
+ render_hwaddress=False)
+ self.assertNotIn(self.my_mac, rendered)
+ self.assertNotIn("hwaddress", rendered)
+
+
class TestCmdlineConfigParsing(TestCase):
simple_cfg = {
'config': [{"type": "physical", "name": "eth0",