diff options
author | sridhargaddam <sridhar.gaddam@enovance.com> | 2014-11-28 12:10:32 +0000 |
---|---|---|
committer | sridhargaddam <sridhar.gaddam@enovance.com> | 2015-02-18 19:55:28 +0000 |
commit | 9d9818bf4849c51236b2ffee9746ac09af3fd44f (patch) | |
tree | 8e2a8a844fae12fa35aaeadfea6627daf8cc6032 /oslo_utils/tests/test_netutils.py | |
parent | 8a5a4d87da03919bf019a3c3ffeeddc3959b77d1 (diff) | |
download | oslo-utils-9d9818bf4849c51236b2ffee9746ac09af3fd44f.tar.gz |
Utility API to generate EUI-64 IPv6 address
For a network using IPv6 SLAAC, Neutron would calculate the port
addresses based on EUI-64 specification which is generated via
IPv6 prefix and the interface MAC address. EUI-64 is a standard
algorithm and is explained at the following url
http://packetlife.net/blog/2008/aug/4/eui-64-ipv6
This utility api is present in both Neutron [1] and Tempest [2] to
validate certain IPv6 scenarios. Hence, having it in oslo.utils
would be ideal.
[1] - https://github.com/openstack/neutron/blob/master/neutron/common/ipv6_utils.py#L32
[2] - https://github.com/openstack/tempest/blob/master/tempest/common/utils/data_utils.py#L85
Change-Id: I3d1c6b22ecddf7faad83cc12c674a9c5c96b1759
Diffstat (limited to 'oslo_utils/tests/test_netutils.py')
-rw-r--r-- | oslo_utils/tests/test_netutils.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/oslo_utils/tests/test_netutils.py b/oslo_utils/tests/test_netutils.py index 7e1b545..1bc4990 100644 --- a/oslo_utils/tests/test_netutils.py +++ b/oslo_utils/tests/test_netutils.py @@ -231,3 +231,74 @@ class NetworkUtilsTest(test_base.BaseTestCase): addr = netutils._get_my_ipv4_address() self.assertEqual('127.0.0.1', addr) self.assertFalse(ifaddr.called) + + +class IPv6byEUI64TestCase(test_base.BaseTestCase): + """Unit tests to generate IPv6 by EUI-64 operations.""" + + def test_generate_IPv6_by_EUI64(self): + addr = netutils.get_ipv6_addr_by_EUI64('2001:db8::', + '00:16:3e:33:44:55') + self.assertEqual('2001:db8::216:3eff:fe33:4455', addr.format()) + + def test_generate_IPv6_with_IPv4_prefix(self): + ipv4_prefix = '10.0.8' + mac = '00:16:3e:33:44:55' + self.assertRaises(ValueError, lambda: + netutils.get_ipv6_addr_by_EUI64(ipv4_prefix, mac)) + + def test_generate_IPv6_with_bad_mac(self): + bad_mac = '00:16:3e:33:44:5Z' + prefix = '2001:db8::' + self.assertRaises(ValueError, lambda: + netutils.get_ipv6_addr_by_EUI64(prefix, bad_mac)) + + def test_generate_IPv6_with_bad_prefix(self): + mac = '00:16:3e:33:44:55' + bad_prefix = 'bb' + self.assertRaises(ValueError, lambda: + netutils.get_ipv6_addr_by_EUI64(bad_prefix, mac)) + + def test_generate_IPv6_with_error_prefix_type(self): + mac = '00:16:3e:33:44:55' + prefix = 123 + self.assertRaises(TypeError, lambda: + netutils.get_ipv6_addr_by_EUI64(prefix, mac)) + + +class TestIsIPv6Enabled(test_base.BaseTestCase): + + def setUp(self): + super(TestIsIPv6Enabled, self).setUp() + + def reset_detection_flag(): + netutils._IS_IPV6_ENABLED = None + reset_detection_flag() + self.addCleanup(reset_detection_flag) + self.mock_exists = mock.patch("os.path.exists", + return_value=True).start() + mock_open = mock.patch("six.moves.builtins.open").start() + self.mock_read = mock_open.return_value.__enter__.return_value.read + + def test_enabled(self): + self.mock_read.return_value = "0" + enabled = netutils.is_ipv6_enabled() + self.assertTrue(enabled) + + def test_disabled(self): + self.mock_read.return_value = "1" + enabled = netutils.is_ipv6_enabled() + self.assertFalse(enabled) + + def test_disabled_non_exists(self): + self.mock_exists.return_value = False + enabled = netutils.is_ipv6_enabled() + self.assertFalse(enabled) + self.assertFalse(self.mock_read.called) + + def test_memoize(self): + self.mock_read.return_value = "0" + netutils.is_ipv6_enabled() + enabled = netutils.is_ipv6_enabled() + self.assertTrue(enabled) + self.mock_read.assert_called_once_with() |