sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
1064ba
From 8fd2f03c40606ad25135385f7badf42e48d713b6 Mon Sep 17 00:00:00 2001
1064ba
From: Ryan Harper <ryan.harper@canonical.com>
1064ba
Date: Fri, 9 Jun 2017 12:35:11 -0500
1064ba
Subject: [PATCH 2/2] sysconfig: include GATEWAY value if set in subnet
1064ba
1064ba
Render the GATEWAY= value in interface files which have a gateway in the
1064ba
subnet configuration.
1064ba
1064ba
LP: #1686856
1064ba
(cherry picked from commit d1e8eb73aca6a3f5cee415774dcf540e934ec250)
1064ba
1064ba
Resolves: rhbz#1465730
1064ba
Signed-off-by: Ryan McCabe <rmccabe@redhat.com>
1064ba
(cherry picked from commit 158f53f223b763ddfbfa5967c58866424ae02689)
1064ba
1064ba
Resolves: rhbz#1492726
1064ba
Signed-off-by: Ryan McCabe <rmccabe@redhat.com>
1064ba
---
1064ba
 cloudinit/net/sysconfig.py                     |  3 ++
1064ba
 tests/unittests/test_distros/test_netconfig.py |  2 ++
1064ba
 tests/unittests/test_net.py                    | 50 +++++++++++++++++++++++++-
1064ba
 3 files changed, 54 insertions(+), 1 deletion(-)
1064ba
1064ba
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py
1064ba
index 25c29104..ca031691 100644
1064ba
--- a/cloudinit/net/sysconfig.py
1064ba
+++ b/cloudinit/net/sysconfig.py
1064ba
@@ -347,6 +347,9 @@ class Renderer(renderer.Renderer):
1064ba
                             iface_cfg['NETMASK' + str(ipv4_index)] = \
1064ba
                                 subnet['netmask']
1064ba
 
1064ba
+                if 'gateway' in subnet:
1064ba
+                    iface_cfg['GATEWAY'] = subnet['gateway']
1064ba
+
1064ba
     @classmethod
1064ba
     def _render_subnet_routes(cls, iface_cfg, route_cfg, subnets):
1064ba
         for i, subnet in enumerate(subnets, start=len(iface_cfg.children)):
1064ba
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
1064ba
index 861cf8ef..108d5741 100644
1064ba
--- a/tests/unittests/test_distros/test_netconfig.py
1064ba
+++ b/tests/unittests/test_distros/test_netconfig.py
1064ba
@@ -260,6 +260,7 @@ BOOTPROTO=none
1064ba
 DEVICE=eth0
1064ba
 IPADDR=192.168.1.5
1064ba
 NETMASK=255.255.255.0
1064ba
+GATEWAY=192.168.1.254
1064ba
 ONBOOT=yes
1064ba
 TYPE=Ethernet
1064ba
 USERCTL=no
1064ba
@@ -396,6 +397,7 @@ IPV6_AUTOCONF=no
1064ba
 BOOTPROTO=none
1064ba
 DEVICE=eth0
1064ba
 IPV6ADDR=2607:f0d0:1002:0011::2/64
1064ba
+GATEWAY=2607:f0d0:1002:0011::1
1064ba
 IPV6INIT=yes
1064ba
 ONBOOT=yes
1064ba
 TYPE=Ethernet
1064ba
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
1064ba
index 7e389c10..4a32eb88 100644
1064ba
--- a/tests/unittests/test_net.py
1064ba
+++ b/tests/unittests/test_net.py
1064ba
@@ -615,6 +615,7 @@ pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 11.0.0.1 metric 3 || true
1064ba
     }
1064ba
 }
1064ba
 
1064ba
+
1064ba
 CONFIG_V1_EXPLICIT_LOOPBACK = {
1064ba
     'version': 1,
1064ba
     'config': [{'name': 'eth0', 'type': 'physical',
1064ba
@@ -624,6 +625,30 @@ CONFIG_V1_EXPLICIT_LOOPBACK = {
1064ba
                ]}
1064ba
 
1064ba
 
1064ba
+CONFIG_V1_SIMPLE_SUBNET = {
1064ba
+    'version': 1,
1064ba
+    'config': [{'mac_address': '52:54:00:12:34:00',
1064ba
+                'name': 'interface0',
1064ba
+                'subnets': [{'address': '10.0.2.15',
1064ba
+                             'gateway': '10.0.2.2',
1064ba
+                             'netmask': '255.255.255.0',
1064ba
+                             'type': 'static'}],
1064ba
+                'type': 'physical'}]}
1064ba
+
1064ba
+
1064ba
+DEFAULT_DEV_ATTRS = {
1064ba
+    'eth1000': {
1064ba
+        "bridge": False,
1064ba
+        "carrier": False,
1064ba
+        "dormant": False,
1064ba
+        "operstate": "down",
1064ba
+        "address": "07-1C-C6-75-A4-BE",
1064ba
+        "device/driver": None,
1064ba
+        "device/device": None,
1064ba
+    }
1064ba
+}
1064ba
+
1064ba
+
1064ba
 def _setup_test(tmp_dir, mock_get_devicelist, mock_read_sys_net,
1064ba
                 mock_sys_dev_path):
1064ba
     mock_get_devicelist.return_value = ['eth1000']
1064ba
@@ -793,6 +818,30 @@ USERCTL=no
1064ba
                 with open(os.path.join(render_dir, fn)) as fh:
1064ba
                     self.assertEqual(expected_content, fh.read())
1064ba
 
1064ba
+    def test_network_config_v1_samples(self):
1064ba
+        ns = network_state.parse_net_config_data(CONFIG_V1_SIMPLE_SUBNET)
1064ba
+        render_dir = self.tmp_path("render")
1064ba
+        os.makedirs(render_dir)
1064ba
+        renderer = sysconfig.Renderer()
1064ba
+        renderer.render_network_state(render_dir, ns)
1064ba
+        found = dir2dict(render_dir)
1064ba
+        nspath = '/etc/sysconfig/network-scripts/'
1064ba
+        self.assertNotIn(nspath + 'ifcfg-lo', found.keys())
1064ba
+        expected = """\
1064ba
+# Created by cloud-init on instance boot automatically, do not edit.
1064ba
+#
1064ba
+BOOTPROTO=none
1064ba
+DEVICE=interface0
1064ba
+GATEWAY=10.0.2.2
1064ba
+HWADDR=52:54:00:12:34:00
1064ba
+IPADDR=10.0.2.15
1064ba
+NETMASK=255.255.255.0
1064ba
+ONBOOT=yes
1064ba
+TYPE=Ethernet
1064ba
+USERCTL=no
1064ba
+"""
1064ba
+        self.assertEqual(expected, found[nspath + 'ifcfg-interface0'])
1064ba
+
1064ba
     def test_config_with_explicit_loopback(self):
1064ba
         ns = network_state.parse_net_config_data(CONFIG_V1_EXPLICIT_LOOPBACK)
1064ba
         render_dir = self.tmp_path("render")
1064ba
@@ -807,7 +856,6 @@ USERCTL=no
1064ba
 #
1064ba
 BOOTPROTO=dhcp
1064ba
 DEVICE=eth0
1064ba
-NM_CONTROLLED=no
1064ba
 ONBOOT=yes
1064ba
 TYPE=Ethernet
1064ba
 USERCTL=no
1064ba
-- 
1064ba
2.13.6
1064ba