Refactoring the LibvirtComputingResource - Adding LibvirtPingTestCommandWrapper - 3 unit tests added - KVM hypervisor plugin with 11.2% coverage
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/be1e5176 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/be1e5176 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/be1e5176 Branch: refs/heads/CLOUDSTACK-8301 Commit: be1e5176156d9b7eb78e53c703b48d679ad52f56 Parents: 28e5546 Author: wilderrodrigues <[email protected]> Authored: Thu Apr 23 13:57:13 2015 +0200 Committer: wilderrodrigues <[email protected]> Committed: Wed May 6 19:20:42 2015 +0200 ---------------------------------------------------------------------- .../kvm/resource/LibvirtComputingResource.java | 44 ++----------- .../wrapper/LibvirtPingTestCommandWrapper.java | 65 ++++++++++++++++++++ .../resource/wrapper/LibvirtRequestWrapper.java | 2 + .../resource/LibvirtComputingResourceTest.java | 39 ++++++++++-- 4 files changed, 107 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/be1e5176/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 893f82d..1c7aa24 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 @@ -135,7 +135,6 @@ import com.cloud.agent.api.OvsVpcRoutingPolicyConfigCommand; import com.cloud.agent.api.PingCommand; import com.cloud.agent.api.PingRoutingCommand; import com.cloud.agent.api.PingRoutingWithNwGroupsCommand; -import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PlugNicAnswer; import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PvlanSetupCommand; @@ -405,6 +404,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return _migrateSpeed; } + public String getPingTestPath() { + return _pingTestPath; + } + private static final class KeyValueInterpreter extends OutputInterpreter { private final Map<String, String> map = new HashMap<String, String>(); @@ -1311,9 +1314,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } try { - if (cmd instanceof PingTestCommand) { - return execute((PingTestCommand)cmd); - } else if (cmd instanceof CheckVirtualMachineCommand) { + if (cmd instanceof CheckVirtualMachineCommand) { return execute((CheckVirtualMachineCommand)cmd); } else if (cmd instanceof ReadyCommand) { return execute((ReadyCommand)cmd); @@ -3029,41 +3030,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } - private Answer execute(final PingTestCommand cmd) { - String result = null; - final String computingHostIp = cmd.getComputingHostIp(); // TODO, split - // the - // command - // into 2 - // types - - if (computingHostIp != null) { - result = doPingTest(computingHostIp); - } else if (cmd.getRouterIp() != null && cmd.getPrivateIp() != null) { - result = doPingTest(cmd.getRouterIp(), cmd.getPrivateIp()); - } else { - return new Answer(cmd, false, "routerip and private ip is null"); - } - - if (result != null) { - return new Answer(cmd, false, result); - } - return new Answer(cmd); - } - - private String doPingTest(final String computingHostIp) { - final Script command = new Script(_pingTestPath, 10000, s_logger); - command.add("-h", computingHostIp); - return command.execute(); - } - - private String doPingTest(final String domRIp, final String vmIp) { - final Script command = new Script(_pingTestPath, 10000, s_logger); - command.add("-i", domRIp); - command.add("-p", vmIp); - return command.execute(); - } - public String networkUsage(final String privateIpAddress, final String option, final String vif) { final Script getUsage = new Script(_routerProxyPath, s_logger); getUsage.add("netusage.sh"); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/be1e5176/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPingTestCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPingTestCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPingTestCommandWrapper.java new file mode 100644 index 0000000..3ef9e2c --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPingTestCommandWrapper.java @@ -0,0 +1,65 @@ +// +// 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.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.PingTestCommand; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.resource.CommandWrapper; +import com.cloud.utils.script.Script; + +public final class LibvirtPingTestCommandWrapper extends CommandWrapper<PingTestCommand, Answer, LibvirtComputingResource> { + + private static final Logger s_logger = Logger.getLogger(LibvirtPingTestCommandWrapper.class); + + @Override + public Answer execute(final PingTestCommand command, final LibvirtComputingResource libvirtComputingResource) { + String result = null; + final String computingHostIp = command.getComputingHostIp(); // TODO, split the command into 2 types + + if (computingHostIp != null) { + result = doPingTest(libvirtComputingResource, computingHostIp); + } else if (command.getRouterIp() != null && command.getPrivateIp() != null) { + result = doPingTest(libvirtComputingResource, command.getRouterIp(), command.getPrivateIp()); + } else { + return new Answer(command, false, "routerip and private ip is null"); + } + + if (result != null) { + return new Answer(command, false, result); + } + return new Answer(command); + } + + protected String doPingTest(final LibvirtComputingResource libvirtComputingResource, final String computingHostIp) { + final Script command = new Script(libvirtComputingResource.getPingTestPath(), 10000, s_logger); + command.add("-h", computingHostIp); + return command.execute(); + } + + protected String doPingTest(final LibvirtComputingResource libvirtComputingResource, final String domRIp, final String vmIp) { + final Script command = new Script(libvirtComputingResource.getPingTestPath(), 10000, s_logger); + command.add("-i", domRIp); + command.add("-p", vmIp); + return command.execute(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/be1e5176/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 ee444b8..aeac43c 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 @@ -27,6 +27,7 @@ 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.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; @@ -62,6 +63,7 @@ public class LibvirtRequestWrapper extends RequestWrapper { linbvirtCommands.put(CheckHealthCommand.class, new LibvirtCheckHealthCommandWrapper()); linbvirtCommands.put(PrepareForMigrationCommand.class, new LibvirtPrepareForMigrationCommandWrapper()); linbvirtCommands.put(MigrateCommand.class, new LibvirtMigrateCommandWrapper()); + linbvirtCommands.put(PingTestCommand.class, new LibvirtPingTestCommandWrapper()); resources.put(LibvirtComputingResource.class, linbvirtCommands); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/be1e5176/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 4aa249e..50c8ca6 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 @@ -68,6 +68,7 @@ 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.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; @@ -596,7 +597,7 @@ public class LibvirtComputingResourceTest { @Test(expected = NumberFormatException.class) public void testGetHostStatsCommand() { - // A bit difficult top test due to the logger being passed and the parser itself relying on the connection. + // A bit difficult to test due to the logger being passed and the parser itself relying on the connection. // Have to spend some more time afterwards in order to refactor the wrapper itself. final String uuid = "e8d6b4d0-bc6d-4613-b8bb-cb9e0600f3c6"; @@ -611,9 +612,6 @@ public class LibvirtComputingResourceTest { @Test public void testCheckHealthCommand() { - // A bit difficult top test due to the logger being passed and the parser itself relying on the connection. - // Have to spend some more time afterwards in order to refactor the wrapper itself. - final CheckHealthCommand command = new CheckHealthCommand(); final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); @@ -743,4 +741,37 @@ public class LibvirtComputingResourceTest { fail(e.getMessage()); } } + + @Test + public void testPingTestHostIpCommand() { + final PingTestCommand command = new PingTestCommand("172.1.10.10"); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertFalse(answer.getResult()); + } + + @Test + public void testPingTestPvtIpCommand() { + final PingTestCommand command = new PingTestCommand("169.17.1.10", "192.168.10.10"); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertFalse(answer.getResult()); + } + + @Test + public void testPingOnlyOneIpCommand() { + final PingTestCommand command = new PingTestCommand("169.17.1.10", null); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertFalse(answer.getResult()); + } } \ No newline at end of file
