Refactoring the CitrixRebootCommand in order to have the new design applied

- Added basic tests
  - Changed the way RebootCommand gets called from RebootRouterCommand
  - Made a couple of methods public in the CitrixResourceBase and its subclasses
This commit is contained in:
wilderrodrigues 2015-03-24 11:39:29 +01:00
parent 9e41b8051e
commit f2fab5c8c3
7 changed files with 81 additions and 17 deletions

View File

@ -3228,7 +3228,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
void rebootVM(final Connection conn, final VM vm, final String vmName) throws Exception {
public void rebootVM(final Connection conn, final VM vm, final String vmName) throws Exception {
Task task = null;
try {
task = vm.cleanRebootAsync(conn);

View File

@ -80,7 +80,7 @@ public class XenServer56Resource extends CitrixResourceBase {
}
@Override
protected void disableVlanNetwork(final Connection conn, final Network network) {
public void disableVlanNetwork(final Connection conn, final Network network) {
try {
final Network.Record networkr = network.getRecord(conn);
if (!networkr.nameLabel.startsWith("VLAN")) {

View File

@ -94,7 +94,7 @@ public class XenServer620SP1Resource extends XenServer620Resource {
}
@Override
protected HashMap<String, HashMap<String, VgpuTypesInfo>> getGPUGroupDetails(final Connection conn) throws XenAPIException, XmlRpcException {
public HashMap<String, HashMap<String, VgpuTypesInfo>> getGPUGroupDetails(final Connection conn) throws XenAPIException, XmlRpcException {
final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = new HashMap<String, HashMap<String, VgpuTypesInfo>>();
final Host host = Host.getByUuid(conn, _host.getUuid());
final Set<PGPU> pgpus = host.getPGPUs(conn);

View File

@ -0,0 +1,68 @@
//
// 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 java.util.Set;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.RebootAnswer;
import com.cloud.agent.api.RebootCommand;
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
import com.cloud.resource.CommandWrapper;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Types.XenAPIException;
import com.xensource.xenapi.VM;
public final class CitrixRebootCommandWrapper extends CommandWrapper<RebootCommand, Answer, CitrixResourceBase> {
private static final Logger s_logger = Logger.getLogger(CitrixRebootCommandWrapper.class);
@Override
public Answer execute(final RebootCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
s_logger.debug("7. The VM " + command.getVmName() + " is in Starting state");
try {
Set<VM> vms = null;
try {
vms = VM.getByNameLabel(conn, command.getVmName());
} catch (final XenAPIException e0) {
s_logger.debug("getByNameLabel failed " + e0.toString());
return new RebootAnswer(command, "getByNameLabel failed " + e0.toString(), false);
} catch (final Exception e0) {
s_logger.debug("getByNameLabel failed " + e0.getMessage());
return new RebootAnswer(command, "getByNameLabel failed", false);
}
for (final VM vm : vms) {
try {
citrixResourceBase.rebootVM(conn, vm, vm.getNameLabel(conn));
} catch (final Exception e) {
final String msg = e.toString();
s_logger.warn(msg, e);
return new RebootAnswer(command, msg, false);
}
}
return new RebootAnswer(command, "reboot succeeded", true);
} finally {
s_logger.debug("8. The VM " + command.getVmName() + " is in Running state");
}
}
}

View File

@ -20,7 +20,6 @@
package com.cloud.hypervisor.xenserver.resource.wrapper;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.RebootAnswer;
import com.cloud.agent.api.RebootCommand;
import com.cloud.agent.api.RebootRouterCommand;
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
@ -33,7 +32,10 @@ public final class CitrixRebootRouterCommandWrapper extends CommandWrapper<Reboo
public Answer execute(final RebootRouterCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final RebootAnswer answer = citrixResourceBase.execute((RebootCommand)command);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
final RebootCommand rebootCommand = new RebootCommand(command.getVmName());
final Answer answer = wrapper.execute(rebootCommand, citrixResourceBase);
if (answer.getResult()) {
final String cnct = citrixResourceBase.connect(conn, command.getVmName(), command.getPrivateIpAddress());

View File

@ -28,6 +28,7 @@ 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.RebootCommand;
import com.cloud.agent.api.RebootRouterCommand;
import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
@ -65,6 +66,7 @@ public class CitrixRequestWrapper extends RequestWrapper {
map.put(GetVmDiskStatsCommand.class, new CitrixGetVmDiskStatsCommandWrapper());
map.put(CheckHealthCommand.class, new CitrixCheckHealthCommandWrapper());
map.put(StopCommand.class, new CitrixStopCommandWrapper());
map.put(RebootCommand.class, new CitrixRebootCommandWrapper());
}
public static CitrixRequestWrapper getInstance() {

View File

@ -1,10 +1,8 @@
package com.cloud.hypervisor.xenserver.resource.wrapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ -24,7 +22,6 @@ 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;
@ -64,21 +61,16 @@ public class CitrixRequestWrapperTest {
@Test
public void testExecuteRebootRouterCommand() {
final RebootRouterCommand rebootCommand = new RebootRouterCommand("", "");
final RebootRouterCommand rebootRouterCommand = new RebootRouterCommand("Test", "127.0.0.1");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
doReturn(rebootAnswer).when(citrixResourceBase).execute((RebootCommand)rebootCommand);
doReturn(false).when(rebootAnswer).getResult();
final Answer answer = wrapper.execute(rebootRouterCommand, citrixResourceBase);
final Answer answer = wrapper.execute(rebootCommand, citrixResourceBase);
verify(citrixResourceBase, times(2)).getConnection();
verify(citrixResourceBase, times(1)).getConnection();
verify(citrixResourceBase, times(1)).execute((RebootCommand)rebootCommand);
assertFalse(rebootAnswer.getResult());
assertEquals(answer, rebootAnswer);
assertFalse(answer.getResult());
}
@Test