This is an automated email from the ASF dual-hosted git repository.
DaanHoogland pushed a commit to branch 4.22
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.22 by this push:
new 348ceaff33d kvm: get ip address of Windows vms via virsh domifaddr
(#12651)
348ceaff33d is described below
commit 348ceaff33dcf50417ff1c5b78f77d48db95c7e2
Author: Wei Zhou <[email protected]>
AuthorDate: Wed Jul 1 20:29:54 2026 +0200
kvm: get ip address of Windows vms via virsh domifaddr (#12651)
---
.../LibvirtGetVmIpAddressCommandWrapper.java | 6 ++--
.../LibvirtGetVmIpAddressCommandWrapperTest.java | 35 +++++++++++++++++++++-
2 files changed, 37 insertions(+), 4 deletions(-)
diff --git
a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java
b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java
index 93545f3adcf..c7fde141410 100644
---
a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java
+++
b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java
@@ -119,7 +119,7 @@ public final class LibvirtGetVmIpAddressCommandWrapper
extends CommandWrapper<Ge
continue;
}
String device = parts[0];
- String mac = parts[1];
+ String mac = parts[parts.length - 3];
if (found) {
if (!device.equals("-") || !mac.equals("-")) {
break;
@@ -128,8 +128,8 @@ public final class LibvirtGetVmIpAddressCommandWrapper
extends CommandWrapper<Ge
continue;
}
found = true;
- String ipFamily = parts[2];
- String ipPart = parts[3].split("/")[0];
+ String ipFamily = parts[parts.length - 2];
+ String ipPart = parts[parts.length - 1].split("/")[0];
if (ipFamily.equals("ipv4")) {
ipv4 = ipPart;
} else if (ipFamily.equals("ipv6")) {
diff --git
a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapperTest.java
b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapperTest.java
index 511306854cf..f104eadd18b 100644
---
a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapperTest.java
+++
b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapperTest.java
@@ -52,6 +52,12 @@ public class LibvirtGetVmIpAddressCommandWrapperTest {
" net4 2e:9b:60:dc:49:30 N/A N/A\n" + //
" lxc5b7327203b6f 92:b2:77:0b:a9:20 N/A N/A\n";
+ private static String VIRSH_DOMIF_OUTPUT_WINDOWS = " Name MAC
address Protocol Address\n" + //
+
"-------------------------------------------------------------------------------\n"
+ //
+ " Ethernet Instance 0 02:0c:02:f9:00:80 ipv4
192.168.0.10/24\n" + //
+ " Loopback Pseudo-Interface 1 ipv6 ::1/128\n" + //
+ " - - ipv4 127.0.0.1/8\n";
+
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
@@ -118,7 +124,34 @@ public class LibvirtGetVmIpAddressCommandWrapperTest {
when(getVmIpAddressCommand.getVmNetworkCidr()).thenReturn("192.168.0.0/24");
when(getVmIpAddressCommand.getMacAddress()).thenReturn("02:0c:02:f9:00:80");
when(getVmIpAddressCommand.isWindows()).thenReturn(true);
- when(Script.executePipedCommands(anyList(),
anyLong())).thenReturn(new Pair<>(0, "192.168.0.10"));
+ when(Script.executePipedCommands(anyList(),
anyLong())).thenReturn(new Pair<>(0, VIRSH_DOMIF_OUTPUT_WINDOWS));
+
+ Answer answer = commandWrapper.execute(getVmIpAddressCommand,
libvirtComputingResource);
+
+ assertTrue(answer.getResult());
+ assertEquals("192.168.0.10", answer.getDetails());
+ } finally {
+ if (scriptMock != null)
+ scriptMock.close();
+ }
+ }
+
+
+ @Test
+ public void testExecuteWithWindowsVm2() {
+ LibvirtComputingResource libvirtComputingResource =
mock(LibvirtComputingResource.class);
+ GetVmIpAddressCommand getVmIpAddressCommand =
mock(GetVmIpAddressCommand.class);
+ LibvirtGetVmIpAddressCommandWrapper commandWrapper = new
LibvirtGetVmIpAddressCommandWrapper();
+ MockedStatic<Script> scriptMock = null;
+
+ try {
+ scriptMock = mockStatic(Script.class);
+
+ when(getVmIpAddressCommand.getVmName()).thenReturn("validVmName");
+
when(getVmIpAddressCommand.getVmNetworkCidr()).thenReturn("192.168.0.0/24");
+
when(getVmIpAddressCommand.getMacAddress()).thenReturn("02:0c:02:f9:00:80");
+ when(getVmIpAddressCommand.isWindows()).thenReturn(true);
+ when(Script.executePipedCommands(anyList(),
anyLong())).thenReturn(new Pair<>(0, "")).thenReturn(new Pair<>(0,
"192.168.0.10"));
Answer answer = commandWrapper.execute(getVmIpAddressCommand,
libvirtComputingResource);