diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unittests/helpers.py | 12 | ||||
-rw-r--r-- | tests/unittests/test_net.py | 208 |
2 files changed, 220 insertions, 0 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py index 8d46a8bf..972245df 100644 --- a/tests/unittests/helpers.py +++ b/tests/unittests/helpers.py @@ -264,6 +264,18 @@ def populate_dir(path, files): fp.close() +def dir2dict(startdir, prefix=None): + flist = {} + if prefix is None: + prefix = startdir + for root, dirs, files in os.walk(startdir): + for fname in files: + fpath = os.path.join(root, fname) + key = fpath[len(prefix):] + flist[key] = util.load_file(fpath) + return flist + + try: skipIf = unittest.skipIf except AttributeError: diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index 3ae00fc6..a9268d30 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -8,6 +8,7 @@ from cloudinit import util from .helpers import mock from .helpers import TestCase +from .helpers import dir2dict import base64 import copy @@ -17,6 +18,7 @@ import json import os import shutil import tempfile +import yaml DHCP_CONTENT_1 = """ DEVICE='eth0' @@ -141,6 +143,163 @@ nameserver 172.19.0.12 } ] +EXAMPLE_ENI = """ +auto lo +iface lo inet loopback + dns-nameservers 10.0.0.1 + dns-search foo.com + +auto eth0 +iface eth0 inet static + address 1.2.3.12 + netmask 255.255.255.248 + broadcast 1.2.3.15 + gateway 1.2.3.9 + dns-nameservers 69.9.160.191 69.9.191.4 +auto eth1 +iface eth1 inet static + address 10.248.2.4 + netmask 255.255.255.248 + broadcast 10.248.2.7 +""" + +NETWORK_YAML_SMALL = """ +version: 1 +config: + # Physical interfaces. + - type: physical + name: eth0 + mac_address: "c0:d6:9f:2c:e8:80" + subnets: + - type: dhcp4 + - type: static + address: 192.168.21.3/24 + dns_nameservers: + - 8.8.8.8 + - 8.8.4.4 + dns_search: barley.maas sach.maas + - type: physical + name: eth1 + mac_address: "cf:d6:af:48:e8:80" + - type: nameserver + address: + - 1.2.3.4 + - 5.6.7.8 + search: + - wark.maas +""" +NETWORK_YAML_ALL = """ +version: 1 +config: + # Physical interfaces. + - type: physical + name: eth0 + mac_address: "c0:d6:9f:2c:e8:80" + - type: physical + name: eth1 + mac_address: "aa:d6:9f:2c:e8:80" + - type: physical + name: eth2 + mac_address: "c0:bb:9f:2c:e8:80" + - type: physical + name: eth3 + mac_address: "66:bb:9f:2c:e8:80" + - type: physical + name: eth4 + mac_address: "98:bb:9f:2c:e8:80" + # specify how ifupdown should treat iface + # control is one of ['auto', 'hotplug', 'manual'] + # with manual meaning ifup/ifdown should not affect the iface + # useful for things like iscsi root + dhcp + - type: physical + name: eth5 + mac_address: "98:bb:9f:2c:e8:8a" + subnets: + - type: dhcp + control: manual + # VLAN interface. + - type: vlan + name: eth0.101 + vlan_link: eth0 + vlan_id: 101 + mtu: 1500 + subnets: + - type: static + address: 192.168.0.2/24 + gateway: 192.168.0.1 + dns_nameservers: + - 192.168.0.10 + - 10.23.23.134 + dns_search: + - barley.maas + - sacchromyces.maas + - brettanomyces.maas + - type: static + address: 192.168.2.10/24 + # Bond. + - type: bond + name: bond0 + # if 'mac_address' is omitted, the MAC is taken from + # the first slave. + mac_address: "aa:bb:cc:dd:ee:ff" + bond_interfaces: + - eth1 + - eth2 + params: + bond-mode: active-backup + subnets: + - type: dhcp6 + # A Bond VLAN. + - type: vlan + name: bond0.200 + vlan_link: bond0 + vlan_id: 200 + subnets: + - type: dhcp4 + # A bridge. + - type: bridge + name: br0 + bridge_interfaces: + - eth3 + - eth4 + ipv4_conf: + rp_filter: 1 + proxy_arp: 0 + forwarding: 1 + ipv6_conf: + autoconf: 1 + disable_ipv6: 1 + use_tempaddr: 1 + forwarding: 1 + # basically anything in /proc/sys/net/ipv6/conf/.../ + params: + bridge_stp: 'off' + bridge_fd: 0 + bridge_maxwait: 0 + subnets: + - type: static + address: 192.168.14.2/24 + - type: static + address: 2001:1::1/64 # default to /64 + # A global nameserver. + - type: nameserver + address: 8.8.8.8 + search: barley.maas + # global nameservers and search in list form + - type: nameserver + address: + - 4.4.4.4 + - 8.8.4.4 + search: + - wark.maas + - foobar.maas + # A global route. + - type: route + destination: 10.0.0.0/8 + gateway: 11.0.0.1 + metric: 3 +""" + def _setup_test(tmp_dir, mock_get_devicelist, mock_sys_netdev_info, mock_sys_dev_path): @@ -323,6 +482,55 @@ class TestCmdlineConfigParsing(TestCase): self.assertEqual(found, self.simple_cfg) +class TestEniRoundTrip(TestCase): + def setUp(self): + super(TestCase, self).setUp() + self.tmp_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.tmp_dir) + + def _render_and_read(self, network_config=None, state=None, eni_path=None, + links_prefix=None, netrules_path=None): + if network_config: + ns = network_state.parse_net_config_data(network_config) + elif state: + ns = state + else: + raise ValueError("Expected data or state, got neither") + + if eni_path is None: + eni_path = 'etc/network/interfaces' + + renderer = eni.Renderer( + config={'eni_path': eni_path, 'links_path_prefix': links_prefix, + 'netrules_path': netrules_path}) + + renderer.render_network_state(self.tmp_dir, ns) + return dir2dict(self.tmp_dir) + + def testsimple_convert_and_render(self): + network_config = eni.convert_eni_data(EXAMPLE_ENI) + ns = network_state.parse_net_config_data(network_config) + eni_path = 'etc/network/interfaces.d/my.interfaces' + eni_full_path = os.path.join(self.tmp_dir, eni_path) + renderer = eni.Renderer(config={'eni_path': eni_path}) + renderer.render_network_state(self.tmp_dir, ns) + eni_content = util.load_file(eni_full_path) + print("Eni looks like: %s" % eni_content) + raise Exception("FOO1") + + def testsimple_render_all(self): + files = self._render_and_read(network_config=yaml.load(NETWORK_YAML_ALL)) + print("files: %s" % files) + raise Exception("FOO2") + + def skiptestsimple_render_small(self): + network_config = yaml.load(NETWORK_YAML_SMALL) + ns = network_state.parse_net_config_data(network_config) + eni = net.render_interfaces(ns) + print("Eni looks like:\n%s" % eni) + raise Exception("FOO3") + + def _gzip_data(data): with io.BytesIO() as iobuf: gzfp = gzip.GzipFile(mode="wb", fileobj=iobuf) |