From e50564fbe27439c8ccfaf932d4a9b9deebba73aa Mon Sep 17 00:00:00 2001 From: KTokarze Date: Wed, 5 Nov 2025 02:14:32 -0800 Subject: [PATCH] fix: Try to connect to VM management IP without retrieving it from host Catch HyperVException when VM management IP address is not retrieved by host OS and try to connect to VM anyway. Fixes #8 Signed-off-by: Kacper Tokarzewski --- mfd_hyperv/hypervisor.py | 5 ++- tests/unit/test_mfd_hyperv/test_hypervisor.py | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mfd_hyperv/hypervisor.py b/mfd_hyperv/hypervisor.py index 98f0156..2b688d8 100644 --- a/mfd_hyperv/hypervisor.py +++ b/mfd_hyperv/hypervisor.py @@ -114,7 +114,10 @@ def create_vm( self._connection.execute_powershell(command=command, custom_exception=HyperVExecutionException) self.start_vm(vm_params.name) - mng_ip = self._wait_vm_mng_ips(vm_params.name) + try: + mng_ip = self._wait_vm_mng_ips(vm_params.name, timeout=180) + except HyperVException as e: + logger.error(f"Failed to get VM {vm_params.name} management IP: {e}") if dynamic_mng_ip: vm_params.mng_ip = mng_ip diff --git a/tests/unit/test_mfd_hyperv/test_hypervisor.py b/tests/unit/test_mfd_hyperv/test_hypervisor.py index 9a7cfbf..6a54537 100644 --- a/tests/unit/test_mfd_hyperv/test_hypervisor.py +++ b/tests/unit/test_mfd_hyperv/test_hypervisor.py @@ -119,6 +119,39 @@ def test_create_vm(self, mocker, hypervisor): hypervisor.create_vm(vm_params, dynamic_mng_ip=True) assert len(hypervisor.vms) == 1 + def test_create_vm_ip_not_found(self, mocker, hypervisor): + vm_params = VMParams( + name="vm_name", + cpu_count=4, + hw_threads_per_core=0, + memory=4096, + generation=2, + vm_dir_path="path", + diff_disk_path="diff_disk_path", + mng_interface_name="mng", + mng_mac_address=MACAddress("00:00:00:00:00:00"), + mng_ip="1.1.1.1", + vswitch_name="vswitch", + ) + output = """ + VMName : Base_W19_VM001 + IPAddresses : {192.168.1.1, fe80::6994:9bd4:d0aa:ff4d} + MacAddress : 525A005BDA10 + """ + + hypervisor._connection.execute_powershell.return_value = ConnectionCompletedProcess( + return_code=0, args="command", stdout=output, stderr="stderr" + ) + + mocker.patch("mfd_hyperv.hypervisor.HypervHypervisor.start_vm", return_value=None) + mocker.patch("mfd_hyperv.hypervisor.RPyCConnection", autospec=True) + mocker.patch("mfd_hyperv.hypervisor.VM", autospec=True) + mocker.patch.object(hypervisor, "_wait_vm_mng_ips", side_effect=HyperVException("No IP found")) + + hypervisor.create_vm(vm_params, dynamic_mng_ip=False) + assert len(hypervisor.vms) == 1 + assert vm_params.mng_ip == "1.1.1.1" + def test_remove_vm_all(self, hypervisor_with_2_vms): hypervisor_with_2_vms._connection.execute_powershell.return_value = ConnectionCompletedProcess( return_code=0, args="command", stdout="stdout", stderr="stderr"