Skip to content

Commit 240bcb8

Browse files
Refactoring the GetGPUStatsCommand wrapper in order to cope with new design
- Unit tests added: 100% coverage
1 parent bcee728 commit 240bcb8

7 files changed

Lines changed: 181 additions & 37 deletions

File tree

‎core/src/com/cloud/agent/api/GetGPUStatsAnswer.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ public class GetGPUStatsAnswer extends Answer {
2828

2929
private HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails;
3030

31-
public GetGPUStatsAnswer(GetGPUStatsCommand cmd, HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails) {
31+
public GetGPUStatsAnswer(final GetGPUStatsCommand cmd, final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails) {
3232
super(cmd);
3333
this.groupDetails = groupDetails;
3434
}
3535

36+
public GetGPUStatsAnswer(final GetGPUStatsCommand cmd, final boolean success, final String details) {
37+
super(cmd, success, details);
38+
}
39+
3640
public HashMap<String, HashMap<String, VgpuTypesInfo>> getGroupDetails() {
37-
return this.groupDetails;
41+
return groupDetails;
3842
}
39-
}
43+
}

‎plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServer620SP1Resource.java‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
import org.apache.log4j.Logger;
2929
import org.apache.xmlrpc.XmlRpcException;
3030

31-
import com.cloud.agent.api.Answer;
32-
import com.cloud.agent.api.Command;
33-
import com.cloud.agent.api.GetGPUStatsAnswer;
34-
import com.cloud.agent.api.GetGPUStatsCommand;
3531
import com.cloud.agent.api.StartCommand;
3632
import com.cloud.agent.api.StartupRoutingCommand;
3733
import com.cloud.agent.api.VgpuTypesInfo;
@@ -52,28 +48,6 @@ public class XenServer620SP1Resource extends XenServer620Resource {
5248

5349
private static final Logger s_logger = Logger.getLogger(XenServer620SP1Resource.class);
5450

55-
@Override
56-
public Answer executeRequest(final Command cmd) {
57-
final Class<? extends Command> clazz = cmd.getClass();
58-
if (clazz == GetGPUStatsCommand.class) {
59-
return execute((GetGPUStatsCommand) cmd);
60-
} else {
61-
return super.executeRequest(cmd);
62-
}
63-
}
64-
65-
protected GetGPUStatsAnswer execute(final GetGPUStatsCommand cmd) {
66-
final Connection conn = getConnection();
67-
HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = new HashMap<String, HashMap<String, VgpuTypesInfo>>();
68-
try {
69-
groupDetails = getGPUGroupDetails(conn);
70-
} catch (final Exception e) {
71-
final String msg = "Unable to get GPU stats" + e.toString();
72-
s_logger.warn(msg, e);
73-
}
74-
return new GetGPUStatsAnswer(cmd, groupDetails);
75-
}
76-
7751
@Override
7852
protected void fillHostInfo(final Connection conn, final StartupRoutingCommand cmd) {
7953
super.fillHostInfo(conn, cmd);

‎plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/Xenserver625Resource.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@
4141

4242
@Local(value=ServerResource.class)
4343
public class Xenserver625Resource extends XenServerResourceNewBase {
44-
private static final Logger s_logger = Logger.getLogger(Xenserver625Resource.class);
4544

46-
public Xenserver625Resource() {
47-
super();
48-
}
45+
private static final Logger s_logger = Logger.getLogger(Xenserver625Resource.class);
4946

5047
@Override
5148
protected List<File> getPatchFiles() {

‎plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import java.util.Hashtable;
2323

24-
import org.apache.log4j.Logger;
25-
2624
import com.cloud.agent.api.Answer;
2725
import com.cloud.agent.api.AttachIsoCommand;
2826
import com.cloud.agent.api.AttachVolumeCommand;
@@ -38,6 +36,7 @@
3836
import com.cloud.agent.api.DeleteStoragePoolCommand;
3937
import com.cloud.agent.api.DeleteVMSnapshotCommand;
4038
import com.cloud.agent.api.FenceCommand;
39+
import com.cloud.agent.api.GetGPUStatsCommand;
4140
import com.cloud.agent.api.GetHostStatsCommand;
4241
import com.cloud.agent.api.GetStorageStatsCommand;
4342
import com.cloud.agent.api.GetVmDiskStatsCommand;
@@ -94,8 +93,6 @@
9493

9594
public class CitrixRequestWrapper extends RequestWrapper {
9695

97-
private static final Logger s_logger = Logger.getLogger(CitrixRequestWrapper.class);
98-
9996
private static CitrixRequestWrapper instance;
10097

10198
static {
@@ -186,6 +183,11 @@ private void init() {
186183
final Hashtable<Class<? extends Command>, CommandWrapper> xenServer56P1Commands = new Hashtable<Class<? extends Command>, CommandWrapper>();
187184
xenServer56P1Commands.put(FenceCommand.class, new XenServer56FP1FenceCommandWrapper());
188185
resources.put(XenServer56FP1Resource.class, xenServer56P1Commands);
186+
187+
// XenServer620SP1Resource commands
188+
final Hashtable<Class<? extends Command>, CommandWrapper> xenServer620SP1Commands = new Hashtable<Class<? extends Command>, CommandWrapper>();
189+
xenServer620SP1Commands.put(GetGPUStatsCommand.class, new XenServer620SP1GetGPUStatsCommandWrapper());
190+
resources.put(XenServer56FP1Resource.class, xenServer620SP1Commands);
189191
}
190192

191193
public static CitrixRequestWrapper getInstance() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.hypervisor.xenserver.resource.wrapper;
21+
22+
import java.util.HashMap;
23+
24+
import org.apache.log4j.Logger;
25+
26+
import com.cloud.agent.api.Answer;
27+
import com.cloud.agent.api.GetGPUStatsAnswer;
28+
import com.cloud.agent.api.GetGPUStatsCommand;
29+
import com.cloud.agent.api.VgpuTypesInfo;
30+
import com.cloud.hypervisor.xenserver.resource.XenServer620SP1Resource;
31+
import com.cloud.resource.CommandWrapper;
32+
import com.xensource.xenapi.Connection;
33+
34+
public final class XenServer620SP1GetGPUStatsCommandWrapper extends CommandWrapper<GetGPUStatsCommand, Answer, XenServer620SP1Resource> {
35+
36+
private static final Logger s_logger = Logger.getLogger(XenServer620SP1GetGPUStatsCommandWrapper.class);
37+
38+
@Override
39+
public Answer execute(final GetGPUStatsCommand command, final XenServer620SP1Resource xenServer620SP1Resource) {
40+
final Connection conn = xenServer620SP1Resource.getConnection();
41+
HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = new HashMap<String, HashMap<String, VgpuTypesInfo>>();
42+
try {
43+
groupDetails = xenServer620SP1Resource.getGPUGroupDetails(conn);
44+
} catch (final Exception e) {
45+
final String msg = "Unable to get GPU stats" + e.toString();
46+
s_logger.warn(msg, e);
47+
return new GetGPUStatsAnswer(command, false, msg);
48+
}
49+
return new GetGPUStatsAnswer(command, groupDetails);
50+
}
51+
}

‎plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ public void testMaintainCommand() {
589589
when(client.execute("host.get_by_uuid", new Object[] { "befc4dcd", uuid })).thenReturn(spiedMap);
590590
PowerMockito.when(conn, "dispatch", "host.get_by_uuid", params).thenReturn(spiedMap);
591591
} catch (final Exception e) {
592+
fail(e.getMessage());
592593
}
593594

594595
// try {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.hypervisor.xenserver.resource.wrapper;
18+
19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
23+
import static org.mockito.Mockito.times;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.when;
26+
27+
import java.util.HashMap;
28+
29+
import org.apache.xmlrpc.XmlRpcException;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.mockito.Mock;
33+
import org.mockito.Mockito;
34+
import org.powermock.modules.junit4.PowerMockRunner;
35+
36+
import com.cloud.agent.api.Answer;
37+
import com.cloud.agent.api.GetGPUStatsCommand;
38+
import com.cloud.agent.api.VgpuTypesInfo;
39+
import com.cloud.hypervisor.xenserver.resource.XenServer620SP1Resource;
40+
import com.cloud.utils.exception.CloudRuntimeException;
41+
import com.xensource.xenapi.Connection;
42+
import com.xensource.xenapi.Types.XenAPIException;
43+
44+
@RunWith(PowerMockRunner.class)
45+
public class XenServer620SP1WrapperTest {
46+
47+
@Mock
48+
private XenServer620SP1Resource xenServer620SP1Resource;
49+
50+
@Test
51+
public void testGetGPUStatsCommand() {
52+
final String guuid = "246a5b75-05ed-4bbc-a171-2d1fe94a1b0e";
53+
54+
final Connection conn = Mockito.mock(Connection.class);
55+
56+
final GetGPUStatsCommand gpuStats = new GetGPUStatsCommand(guuid, "xen");
57+
58+
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
59+
assertNotNull(wrapper);
60+
61+
when(xenServer620SP1Resource.getConnection()).thenReturn(conn);
62+
try {
63+
when(xenServer620SP1Resource.getGPUGroupDetails(conn)).thenReturn(new HashMap<String, HashMap<String, VgpuTypesInfo>>());
64+
} catch (final XenAPIException e) {
65+
fail(e.getMessage());
66+
} catch (final XmlRpcException e) {
67+
fail(e.getMessage());
68+
}
69+
70+
final Answer answer = wrapper.execute(gpuStats, xenServer620SP1Resource);
71+
verify(xenServer620SP1Resource, times(1)).getConnection();
72+
try {
73+
verify(xenServer620SP1Resource, times(1)).getGPUGroupDetails(conn);
74+
} catch (final XenAPIException e) {
75+
fail(e.getMessage());
76+
} catch (final XmlRpcException e) {
77+
fail(e.getMessage());
78+
}
79+
80+
assertTrue(answer.getResult());
81+
}
82+
83+
@Test
84+
public void testGetGPUStatsCommandFailure() {
85+
final String guuid = "246a5b75-05ed-4bbc-a171-2d1fe94a1b0e";
86+
87+
final Connection conn = Mockito.mock(Connection.class);
88+
89+
final GetGPUStatsCommand gpuStats = new GetGPUStatsCommand(guuid, "xen");
90+
91+
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
92+
assertNotNull(wrapper);
93+
94+
when(xenServer620SP1Resource.getConnection()).thenReturn(conn);
95+
try {
96+
when(xenServer620SP1Resource.getGPUGroupDetails(conn)).thenThrow(new CloudRuntimeException("Failed!"));
97+
} catch (final XenAPIException e) {
98+
fail(e.getMessage());
99+
} catch (final XmlRpcException e) {
100+
fail(e.getMessage());
101+
}
102+
103+
final Answer answer = wrapper.execute(gpuStats, xenServer620SP1Resource);
104+
verify(xenServer620SP1Resource, times(1)).getConnection();
105+
try {
106+
verify(xenServer620SP1Resource, times(1)).getGPUGroupDetails(conn);
107+
} catch (final XenAPIException e) {
108+
fail(e.getMessage());
109+
} catch (final XmlRpcException e) {
110+
fail(e.getMessage());
111+
}
112+
113+
assertFalse(answer.getResult());
114+
}
115+
}

0 commit comments

Comments
 (0)