Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cloudbaseinit/osutils/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,12 @@ def _set_static_network_config_legacy(name, address, netmask, gateway,

return reboot_required

# A network adapter created just before, like a team NIC, gets its IP
# interface a moment later.
@staticmethod
@retry_decorator.retry_decorator(
max_retry_count=10, max_sleep_time=5,
exceptions=exception.ItemNotFoundException)
def _fix_network_adapter_dhcp(interface_name,
enable_dhcp,
address_family):
Expand Down
21 changes: 17 additions & 4 deletions cloudbaseinit/tests/osutils/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,12 @@ def test_fix_network_adapter_dhcp(self):
def test_fix_network_adapter_dhcp_no_network_adapter(self):
self._test_fix_network_adapter_dhcp(False)

def _test_fix_network_adapter_dhcp(self, no_net_interface_found):
def test_fix_network_adapter_dhcp_network_adapter_late(self):
self._test_fix_network_adapter_dhcp(False, found_after=2)

@mock.patch('time.sleep')
def _test_fix_network_adapter_dhcp(self, no_net_interface_found,
mock_sleep, found_after=0):
mock_interface_name = "eth12"
mock_enable_dhcp = True
mock_address_family = self.windows_utils.AF_INET
Expand All @@ -2031,22 +2036,30 @@ def _test_fix_network_adapter_dhcp(self, no_net_interface_found):
existing_net_interface = mock.Mock()
existing_net_interface.Dhcp = 0

if not no_net_interface_found:
if found_after:
conn.MSFT_NetIPInterface.side_effect = (
[[]] * found_after + [[existing_net_interface]])
elif not no_net_interface_found:
conn.MSFT_NetIPInterface.return_value = [existing_net_interface]

if no_net_interface_found:
if no_net_interface_found and not found_after:
with self.assertRaises(exception.ItemNotFoundException):
self._winutils._fix_network_adapter_dhcp(
mock_interface_name, mock_enable_dhcp,
mock_address_family)
self.assertEqual(11, conn.MSFT_NetIPInterface.call_count)
self.assertEqual(10, mock_sleep.call_count)
else:
self._winutils._fix_network_adapter_dhcp(
mock_interface_name, mock_enable_dhcp,
mock_address_family)

conn.MSFT_NetIPInterface.assert_called_once_with(
conn.MSFT_NetIPInterface.assert_called_with(
InterfaceAlias=mock_interface_name,
AddressFamily=mock_address_family)
self.assertEqual(found_after + 1,
conn.MSFT_NetIPInterface.call_count)
self.assertEqual(found_after, mock_sleep.call_count)
self.assertEqual(existing_net_interface.Dhcp, 1)
existing_net_interface.put.assert_called_once()

Expand Down