Skip to content

Commit

Permalink
Retry for 5 times when renaming adapter to get more reliable
Browse files Browse the repository at this point in the history
Change-Id: Ia148c3ee5bcf55a7ae9cf8c1834d3991bfe6609e
  • Loading branch information
laozc committed Aug 12, 2024
1 parent 7cb24ea commit 8f43e77
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
16 changes: 14 additions & 2 deletions cloudbaseinit/osutils/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,24 @@ def set_network_adapter_mtu(self, name, mtu):
'Setting MTU for interface "%(name)s" with '
'value "%(mtu)s" failed' % {'name': name, 'mtu': mtu})

@retry_decorator.retry_decorator(
max_retry_count=5,
max_sleep_time=10,
exceptions=exception.CloudbaseInitException)
def rename_network_adapter(self, old_name, new_name):
net_adapter = self._get_network_msft_adapter(old_name)
try:
LOG.info('Renaming interface "%(old_name)s" to "%(new_name)s"...' %
{'old_name': old_name,
'new_name': new_name})
net_adapter = self._get_network_msft_adapter(old_name)
net_adapter.rename(new_name)
self._get_network_msft_adapter(new_name)
except Exception:
except Exception as ex:
LOG.warning('Renaming interface "%(old_name)s" to "%(new_name)s" '
'failed: %(err)s' %
{'old_name': old_name,
'new_name': new_name,
'err': ex})
raise exception.CloudbaseInitException(
'Renaming interface "%(old_name)s" to "%(new_name)s" '
'failed' % {'old_name': old_name, 'new_name': new_name})
Expand Down
38 changes: 26 additions & 12 deletions cloudbaseinit/tests/osutils/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def _test_add_user_to_local_group(self,

is_in_alias = ret_value != self._winutils.ERROR_MEMBER_IN_ALIAS

if ret_value is not 0 and is_in_alias:
if ret_value and is_in_alias:
self.assertRaises(
exception.CloudbaseInitException,
self._winutils.add_user_to_local_group,
Expand Down Expand Up @@ -833,21 +833,30 @@ def test_set_static_network_config_ipv6(self):

@mock.patch('cloudbaseinit.osutils.windows.WindowsUtils'
'._get_network_msft_adapter')
def _test_rename_network_adapter(self, rename_exception,
@mock.patch('time.sleep')
def _test_rename_network_adapter(self,
rename_adapter_retries,
rename_adapter_failed,
mock_sleep,
mock_get_network_adapter):
old_name = "fake_old"
new_name = "fake_new"

adapter = mock.Mock()
adapter.Name = mock.sentinel.old_name
mock_get_network_adapter.return_value = adapter
get_network_adapter_called = 1
if rename_exception:
adapter.rename.side_effect = Exception("fake exception")
get_network_adapter_called = 1 + rename_adapter_retries
rename_adapter_side_effect = []
for i in range(rename_adapter_retries):
rename_adapter_side_effect.append(Exception("fake exception"))
if rename_adapter_failed:
adapter.rename.side_effect = rename_adapter_side_effect
with self.assertRaises(exception.CloudbaseInitException):
self._winutils.rename_network_adapter(old_name, new_name)
else:
get_network_adapter_called = 2
get_network_adapter_called += 1
rename_adapter_side_effect.append(None)
adapter.rename.side_effect = rename_adapter_side_effect
self._winutils.rename_network_adapter(old_name, new_name)

adapter.rename.assert_called()
Expand All @@ -856,9 +865,11 @@ def _test_rename_network_adapter(self, rename_exception,
get_network_adapter_called)
self.assertEqual(mock_get_network_adapter.call_args_list[0].args,
(old_name,))
if not exception:
self.assertEqual(mock_get_network_adapter.call_args_list[1].args,
(new_name,))
if not rename_adapter_failed:
self.assertEqual(
mock_get_network_adapter.
call_args_list[get_network_adapter_called - 1].args,
(new_name,))

def _test_get_config_key_name(self, section):
response = self._winutils._get_config_key_name(section)
Expand All @@ -875,10 +886,13 @@ def test_get_config_key_name_no_section(self):
self._test_get_config_key_name(None)

def test_rename_network_adapter(self):
self._test_rename_network_adapter(False)
self._test_rename_network_adapter(0, False)

def test_rename_network_adapter_with_retry(self):
self._test_rename_network_adapter(3, False)

def test_rename_network_adapter_fail(self):
self._test_rename_network_adapter(True)
def test_rename_network_adapter_failed(self):
self._test_rename_network_adapter(5, True)

@mock.patch('cloudbaseinit.osutils.windows.WindowsUtils'
'._get_config_key_name')
Expand Down

0 comments on commit 8f43e77

Please sign in to comment.