kvm: get ip address of Windows vms via virsh domifaddr (#12651)

This commit is contained in:
Wei Zhou 2026-07-01 20:29:54 +02:00 committed by GitHub
parent 006831daaf
commit 348ceaff33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 4 deletions

View File

@ -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")) {

View File

@ -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);