Search in sources :

Example 11 with LibvirtDiskDef

use of com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testMigrateCommand.

@Test
public void testMigrateCommand() {
    final Connect conn = Mockito.mock(Connect.class);
    final Connect dconn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final String vmName = "Test";
    final String destIp = "127.0.0.100";
    final boolean isWindows = false;
    final VirtualMachineTO vmTO = Mockito.mock(VirtualMachineTO.class);
    final boolean executeInSequence = false;
    final MigrateCommand command = new MigrateCommand(vmName, destIp, isWindows, vmTO, executeInSequence);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
        when(libvirtUtilitiesHelper.retrieveQemuConnection("qemu+tcp://" + command.getDestinationIp() + "/system")).thenReturn(dconn);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    final List<InterfaceDef> ifaces = new ArrayList<>();
    ifaces.add(interfaceDef);
    when(libvirtComputingResource.getInterfaces(conn, vmName)).thenReturn(ifaces);
    final LibvirtDiskDef diskDef = Mockito.mock(LibvirtDiskDef.class);
    final List<LibvirtDiskDef> disks = new ArrayList<>();
    disks.add(diskDef);
    when(libvirtComputingResource.getDisks(conn, vmName)).thenReturn(disks);
    final Domain dm = Mockito.mock(Domain.class);
    try {
        when(conn.domainLookupByName(vmName)).thenReturn(dm);
        when(libvirtComputingResource.getPrivateIp()).thenReturn("127.0.0.1");
        when(dm.getXMLDesc(8)).thenReturn("host_domain");
        when(dm.getXMLDesc(1)).thenReturn("host_domain");
        when(dm.isPersistent()).thenReturn(1);
        doNothing().when(dm).undefine();
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
        verify(libvirtUtilitiesHelper, times(1)).retrieveQemuConnection("qemu+tcp://" + command.getDestinationIp() + "/system");
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    verify(libvirtComputingResource, times(1)).getInterfaces(conn, vmName);
    verify(libvirtComputingResource, times(1)).getDisks(conn, vmName);
    try {
        verify(conn, times(1)).domainLookupByName(vmName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    try {
        verify(dm, times(1)).getXMLDesc(8);
    } catch (final Throwable t) {
        try {
            verify(dm, times(1)).getXMLDesc(1);
        } catch (final LibvirtException e) {
            fail(e.getMessage());
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) MigrateCommand(com.cloud.agent.api.MigrateCommand) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtDiskDef(com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef) Domain(org.libvirt.Domain) Test(org.junit.Test)

Example 12 with LibvirtDiskDef

use of com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.

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();
    Mockito.when(domain.getInfo()).thenReturn(domainInfo);
    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<LibvirtDiskDef> getDisks(final Connect conn, final String vmName) {
            final LibvirtDiskDef diskDef = new LibvirtDiskDef();
            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);
}
Also used : DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Connect(org.libvirt.Connect) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef) LibvirtDiskDef(com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef) NodeInfo(org.libvirt.NodeInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DomainBlockStats(org.libvirt.DomainBlockStats) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain) Test(org.junit.Test)

Example 13 with LibvirtDiskDef

use of com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.

the class LibvirtVMDefTest method testDiskDef.

public void testDiskDef() {
    final String filePath = "/var/lib/libvirt/images/disk.qcow2";
    final String diskLabel = "vda";
    final LibvirtDiskDef disk = new LibvirtDiskDef();
    final LibvirtDiskDef.DiskBus bus = LibvirtDiskDef.DiskBus.VIRTIO;
    final LibvirtDiskDef.DiskFmtType type = LibvirtDiskDef.DiskFmtType.QCOW2;
    final LibvirtDiskDef.DiskCacheMode cacheMode = LibvirtDiskDef.DiskCacheMode.WRITEBACK;
    disk.defFileBasedDisk(filePath, diskLabel, bus, type);
    disk.setCacheMode(cacheMode);
    assertEquals(filePath, disk.getDiskPath());
    assertEquals(diskLabel, disk.getDiskLabel());
    assertEquals(bus, disk.getBusType());
    assertEquals(LibvirtDiskDef.DeviceType.DISK, disk.getDeviceType());
    final String xmlDef = disk.toString();
    final String expectedXml = "<disk  device='disk' type='file'>\n<driver name='qemu' type='" + type.toString() + "' cache='" + cacheMode.toString() + "' />\n" + "<source file='" + filePath + "'/>\n<target dev='" + diskLabel + "' bus='" + bus.toString() + "'/>\n</disk>\n";
    assertEquals(xmlDef, expectedXml);
}
Also used : LibvirtDiskDef(com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef)

Aggregations

LibvirtDiskDef (com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef)13 Domain (org.libvirt.Domain)8 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef)6 Connect (org.libvirt.Connect)5 LibvirtException (org.libvirt.LibvirtException)4 IOException (java.io.IOException)3 DomainBlockStats (org.libvirt.DomainBlockStats)3 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)2 KvmPhysicalDisk (com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk)2 KvmStoragePool (com.cloud.hypervisor.kvm.storage.KvmStoragePool)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Test (org.junit.Test)2 DomainInfo (org.libvirt.DomainInfo)2 Answer (com.cloud.agent.api.Answer)1 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)1 MigrateAnswer (com.cloud.agent.api.MigrateAnswer)1