Refactoring the LibvirtComputingResource - Adding LibvirtGetVncPortCommandWrapper - 2 unit tests added - KVM hypervisor plugin with 12.1% coverage
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/f14c7c20 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/f14c7c20 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/f14c7c20 Branch: refs/heads/CLOUDSTACK-8301 Commit: f14c7c207494c1c01d73713f6a2c4ab5452d07e9 Parents: 7319a12 Author: wilderrodrigues <[email protected]> Authored: Fri Apr 24 13:17:58 2015 +0200 Committer: wilderrodrigues <[email protected]> Committed: Wed May 6 19:20:43 2015 +0200 ---------------------------------------------------------------------- .../kvm/resource/LibvirtComputingResource.java | 16 +----- .../LibvirtGetVncPortCommandWrapper.java | 45 ++++++++++++++++ .../resource/wrapper/LibvirtRequestWrapper.java | 2 + .../resource/LibvirtComputingResourceTest.java | 57 ++++++++++++++++++++ 4 files changed, 105 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f14c7c20/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 07cb4dd..788a3e4 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 @@ -100,8 +100,6 @@ import com.cloud.agent.api.FenceAnswer; import com.cloud.agent.api.FenceCommand; import com.cloud.agent.api.GetStorageStatsAnswer; import com.cloud.agent.api.GetStorageStatsCommand; -import com.cloud.agent.api.GetVncPortAnswer; -import com.cloud.agent.api.GetVncPortCommand; import com.cloud.agent.api.HostVmStateReportEntry; import com.cloud.agent.api.MaintainAnswer; import com.cloud.agent.api.MaintainCommand; @@ -1300,9 +1298,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } try { - if (cmd instanceof GetVncPortCommand) { - return execute((GetVncPortCommand)cmd); - } else if (cmd instanceof ModifySshKeysCommand) { + if (cmd instanceof ModifySshKeysCommand) { return execute((ModifySshKeysCommand)cmd); } else if (cmd instanceof MaintainCommand) { return execute((MaintainCommand)cmd); @@ -2865,16 +2861,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return new Answer(cmd, result, ""); } - protected GetVncPortAnswer execute(final GetVncPortCommand cmd) { - try { - final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getName()); - final Integer vncPort = getVncPort(conn, cmd.getName()); - return new GetVncPortAnswer(cmd, _privateIp, 5900 + vncPort); - } catch (final LibvirtException e) { - return new GetVncPortAnswer(cmd, e.toString()); - } - } - protected MaintainAnswer execute(final MaintainCommand cmd) { return new MaintainAnswer(cmd); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f14c7c20/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java new file mode 100644 index 0000000..9b76374 --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java @@ -0,0 +1,45 @@ +// +// 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.GetVncPortAnswer; +import com.cloud.agent.api.GetVncPortCommand; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.resource.CommandWrapper; + +public final class LibvirtGetVncPortCommandWrapper extends CommandWrapper<GetVncPortCommand, Answer, LibvirtComputingResource> { + + @Override + public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) { + try { + final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + + final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getName()); + final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName()); + return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort); + } catch (final LibvirtException e) { + return new GetVncPortAnswer(command, e.toString()); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f14c7c20/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 72d3a87..daa5a0c 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 @@ -29,6 +29,7 @@ import com.cloud.agent.api.Command; import com.cloud.agent.api.GetHostStatsCommand; import com.cloud.agent.api.GetVmDiskStatsCommand; import com.cloud.agent.api.GetVmStatsCommand; +import com.cloud.agent.api.GetVncPortCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationCommand; @@ -76,6 +77,7 @@ public class LibvirtRequestWrapper extends RequestWrapper { linbvirtCommands.put(AttachVolumeCommand.class, new LibvirtAttachVolumeCommandWrapper()); linbvirtCommands.put(WatchConsoleProxyLoadCommand.class, new LibvirtWatchConsoleProxyLoadCommandWrapper()); linbvirtCommands.put(CheckConsoleProxyLoadCommand.class, new LibvirtCheckConsoleProxyLoadCommandWrapper()); + linbvirtCommands.put(GetVncPortCommand.class, new LibvirtGetVncPortCommandWrapper()); resources.put(LibvirtComputingResource.class, linbvirtCommands); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f14c7c20/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 8f56ee0..09a9e36 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 @@ -71,6 +71,7 @@ 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.GetVncPortCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationCommand; @@ -1144,4 +1145,60 @@ public class LibvirtComputingResourceTest { final Answer answer = wrapper.execute(command, libvirtComputingResource); assertFalse(answer.getResult()); } + + @Test + public void testGetVncPortCommand() { + final Connect conn = Mockito.mock(Connect.class); + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final GetVncPortCommand command = new GetVncPortCommand(1l, "host"); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getName())).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(command.getName()); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testGetVncPortCommandLibvirtException() { + final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class); + + final GetVncPortCommand command = new GetVncPortCommand(1l, "host"); + + when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper); + try { + when(libvirtConnectionWrapper.getConnectionByVmName(command.getName())).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(command.getName()); + } catch (final LibvirtException e) { + fail(e.getMessage()); + } + } } \ No newline at end of file
