Search in sources :

Example 36 with LibvirtUtilitiesHelper

use of com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStartCommandHostMemory.

@Test
public void testStartCommandHostMemory() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final int vmId = 1;
    final int[] vms = new int[] { vmId };
    final Domain dm = Mockito.mock(Domain.class);
    final String vmName = "Test";
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachine.Type.User);
    when(vmSpec.getName()).thenReturn(vmName);
    when(vmSpec.getMaxRam()).thenReturn(512L);
    when(libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        when(conn.listDomains()).thenReturn(vms);
        when(conn.domainLookupByID(vmId)).thenReturn(dm);
        when(dm.getMaxMemory()).thenReturn(1024L);
        when(dm.getName()).thenReturn(vmName);
        when(libvirtComputingResource.getTotalMemory()).thenReturn(2048 * 1024L);
        doNothing().when(libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
        fail(e.getMessage());
    }
    when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(true);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.agent.api.StartCommand) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) 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) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) Domain(org.libvirt.Domain) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 37 with LibvirtUtilitiesHelper

use of com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testRebootCommand.

@Test
public void testRebootCommand() {
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final String vmName = "Test";
    final RebootCommand command = new RebootCommand(vmName, true);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
    } catch (final LibvirtException 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);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : 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) RebootCommand(com.cloud.agent.api.RebootCommand) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 38 with LibvirtUtilitiesHelper

use of com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStartCommandInternalError.

@Test
public void testStartCommandInternalError() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final String vmName = "Test";
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
    when(vmSpec.getName()).thenReturn(vmName);
    when(libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        doThrow(InternalErrorException.class).when(libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.agent.api.StartCommand) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) 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) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 39 with LibvirtUtilitiesHelper

use of com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testResizeVolumeCommand.

@Test
public void testResizeVolumeCommand() {
    final String path = "nfs:/127.0.0.1/storage/secondary";
    final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
    final Long currentSize = 100l;
    final Long newSize = 200l;
    final boolean shrinkOk = true;
    final String vmInstance = "Test";
    final ResizeVolumeCommand command = new ResizeVolumeCommand(path, pool, currentSize, newSize, shrinkOk, vmInstance);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool storagePool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk vol = Mockito.mock(KvmPhysicalDisk.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final StorageVol v = Mockito.mock(StorageVol.class);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(storagePool);
    when(storagePool.getPhysicalDisk(path)).thenReturn(vol);
    when(vol.getPath()).thenReturn(path);
    when(libvirtComputingResource.getResizeScriptType(storagePool, vol)).thenReturn("FILE");
    when(storagePool.getType()).thenReturn(StoragePoolType.RBD);
    when(vol.getFormat()).thenReturn(PhysicalDiskFormat.FILE);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnection()).thenReturn(conn);
        when(conn.storageVolLookupByPath(path)).thenReturn(v);
        when(conn.getLibVirVersion()).thenReturn(10010l);
    } catch (final LibvirtException 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)).getStoragePoolMgr();
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnection();
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) 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) ResizeVolumeCommand(com.cloud.agent.api.storage.ResizeVolumeCommand) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 40 with LibvirtUtilitiesHelper

use of com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testRebootCommandException2.

@Test
public void testRebootCommandException2() {
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final String vmName = "Test";
    final RebootCommand command = new RebootCommand(vmName, true);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
        when(libvirtComputingResource.rebootVm(conn, command.getVmName())).thenThrow(LibvirtException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : 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) RebootCommand(com.cloud.agent.api.RebootCommand) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Aggregations

Answer (com.cloud.agent.api.Answer)128 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)128 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)128 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)128 Test (org.junit.Test)128 LibvirtException (org.libvirt.LibvirtException)103 Connect (org.libvirt.Connect)72 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)67 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)66 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)66 AttachAnswer (com.cloud.storage.command.AttachAnswer)61 NicTO (com.cloud.agent.api.to.NicTO)38 InternalErrorException (com.cloud.exception.InternalErrorException)28 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)27 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)25 KvmStoragePoolManager (com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager)24 StoragePool (com.cloud.storage.StoragePool)24 ArrayList (java.util.ArrayList)22 Domain (org.libvirt.Domain)22 URISyntaxException (java.net.URISyntaxException)19