Repository: cloudstack Updated Branches: refs/heads/CLOUDSTACK-8301 e5cee452b -> 8ff90008b
Refactoring the LibvirtComputingResource - Adding LibvirtAttachIsoCommandWrapper, LibvirtAttachIsoCommandWrapper and LibvirtAttachIsoCommandWrapper - 7 unit tests added - KVM hypervisor plugin with 11.5% coverage Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/4d216f1a Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/4d216f1a Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/4d216f1a Branch: refs/heads/CLOUDSTACK-8301 Commit: 4d216f1a6382c42cf94a947aa5432f4d18782811 Parents: be1e517 Author: wilderrodrigues <[email protected]> Authored: Thu Apr 23 14:24:53 2015 +0200 Committer: wilderrodrigues <[email protected]> Committed: Wed May 6 19:20:42 2015 +0200 ---------------------------------------------------------------------- .../kvm/resource/LibvirtComputingResource.java | 53 +---- .../wrapper/LibvirtAttachIsoCommandWrapper.java | 52 +++++ ...ibvirtCheckVirtualMachineCommandWrapper.java | 51 +++++ .../wrapper/LibvirtReadyCommandWrapper.java | 34 ++++ .../resource/wrapper/LibvirtRequestWrapper.java | 6 + .../resource/LibvirtComputingResourceTest.java | 196 +++++++++++++++++++ 6 files changed, 343 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d216f1a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java ---------------------------------------------------------------------- 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 1c7aa24..0fb20ee 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 @@ -87,7 +87,6 @@ import com.ceph.rbd.Rbd; import com.ceph.rbd.RbdException; import com.ceph.rbd.RbdImage; import com.cloud.agent.api.Answer; -import com.cloud.agent.api.AttachIsoCommand; import com.cloud.agent.api.AttachVolumeAnswer; import com.cloud.agent.api.AttachVolumeCommand; import com.cloud.agent.api.BackupSnapshotAnswer; @@ -95,8 +94,6 @@ import com.cloud.agent.api.BackupSnapshotCommand; import com.cloud.agent.api.CheckNetworkAnswer; import com.cloud.agent.api.CheckNetworkCommand; import com.cloud.agent.api.CheckOnHostCommand; -import com.cloud.agent.api.CheckVirtualMachineAnswer; -import com.cloud.agent.api.CheckVirtualMachineCommand; import com.cloud.agent.api.CleanupNetworkRulesCmd; import com.cloud.agent.api.Command; import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand; @@ -138,8 +135,6 @@ import com.cloud.agent.api.PingRoutingWithNwGroupsCommand; import com.cloud.agent.api.PlugNicAnswer; import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PvlanSetupCommand; -import com.cloud.agent.api.ReadyAnswer; -import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.SecurityGroupRuleAnswer; import com.cloud.agent.api.SecurityGroupRulesCmd; import com.cloud.agent.api.SetupGuestNetworkCommand; @@ -1314,13 +1309,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } try { - if (cmd instanceof CheckVirtualMachineCommand) { - return execute((CheckVirtualMachineCommand)cmd); - } else if (cmd instanceof ReadyCommand) { - return execute((ReadyCommand)cmd); - } else if (cmd instanceof AttachIsoCommand) { - return execute((AttachIsoCommand)cmd); - } else if (cmd instanceof AttachVolumeCommand) { + if (cmd instanceof AttachVolumeCommand) { return execute((AttachVolumeCommand)cmd); } else if (cmd instanceof CheckConsoleProxyLoadCommand) { return execute((CheckConsoleProxyLoadCommand)cmd); @@ -2951,21 +2940,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result); } - private Answer execute(final AttachIsoCommand cmd) { - try { - final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); - attachOrDetachISO(conn, cmd.getVmName(), cmd.getIsoPath(), cmd.isAttach()); - } catch (final LibvirtException e) { - return new Answer(cmd, false, e.toString()); - } catch (final URISyntaxException e) { - return new Answer(cmd, false, e.toString()); - } catch (final InternalErrorException e) { - return new Answer(cmd, false, e.toString()); - } - - return new Answer(cmd); - } - private AttachVolumeAnswer execute(final AttachVolumeCommand cmd) { try { final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); @@ -2983,16 +2957,12 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return new AttachVolumeAnswer(cmd, cmd.getDeviceId(), cmd.getVolumePath()); } - private Answer execute(final ReadyCommand cmd) { - return new ReadyAnswer(cmd); - } - protected PowerState convertToPowerState(final DomainState ps) { final PowerState state = s_powerStatesTable.get(ps); return state == null ? PowerState.PowerUnknown : state; } - protected PowerState getVmState(final Connect conn, final String vmName) { + public PowerState getVmState(final Connect conn, final String vmName) { int retry = 3; Domain vms = null; while (retry-- > 0) { @@ -3015,21 +2985,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return PowerState.PowerOff; } - private Answer execute(final CheckVirtualMachineCommand cmd) { - try { - final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); - final PowerState state = getVmState(conn, cmd.getVmName()); - Integer vncPort = null; - if (state == PowerState.PowerOn) { - vncPort = getVncPort(conn, cmd.getVmName()); - } - - return new CheckVirtualMachineAnswer(cmd, state, vncPort); - } catch (final LibvirtException e) { - return new CheckVirtualMachineAnswer(cmd, e.getMessage()); - } - } - public String networkUsage(final String privateIpAddress, final String option, final String vif) { final Script getUsage = new Script(_routerProxyPath, s_logger); getUsage.add("netusage.sh"); @@ -3719,7 +3674,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return _storagePoolMgr; } - protected synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException, + public synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException { String isoXml = null; if (isoPath != null && isAttach) { @@ -4948,4 +4903,4 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } return device; } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d216f1a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java new file mode 100644 index 0000000..4380b66 --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java @@ -0,0 +1,52 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.hypervisor.kvm.resource.wrapper; + +import java.net.URISyntaxException; + +import org.libvirt.Connect; +import org.libvirt.LibvirtException; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.AttachIsoCommand; +import com.cloud.exception.InternalErrorException; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.resource.CommandWrapper; + +public final class LibvirtAttachIsoCommandWrapper extends CommandWrapper<AttachIsoCommand, Answer, LibvirtComputingResource> { + + @Override + public Answer execute(final AttachIsoCommand command, final LibvirtComputingResource libvirtComputingResource) { + try { + final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + + final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + libvirtComputingResource.attachOrDetachISO(conn, command.getVmName(), command.getIsoPath(), command.isAttach()); + } catch (final LibvirtException e) { + return new Answer(command, false, e.toString()); + } catch (final URISyntaxException e) { + return new Answer(command, false, e.toString()); + } catch (final InternalErrorException e) { + return new Answer(command, false, e.toString()); + } + + return new Answer(command); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d216f1a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java new file mode 100644 index 0000000..3ad3300 --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java @@ -0,0 +1,51 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.hypervisor.kvm.resource.wrapper; + +import org.libvirt.Connect; +import org.libvirt.LibvirtException; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.CheckVirtualMachineAnswer; +import com.cloud.agent.api.CheckVirtualMachineCommand; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.resource.CommandWrapper; +import com.cloud.vm.VirtualMachine.PowerState; + +public final class LibvirtCheckVirtualMachineCommandWrapper extends CommandWrapper<CheckVirtualMachineCommand, Answer, LibvirtComputingResource> { + + @Override + public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) { + try { + final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + + final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName()); + Integer vncPort = null; + if (state == PowerState.PowerOn) { + vncPort = libvirtComputingResource.getVncPort(conn, command.getVmName()); + } + + return new CheckVirtualMachineAnswer(command, state, vncPort); + } catch (final LibvirtException e) { + return new CheckVirtualMachineAnswer(command, e.getMessage()); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d216f1a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java new file mode 100644 index 0000000..7fce909 --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java @@ -0,0 +1,34 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.hypervisor.kvm.resource.wrapper; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ReadyAnswer; +import com.cloud.agent.api.ReadyCommand; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.resource.CommandWrapper; + +public final class LibvirtReadyCommandWrapper extends CommandWrapper<ReadyCommand, Answer, LibvirtComputingResource> { + + @Override + public Answer execute(final ReadyCommand command, final LibvirtComputingResource libvirtComputingResource) { + return new ReadyAnswer(command); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d216f1a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java index aeac43c..ff0a8a5 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java @@ -21,7 +21,9 @@ package com.cloud.hypervisor.kvm.resource.wrapper; import java.util.Hashtable; import com.cloud.agent.api.Answer; +import com.cloud.agent.api.AttachIsoCommand; import com.cloud.agent.api.CheckHealthCommand; +import com.cloud.agent.api.CheckVirtualMachineCommand; import com.cloud.agent.api.Command; import com.cloud.agent.api.GetHostStatsCommand; import com.cloud.agent.api.GetVmDiskStatsCommand; @@ -29,6 +31,7 @@ import com.cloud.agent.api.GetVmStatsCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationCommand; +import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.StopCommand; @@ -64,6 +67,9 @@ public class LibvirtRequestWrapper extends RequestWrapper { linbvirtCommands.put(PrepareForMigrationCommand.class, new LibvirtPrepareForMigrationCommandWrapper()); linbvirtCommands.put(MigrateCommand.class, new LibvirtMigrateCommandWrapper()); linbvirtCommands.put(PingTestCommand.class, new LibvirtPingTestCommandWrapper()); + linbvirtCommands.put(CheckVirtualMachineCommand.class, new LibvirtCheckVirtualMachineCommandWrapper()); + linbvirtCommands.put(ReadyCommand.class, new LibvirtReadyCommandWrapper()); + linbvirtCommands.put(AttachIsoCommand.class, new LibvirtAttachIsoCommandWrapper()); resources.put(LibvirtComputingResource.class, linbvirtCommands); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d216f1a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java ---------------------------------------------------------------------- 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 50c8ca6..f122a2a 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 @@ -30,6 +30,7 @@ import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -63,13 +64,16 @@ import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.cloud.agent.api.Answer; +import com.cloud.agent.api.AttachIsoCommand; import com.cloud.agent.api.CheckHealthCommand; +import com.cloud.agent.api.CheckVirtualMachineCommand; import com.cloud.agent.api.GetHostStatsCommand; import com.cloud.agent.api.GetVmDiskStatsCommand; import com.cloud.agent.api.GetVmStatsCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationCommand; +import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.StopCommand; @@ -78,6 +82,7 @@ import com.cloud.agent.api.to.DiskTO; import com.cloud.agent.api.to.NicTO; import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource; +import com.cloud.exception.InternalErrorException; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef; import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtConnectionWrapper; @@ -88,6 +93,7 @@ import com.cloud.storage.Volume; import com.cloud.template.VirtualMachineTemplate.BootloaderType; import com.cloud.utils.Pair; import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachine.PowerState; @RunWith(PowerMockRunner.class) public class LibvirtComputingResourceTest { @@ -774,4 +780,194 @@ public class LibvirtComputingResourceTest { final Answer answer = wrapper.execute(command, libvirtComputingResource); assertFalse(answer.getResult()); } + + @Test + public void testCheckVirtualMachineCommand() { + final Connect conn = Mockito.mock(Connect.class); + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final String vmName = "Test"; + final CheckVirtualMachineCommand command = new CheckVirtualMachineCommand(vmName); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.getConnectionByVmName(vmName)).thenReturn(conn); + } 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); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(vmName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testExceptionCheckVirtualMachineCommand() { + final Connect conn = Mockito.mock(Connect.class); + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final String vmName = "Test"; + final CheckVirtualMachineCommand command = new CheckVirtualMachineCommand(vmName); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.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)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(vmName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @Test + public void testReadyCommand() { + final ReadyCommand command = new ReadyCommand(1l); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + } + + @Test + public void testAttachIsoCommand() { + final Connect conn = Mockito.mock(Connect.class); + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final String vmName = "Test"; + final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.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)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(vmName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testAttachIsoCommandLibvirtException() { + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final String vmName = "Test"; + final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.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)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(vmName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testAttachIsoCommandURISyntaxException() { + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final String vmName = "Test"; + final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.getConnectionByVmName(vmName)).thenThrow(URISyntaxException.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)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(vmName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testAttachIsoCommandInternalErrorException() { + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final String vmName = "Test"; + final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.getConnectionByVmName(vmName)).thenThrow(InternalErrorException.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)).getLibvirtConnectionWrapper(); + try { + verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(vmName); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } } \ No newline at end of file
