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)

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 0b8419a..aea8a67 100644
--- a/cloudinit/net/netplan.py
+++ b/cloudinit/net/netplan.py
@@ -329,6 +329,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 052b067..73a4c91 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -6782,10 +6782,13 @@ class TestNetplanPostcommands(CiTestCase):
         mock_netplan_generate.assert_called_with(run=True, same_content=False)
         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.27.0

