diff options
-rw-r--r-- | cloudinit/net/sysconfig.py | 20 | ||||
-rw-r--r-- | tests/unittests/test_net.py | 80 |
2 files changed, 95 insertions, 5 deletions
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py index 01770213..a4bcaaad 100644 --- a/cloudinit/net/sysconfig.py +++ b/cloudinit/net/sysconfig.py @@ -24,6 +24,19 @@ from . import network_state from .udev import generate_udev_rule +def _make_header(sep='#'): + lines = [ + "Created by cloud-init on instance boot automatically, do not edit.", + "", + ] + for i in range(0, len(lines)): + if lines[i]: + lines[i] = sep + " " + lines[i] + else: + lines[i] = sep + return "\n".join(lines) + + def _filter_by_type(match_type): return lambda iface: match_type == iface['type'] @@ -80,9 +93,6 @@ class NetworkStateHelper(object): class ConfigMap(object): """Sysconfig like dictionary object.""" - default_header = ('# Created by cloud-init on instance' - ' boot automatically, do not edit.\n#') - # Why does redhat prefer yes/no to true/false?? _bool_map = { True: 'yes', @@ -103,7 +113,7 @@ class ConfigMap(object): def to_string(self): buf = six.StringIO() - buf.write(self.default_header) + buf.write(_make_header()) if self._conf: buf.write("\n") for key in sorted(self._conf.keys()): @@ -365,7 +375,7 @@ class Renderer(object): content.add_nameserver(ns) for d in network_state.dns_searchdomains: content.add_search_domain(d) - return str(content) + return "\n".join([_make_header(';'), str(content)]) @classmethod def _render_bridge_interfaces(cls, network_state, iface_contents): diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index e899c749..4f4515a9 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -3,6 +3,7 @@ from cloudinit.net import cmdline from cloudinit.net import eni from cloudinit.net import network_state from cloudinit.net import sysconfig +from cloudinit.sources.helpers import openstack from cloudinit import util from .helpers import mock @@ -144,6 +145,85 @@ USERCTL=no """.lstrip() self.assertEqual(expected_content, content) + def test_openstack_rendering_sample(self): + ex_input = { + "services": [{"type": "dns", "address": "172.19.0.12"}], + "networks": [ + {"network_id": "dacd568d-5be6-4786-91fe-750c374b78b4", + "type": "ipv4", "netmask": "255.255.252.0", + "link": "tap1a81968a-79", + "routes": [{"netmask": "0.0.0.0", + "network": "0.0.0.0", + "gateway": "172.19.3.254"}], + "ip_address": "172.19.1.34", "id": "network0"}], + "links": [{"ethernet_mac_address": "fa:16:3e:ed:9a:59", + "mtu": None, "type": "bridge", "id": + "tap1a81968a-79", + "vif_id": "1a81968a-797a-400f-8a80-567f997eb93f"}]} + ex_mac_addrs = { + 'fa:16:3e:ed:9a:59': 'eth0', + } + network_cfg = openstack.convert_net_json( + ex_input, known_macs=ex_mac_addrs) + ns = network_state.parse_net_config_data(network_cfg, + skip_broken=False) + + tmp_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, tmp_dir) + render_dir = os.path.join(tmp_dir, "render") + renderer = sysconfig.Renderer() + renderer.render_network_state(render_dir, ns) + + render_file = 'etc/sysconfig/network-scripts/ifcfg-eth0' + with open(os.path.join(render_dir, render_file)) as fh: + content = fh.read() + expected_content = """ +# Created by cloud-init on instance boot automatically, do not edit. +# +BOOTPROTO=static +DEFROUTE=yes +DEVICE=eth0 +GATEWAY=172.19.3.254 +HWADDR=fa:16:3e:ed:9a:59 +IPADDR=172.19.1.34 +NETMASK=255.255.252.0 +NM_CONTROLLED=no +ONBOOT=yes +TYPE=Ethernet +USERCTL=no +""".lstrip() + self.assertEqual(expected_content, content) + + route_render_file = 'etc/sysconfig/network-scripts/route-eth0' + with open(os.path.join(render_dir, route_render_file)) as fh: + content = fh.read() + expected_content = """ +# Created by cloud-init on instance boot automatically, do not edit. +# +ADDRESS0=0.0.0.0 +GATEWAY0=172.19.3.254 +NETMASK0=0.0.0.0 +""".lstrip() + self.assertEqual(expected_content, content) + + resolv_render_file = 'etc/resolv.conf' + with open(os.path.join(render_dir, resolv_render_file)) as fh: + content = fh.read() + expected_content = """ +; Created by cloud-init on instance boot automatically, do not edit. +; +nameserver 172.19.0.12 +""".lstrip() + self.assertEqual(expected_content, content) + + rules_render_file = 'etc/udev/rules.d/70-persistent-net.rules' + with open(os.path.join(render_dir, rules_render_file)) as fh: + content = fh.read() + expected_content = "".join([ + 'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ', + 'ATTR{address}=="fa:16:3e:ed:9a:59", NAME="eth0"\n']) + self.assertEqual(expected_content, content) + class TestEniNetRendering(TestCase): |