69219a
From 26a150ec6af91ae3ed5053069aa0c08d7064800f Mon Sep 17 00:00:00 2001
69219a
From: Eduardo Otubo <otubo@redhat.com>
69219a
Date: Mon, 4 May 2020 12:39:52 +0200
69219a
Subject: [PATCH 1/6] net: append type:dhcp[46] only if dhcp[46] is True in v2
69219a
 netconfig
69219a
69219a
RH-Author: Eduardo Otubo <otubo@redhat.com>
69219a
Message-id: <20200327152826.13343-2-otubo@redhat.com>
69219a
Patchwork-id: 94459
69219a
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
69219a
Bugzilla: 1811753
69219a
RH-Acked-by: Cathy Avery <cavery@redhat.com>
69219a
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
69219a
69219a
commit bd35300ba36bd63686715fa9661516a518781f6d
69219a
Author: Kurt Stieger <kurt@easygo.at>
69219a
Date:   Mon Mar 4 15:54:25 2019 +0000
69219a
69219a
    net: append type:dhcp[46] only if dhcp[46] is True in v2 netconfig
69219a
69219a
    When providing netplan configuration to cloud-init, the internal
69219a
    network state would enable DHCP if the 'dhcp' key was present in
69219a
    the source config.  In netplan, dhcp[46] is a boolean and the
69219a
    value of the boolean should control whether DHCP is enabled rather
69219a
    than the presence of the key.  This issue leaded to  inconsistant
69219a
    sysconfig/network-scripts on fedora. 'BOOTPROTO' was always 'dhcp',
69219a
    even if the address config was static.
69219a
69219a
    After this change a dhcp subnet is added only if the 'dhcp' setting
69219a
    in source cfg dict is True.
69219a
69219a
    LP: #1818032
69219a
69219a
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
69219a
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
69219a
---
69219a
 cloudinit/net/network_state.py |  4 +--
69219a
 tests/unittests/test_net.py    | 62 ++++++++++++++++++++++++++++++++++++++++++
69219a
 2 files changed, 64 insertions(+), 2 deletions(-)
69219a
69219a
diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
69219a
index f76e508..539b76d 100644
69219a
--- a/cloudinit/net/network_state.py
69219a
+++ b/cloudinit/net/network_state.py
69219a
@@ -706,9 +706,9 @@ class NetworkStateInterpreter(object):
69219a
         """Common ipconfig extraction from v2 to v1 subnets array."""
69219a
 
69219a
         subnets = []
69219a
-        if 'dhcp4' in cfg:
69219a
+        if cfg.get('dhcp4'):
69219a
             subnets.append({'type': 'dhcp4'})
69219a
-        if 'dhcp6' in cfg:
69219a
+        if cfg.get('dhcp6'):
69219a
             self.use_ipv6 = True
69219a
             subnets.append({'type': 'dhcp6'})
69219a
 
69219a
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
69219a
index 012c43b..4224301 100644
69219a
--- a/tests/unittests/test_net.py
69219a
+++ b/tests/unittests/test_net.py
69219a
@@ -103,6 +103,24 @@ STATIC_EXPECTED_1 = {
69219a
                  'address': '10.0.0.2'}],
69219a
 }
69219a
 
69219a
+NETPLAN_DHCP_FALSE = """
69219a
+version: 2
69219a
+ethernets:
69219a
+  ens3:
69219a
+    match:
69219a
+      macaddress: 52:54:00:ab:cd:ef
69219a
+    dhcp4: false
69219a
+    dhcp6: false
69219a
+    addresses:
69219a
+      - 192.168.42.100/24
69219a
+      - 2001:db8::100/32
69219a
+    gateway4: 192.168.42.1
69219a
+    gateway6: 2001:db8::1
69219a
+    nameservers:
69219a
+      search: [example.com]
69219a
+      addresses: [192.168.42.53, 1.1.1.1]
69219a
+"""
69219a
+
69219a
 # Examples (and expected outputs for various renderers).
69219a
 OS_SAMPLES = [
69219a
     {
69219a
@@ -2146,6 +2164,50 @@ USERCTL=no
69219a
         self._compare_files_to_expected(entry[self.expected_name], found)
69219a
         self._assert_headers(found)
69219a
 
69219a
+    def test_netplan_dhcp_false_disable_dhcp_in_state(self):
69219a
+        """netplan config with dhcp[46]: False should not add dhcp in state"""
69219a
+        net_config = yaml.load(NETPLAN_DHCP_FALSE)
69219a
+        ns = network_state.parse_net_config_data(net_config,
69219a
+                                                 skip_broken=False)
69219a
+
69219a
+        dhcp_found = [snet for iface in ns.iter_interfaces()
69219a
+                      for snet in iface['subnets'] if 'dhcp' in snet['type']]
69219a
+
69219a
+        self.assertEqual([], dhcp_found)
69219a
+
69219a
+    def test_netplan_dhcp_false_no_dhcp_in_sysconfig(self):
69219a
+        """netplan cfg with dhcp[46]: False should not have bootproto=dhcp"""
69219a
+
69219a
+        entry = {
69219a
+            'yaml': NETPLAN_DHCP_FALSE,
69219a
+            'expected_sysconfig': {
69219a
+                'ifcfg-ens3': textwrap.dedent("""\
69219a
+                   BOOTPROTO=none
69219a
+                   DEFROUTE=yes
69219a
+                   DEVICE=ens3
69219a
+                   DNS1=192.168.42.53
69219a
+                   DNS2=1.1.1.1
69219a
+                   DOMAIN=example.com
69219a
+                   GATEWAY=192.168.42.1
69219a
+                   HWADDR=52:54:00:ab:cd:ef
69219a
+                   IPADDR=192.168.42.100
69219a
+                   IPV6ADDR=2001:db8::100/32
69219a
+                   IPV6INIT=yes
69219a
+                   IPV6_DEFAULTGW=2001:db8::1
69219a
+                   NETMASK=255.255.255.0
69219a
+                   NM_CONTROLLED=no
69219a
+                   ONBOOT=yes
69219a
+                   STARTMODE=auto
69219a
+                   TYPE=Ethernet
69219a
+                   USERCTL=no
69219a
+                   """),
69219a
+            }
69219a
+        }
69219a
+
69219a
+        found = self._render_and_read(network_config=yaml.load(entry['yaml']))
69219a
+        self._compare_files_to_expected(entry['expected_sysconfig'], found)
69219a
+        self._assert_headers(found)
69219a
+
69219a
 
69219a
 class TestOpenSuseSysConfigRendering(CiTestCase):
69219a
 
69219a
-- 
69219a
1.8.3.1
69219a