Refactoring the GetGPUStatsCommand wrapper in order to cope with new design

- Unit tests added: 100% coverage
This commit is contained in:
wilderrodrigues 2015-04-03 11:52:53 +02:00
parent bcee7281c6
commit 240bcb8120
7 changed files with 182 additions and 38 deletions

View File

@ -28,12 +28,16 @@ public class GetGPUStatsAnswer extends Answer {
private HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails;
public GetGPUStatsAnswer(GetGPUStatsCommand cmd, HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails) {
public GetGPUStatsAnswer(final GetGPUStatsCommand cmd, final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails) {
super(cmd);
this.groupDetails = groupDetails;
}
public HashMap<String, HashMap<String, VgpuTypesInfo>> getGroupDetails() {
return this.groupDetails;
public GetGPUStatsAnswer(final GetGPUStatsCommand cmd, final boolean success, final String details) {
super(cmd, success, details);
}
}
public HashMap<String, HashMap<String, VgpuTypesInfo>> getGroupDetails() {
return groupDetails;
}
}

View File

@ -28,10 +28,6 @@ import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.GetGPUStatsAnswer;
import com.cloud.agent.api.GetGPUStatsCommand;
import com.cloud.agent.api.StartCommand;
import com.cloud.agent.api.StartupRoutingCommand;
import com.cloud.agent.api.VgpuTypesInfo;
@ -52,28 +48,6 @@ public class XenServer620SP1Resource extends XenServer620Resource {
private static final Logger s_logger = Logger.getLogger(XenServer620SP1Resource.class);
@Override
public Answer executeRequest(final Command cmd) {
final Class<? extends Command> clazz = cmd.getClass();
if (clazz == GetGPUStatsCommand.class) {
return execute((GetGPUStatsCommand) cmd);
} else {
return super.executeRequest(cmd);
}
}
protected GetGPUStatsAnswer execute(final GetGPUStatsCommand cmd) {
final Connection conn = getConnection();
HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = new HashMap<String, HashMap<String, VgpuTypesInfo>>();
try {
groupDetails = getGPUGroupDetails(conn);
} catch (final Exception e) {
final String msg = "Unable to get GPU stats" + e.toString();
s_logger.warn(msg, e);
}
return new GetGPUStatsAnswer(cmd, groupDetails);
}
@Override
protected void fillHostInfo(final Connection conn, final StartupRoutingCommand cmd) {
super.fillHostInfo(conn, cmd);

View File

@ -41,11 +41,8 @@ import com.xensource.xenapi.VM;
@Local(value=ServerResource.class)
public class Xenserver625Resource extends XenServerResourceNewBase {
private static final Logger s_logger = Logger.getLogger(Xenserver625Resource.class);
public Xenserver625Resource() {
super();
}
private static final Logger s_logger = Logger.getLogger(Xenserver625Resource.class);
@Override
protected List<File> getPatchFiles() {

View File

@ -21,8 +21,6 @@ package com.cloud.hypervisor.xenserver.resource.wrapper;
import java.util.Hashtable;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.AttachIsoCommand;
import com.cloud.agent.api.AttachVolumeCommand;
@ -38,6 +36,7 @@ import com.cloud.agent.api.CreateVMSnapshotCommand;
import com.cloud.agent.api.DeleteStoragePoolCommand;
import com.cloud.agent.api.DeleteVMSnapshotCommand;
import com.cloud.agent.api.FenceCommand;
import com.cloud.agent.api.GetGPUStatsCommand;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetStorageStatsCommand;
import com.cloud.agent.api.GetVmDiskStatsCommand;
@ -94,8 +93,6 @@ import com.cloud.resource.ServerResource;
public class CitrixRequestWrapper extends RequestWrapper {
private static final Logger s_logger = Logger.getLogger(CitrixRequestWrapper.class);
private static CitrixRequestWrapper instance;
static {
@ -186,6 +183,11 @@ public class CitrixRequestWrapper extends RequestWrapper {
final Hashtable<Class<? extends Command>, CommandWrapper> xenServer56P1Commands = new Hashtable<Class<? extends Command>, CommandWrapper>();
xenServer56P1Commands.put(FenceCommand.class, new XenServer56FP1FenceCommandWrapper());
resources.put(XenServer56FP1Resource.class, xenServer56P1Commands);
// XenServer620SP1Resource commands
final Hashtable<Class<? extends Command>, CommandWrapper> xenServer620SP1Commands = new Hashtable<Class<? extends Command>, CommandWrapper>();
xenServer620SP1Commands.put(GetGPUStatsCommand.class, new XenServer620SP1GetGPUStatsCommandWrapper());
resources.put(XenServer56FP1Resource.class, xenServer620SP1Commands);
}
public static CitrixRequestWrapper getInstance() {

View File

@ -0,0 +1,51 @@
//
// 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.HashMap;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetGPUStatsAnswer;
import com.cloud.agent.api.GetGPUStatsCommand;
import com.cloud.agent.api.VgpuTypesInfo;
import com.cloud.hypervisor.xenserver.resource.XenServer620SP1Resource;
import com.cloud.resource.CommandWrapper;
import com.xensource.xenapi.Connection;
public final class XenServer620SP1GetGPUStatsCommandWrapper extends CommandWrapper<GetGPUStatsCommand, Answer, XenServer620SP1Resource> {
private static final Logger s_logger = Logger.getLogger(XenServer620SP1GetGPUStatsCommandWrapper.class);
@Override
public Answer execute(final GetGPUStatsCommand command, final XenServer620SP1Resource xenServer620SP1Resource) {
final Connection conn = xenServer620SP1Resource.getConnection();
HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = new HashMap<String, HashMap<String, VgpuTypesInfo>>();
try {
groupDetails = xenServer620SP1Resource.getGPUGroupDetails(conn);
} catch (final Exception e) {
final String msg = "Unable to get GPU stats" + e.toString();
s_logger.warn(msg, e);
return new GetGPUStatsAnswer(command, false, msg);
}
return new GetGPUStatsAnswer(command, groupDetails);
}
}

View File

@ -589,6 +589,7 @@ public class CitrixRequestWrapperTest {
when(client.execute("host.get_by_uuid", new Object[] { "befc4dcd", uuid })).thenReturn(spiedMap);
PowerMockito.when(conn, "dispatch", "host.get_by_uuid", params).thenReturn(spiedMap);
} catch (final Exception e) {
fail(e.getMessage());
}
// try {

View File

@ -0,0 +1,115 @@
// 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 static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import org.apache.xmlrpc.XmlRpcException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetGPUStatsCommand;
import com.cloud.agent.api.VgpuTypesInfo;
import com.cloud.hypervisor.xenserver.resource.XenServer620SP1Resource;
import com.cloud.utils.exception.CloudRuntimeException;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Types.XenAPIException;
@RunWith(PowerMockRunner.class)
public class XenServer620SP1WrapperTest {
@Mock
private XenServer620SP1Resource xenServer620SP1Resource;
@Test
public void testGetGPUStatsCommand() {
final String guuid = "246a5b75-05ed-4bbc-a171-2d1fe94a1b0e";
final Connection conn = Mockito.mock(Connection.class);
final GetGPUStatsCommand gpuStats = new GetGPUStatsCommand(guuid, "xen");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(xenServer620SP1Resource.getConnection()).thenReturn(conn);
try {
when(xenServer620SP1Resource.getGPUGroupDetails(conn)).thenReturn(new HashMap<String, HashMap<String, VgpuTypesInfo>>());
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
final Answer answer = wrapper.execute(gpuStats, xenServer620SP1Resource);
verify(xenServer620SP1Resource, times(1)).getConnection();
try {
verify(xenServer620SP1Resource, times(1)).getGPUGroupDetails(conn);
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
assertTrue(answer.getResult());
}
@Test
public void testGetGPUStatsCommandFailure() {
final String guuid = "246a5b75-05ed-4bbc-a171-2d1fe94a1b0e";
final Connection conn = Mockito.mock(Connection.class);
final GetGPUStatsCommand gpuStats = new GetGPUStatsCommand(guuid, "xen");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(xenServer620SP1Resource.getConnection()).thenReturn(conn);
try {
when(xenServer620SP1Resource.getGPUGroupDetails(conn)).thenThrow(new CloudRuntimeException("Failed!"));
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
final Answer answer = wrapper.execute(gpuStats, xenServer620SP1Resource);
verify(xenServer620SP1Resource, times(1)).getConnection();
try {
verify(xenServer620SP1Resource, times(1)).getGPUGroupDetails(conn);
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
assertFalse(answer.getResult());
}
}