From 2856f4c8a440eba1127ac09f2b411d436c62e777 Mon Sep 17 00:00:00 2001
From: Brett Holman <brett.holman@canonical.com>
Date: Wed, 29 May 2024 16:08:35 -0600
Subject: [PATCH] fix(netplan): Fix predictable interface rename issue (#5339)

Reference:https://github.com/canonical/cloud-init/commit/2856f4c8a440eba1127ac09f2b411d436c62e777
Conflict:NA

When predictable naming is disabled, the following command may exit with
a non-zero exit code.

    udevadm test-builtin net_setup_link

This code only ran to check for udev rename races, which cannot happen
when systemd renaming is disabled. Skip when disabled.

Fixes GH-3950
---
 cloudinit/net/netplan.py    | 3 +++
 tests/unittests/test_net.py | 5 ++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/cloudinit/net/netplan.py b/cloudinit/net/netplan.py
index 7d6740d..7e32167 100644
--- a/cloudinit/net/netplan.py
+++ b/cloudinit/net/netplan.py
@@ -263,6 +263,9 @@ class Renderer(renderer.Renderer):
         if not run:
             LOG.debug("netplan net_setup_link postcmd disabled")
             return
+        elif "net.ifnames=0" in util.get_cmdline():
+            LOG.debug("Predictable interface names disabled.")
+            return
         setup_lnk = ['udevadm', 'test-builtin', 'net_setup_link']
 
          # It's possible we can race a udev rename and attempt to run
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index 4a1d7c0..27b28ca 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -4531,10 +4531,13 @@ class TestNetplanPostcommands(CiTestCase):
         mock_netplan_generate.assert_called_with(run=True)
         mock_net_setup_link.assert_called_with(run=True)
 
+    @mock.patch("cloudinit.util.get_cmdline")
     @mock.patch('cloudinit.util.SeLinuxGuard')
     @mock.patch.object(netplan, "get_devicelist")
     @mock.patch('cloudinit.subp.subp')
-    def test_netplan_postcmds(self, mock_subp, mock_devlist, mock_sel):
+    def test_netplan_postcmds(
+        self, mock_subp, mock_devlist, mock_sel, m_get_cmdline
+    ):
         mock_sel.__enter__ = mock.Mock(return_value=False)
         mock_sel.__exit__ = mock.Mock()
         mock_devlist.side_effect = [['lo']]
-- 
2.33.0


