diff options
author | Dmitry Zykov <44090270+zykovd@users.noreply.github.com> | 2023-05-12 21:48:10 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-12 13:48:10 -0500 |
commit | 7c52b8a5fbcfe1e7442bbd011956b52e303745c5 (patch) | |
tree | 633b6e2e8dfa7f81ac6e3b5e629b7b14057899d1 /tests/unittests/test_netinfo.py | |
parent | 73f34575da4f99a376998516a13c3a79cc640ae3 (diff) | |
download | cloud-init-git-7c52b8a5fbcfe1e7442bbd011956b52e303745c5.tar.gz |
Fix KeyError in iproute pformat (#3287)
This fixes KeyError on specific network configuration when running
cloud-init on "network" stage. The same problem was mentioned in
#746 and #1041.
Diffstat (limited to 'tests/unittests/test_netinfo.py')
-rw-r--r-- | tests/unittests/test_netinfo.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/unittests/test_netinfo.py b/tests/unittests/test_netinfo.py index 7612a28b..49c92f7e 100644 --- a/tests/unittests/test_netinfo.py +++ b/tests/unittests/test_netinfo.py @@ -26,8 +26,17 @@ SAMPLE_ROUTE_OUT_V4 = readResource("netinfo/sample-route-output-v4") SAMPLE_ROUTE_OUT_V6 = readResource("netinfo/sample-route-output-v6") SAMPLE_IPROUTE_OUT_V4 = readResource("netinfo/sample-iproute-output-v4") SAMPLE_IPROUTE_OUT_V6 = readResource("netinfo/sample-iproute-output-v6") +SAMPLE_IPROUTE_OUT_V6_MISSING_GATEWAY = readResource( + "netinfo/sample-iproute-output-v6-missing-gateway" +) +SAMPLE_IPROUTE_OUT_V4_MISSING_GATEWAY = readResource( + "netinfo/sample-iproute-output-v4-missing-gateway" +) NETDEV_FORMATTED_OUT = readResource("netinfo/netdev-formatted-output") ROUTE_FORMATTED_OUT = readResource("netinfo/route-formatted-output") +ROUTE_FORMATTED_OUT_MISSING_GATEWAY = readResource( + "netinfo/route-formatted-output-missing-gateway" +) FREEBSD_NETDEV_OUT = readResource("netinfo/freebsd-netdev-formatted-output") @@ -225,6 +234,24 @@ class TestNetInfo: @mock.patch("cloudinit.netinfo.subp.which") @mock.patch("cloudinit.netinfo.subp.subp") + def test_route_iproute_pformat_missing_gateway(self, m_subp, m_which): + """route_pformat properly rendering info with missing gateway.""" + + def subp_iproute_selector(*args, **kwargs): + if ["ip", "-o", "route", "list"] == args[0]: + return (SAMPLE_IPROUTE_OUT_V4_MISSING_GATEWAY, "") + v6cmd = ["ip", "--oneline", "-6", "route", "list", "table", "all"] + if v6cmd == args[0]: + return (SAMPLE_IPROUTE_OUT_V6_MISSING_GATEWAY, "") + raise RuntimeError("Unexpected subp call %s" % args[0]) + + m_subp.side_effect = subp_iproute_selector + m_which.side_effect = lambda x: x if x == "ip" else None + content = route_pformat() + assert ROUTE_FORMATTED_OUT_MISSING_GATEWAY == content + + @mock.patch("cloudinit.netinfo.subp.which") + @mock.patch("cloudinit.netinfo.subp.subp") def test_route_warn_on_missing_commands(self, m_subp, m_which, caplog): """route_pformat warns when missing both ip and 'netstat'.""" m_which.return_value = None # Niether ip nor netstat found |