diff options
Diffstat (limited to 'tests/unittests/net')
-rw-r--r-- | tests/unittests/net/test_init.py | 4 | ||||
-rw-r--r-- | tests/unittests/net/test_network_state.py | 58 |
2 files changed, 60 insertions, 2 deletions
diff --git a/tests/unittests/net/test_init.py b/tests/unittests/net/test_init.py index b245da94..18b3fe59 100644 --- a/tests/unittests/net/test_init.py +++ b/tests/unittests/net/test_init.py @@ -629,7 +629,9 @@ class TestEphemeralIPV4Network(CiTestCase): with net.EphemeralIPv4Network(**params): pass error = context_manager.exception - self.assertIn("Cannot setup network: netmask", str(error)) + self.assertIn( + "Cannot setup network, invalid prefix or netmask: ", str(error) + ) self.assertEqual(0, m_subp.call_count) def test_ephemeral_ipv4_network_performs_teardown(self, m_subp): diff --git a/tests/unittests/net/test_network_state.py b/tests/unittests/net/test_network_state.py index 88da9f94..471d969a 100644 --- a/tests/unittests/net/test_network_state.py +++ b/tests/unittests/net/test_network_state.py @@ -1,5 +1,5 @@ # This file is part of cloud-init. See LICENSE file for license information. - +import ipaddress from unittest import mock import pytest @@ -163,4 +163,60 @@ class TestNetworkStateParseNameservers: ] == sorted(config.dns_searchdomains) +class TestNetworkStateHelperFunctions(CiTestCase): + def test_mask_to_net_prefix_ipv4(self): + netmask_value = "255.255.255.0" + expected = 24 + prefix_value = network_state.ipv4_mask_to_net_prefix(netmask_value) + assert prefix_value == expected + + def test_mask_to_net_prefix_all_bits_ipv4(self): + netmask_value = "255.255.255.255" + expected = 32 + prefix_value = network_state.ipv4_mask_to_net_prefix(netmask_value) + assert prefix_value == expected + + def test_mask_to_net_prefix_to_many_bits_ipv4(self): + netmask_value = "33" + self.assertRaises( + ValueError, network_state.ipv4_mask_to_net_prefix, netmask_value + ) + + def test_mask_to_net_prefix_all_bits_ipv6(self): + netmask_value = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + expected = 128 + prefix_value = network_state.ipv6_mask_to_net_prefix(netmask_value) + assert prefix_value == expected + + def test_mask_to_net_prefix_ipv6(self): + netmask_value = "ffff:ffff:ffff:ffff::" + expected = 64 + prefix_value = network_state.ipv6_mask_to_net_prefix(netmask_value) + assert prefix_value == expected + + def test_mask_to_net_prefix_raises_value_error(self): + netmask_value = "ff:ff:ff:ff::" + self.assertRaises( + ValueError, network_state.ipv6_mask_to_net_prefix, netmask_value + ) + + def test_mask_to_net_prefix_to_many_bits_ipv6(self): + netmask_value = "129" + self.assertRaises( + ValueError, network_state.ipv6_mask_to_net_prefix, netmask_value + ) + + def test_mask_to_net_prefix_ipv4_object(self): + netmask_value = ipaddress.IPv4Address("255.255.255.255") + expected = 32 + prefix_value = network_state.ipv4_mask_to_net_prefix(netmask_value) + assert prefix_value == expected + + def test_mask_to_net_prefix_ipv6_object(self): + netmask_value = ipaddress.IPv6Address("ffff:ffff:ffff::") + expected = 48 + prefix_value = network_state.ipv6_mask_to_net_prefix(netmask_value) + assert prefix_value == expected + + # vi: ts=4 expandtab |