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)

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

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

diff --git a/cloudinit/net/netops/iproute2.py b/cloudinit/net/netops/iproute2.py
index 08d79b1..25ccbc1 100644
--- a/cloudinit/net/netops/iproute2.py
+++ b/cloudinit/net/netops/iproute2.py
@@ -30,7 +30,7 @@ class Iproute2(netops.NetOps):
         source_address: Optional[str] = None,
     ):
         subp.subp(
-            ["ip", "-4", "route", "add", route]
+            ["ip", "-4", "route", "replace", route]
             + (["via", gateway] if gateway and gateway != "0.0.0.0" else [])
             + [
                 "dev",
diff --git a/tests/unittests/net/test_init.py b/tests/unittests/net/test_init.py
index 51e54d0..16fe8e9 100644
--- a/tests/unittests/net/test_init.py
+++ b/tests/unittests/net/test_init.py
@@ -1084,7 +1084,7 @@ class TestEphemeralIPV4Network(CiTestCase):
                     "ip",
                     "-4",
                     "route",
-                    "add",
+                    "replace",
                     "192.168.2.1",
                     "dev",
                     "eth0",
@@ -1097,7 +1097,7 @@ class TestEphemeralIPV4Network(CiTestCase):
                     "ip",
                     "-4",
                     "route",
-                    "add",
+                    "replace",
                     "default",
                     "via",
                     "192.168.2.1",
-- 
2.27.0

