use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testNetworkElementCommand.
@Test
public void testNetworkElementCommand() {
final CheckRouterCommand command = new CheckRouterCommand();
final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
when(virtRouterResource.executeRequest(command)).thenReturn(new CheckRouterAnswer(command, "mock_resource"));
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
}
use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCheckSshCommand.
@Test
public void testCheckSshCommand() {
final String instanceName = "Test";
final String ip = "127.0.0.1";
final int port = 22;
final CheckSshCommand command = new CheckSshCommand(instanceName, ip, port);
final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
final String privateIp = command.getIp();
final int cmdPort = command.getPort();
when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
when(virtRouterResource.connect(privateIp, cmdPort)).thenReturn(true);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getVirtRouterResource();
verify(virtRouterResource, times(1)).connect(privateIp, cmdPort);
}
use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCheckSshCommandFailure.
@Test
public void testCheckSshCommandFailure() {
final String instanceName = "Test";
final String ip = "127.0.0.1";
final int port = 22;
final CheckSshCommand command = new CheckSshCommand(instanceName, ip, port);
final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
final String privateIp = command.getIp();
final int cmdPort = command.getPort();
when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
when(virtRouterResource.connect(privateIp, cmdPort)).thenReturn(false);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getVirtRouterResource();
verify(virtRouterResource, times(1)).connect(privateIp, cmdPort);
}
use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.
the class LibvirtStartCommandWrapper method execute.
@Override
public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) {
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final String vmName = vmSpec.getName();
LibvirtVmDef vm = null;
DomainState state = DomainState.VIR_DOMAIN_SHUTOFF;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = null;
try {
vm = libvirtComputingResource.createVmFromSpec(vmSpec);
conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
final Long remainingMem = getFreeMemory(conn, libvirtComputingResource);
if (remainingMem == null) {
return new StartAnswer(command, "Failed to get free memory");
} else if (remainingMem < vmSpec.getMinRam()) {
return new StartAnswer(command, "Not enough memory on the host, remaining: " + remainingMem + ", asking: " + vmSpec.getMinRam());
}
final NicTO[] nics = vmSpec.getNics();
libvirtComputingResource.createVbd(conn, vmSpec, vmName, vm);
if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) {
return new StartAnswer(command, "Failed to connect physical disks to host");
}
libvirtComputingResource.createVifs(vmSpec, vm);
s_logger.debug("starting " + vmName + ": " + vm.toString());
libvirtComputingResource.startVm(conn, vmName, vm.toString());
// system vms
if (vmSpec.getType() != VirtualMachineType.User) {
// pass cmdline with config for the systemvm to configure itself
if (libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())) {
s_logger.debug("Passing cmdline succeeded");
} else {
final String errorMessage = "Passing cmdline failed, aborting.";
s_logger.debug(errorMessage);
return new StartAnswer(command, errorMessage);
}
String controlIp = null;
for (final NicTO nic : nics) {
if (nic.getType() == TrafficType.Control) {
controlIp = nic.getIp();
break;
}
}
// connect to the router by using its linklocal address (that should now be configured)
s_logger.debug("Starting ssh attempts to " + controlIp);
final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
if (!virtRouterResource.connect(controlIp, 30, 5000)) {
final String errorMessage = "Unable to login to router via linklocal address " + controlIp + " after 30 tries, aborting.";
s_logger.debug(errorMessage);
return new StartAnswer(command, errorMessage);
}
s_logger.debug("Successfully completed ssh attempts to " + controlIp);
}
state = DomainState.VIR_DOMAIN_RUNNING;
return new StartAnswer(command);
} catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
s_logger.warn("Exception while starting VM: " + ExceptionUtils.getRootCauseMessage(e));
if (conn != null) {
libvirtComputingResource.handleVmStartFailure(vm);
}
return new StartAnswer(command, e.getMessage());
} finally {
if (state != DomainState.VIR_DOMAIN_RUNNING) {
storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec);
}
}
}
use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testStartCommand.
@Test
public void testStartCommand() {
final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
final Host host = Mockito.mock(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 VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
final NicTO nic = Mockito.mock(NicTO.class);
final NicTO[] nics = new NicTO[] { nic };
final int[] vms = new int[0];
final String vmName = "Test";
final String controlIp = "127.0.0.1";
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(vmSpec.getNics()).thenReturn(nics);
when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
when(vmSpec.getName()).thenReturn(vmName);
when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
when(conn.listDomains()).thenReturn(vms);
doNothing().when(this.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);
try {
doNothing().when(this.libvirtComputingResource).createVifs(vmSpec, vmDef);
when(this.libvirtComputingResource.startVm(conn, vmName, vmDef.toString())).thenReturn("SUCCESS");
when(vmSpec.getBootArgs()).thenReturn("ls -lart");
when(this.libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())).thenReturn(true);
when(nic.getIp()).thenReturn(controlIp);
when(nic.getType()).thenReturn(TrafficType.Control);
when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
when(virtRouterResource.connect(controlIp, 30, 5000)).thenReturn(true);
} catch (final InternalErrorException e) {
fail(e.getMessage());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
Aggregations