From dd21327846c92d31ff43a3395c4abfddc19ee0fd Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Wed, 25 Jan 2012 15:50:29 -0600 Subject: [PATCH] mac_addresses now come across in UNIX format, the format preferred by hypervisors. update: apparently re is no bueno Change-Id: Ie024e645b4c9a7c39e1e6cc0016a5eb11982312d --- melange/ipam/models.py | 9 +++++++++ melange/ipam/views.py | 2 +- melange/tests/unit/test_ipam_models.py | 15 ++++++++++++++- melange/tests/unit/test_ipam_service.py | 4 ++-- melange/tests/unit/test_ipam_views.py | 2 +- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/melange/ipam/models.py b/melange/ipam/models.py index 360e0961..6900cc15 100644 --- a/melange/ipam/models.py +++ b/melange/ipam/models.py @@ -765,6 +765,10 @@ class MacAddress(ModelBase): def eui_format(self): return str(netaddr.EUI(self.address)) + @property + def unix_format(self): + return self.eui_format.replace('-', ':') + def _before_save(self): self.address = int(netaddr.EUI(self.address)) @@ -886,6 +890,11 @@ class Interface(ModelBase): if self.mac_address: return self.mac_address.eui_format + @property + def mac_address_unix_format(self): + if self.mac_address: + return self.mac_address.unix_format + def _validate(self): self._validate_presence_of("tenant_id") self._validate_uniqueness_of_virtual_interface_id() diff --git a/melange/ipam/views.py b/melange/ipam/views.py index c8a6846f..a8143693 100644 --- a/melange/ipam/views.py +++ b/melange/ipam/views.py @@ -71,7 +71,7 @@ class InterfaceConfigurationView(object): def data(self): data = self.interface.data() - data['mac_address'] = self.interface.mac_address_eui_format + data['mac_address'] = self.interface.mac_address_unix_format ip_addresses = self.interface.ip_addresses data['ip_addresses'] = IpConfigurationView(*ip_addresses).data() return data diff --git a/melange/tests/unit/test_ipam_models.py b/melange/tests/unit/test_ipam_models.py index 6133c904..3661118e 100644 --- a/melange/tests/unit/test_ipam_models.py +++ b/melange/tests/unit/test_ipam_models.py @@ -2336,7 +2336,7 @@ class TestInterface(tests.BaseTest): self.assertEqual(interface.mac_address, mac) - def test_returns_eui_formatted_mac_address(self): + def test_eui_formatted_mac_address(self): interface = factory_models.InterfaceFactory() models.MacAddress.create(interface_id=interface.id, address="ab:bc:cd:12:23:34") @@ -2349,6 +2349,19 @@ class TestInterface(tests.BaseTest): self.assertIsNone(interface.mac_address_eui_format) + def test_unix_formatted_mac_address(self): + interface = factory_models.InterfaceFactory() + models.MacAddress.create(interface_id=interface.id, + address="ab:bc:cd:12:23:34") + + self.assertEqual(interface.mac_address_unix_format, + "AB:BC:CD:12:23:34") + + def test_mac_address_unix_format_is_none_when_no_mac_address(self): + interface = factory_models.InterfaceFactory() + + self.assertIsNone(interface.mac_address_unix_format) + def test_ip_addresses(self): interface = factory_models.InterfaceFactory() diff --git a/melange/tests/unit/test_ipam_service.py b/melange/tests/unit/test_ipam_service.py index f94014a3..2e830f24 100644 --- a/melange/tests/unit/test_ipam_service.py +++ b/melange/tests/unit/test_ipam_service.py @@ -2168,7 +2168,7 @@ class TestInterfacesController(ControllerTestBase): interface_id=created_interface.id) self.assertIsNotNone(allocated_mac) self.assertEqual(response.json['interface']['mac_address'], - allocated_mac.eui_format) + allocated_mac.unix_format) def test_create_interface_allocates_ips_from_network(self): block = factory_models.IpBlockFactory(network_id="net1", @@ -2271,7 +2271,7 @@ class TestInterfacesController(ControllerTestBase): self.assertEqual(response.status_int, 200) iface_data = response.json["interface"] self.assertEqual(iface_data['id'], iface.virtual_interface_id) - self.assertEqual(iface_data['mac_address'], mac.eui_format) + self.assertEqual(iface_data['mac_address'], mac.unix_format) self.assertEqual(len(iface_data['ip_addresses']), 2) self.assertEqual(iface_data['ip_addresses'], views.IpConfigurationView(*iface.ip_addresses).data()) diff --git a/melange/tests/unit/test_ipam_views.py b/melange/tests/unit/test_ipam_views.py index 4318d12a..f2676e42 100644 --- a/melange/tests/unit/test_ipam_views.py +++ b/melange/tests/unit/test_ipam_views.py @@ -104,7 +104,7 @@ class TestInterfaceConfigurationView(tests.BaseTest): data = views.InterfaceConfigurationView(interface).data() - self.assertEqual(data['mac_address'], mac.eui_format) + self.assertEqual(data['mac_address'], mac.unix_format) self.assertEqual(data['id'], interface.virtual_interface_id) def test_data_returns_ip_address_configuration_information(self):