From e432a31d6ea4263027c327559bb08adf3a91ad6d Mon Sep 17 00:00:00 2001
From: Brett Holman <brett.holman@canonical.com>
Date: Wed, 29 May 2024 16:03:46 -0600
Subject: [PATCH] fix(net): Make duplicate route add succeed. (#5343)

Reference:https://github.com/canonical/cloud-init/commit/e432a31d6ea4263027c327559bb08adf3a91ad6d
Conflict:(1)change cloudinit/net/__init__.py not cloudinit/net/netops/iproute2.py.
(2)change cloudinit/net/tests/test_init.py not tests/unittests/net/test_init.py.
(3)change 'ip -4 route add' to 'ip -4 route replace', other differences are significant.

This behaves the same but doesn't fail when adding an existing route.

Fixes GH-3441
Fixes GH-3595
---
 cloudinit/net/__init__.py        | 4 ++--
 cloudinit/net/tests/test_init.py | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py
index d3ac4c8..f05642d 100644
--- a/cloudinit/net/__init__.py
+++ b/cloudinit/net/__init__.py
@@ -1256,14 +1256,14 @@ class EphemeralIPv4Network(object):
                 self.interface, out.strip())
             return
         subp.subp(
-            ['ip', '-4', 'route', 'add', self.router, 'dev', self.interface,
+            ['ip', '-4', 'route', 'replace', self.router, 'dev', self.interface,
              'src', self.ip], capture=True)
         self.cleanup_cmds.insert(
             0,
             ['ip', '-4', 'route', 'del', self.router, 'dev', self.interface,
              'src', self.ip])
         subp.subp(
-            ['ip', '-4', 'route', 'add', 'default', 'via', self.router,
+            ['ip', '-4', 'route', 'replace', 'default', 'via', self.router,
              'dev', self.interface], capture=True)
         self.cleanup_cmds.insert(
             0, ['ip', '-4', 'route', 'del', 'default', 'dev', self.interface])
diff --git a/cloudinit/net/tests/test_init.py b/cloudinit/net/tests/test_init.py
index a47b7c8..6eda482 100644
--- a/cloudinit/net/tests/test_init.py
+++ b/cloudinit/net/tests/test_init.py
@@ -764,10 +764,10 @@ class TestEphemeralIPV4Network(CiTestCase):
                 capture=True),
             mock.call(
                 ['ip', 'route', 'show', '0.0.0.0/0'], capture=True),
-            mock.call(['ip', '-4', 'route', 'add', '192.168.2.1',
+            mock.call(['ip', '-4', 'route', 'replace', '192.168.2.1',
                        'dev', 'eth0', 'src', '192.168.2.2'], capture=True),
             mock.call(
-                ['ip', '-4', 'route', 'add', 'default', 'via',
+                ['ip', '-4', 'route', 'replace', 'default', 'via',
                  '192.168.2.1', 'dev', 'eth0'], capture=True)]
         expected_teardown_calls = [
             mock.call(['ip', '-4', 'route', 'del', 'default', 'dev', 'eth0'],
-- 
2.33.0


