From 69cf34796142517d7dc30d3d35b83e9ff1ad9ac4 Mon Sep 17 00:00:00 2001 From: Lukas Kranz Date: Wed, 18 Oct 2023 09:33:00 +0200 Subject: [PATCH] Use WMI to rename adapter Currently renaming the adapter on windows 22 core does not cause an error but also has no effect INFO cloudbaseinit.plugins.common.networkconfig [-] Renaming network adapter "Ethernet" to "some-adapter-name" ... ERROR cloudbaseinit.init cloudbaseinit.exception.ItemNotFoundException: Network interface with name "some-adapter-name" not found Change-Id: I6b05b313742dd6ff2553dd67b374a8aa4fefd0a7 --- cloudbaseinit/osutils/windows.py | 13 ++++---- cloudbaseinit/tests/osutils/test_windows.py | 36 ++++++++------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index 5630d212..ae5b1884 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -858,13 +858,12 @@ class WindowsUtils(base.BaseOSUtils): 'value "%(mtu)s" failed' % {'name': name, 'mtu': mtu}) def rename_network_adapter(self, old_name, new_name): - base_dir = self._get_system_dir() - netsh_path = os.path.join(base_dir, 'netsh.exe') - - args = [netsh_path, "interface", "set", "interface", - 'name=%s' % old_name, 'newname=%s' % new_name] - (out, err, ret_val) = self.execute_process(args, shell=False) - if ret_val: + net_adapter = self._get_network_adapter(old_name) + net_adapter.NetConnectionID = new_name + try: + net_adapter.put() + self._get_network_adapter(new_name) + except Exception: raise exception.CloudbaseInitException( 'Renaming interface "%(old_name)s" to "%(new_name)s" ' 'failed' % {'old_name': old_name, 'new_name': new_name}) diff --git a/cloudbaseinit/tests/osutils/test_windows.py b/cloudbaseinit/tests/osutils/test_windows.py index a9eec533..6f5d266d 100644 --- a/cloudbaseinit/tests/osutils/test_windows.py +++ b/cloudbaseinit/tests/osutils/test_windows.py @@ -839,28 +839,23 @@ class TestWindowsUtils(testutils.CloudbaseInitTestBase): self._test_set_static_network_config(ipv6=True) @mock.patch('cloudbaseinit.osutils.windows.WindowsUtils' - '.execute_process') - @mock.patch('cloudbaseinit.osutils.windows.WindowsUtils' - '._get_system_dir') - def _test_rename_network_adapter(self, should_fail, mock_get_system_dir, - mock_execute_process): - base_dir = "fake path" + '._get_network_adapter') + def _test_rename_network_adapter(self, mock_get_network_adapter): old_name = "fake_old" new_name = "fake_new" - mock_get_system_dir.return_value = base_dir - ret_val = 1 if should_fail else 0 - mock_execute_process.return_value = (None, None, ret_val) - if should_fail: - with self.assertRaises(exception.CloudbaseInitException): - self._winutils.rename_network_adapter(old_name, new_name) - else: - self._winutils.rename_network_adapter(old_name, new_name) + conn = self._wmi_mock.WMI + adapter = mock.Mock() + adapter.Name = mock.sentinel.old_name + mock_get_network_adapter.return_value = adapter - mock_get_system_dir.assert_called_once_with() - args = [os.path.join(base_dir, "netsh.exe"), "interface", "set", - "interface", 'name=%s' % old_name, 'newname=%s' % new_name] - mock_execute_process.assert_called_once_with(args, shell=False) + self._winutils.rename_network_adapter(old_name, new_name) + adapter.put.assert_called() + self.assertEqual(new_name, adapter.NetConnectionID) + + mock_get_network_adapter.assert_called_once_with(old_name) + + conn.Win32_NetworkAdapter.called_with(old_name) def _test_get_config_key_name(self, section): response = self._winutils._get_config_key_name(section) @@ -877,10 +872,7 @@ class TestWindowsUtils(testutils.CloudbaseInitTestBase): self._test_get_config_key_name(None) def test_rename_network_adapter(self): - self._test_rename_network_adapter(False) - - def test_rename_network_adapter_fail(self): - self._test_rename_network_adapter(True) + self._test_rename_network_adapter() @mock.patch('cloudbaseinit.osutils.windows.WindowsUtils' '._get_config_key_name')