diff options
author | Joshua Harlow <harlowja@gmail.com> | 2016-06-06 18:42:29 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@gmail.com> | 2016-06-06 18:42:29 -0700 |
commit | f640797e342b6efbfb838a6350b312935222e992 (patch) | |
tree | 4e893298101cf3141d80b4bf2a2d6e009462502d /tests/unittests/test_datasource/test_configdrive.py | |
parent | 85a53d66ad0241b2d6453d902487bb2edc1512b8 (diff) | |
parent | bc9bd58d1533d996029770da758f73217c15af33 (diff) | |
download | vyos-cloud-init-f640797e342b6efbfb838a6350b312935222e992.tar.gz vyos-cloud-init-f640797e342b6efbfb838a6350b312935222e992.zip |
Rebase against master
Diffstat (limited to 'tests/unittests/test_datasource/test_configdrive.py')
-rw-r--r-- | tests/unittests/test_datasource/test_configdrive.py | 125 |
1 files changed, 115 insertions, 10 deletions
diff --git a/tests/unittests/test_datasource/test_configdrive.py b/tests/unittests/test_datasource/test_configdrive.py index 5395e544..b898e2bd 100644 --- a/tests/unittests/test_datasource/test_configdrive.py +++ b/tests/unittests/test_datasource/test_configdrive.py @@ -6,6 +6,8 @@ import six import tempfile from cloudinit import helpers +from cloudinit.net import eni +from cloudinit.net import network_state from cloudinit import settings from cloudinit.sources import DataSourceConfigDrive as ds from cloudinit.sources.helpers import openstack @@ -64,7 +66,7 @@ NETWORK_DATA = { 'type': 'ovs', 'mtu': None, 'id': 'tap2f88d109-5b'}, {'vif_id': '1a5382f8-04c5-4d75-ab98-d666c1ef52cc', 'ethernet_mac_address': 'fa:16:3e:05:30:fe', - 'type': 'ovs', 'mtu': None, 'id': 'tap1a5382f8-04'} + 'type': 'ovs', 'mtu': None, 'id': 'tap1a5382f8-04', 'name': 'nic0'} ], 'networks': [ {'link': 'tap2ecc7709-b3', 'type': 'ipv4_dhcp', @@ -79,6 +81,35 @@ NETWORK_DATA = { ] } +NETWORK_DATA_2 = { + "services": [ + {"type": "dns", "address": "1.1.1.191"}, + {"type": "dns", "address": "1.1.1.4"}], + "networks": [ + {"network_id": "d94bbe94-7abc-48d4-9c82-4628ea26164a", "type": "ipv4", + "netmask": "255.255.255.248", "link": "eth0", + "routes": [{"netmask": "0.0.0.0", "network": "0.0.0.0", + "gateway": "2.2.2.9"}], + "ip_address": "2.2.2.10", "id": "network0-ipv4"}, + {"network_id": "ca447c83-6409-499b-aaef-6ad1ae995348", "type": "ipv4", + "netmask": "255.255.255.224", "link": "eth1", + "routes": [], "ip_address": "3.3.3.24", "id": "network1-ipv4"}], + "links": [ + {"ethernet_mac_address": "fa:16:3e:dd:50:9a", "mtu": 1500, + "type": "vif", "id": "eth0", "vif_id": "vif-foo1"}, + {"ethernet_mac_address": "fa:16:3e:a8:14:69", "mtu": 1500, + "type": "vif", "id": "eth1", "vif_id": "vif-foo2"}] +} + + +KNOWN_MACS = { + 'fa:16:3e:69:b0:58': 'enp0s1', + 'fa:16:3e:d4:57:ad': 'enp0s2', + 'fa:16:3e:dd:50:9a': 'foo1', + 'fa:16:3e:a8:14:69': 'foo2', + 'fa:16:3e:ed:9a:59': 'foo3', +} + CFG_DRIVE_FILES_V2 = { 'ec2/2009-04-04/meta-data.json': json.dumps(EC2_META), 'ec2/2009-04-04/user-data': USER_DATA, @@ -358,13 +389,14 @@ class TestNetJson(TestCase): """Verify that network_data is present in ds in config-drive-v2.""" populate_dir(self.tmp, CFG_DRIVE_FILES_V2) myds = cfg_ds_from_dir(self.tmp) - self.assertEqual(myds.network_json, NETWORK_DATA) + self.assertIsNotNone(myds.network_json) def test_network_config_is_converted(self): """Verify that network_data is converted and present on ds object.""" populate_dir(self.tmp, CFG_DRIVE_FILES_V2) myds = cfg_ds_from_dir(self.tmp) - network_config = openstack.convert_net_json(NETWORK_DATA) + network_config = openstack.convert_net_json(NETWORK_DATA, + known_macs=KNOWN_MACS) self.assertEqual(myds.network_config, network_config) def test_network_config_conversions(self): @@ -404,19 +436,22 @@ class TestNetJson(TestCase): 'subnets': [{'type': 'dhcp4'}], 'type': 'physical', 'mac_address': 'fa:16:3e:69:b0:58', - 'name': 'tap2ecc7709-b3', + 'name': 'enp0s1', + 'mtu': None, }, { 'subnets': [{'type': 'dhcp4'}], 'type': 'physical', 'mac_address': 'fa:16:3e:d4:57:ad', - 'name': 'tap2f88d109-5b', + 'name': 'enp0s2', + 'mtu': None, }, { 'subnets': [{'type': 'dhcp4'}], 'type': 'physical', 'mac_address': 'fa:16:3e:05:30:fe', - 'name': 'tap1a5382f8-04', + 'name': 'nic0', + 'mtu': None, }, { 'type': 'nameserver', @@ -433,10 +468,10 @@ class TestNetJson(TestCase): 'version': 1, 'config': [ { - 'name': 'tap1a81968a-79', + 'name': 'foo3', 'mac_address': 'fa:16:3e:ed:9a:59', 'mtu': None, - 'type': 'bridge', + 'type': 'physical', 'subnets': [ { 'address': '172.19.1.34', @@ -459,20 +494,90 @@ class TestNetJson(TestCase): }, ] for in_data, out_data in zip(in_datas, out_datas): - self.assertEqual(openstack.convert_net_json(in_data), - out_data) + conv_data = openstack.convert_net_json(in_data, + known_macs=KNOWN_MACS) + self.assertEqual(out_data, conv_data) + + +class TestConvertNetworkData(TestCase): + def setUp(self): + super(TestConvertNetworkData, self).setUp() + self.tmp = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.tmp) + + def _getnames_in_config(self, ncfg): + return set([n['name'] for n in ncfg['config'] + if n['type'] == 'physical']) + + def test_conversion_fills_names(self): + ncfg = openstack.convert_net_json(NETWORK_DATA, known_macs=KNOWN_MACS) + expected = set(['nic0', 'enp0s1', 'enp0s2']) + found = self._getnames_in_config(ncfg) + self.assertEqual(found, expected) + + @mock.patch('cloudinit.net.get_interfaces_by_mac') + def test_convert_reads_system_prefers_name(self, get_interfaces_by_mac): + macs = KNOWN_MACS.copy() + macs.update({'fa:16:3e:05:30:fe': 'foonic1', + 'fa:16:3e:69:b0:58': 'ens1'}) + get_interfaces_by_mac.return_value = macs + + ncfg = openstack.convert_net_json(NETWORK_DATA) + expected = set(['nic0', 'ens1', 'enp0s2']) + found = self._getnames_in_config(ncfg) + self.assertEqual(found, expected) + + def test_convert_raises_value_error_on_missing_name(self): + macs = {'aa:aa:aa:aa:aa:00': 'ens1'} + self.assertRaises(ValueError, openstack.convert_net_json, + NETWORK_DATA, known_macs=macs) + + def test_conversion_with_route(self): + ncfg = openstack.convert_net_json(NETWORK_DATA_2, + known_macs=KNOWN_MACS) + # not the best test, but see that we get a route in the + # network config and that it gets rendered to an ENI file + routes = [] + for n in ncfg['config']: + for s in n.get('subnets', []): + routes.extend(s.get('routes', [])) + self.assertIn( + {'network': '0.0.0.0', 'netmask': '0.0.0.0', 'gateway': '2.2.2.9'}, + routes) + eni_renderer = eni.Renderer() + eni_renderer.render_network_state( + self.tmp, network_state.parse_net_config_data(ncfg)) + with open(os.path.join(self.tmp, "etc", + "network", "interfaces"), 'r') as f: + eni_rendering = f.read() + self.assertIn("route add default gw 2.2.2.9", eni_rendering) def cfg_ds_from_dir(seed_d): cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) cfg_ds.seed_dir = seed_d + cfg_ds.known_macs = KNOWN_MACS.copy() if not cfg_ds.get_data(skip_first_boot=True): raise RuntimeError("Data source did not extract itself from" " seed directory %s" % seed_d) return cfg_ds +def populate_ds_from_read_config(cfg_ds, source, results): + """Patch the DataSourceConfigDrive from the results of + read_config_drive_dir hopefully in line with what it would have + if cfg_ds.get_data had been successfully called""" + cfg_ds.source = source + cfg_ds.metadata = results.get('metadata') + cfg_ds.ec2_metadata = results.get('ec2-metadata') + cfg_ds.userdata_raw = results.get('userdata') + cfg_ds.version = results.get('version') + cfg_ds.network_json = results.get('networkdata') + cfg_ds._network_config = openstack.convert_net_json( + cfg_ds.network_json, known_macs=KNOWN_MACS) + + def populate_dir(seed_dir, files): for (name, content) in files.items(): path = os.path.join(seed_dir, name) |