Refactoring the CitrixCheckVirtualMachineCommand toc ope with new design

- Added basic tests
  - Added tests for RebootRouterCommand
  - Removed the code from CitrixResourceBase that has already been refactored
This commit is contained in:
wilderrodrigues 2015-03-24 11:54:21 +01:00
parent f2fab5c8c3
commit cb07b0ba08
4 changed files with 92 additions and 26 deletions

View File

@ -189,6 +189,7 @@ import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource;
import com.cloud.exception.InternalErrorException;
import com.cloud.host.Host.Type;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.hypervisor.xenserver.resource.wrapper.CitrixRequestWrapper;
import com.cloud.network.Networks;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.Networks.IsolationType;
@ -426,33 +427,20 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
@Override
public Answer executeRequest(final Command cmd) {
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
try {
final Answer answer = wrapper.execute(cmd, this);
return answer;
} catch (final Exception e) {
// Ignore it for now. Just removing the command that have already been
// replaced by the new code.
}
final Class<? extends Command> clazz = cmd.getClass();
if (clazz == CreateCommand.class) {
return execute((CreateCommand)cmd);
} else if (cmd instanceof NetworkElementCommand) {
if (cmd instanceof NetworkElementCommand) {
return _vrResource.executeRequest((NetworkElementCommand)cmd);
} else if (clazz == CheckConsoleProxyLoadCommand.class) {
return execute((CheckConsoleProxyLoadCommand)cmd);
} else if (clazz == WatchConsoleProxyLoadCommand.class) {
return execute((WatchConsoleProxyLoadCommand)cmd);
} else if (clazz == ReadyCommand.class) {
return execute((ReadyCommand)cmd);
} else if (clazz == GetHostStatsCommand.class) {
return execute((GetHostStatsCommand)cmd);
} else if (clazz == GetVmStatsCommand.class) {
return execute((GetVmStatsCommand)cmd);
} else if (clazz == GetVmDiskStatsCommand.class) {
return execute((GetVmDiskStatsCommand)cmd);
} else if (clazz == CheckHealthCommand.class) {
return execute((CheckHealthCommand)cmd);
} else if (clazz == StopCommand.class) {
return execute((StopCommand)cmd);
} else if (clazz == RebootRouterCommand.class) {
return execute((RebootRouterCommand)cmd);
} else if (clazz == RebootCommand.class) {
return execute((RebootCommand)cmd);
} else if (clazz == CheckVirtualMachineCommand.class) {
return execute((CheckVirtualMachineCommand)cmd);
} else if (clazz == PrepareForMigrationCommand.class) {
return execute((PrepareForMigrationCommand)cmd);
} else if (clazz == MigrateCommand.class) {
@ -2621,7 +2609,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
return vmStates;
}
protected PowerState getVmState(final Connection conn, final String vmName) {
public PowerState getVmState(final Connection conn, final String vmName) {
int retry = 3;
while (retry-- > 0) {
try {

View File

@ -0,0 +1,48 @@
//
// 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.xenserver.resource.wrapper;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CheckVirtualMachineAnswer;
import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
import com.cloud.resource.CommandWrapper;
import com.cloud.vm.VirtualMachine.PowerState;
import com.xensource.xenapi.Connection;
public final class CitrixCheckVirtualMachineCommandWrapper extends CommandWrapper<CheckVirtualMachineCommand, Answer, CitrixResourceBase> {
private static final Logger s_logger = Logger.getLogger(CitrixCheckVirtualMachineCommandWrapper.class);
@Override
public Answer execute(final CheckVirtualMachineCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final String vmName = command.getVmName();
final PowerState powerState = citrixResourceBase.getVmState(conn, vmName);
final Integer vncPort = null;
if (powerState == PowerState.PowerOn) {
s_logger.debug("3. The VM " + vmName + " is in Running state");
}
return new CheckVirtualMachineAnswer(command, powerState, vncPort);
}
}

View File

@ -23,6 +23,7 @@ import java.util.Hashtable;
import com.cloud.agent.api.Answer;
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;
@ -67,6 +68,7 @@ public class CitrixRequestWrapper extends RequestWrapper {
map.put(CheckHealthCommand.class, new CitrixCheckHealthCommandWrapper());
map.put(StopCommand.class, new CitrixStopCommandWrapper());
map.put(RebootCommand.class, new CitrixRebootCommandWrapper());
map.put(CheckVirtualMachineCommand.class, new CitrixCheckVirtualMachineCommandWrapper());
}
public static CitrixRequestWrapper getInstance() {

View File

@ -16,12 +16,14 @@ import org.mockito.runners.MockitoJUnitRunner;
import com.cloud.agent.api.Answer;
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;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.RebootAnswer;
import com.cloud.agent.api.RebootCommand;
import com.cloud.agent.api.RebootRouterCommand;
import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
@ -182,6 +184,32 @@ public class CitrixRequestWrapperTest {
assertFalse(answer.getResult());
}
@Test
public void testRebootCommand() {
final RebootCommand rebootCommand = new RebootCommand("Test");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(rebootCommand, citrixResourceBase);
verify(citrixResourceBase, times(1)).getConnection();
assertFalse(answer.getResult());
}
@Test
public void testCheckVirtualMachineCommandCommand() {
final CheckVirtualMachineCommand statsCommand = new CheckVirtualMachineCommand("Test");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(statsCommand, citrixResourceBase);
assertTrue(answer.getResult());
}
}
class NotAValidCommand extends Command {