Skip to content

Commit 27f25a6

Browse files
committed
kvm: get ip address of Windows vms via virsh domifaddr
1 parent 7324ef4 commit 27f25a6

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private Pair<String, String> getIpAddresses(String output, String macAddress) {
119119
continue;
120120
}
121121
String device = parts[0];
122-
String mac = parts[1];
122+
String mac = parts[parts.length - 3];
123123
if (found) {
124124
if (!device.equals("-") || !mac.equals("-")) {
125125
break;
@@ -128,8 +128,8 @@ private Pair<String, String> getIpAddresses(String output, String macAddress) {
128128
continue;
129129
}
130130
found = true;
131-
String ipFamily = parts[2];
132-
String ipPart = parts[3].split("/")[0];
131+
String ipFamily = parts[parts.length - 2];
132+
String ipPart = parts[parts.length - 1].split("/")[0];
133133
if (ipFamily.equals("ipv4")) {
134134
ipv4 = ipPart;
135135
} else if (ipFamily.equals("ipv6")) {

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapperTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public class LibvirtGetVmIpAddressCommandWrapperTest {
5252
" net4 2e:9b:60:dc:49:30 N/A N/A\n" + //
5353
" lxc5b7327203b6f 92:b2:77:0b:a9:20 N/A N/A\n";
5454

55+
private static String VIRSH_DOMIF_OUTPUT_WINDOWS = " Name MAC address Protocol Address\n" + //
56+
"-------------------------------------------------------------------------------\n" + //
57+
" Ethernet Instance 0 02:0c:02:f9:00:80 ipv4 192.168.0.10/24\n" + //
58+
" Loopback Pseudo-Interface 1 ipv6 ::1/128\n" + //
59+
" - - ipv4 127.0.0.1/8\n";
60+
5561
@Before
5662
public void setUp() {
5763
MockitoAnnotations.openMocks(this);
@@ -118,7 +124,34 @@ public void testExecuteWithWindowsVm() {
118124
when(getVmIpAddressCommand.getVmNetworkCidr()).thenReturn("192.168.0.0/24");
119125
when(getVmIpAddressCommand.getMacAddress()).thenReturn("02:0c:02:f9:00:80");
120126
when(getVmIpAddressCommand.isWindows()).thenReturn(true);
121-
when(Script.executePipedCommands(anyList(), anyLong())).thenReturn(new Pair<>(0, "192.168.0.10"));
127+
when(Script.executePipedCommands(anyList(), anyLong())).thenReturn(new Pair<>(0, VIRSH_DOMIF_OUTPUT_WINDOWS));
128+
129+
Answer answer = commandWrapper.execute(getVmIpAddressCommand, libvirtComputingResource);
130+
131+
assertTrue(answer.getResult());
132+
assertEquals("192.168.0.10", answer.getDetails());
133+
} finally {
134+
if (scriptMock != null)
135+
scriptMock.close();
136+
}
137+
}
138+
139+
140+
@Test
141+
public void testExecuteWithWindowsVm2() {
142+
LibvirtComputingResource libvirtComputingResource = mock(LibvirtComputingResource.class);
143+
GetVmIpAddressCommand getVmIpAddressCommand = mock(GetVmIpAddressCommand.class);
144+
LibvirtGetVmIpAddressCommandWrapper commandWrapper = new LibvirtGetVmIpAddressCommandWrapper();
145+
MockedStatic<Script> scriptMock = null;
146+
147+
try {
148+
scriptMock = mockStatic(Script.class);
149+
150+
when(getVmIpAddressCommand.getVmName()).thenReturn("validVmName");
151+
when(getVmIpAddressCommand.getVmNetworkCidr()).thenReturn("192.168.0.0/24");
152+
when(getVmIpAddressCommand.getMacAddress()).thenReturn("02:0c:02:f9:00:80");
153+
when(getVmIpAddressCommand.isWindows()).thenReturn(true);
154+
when(Script.executePipedCommands(anyList(), anyLong())).thenReturn(new Pair<>(0, "")).thenReturn(new Pair<>(0, "192.168.0.10"));
122155

123156
Answer answer = commandWrapper.execute(getVmIpAddressCommand, libvirtComputingResource);
124157

0 commit comments

Comments
 (0)