diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index d7b4b1877cb..a04074bee94 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -67,8 +67,6 @@ import com.cloud.agent.api.PingCommand; import com.cloud.agent.api.PingRoutingCommand; import com.cloud.agent.api.PingRoutingWithNwGroupsCommand; import com.cloud.agent.api.SetupGuestNetworkCommand; -import com.cloud.agent.api.StartAnswer; -import com.cloud.agent.api.StartCommand; import com.cloud.agent.api.StartupCommand; import com.cloud.agent.api.StartupRoutingCommand; import com.cloud.agent.api.StartupStorageCommand; @@ -118,7 +116,6 @@ import com.cloud.hypervisor.kvm.storage.KVMStoragePool; import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; import com.cloud.hypervisor.kvm.storage.KVMStorageProcessor; import com.cloud.network.Networks.BroadcastDomainType; -import com.cloud.network.Networks.IsolationType; import com.cloud.network.Networks.RouterPrivateIpStrategy; import com.cloud.network.Networks.TrafficType; import com.cloud.resource.ServerResource; @@ -1173,7 +1170,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return "0".equals(command.execute(null)); } - private boolean passCmdLine(final String vmName, final String cmdLine) throws InternalErrorException { + public boolean passCmdLine(final String vmName, final String cmdLine) throws InternalErrorException { final Script command = new Script(_patchViaSocketPath, 5 * 1000, s_logger); String result; command.add("-n", vmName); @@ -1199,7 +1196,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } - protected String startVM(final Connect conn, final String vmName, final String domainXML) throws LibvirtException, InternalErrorException { + public String startVM(final Connect conn, final String vmName, final String domainXML) throws LibvirtException, InternalErrorException { try { /* We create a transient domain here. When this method gets @@ -1253,19 +1250,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv try { return wrapper.execute(cmd, this); } catch (final Exception e) { - //[TODO] ignore for now, we still need to finish the other commands. - //return Answer.createUnsupportedCommandAnswer(cmd); - } - - try { - if (cmd instanceof StartCommand) { - return execute((StartCommand)cmd); - } else { - s_logger.warn("Unsupported command "); - return Answer.createUnsupportedCommandAnswer(cmd); - } - } catch (final IllegalArgumentException e) { - return new Answer(cmd, false, e.getMessage()); + return Answer.createUnsupportedCommandAnswer(cmd); } } @@ -1791,7 +1776,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return stats; } - protected void handleVmStartFailure(final Connect conn, final String vmName, final LibvirtVMDef vm) { + public void handleVmStartFailure(final Connect conn, final String vmName, final LibvirtVMDef vm) { if (vm != null && vm.getDevices() != null) { cleanupVMNetworks(conn, vm.getDevices().getInterfaces()); } @@ -1814,25 +1799,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return uuid; } - private void getOsVersion() { - final String version = Script.runSimpleBashScript("cat /etc/redhat-release | awk '{print $7}'"); - if (version != null) { - final String[] versions = version.split("\\."); - if (versions.length == 2) { - final String major = versions[0]; - final String minor = versions[1]; - try { - final Integer m = Integer.parseInt(major); - final Integer min = Integer.parseInt(minor); - hostOsVersion = new Pair<>(m, min); - } catch(final NumberFormatException e) { - - } - } - } - } - - protected LibvirtVMDef createVMFromSpec(final VirtualMachineTO vmTO) { + public LibvirtVMDef createVMFromSpec(final VirtualMachineTO vmTO) { final LibvirtVMDef vm = new LibvirtVMDef(); vm.setDomainName(vmTO.getName()); String uuid = vmTO.getUuid(); @@ -1972,7 +1939,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return vm; } - protected void createVifs(final VirtualMachineTO vmSpec, final LibvirtVMDef vm) throws InternalErrorException, LibvirtException { + public void createVifs(final VirtualMachineTO vmSpec, final LibvirtVMDef vm) throws InternalErrorException, LibvirtException { final NicTO[] nics = vmSpec.getNics(); final Map params = vmSpec.getDetails(); String nicAdapter = ""; @@ -1988,107 +1955,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } - protected StartAnswer execute(final StartCommand cmd) { - final VirtualMachineTO vmSpec = cmd.getVirtualMachine(); - vmSpec.setVncAddr(cmd.getHostIp()); - final String vmName = vmSpec.getName(); - LibvirtVMDef vm = null; - - DomainState state = DomainState.VIR_DOMAIN_SHUTOFF; - Connect conn = null; - try { - final NicTO[] nics = vmSpec.getNics(); - - for (final NicTO nic : nics) { - if (vmSpec.getType() != VirtualMachine.Type.User) { - nic.setPxeDisable(true); - } - } - - vm = createVMFromSpec(vmSpec); - - conn = LibvirtConnection.getConnectionByType(vm.getHvsType()); - - createVbd(conn, vmSpec, vmName, vm); - - if (!_storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) { - return new StartAnswer(cmd, "Failed to connect physical disks to host"); - } - - createVifs(vmSpec, vm); - - s_logger.debug("starting " + vmName + ": " + vm.toString()); - startVM(conn, vmName, vm.toString()); - - for (final NicTO nic : nics) { - if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) { - if (vmSpec.getType() != VirtualMachine.Type.User) { - configureDefaultNetworkRulesForSystemVm(conn, vmName); - break; - } else { - final List nicSecIps = nic.getNicSecIps(); - String secIpsStr; - final StringBuilder sb = new StringBuilder(); - if (nicSecIps != null) { - for (final String ip : nicSecIps) { - sb.append(ip).append(":"); - } - secIpsStr = sb.toString(); - } else { - secIpsStr = "0:"; - } - default_network_rules(conn, vmName, nic, vmSpec.getId(), secIpsStr); - } - } - } - - // pass cmdline info to system vms - if (vmSpec.getType() != VirtualMachine.Type.User) { - //wait and try passCmdLine for 5 minutes at most for CLOUDSTACK-2823 - String controlIp = null; - for (final NicTO nic : nics) { - if (nic.getType() == TrafficType.Control) { - controlIp = nic.getIp(); - break; - } - } - for (int count = 0; count < 30; count++) { - passCmdLine(vmName, vmSpec.getBootArgs()); - //check router is up? - final boolean result = _virtRouterResource.connect(controlIp, 1, 5000); - if (result) { - break; - } - } - } - - state = DomainState.VIR_DOMAIN_RUNNING; - return new StartAnswer(cmd); - } catch (final LibvirtException e) { - s_logger.warn("LibvirtException ", e); - if (conn != null) { - handleVmStartFailure(conn, vmName, vm); - } - return new StartAnswer(cmd, e.getMessage()); - } catch (final InternalErrorException e) { - s_logger.warn("InternalErrorException ", e); - if (conn != null) { - handleVmStartFailure(conn, vmName, vm); - } - return new StartAnswer(cmd, e.getMessage()); - } catch (final URISyntaxException e) { - s_logger.warn("URISyntaxException ", e); - if (conn != null) { - handleVmStartFailure(conn, vmName, vm); - } - return new StartAnswer(cmd, e.getMessage()); - } finally { - if (state != DomainState.VIR_DOMAIN_RUNNING) { - _storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec); - } - } - } - public String getVolumePath(final Connect conn, final DiskTO volume) throws LibvirtException, URISyntaxException { final DataTO data = volume.getData(); final DataStoreTO store = data.getDataStore(); @@ -2107,7 +1973,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } - protected void createVbd(final Connect conn, final VirtualMachineTO vmSpec, final String vmName, final LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException { + public void createVbd(final Connect conn, final VirtualMachineTO vmSpec, final String vmName, final LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException { final List disks = Arrays.asList(vmSpec.getDisks()); Collections.sort(disks, new Comparator() { @Override @@ -3197,7 +3063,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return true; } - protected boolean default_network_rules(final Connect conn, final String vmName, final NicTO nic, final Long vmId, final String secIpStr) { + public boolean defaultNetworkRules(final Connect conn, final String vmName, final NicTO nic, final Long vmId, final String secIpStr) { if (!_canBridgeFirewall) { return false; } diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java index 966a4314979..225634005c5 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java @@ -36,9 +36,11 @@ import com.ceph.rbd.RbdImage; import com.cloud.agent.api.Answer; import com.cloud.agent.api.ManageSnapshotAnswer; import com.cloud.agent.api.ManageSnapshotCommand; +import com.cloud.agent.api.to.StorageFilerTO; import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; import com.cloud.hypervisor.kvm.storage.KVMStoragePool; +import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; import com.cloud.resource.CommandWrapper; import com.cloud.storage.Storage.StoragePoolType; import com.cloud.utils.script.Script; @@ -66,7 +68,9 @@ public final class LibvirtManageSnapshotCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(LibvirtStartCommandWrapper.class); + + @Override + public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) { + final VirtualMachineTO vmSpec = command.getVirtualMachine(); + vmSpec.setVncAddr(command.getHostIp()); + 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 { + final NicTO[] nics = vmSpec.getNics(); + + for (final NicTO nic : nics) { + if (vmSpec.getType() != VirtualMachine.Type.User) { + nic.setPxeDisable(true); + } + } + + vm = libvirtComputingResource.createVMFromSpec(vmSpec); + conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType()); + 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()); + + for (final NicTO nic : nics) { + if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) { + if (vmSpec.getType() != VirtualMachine.Type.User) { + libvirtComputingResource.configureDefaultNetworkRulesForSystemVm(conn, vmName); + break; + } else { + final List nicSecIps = nic.getNicSecIps(); + String secIpsStr; + final StringBuilder sb = new StringBuilder(); + if (nicSecIps != null) { + for (final String ip : nicSecIps) { + sb.append(ip).append(":"); + } + secIpsStr = sb.toString(); + } else { + secIpsStr = "0:"; + } + libvirtComputingResource.defaultNetworkRules(conn, vmName, nic, vmSpec.getId(), secIpsStr); + } + } + } + + // pass cmdline info to system vms + if (vmSpec.getType() != VirtualMachine.Type.User) { + //wait and try passCmdLine for 5 minutes at most for CLOUDSTACK-2823 + String controlIp = null; + for (final NicTO nic : nics) { + if (nic.getType() == TrafficType.Control) { + controlIp = nic.getIp(); + break; + } + } + for (int count = 0; count < 30; count++) { + libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs()); + //check router is up? + final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource(); + final boolean result = virtRouterResource.connect(controlIp, 1, 5000); + if (result) { + break; + } + } + } + + state = DomainState.VIR_DOMAIN_RUNNING; + return new StartAnswer(command); + } catch (final LibvirtException e) { + s_logger.warn("LibvirtException ", e); + if (conn != null) { + libvirtComputingResource.handleVmStartFailure(conn, vmName, vm); + } + return new StartAnswer(command, e.getMessage()); + } catch (final InternalErrorException e) { + s_logger.warn("InternalErrorException ", e); + if (conn != null) { + libvirtComputingResource.handleVmStartFailure(conn, vmName, vm); + } + return new StartAnswer(command, e.getMessage()); + } catch (final URISyntaxException e) { + s_logger.warn("URISyntaxException ", e); + if (conn != null) { + libvirtComputingResource.handleVmStartFailure(conn, vmName, vm); + } + return new StartAnswer(command, e.getMessage()); + } finally { + if (state != DomainState.VIR_DOMAIN_RUNNING) { + storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec); + } + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java index c0f1cb55ca4..74a41c651b9 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java @@ -65,4 +65,8 @@ public class LibvirtUtilitiesHelper { public String generatereUUIDName() { return UUID.randomUUID().toString(); } + + public Connect getConnectionByType(final String hvsType) throws LibvirtException { + return LibvirtConnection.getConnectionByType(hvsType); + } } \ No newline at end of file diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index 140262fa850..4a1dcc43be5 100644 --- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -121,6 +122,7 @@ import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.SecurityGroupRulesCmd; import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto; +import com.cloud.agent.api.StartCommand; import com.cloud.agent.api.StopCommand; import com.cloud.agent.api.UnPlugNicCommand; import com.cloud.agent.api.UpgradeSnapshotCommand; @@ -758,6 +760,96 @@ public class LibvirtComputingResourceTest { } } + @SuppressWarnings("unchecked") + @Test + public void testRebootCommandException1() { + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + + final String vmName = "Test"; + final RebootCommand command = new RebootCommand(vmName); + + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + try { + when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).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()); + } + } + + @Test + public void testRebootCommandError() { + final Connect conn = Mockito.mock(Connect.class); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + + final String vmName = "Test"; + final RebootCommand command = new RebootCommand(vmName); + + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + try { + when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn); + when(libvirtComputingResource.rebootVM(conn, command.getVmName())).thenReturn("error"); + } 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()); + } + } + + @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); + + 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()); + } + } + @Test public void testRebootRouterCommand() { final VirtualRoutingResource routingResource = Mockito.mock(VirtualRoutingResource.class); @@ -3588,6 +3680,65 @@ public class LibvirtComputingResourceTest { } } + @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()); + } + } + @SuppressWarnings("unchecked") @Test public void testBackupSnapshotCommandLibvirtException() { @@ -4545,4 +4696,355 @@ public class LibvirtComputingResourceTest { final Answer answer = wrapper.execute(command, libvirtComputingResource); assertTrue(answer.getResult()); } + + @Test + public void testStartCommandFailedConnect() { + 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); + 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(false); + + 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()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testStartCommandLibvirtException() { + 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 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())).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)).getStoragePoolMgr(); + verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper(); + try { + verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType()); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @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()); + } + } + + @Test + public void testStartCommandUriException() { + 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(URISyntaxException.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()); + } + } + + @Test + public void testStartCommand() { + 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 VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class); + + final NicTO nic = Mockito.mock(NicTO.class); + final NicTO[] nics = new NicTO[]{nic}; + + final String vmName = "Test"; + final String controlIp = "169.122.10.10"; + + 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); + 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); + try { + doNothing().when(libvirtComputingResource).createVifs(vmSpec, vmDef); + + when(libvirtComputingResource.startVM(conn, vmName, vmDef.toString())).thenReturn("SUCCESS"); + + when(vmSpec.getBootArgs()).thenReturn("ls -lart"); + when(libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())).thenReturn(true); + + when(nic.getIp()).thenReturn(controlIp); + when(nic.getType()).thenReturn(TrafficType.Control); + when(libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource); + when(virtRouterResource.connect(controlIp, 1, 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, libvirtComputingResource); + assertTrue(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()); + } + } + + @Test + public void testStartCommandIsolationEc2() { + 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 VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class); + + final NicTO nic = Mockito.mock(NicTO.class); + final NicTO[] nics = new NicTO[]{nic}; + + final String vmName = "Test"; + final String controlIp = "169.122.10.10"; + + 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); + 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); + try { + doNothing().when(libvirtComputingResource).createVifs(vmSpec, vmDef); + + when(libvirtComputingResource.startVM(conn, vmName, vmDef.toString())).thenReturn("SUCCESS"); + + when(nic.isSecurityGroupEnabled()).thenReturn(true); + when(nic.getIsolationUri()).thenReturn(new URI("ec2://test")); + + + when(vmSpec.getBootArgs()).thenReturn("ls -lart"); + when(libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())).thenReturn(true); + + when(nic.getIp()).thenReturn(controlIp); + when(nic.getType()).thenReturn(TrafficType.Control); + when(libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource); + when(virtRouterResource.connect(controlIp, 1, 5000)).thenReturn(true); + } catch (final InternalErrorException e) { + fail(e.getMessage()); + } catch (final LibvirtException 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); + assertTrue(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()); + } + } } \ No newline at end of file