From adeaab79c83010d379779e2e33288cffc79e0ee1 Mon Sep 17 00:00:00 2001 From: Adam Gandelman Date: Thu, 7 Jan 2016 11:17:55 -0800 Subject: [PATCH] Ensure interface cache up to date The appliance server parses and caches the systems network interfaces the first time it updates them, and never refreshes the cache. When a new router interface is added, the appliance errors because its interface cache has no idea about the NIC that corresponds to the router interface. This ensures we recreate this mapping anytime we need it. Change-Id: Iaff5a84a674d9089447bbdc8dc471f3d75a79af6 Closes-bug: #1531651 --- astara_router/drivers/ip.py | 5 +-- ...face_cache_refreshed-57ce5b0637282b60.yaml | 3 ++ test/unit/drivers/test_ip.py | 42 +++++++++---------- 3 files changed, 24 insertions(+), 26 deletions(-) create mode 100644 releasenotes/notes/ensure_interface_cache_refreshed-57ce5b0637282b60.yaml diff --git a/astara_router/drivers/ip.py b/astara_router/drivers/ip.py index fd452b1..82968a9 100644 --- a/astara_router/drivers/ip.py +++ b/astara_router/drivers/ip.py @@ -51,10 +51,9 @@ class IPManager(base.Manager): def ensure_mapping(self): """ Creates a mapping of generic interface names (e.g., ge0, ge1) to - physical interface names (eth1, eth2) if one does not already exist. + physical interface names (eth1, eth2). """ - if not self.host_mapping: - self.get_interfaces() + self.get_interfaces() def get_interfaces(self): """ diff --git a/releasenotes/notes/ensure_interface_cache_refreshed-57ce5b0637282b60.yaml b/releasenotes/notes/ensure_interface_cache_refreshed-57ce5b0637282b60.yaml new file mode 100644 index 0000000..db91d72 --- /dev/null +++ b/releasenotes/notes/ensure_interface_cache_refreshed-57ce5b0637282b60.yaml @@ -0,0 +1,3 @@ +--- +fixes: + - Bug `1531651 `_ \- Fixes a stale interface cache from preventing additional router interfaces from being attached diff --git a/test/unit/drivers/test_ip.py b/test/unit/drivers/test_ip.py index 9ad47f9..37994d1 100644 --- a/test/unit/drivers/test_ip.py +++ b/test/unit/drivers/test_ip.py @@ -93,7 +93,7 @@ class IPTestCase(TestCase): self.mock_execute.assert_has_calls( [mock.call(['/sbin/ip', 'addr', 'show'])]) - def test_ensure_mapping_uninitialized(self): + def test_ensure_mapping(self): attr = 'get_interfaces' with mock.patch.object(ip.IPManager, attr) as get_ifaces: mgr = ip.IPManager() @@ -101,29 +101,23 @@ class IPTestCase(TestCase): get_ifaces.assert_called_once_with() - def test_ensure_mapping_initialized(self): - attr = 'get_interfaces' - with mock.patch.object(ip.IPManager, attr) as get_ifaces: - mgr = ip.IPManager() - mgr.host_mapping['em0'] = 'ge0' - mgr.ensure_mapping() - - self.assertEqual(get_ifaces.call_count, 0) - - def test_is_valid(self): + @mock.patch.object(ip.IPManager, 'ensure_mapping') + def test_is_valid(self, mock_ensure): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} self.assertTrue(mgr.is_valid('ge0')) - def test_generic_to_host(self): + @mock.patch.object(ip.IPManager, 'ensure_mapping') + def test_generic_to_host(self, mock_ensure): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} self.assertEqual(mgr.generic_to_host('ge0'), 'em0') self.assertIsNone(mgr.generic_to_host('ge1')) - def test_host_to_generic(self): + @mock.patch.object(ip.IPManager, 'ensure_mapping') + def test_host_to_generic(self, ensure_mapping): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} @@ -144,12 +138,12 @@ class IPTestCase(TestCase): iface = mock.Mock() iface.ifname = 'ge0' - mgr = ip.IPManager() - mgr.host_mapping = {'em0': 'ge0'} - mgr.generic_mapping = {'ge0': 'em0'} - - mgr.up(iface) - + attr = 'ensure_mapping' + with mock.patch.object(ip.IPManager, attr) as ensure: + mgr = ip.IPManager() + mgr.host_mapping = {'em0': 'ge0'} + mgr.generic_mapping = {'ge0': 'em0'} + mgr.up(iface) self.mock_execute.assert_has_calls( [mock.call(['/sbin/ip', 'link', 'set', 'em0', 'up'], 'sudo')]) @@ -157,11 +151,13 @@ class IPTestCase(TestCase): iface = mock.Mock() iface.ifname = 'ge0' - mgr = ip.IPManager() - mgr.host_mapping = {'em0': 'ge0'} - mgr.generic_mapping = {'ge0': 'em0'} + attr = 'ensure_mapping' + with mock.patch.object(ip.IPManager, attr) as ensure: + mgr = ip.IPManager() + mgr.host_mapping = {'em0': 'ge0'} + mgr.generic_mapping = {'ge0': 'em0'} - mgr.down(iface) + mgr.down(iface) self.mock_execute.assert_has_calls( [mock.call(['/sbin/ip', 'link', 'set', 'em0', 'down'], 'sudo')])