From 4647f418afb9ced223c089f9d49cd686eccae9e2 Mon Sep 17 00:00:00 2001 From: Jens Rosenboom Date: Fri, 24 Jun 2016 12:20:51 +0200 Subject: [PATCH] Fix test_ipv6 and simplify to_global() The tests test_to_global_with_bad_prefix and test_to_global_with_bad_project are broken, they assert the TypeError because they call to_global with an IPv6 address as MAC parameter and do not check the functionality that they claim to check. test_to_global_with_bad_project gets removed, because there is no checking on the project_id, we simply hash whatever is there. We also make sure that the correct error message is generated and add a check to prevent regressing again. While at it, simplify the code for to_global() in nova/ipv6/rfc2462.py. Change-Id: I2a6f75beca1c37d135dcbb3a6eb0d739f2dd41e2 --- nova/ipv6/account_identifier.py | 9 ++++---- nova/ipv6/rfc2462.py | 14 ++++++------ nova/tests/unit/test_ipv6.py | 40 ++++++++++++++------------------- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/nova/ipv6/account_identifier.py b/nova/ipv6/account_identifier.py index b23818ee82d3..a8da65764feb 100644 --- a/nova/ipv6/account_identifier.py +++ b/nova/ipv6/account_identifier.py @@ -38,15 +38,14 @@ def to_global(prefix, mac, project_id): try: mac_suffix = netaddr.EUI(mac).value & 0xffffff mac_addr = netaddr.IPAddress(mac_suffix) + except netaddr.AddrFormatError: + raise TypeError(_('Bad mac for to_global_ipv6: %s') % mac) + + try: maskIP = netaddr.IPNetwork(prefix).ip return (project_hash ^ static_num ^ mac_addr | maskIP).format() except netaddr.AddrFormatError: - raise TypeError(_('Bad mac for to_global_ipv6: %s') % mac) - except TypeError: raise TypeError(_('Bad prefix for to_global_ipv6: %s') % prefix) - except NameError: - raise TypeError(_('Bad project_id for to_global_ipv6: %s') % - project_id) def to_mac(ipv6_address): diff --git a/nova/ipv6/rfc2462.py b/nova/ipv6/rfc2462.py index 92746e5a079a..9d62f24423a1 100644 --- a/nova/ipv6/rfc2462.py +++ b/nova/ipv6/rfc2462.py @@ -24,15 +24,15 @@ from nova.i18n import _ def to_global(prefix, mac, project_id): try: - mac64 = netaddr.EUI(mac).eui64().words - int_addr = int(''.join(['%02x' % i for i in mac64]), 16) - mac64_addr = netaddr.IPAddress(int_addr) - maskIP = netaddr.IPNetwork(prefix).ip - return (mac64_addr ^ netaddr.IPAddress('::0200:0:0:0') | - maskIP).format() + mac64 = netaddr.EUI(mac).modified_eui64().value + mac64_addr = netaddr.IPAddress(mac64) except netaddr.AddrFormatError: raise TypeError(_('Bad mac for to_global_ipv6: %s') % mac) - except TypeError: + + try: + maskIP = netaddr.IPNetwork(prefix).ip + return (mac64_addr | maskIP).format() + except netaddr.AddrFormatError: raise TypeError(_('Bad prefix for to_global_ipv6: %s') % prefix) diff --git a/nova/tests/unit/test_ipv6.py b/nova/tests/unit/test_ipv6.py index 4aa6c2a80334..7314d128e795 100644 --- a/nova/tests/unit/test_ipv6.py +++ b/nova/tests/unit/test_ipv6.py @@ -35,22 +35,19 @@ class IPv6RFC2462TestCase(test.NoDBTestCase): def test_to_global_with_bad_mac(self): bad_mac = '02:16:3e:33:44:5Z' - self.assertRaises(TypeError, ipv6.to_global, + expected_msg = 'Bad mac for to_global_ipv6: %s' % bad_mac + err = self.assertRaises(TypeError, ipv6.to_global, '2001:db8::', bad_mac, 'test') + self.assertEqual(expected_msg, str(err)) def test_to_global_with_bad_prefix(self): - bad_prefix = '82' - self.assertRaises(TypeError, ipv6.to_global, + bad_prefix = '2001::1::2' + expected_msg = 'Bad prefix for to_global_ipv6: %s' % bad_prefix + err = self.assertRaises(TypeError, ipv6.to_global, bad_prefix, - '2001:db8::216:3eff:fe33:4455', + '02:16:3e:33:44:55', 'test') - - def test_to_global_with_bad_project(self): - bad_project = 'non-existent-project-name' - self.assertRaises(TypeError, ipv6.to_global, - '2001:db8::', - '2001:db8::a94a:8fe5:ff33:4455', - bad_project) + self.assertEqual(expected_msg, str(err)) class IPv6AccountIdentiferTestCase(test.NoDBTestCase): @@ -69,20 +66,17 @@ class IPv6AccountIdentiferTestCase(test.NoDBTestCase): self.assertEqual(mac, '02:16:3e:33:44:55') def test_to_global_with_bad_mac(self): - bad_mac = '02:16:3e:33:44:5X' - self.assertRaises(TypeError, ipv6.to_global, + bad_mac = '02:16:3e:33:44:5Z' + expected_msg = 'Bad mac for to_global_ipv6: %s' % bad_mac + err = self.assertRaises(TypeError, ipv6.to_global, '2001:db8::', bad_mac, 'test') + self.assertEqual(expected_msg, str(err)) def test_to_global_with_bad_prefix(self): - bad_prefix = '78' - self.assertRaises(TypeError, ipv6.to_global, + bad_prefix = '2001::1::2' + expected_msg = 'Bad prefix for to_global_ipv6: %s' % bad_prefix + err = self.assertRaises(TypeError, ipv6.to_global, bad_prefix, - '2001:db8::a94a:8fe5:ff33:4455', + '02:16:3e:33:44:55', 'test') - - def test_to_global_with_bad_project(self): - bad_project = 'non-existent-project-name' - self.assertRaises(TypeError, ipv6.to_global, - '2001:db8::', - '2001:db8::a94a:8fe5:ff33:4455', - bad_project) + self.assertEqual(expected_msg, str(err))