use of org.libvirt.MemoryStatistic in project cloudstack by apache.
the class LibvirtComputingResourceTest method testGetVmStat.
@Test
public void testGetVmStat() throws LibvirtException {
final Connect connect = Mockito.mock(Connect.class);
final Domain domain = Mockito.mock(Domain.class);
final DomainInfo domainInfo = new DomainInfo();
final MemoryStatistic[] domainMem = new MemoryStatistic[2];
domainMem[0] = Mockito.mock(MemoryStatistic.class);
Mockito.when(domain.getInfo()).thenReturn(domainInfo);
Mockito.when(domain.memoryStats(2)).thenReturn(domainMem);
Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
final NodeInfo nodeInfo = new NodeInfo();
nodeInfo.cpus = 8;
nodeInfo.memory = 8 * 1024 * 1024;
nodeInfo.sockets = 2;
nodeInfo.threads = 2;
nodeInfo.model = "Foo processor";
Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
// this is testing the interface stats, returns an increasing number of sent and received bytes
Mockito.when(domain.interfaceStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainInterfaceStats>() {
// increment with less than a KB, so this should be less than 1 KB
static final int increment = 1000;
int rxBytes = 1000;
int txBytes = 1000;
@Override
public DomainInterfaceStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
domainInterfaceStats.rx_bytes = rxBytes += increment;
domainInterfaceStats.tx_bytes = txBytes += increment;
return domainInterfaceStats;
}
});
Mockito.when(domain.blockStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainBlockStats>() {
// a little less than a KB
static final int increment = 1000;
int rdBytes = 0;
int wrBytes = 1024;
@Override
public DomainBlockStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainBlockStats domainBlockStats = new DomainBlockStats();
domainBlockStats.rd_bytes = rdBytes += increment;
domainBlockStats.wr_bytes = wrBytes += increment;
return domainBlockStats;
}
});
final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {
@Override
public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
final InterfaceDef interfaceDef = new InterfaceDef();
return Arrays.asList(interfaceDef);
}
@Override
public List<DiskDef> getDisks(final Connect conn, final String vmName) {
final DiskDef diskDef = new DiskDef();
return Arrays.asList(diskDef);
}
};
libvirtComputingResource.getVmStat(connect, VMNAME);
final VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
// network traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
// IO traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
// Memory limit of VM must be greater than zero
Assert.assertTrue(vmStat.getIntFreeMemoryKBs() >= 0);
Assert.assertTrue(vmStat.getMemoryKBs() >= 0);
Assert.assertTrue(vmStat.getTargetMemoryKBs() >= vmStat.getMemoryKBs());
}
use of org.libvirt.MemoryStatistic in project cloudstack by apache.
the class LibvirtComputingResourceTest method testMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics.
@Test
public void testMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics() throws LibvirtException {
LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource();
MemoryStatistic[] mem = createMemoryStatisticFreeMemory100();
Domain domainMock = getDomainConfiguredToReturnMemoryStatistic(mem);
long memoryFreeInKBs = libvirtComputingResource.getMemoryFreeInKBs(domainMock);
Assert.assertEquals(100, memoryFreeInKBs);
}
use of org.libvirt.MemoryStatistic in project cloudstack by apache.
the class LibvirtComputingResourceTest method createMemoryStatisticFreeMemory100.
private MemoryStatistic[] createMemoryStatisticFreeMemory100() {
virDomainMemoryStats stat = new virDomainMemoryStats();
stat.val = 100;
MemoryStatistic[] mem = new MemoryStatistic[2];
mem[0] = new MemoryStatistic(stat);
return mem;
}
Aggregations