Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private Pair<String, String> getIpAddresses(String output, String macAddress) {
continue;
}
String device = parts[0];
String mac = parts[1];
String mac = parts[parts.length - 3];
if (found) {
if (!device.equals("-") || !mac.equals("-")) {
break;
Expand All @@ -128,8 +128,8 @@ private Pair<String, String> getIpAddresses(String output, String macAddress) {
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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" + //
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test data for line 58 appears to be missing the MAC address column. Based on the virsh domifaddr output format, each line should have 4 columns: Name, MAC address, Protocol, and Address. Line 58 "Loopback Pseudo-Interface 1" only has 3 columns (Name, Protocol, Address), which will cause the parsing logic to incorrectly treat "1" (part of the interface name) as the MAC address when using parts[parts.length - 3]. This should either include a MAC address or use "-" as a placeholder to maintain the expected column structure, similar to line 59.

Suggested change
" Loopback Pseudo-Interface 1 ipv6 ::1/128\n" + //
" Loopback Pseudo-Interface 1 - ipv6 ::1/128\n" + //

Copilot uses AI. Check for mistakes.
" - - ipv4 127.0.0.1/8\n";

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
Expand Down Expand Up @@ -118,7 +124,34 @@ public void testExecuteWithWindowsVm() {
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() {
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name "testExecuteWithWindowsVm2" is not descriptive. Consider renaming it to something more meaningful like "testExecuteWithWindowsVmFallbackToRegistry" to clarify that this test verifies the fallback behavior when virsh domifaddr returns empty output and the system falls back to reading from the Windows registry.

Suggested change
public void testExecuteWithWindowsVm2() {
public void testExecuteWithWindowsVmFallbackToRegistry() {

Copilot uses AI. Check for mistakes.
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);

Expand Down
Loading