Refactoring the LibvirtComputingResource

- Adding LibvirtPingTestCommandWrapper
  - 3 unit tests added
  - KVM hypervisor plugin with 11.2% coverage
This commit is contained in:
wilderrodrigues 2015-04-23 13:57:13 +02:00
parent 28e55462f1
commit be1e517615
4 changed files with 107 additions and 43 deletions

View File

@ -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");

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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());
}
}