Blob Blame History Raw
From 26a150ec6af91ae3ed5053069aa0c08d7064800f Mon Sep 17 00:00:00 2001
From: Eduardo Otubo <otubo@redhat.com>
Date: Mon, 4 May 2020 12:39:52 +0200
Subject: [PATCH 1/6] net: append type:dhcp[46] only if dhcp[46] is True in v2
 netconfig

RH-Author: Eduardo Otubo <otubo@redhat.com>
Message-id: <20200327152826.13343-2-otubo@redhat.com>
Patchwork-id: 94459
O-Subject: [RHEL-8.1.z/RHEL-8.2.z cloud-init PATCHv2 1/6] net: append type:dhcp[46] only if dhcp[46] is True in v2 netconfig
Bugzilla: 1811753
RH-Acked-by: Cathy Avery <cavery@redhat.com>
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>

commit bd35300ba36bd63686715fa9661516a518781f6d
Author: Kurt Stieger <kurt@easygo.at>
Date:   Mon Mar 4 15:54:25 2019 +0000

    net: append type:dhcp[46] only if dhcp[46] is True in v2 netconfig

    When providing netplan configuration to cloud-init, the internal
    network state would enable DHCP if the 'dhcp' key was present in
    the source config.  In netplan, dhcp[46] is a boolean and the
    value of the boolean should control whether DHCP is enabled rather
    than the presence of the key.  This issue leaded to  inconsistant
    sysconfig/network-scripts on fedora. 'BOOTPROTO' was always 'dhcp',
    even if the address config was static.

    After this change a dhcp subnet is added only if the 'dhcp' setting
    in source cfg dict is True.

    LP: #1818032

Signed-off-by: Eduardo Otubo <otubo@redhat.com>
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 cloudinit/net/network_state.py |  4 +--
 tests/unittests/test_net.py    | 62 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
index f76e508..539b76d 100644
--- a/cloudinit/net/network_state.py
+++ b/cloudinit/net/network_state.py
@@ -706,9 +706,9 @@ class NetworkStateInterpreter(object):
         """Common ipconfig extraction from v2 to v1 subnets array."""
 
         subnets = []
-        if 'dhcp4' in cfg:
+        if cfg.get('dhcp4'):
             subnets.append({'type': 'dhcp4'})
-        if 'dhcp6' in cfg:
+        if cfg.get('dhcp6'):
             self.use_ipv6 = True
             subnets.append({'type': 'dhcp6'})
 
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index 012c43b..4224301 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -103,6 +103,24 @@ STATIC_EXPECTED_1 = {
                  'address': '10.0.0.2'}],
 }
 
+NETPLAN_DHCP_FALSE = """
+version: 2
+ethernets:
+  ens3:
+    match:
+      macaddress: 52:54:00:ab:cd:ef
+    dhcp4: false
+    dhcp6: false
+    addresses:
+      - 192.168.42.100/24
+      - 2001:db8::100/32
+    gateway4: 192.168.42.1
+    gateway6: 2001:db8::1
+    nameservers:
+      search: [example.com]
+      addresses: [192.168.42.53, 1.1.1.1]
+"""
+
 # Examples (and expected outputs for various renderers).
 OS_SAMPLES = [
     {
@@ -2146,6 +2164,50 @@ USERCTL=no
         self._compare_files_to_expected(entry[self.expected_name], found)
         self._assert_headers(found)
 
+    def test_netplan_dhcp_false_disable_dhcp_in_state(self):
+        """netplan config with dhcp[46]: False should not add dhcp in state"""
+        net_config = yaml.load(NETPLAN_DHCP_FALSE)
+        ns = network_state.parse_net_config_data(net_config,
+                                                 skip_broken=False)
+
+        dhcp_found = [snet for iface in ns.iter_interfaces()
+                      for snet in iface['subnets'] if 'dhcp' in snet['type']]
+
+        self.assertEqual([], dhcp_found)
+
+    def test_netplan_dhcp_false_no_dhcp_in_sysconfig(self):
+        """netplan cfg with dhcp[46]: False should not have bootproto=dhcp"""
+
+        entry = {
+            'yaml': NETPLAN_DHCP_FALSE,
+            'expected_sysconfig': {
+                'ifcfg-ens3': textwrap.dedent("""\
+                   BOOTPROTO=none
+                   DEFROUTE=yes
+                   DEVICE=ens3
+                   DNS1=192.168.42.53
+                   DNS2=1.1.1.1
+                   DOMAIN=example.com
+                   GATEWAY=192.168.42.1
+                   HWADDR=52:54:00:ab:cd:ef
+                   IPADDR=192.168.42.100
+                   IPV6ADDR=2001:db8::100/32
+                   IPV6INIT=yes
+                   IPV6_DEFAULTGW=2001:db8::1
+                   NETMASK=255.255.255.0
+                   NM_CONTROLLED=no
+                   ONBOOT=yes
+                   STARTMODE=auto
+                   TYPE=Ethernet
+                   USERCTL=no
+                   """),
+            }
+        }
+
+        found = self._render_and_read(network_config=yaml.load(entry['yaml']))
+        self._compare_files_to_expected(entry['expected_sysconfig'], found)
+        self._assert_headers(found)
+
 
 class TestOpenSuseSysConfigRendering(CiTestCase):
 
-- 
1.8.3.1