use of org.libvirt.Connect in project cloudstack by apache.
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 };
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 e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
} catch (final 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());
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtComputingResourceTest method testManageSnapshotCommandLibvirt.
@Test
public void testManageSnapshotCommandLibvirt() {
final StoragePool storagePool = Mockito.mock(StoragePool.class);
;
final String volumePath = "/123/vol";
final String vmName = "Test";
final long snapshotId = 1l;
final String preSnapshotPath = "/snapshot/path";
final String snapshotName = "snap";
final ManageSnapshotCommand command = new ManageSnapshotCommand(snapshotId, volumePath, storagePool, preSnapshotPath, snapshotName, vmName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
final Domain vm = Mockito.mock(Domain.class);
final DomainInfo info = Mockito.mock(DomainInfo.class);
final DomainState state = DomainInfo.DomainState.VIR_DOMAIN_RUNNING;
info.state = state;
final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class);
final StorageFilerTO pool = command.getPool();
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
when(libvirtComputingResource.getDomain(conn, command.getVmName())).thenReturn(vm);
when(vm.getInfo()).thenReturn(info);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primaryPool);
when(primaryPool.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
when(primaryPool.isExternalSnapshot()).thenReturn(false);
try {
when(vm.getUUIDString()).thenReturn("cdb18980-546d-4153-b916-70ee9edf0908");
} 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)).getConnectionByVmName(vmName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtComputingResourceTest method testExceptionCheckVirtualMachineCommand.
@SuppressWarnings("unchecked")
@Test
public void testExceptionCheckVirtualMachineCommand() {
final Connect conn = Mockito.mock(Connect.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final String vmName = "Test";
final CheckVirtualMachineCommand command = new CheckVirtualMachineCommand(vmName);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenThrow(LibvirtException.class);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(libvirtComputingResource.getVmState(conn, command.getVmName())).thenReturn(PowerState.PowerOn);
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());
}
}
use of org.libvirt.Connect in project cloudstack by apache.
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<InterfaceDef>();
ifaces.add(interfaceDef);
when(libvirtComputingResource.getInterfaces(conn, vmName)).thenReturn(ifaces);
final DiskDef diskDef = Mockito.mock(DiskDef.class);
final List<DiskDef> disks = new ArrayList<DiskDef>();
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("<domain type='kvm' id='3'>" + " <devices>" + " <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.1'>" + " <listen type='address' address='10.10.10.1'/>" + " </graphics>" + " </devices>" + "</domain>");
when(dm.getXMLDesc(1)).thenReturn("<domain type='kvm' id='3'>" + " <devices>" + " <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.1'>" + " <listen type='address' address='10.10.10.1'/>" + " </graphics>" + " </devices>" + "</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());
}
}
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtComputingResourceTest method testRebootCommandException2.
@SuppressWarnings("unchecked")
@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());
}
}
Aggregations