|
19 | 19 |
|
20 | 20 | package com.cloud.hypervisor.kvm.resource; |
21 | 21 |
|
22 | | -import com.cloud.agent.api.to.VirtualMachineTO; |
23 | | -import com.cloud.template.VirtualMachineTemplate.BootloaderType; |
24 | | -import com.cloud.utils.Pair; |
25 | | -import com.cloud.vm.VirtualMachine; |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertNotNull; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Random; |
| 28 | +import java.util.UUID; |
26 | 29 |
|
27 | 30 | import junit.framework.Assert; |
| 31 | + |
28 | 32 | import org.apache.commons.lang.SystemUtils; |
29 | 33 | import org.junit.Assume; |
30 | 34 | import org.junit.Test; |
| 35 | +import org.libvirt.Connect; |
| 36 | +import org.libvirt.Domain; |
| 37 | +import org.libvirt.DomainBlockStats; |
| 38 | +import org.libvirt.DomainInfo; |
| 39 | +import org.libvirt.DomainInterfaceStats; |
| 40 | +import org.libvirt.LibvirtException; |
| 41 | +import org.libvirt.NodeInfo; |
| 42 | +import org.mockito.Mockito; |
| 43 | +import org.mockito.invocation.InvocationOnMock; |
| 44 | +import org.mockito.stubbing.Answer; |
31 | 45 |
|
32 | | -import java.util.Random; |
33 | | -import java.util.UUID; |
34 | | - |
35 | | -import static org.junit.Assert.assertEquals; |
36 | | -import static org.junit.Assert.assertNotNull; |
| 46 | +import com.cloud.agent.api.VmStatsEntry; |
| 47 | +import com.cloud.agent.api.to.VirtualMachineTO; |
| 48 | +import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef; |
| 49 | +import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef; |
| 50 | +import com.cloud.template.VirtualMachineTemplate.BootloaderType; |
| 51 | +import com.cloud.utils.Pair; |
| 52 | +import com.cloud.vm.VirtualMachine; |
37 | 53 |
|
38 | 54 | public class LibvirtComputingResourceTest { |
39 | 55 |
|
@@ -210,4 +226,79 @@ public void testUUID() { |
210 | 226 | uuid = lcr.getUuid(uuid); |
211 | 227 | Assert.assertTrue(uuid.equals(oldUuid)); |
212 | 228 | } |
| 229 | + |
| 230 | + private static final String VMNAME = "test"; |
| 231 | + |
| 232 | + @Test |
| 233 | + public void testGetVmStat() throws LibvirtException { |
| 234 | + Connect connect = Mockito.mock(Connect.class); |
| 235 | + Domain domain = Mockito.mock(Domain.class); |
| 236 | + DomainInfo domainInfo = new DomainInfo(); |
| 237 | + Mockito.when(domain.getInfo()).thenReturn(domainInfo); |
| 238 | + Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain); |
| 239 | + NodeInfo nodeInfo = new NodeInfo(); |
| 240 | + nodeInfo.cpus = 8; |
| 241 | + nodeInfo.memory = 8 * 1024 * 1024; |
| 242 | + nodeInfo.sockets = 2; |
| 243 | + nodeInfo.threads = 2; |
| 244 | + nodeInfo.model = "Foo processor"; |
| 245 | + Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo); |
| 246 | + // this is testing the interface stats, returns an increasing number of sent and received bytes |
| 247 | + Mockito.when(domain.interfaceStats(Mockito.anyString())).thenAnswer(new Answer<DomainInterfaceStats>() { |
| 248 | + // increment with less than a KB, so this should be less than 1 KB |
| 249 | + final static int increment = 1000; |
| 250 | + int rx_bytes = 1000; |
| 251 | + int tx_bytes = 1000; |
| 252 | + |
| 253 | + @Override |
| 254 | + public DomainInterfaceStats answer(InvocationOnMock invocation) throws Throwable { |
| 255 | + DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats(); |
| 256 | + domainInterfaceStats.rx_bytes = (this.rx_bytes += increment); |
| 257 | + domainInterfaceStats.tx_bytes = (this.tx_bytes += increment); |
| 258 | + return domainInterfaceStats; |
| 259 | + |
| 260 | + } |
| 261 | + |
| 262 | + }); |
| 263 | + |
| 264 | + Mockito.when(domain.blockStats(Mockito.anyString())).thenAnswer(new Answer<DomainBlockStats>() { |
| 265 | + // a little less than a KB |
| 266 | + final static int increment = 1000; |
| 267 | + |
| 268 | + int rd_bytes = 0; |
| 269 | + int wr_bytes = 1024; |
| 270 | + @Override |
| 271 | + public DomainBlockStats answer(InvocationOnMock invocation) throws Throwable { |
| 272 | + DomainBlockStats domainBlockStats = new DomainBlockStats(); |
| 273 | + |
| 274 | + domainBlockStats.rd_bytes = (rd_bytes += increment); |
| 275 | + domainBlockStats.wr_bytes = (wr_bytes += increment); |
| 276 | + return domainBlockStats; |
| 277 | + } |
| 278 | + |
| 279 | + }); |
| 280 | + |
| 281 | + LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() { |
| 282 | + @Override |
| 283 | + protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) { |
| 284 | + InterfaceDef interfaceDef = new InterfaceDef(); |
| 285 | + return Arrays.asList(interfaceDef); |
| 286 | + } |
| 287 | + |
| 288 | + @Override |
| 289 | + public List<DiskDef> getDisks(Connect conn, String vmName) { |
| 290 | + DiskDef diskDef = new DiskDef(); |
| 291 | + return Arrays.asList(diskDef); |
| 292 | + } |
| 293 | + |
| 294 | + }; |
| 295 | + libvirtComputingResource.getVmStat(connect, VMNAME); |
| 296 | + VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME); |
| 297 | + // network traffic as generated by the logic above, must be greater than zero |
| 298 | + Assert.assertTrue(vmStat.getNetworkReadKBs() > 0); |
| 299 | + Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0); |
| 300 | + // IO traffic as generated by the logic above, must be greater than zero |
| 301 | + Assert.assertTrue(vmStat.getDiskReadKBs() > 0); |
| 302 | + Assert.assertTrue(vmStat.getDiskWriteKBs() > 0); |
| 303 | + } |
213 | 304 | } |
0 commit comments