Refactoring the GetVmStatsCommand

- Added basic tests
This commit is contained in:
wilderrodrigues 2015-03-24 10:52:31 +01:00
parent 0e51379da7
commit 8f04108e23
4 changed files with 101 additions and 2 deletions

View File

@ -2358,7 +2358,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
protected HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final GetVmStatsCommand cmd, final List<String> vmUUIDs, final String hostGuid) {
public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final GetVmStatsCommand cmd, final List<String> vmUUIDs, final String hostGuid) {
final HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
for (final String vmUUID : vmUUIDs) {
@ -6674,7 +6674,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
return false;
}
protected VM getVM(final Connection conn, final String vmName) {
public VM getVM(final Connection conn, final String vmName) {
// Look up VMs with the specified name
Set<VM> vms;
try {

View File

@ -24,6 +24,7 @@ import java.util.Hashtable;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.RebootRouterCommand;
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
@ -57,6 +58,7 @@ public class CitrixRequestWrapper extends RequestWrapper {
map.put(WatchConsoleProxyLoadCommand.class, new CitrixWatchConsoleProxyLoadCommandWrapper());
map.put(ReadyCommand.class, new CitrixReadyCommandWrapper());
map.put(GetHostStatsCommand.class, new GetHostStatsCommandWrapper());
map.put(GetVmStatsCommand.class, new GetVmStatsCommandWrapper());
}
public static CitrixRequestWrapper getInstance() {

View File

@ -0,0 +1,82 @@
//
// 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetVmStatsAnswer;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.VmStatsEntry;
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 GetVmStatsCommandWrapper extends CommandWrapper<GetVmStatsCommand, Answer, CitrixResourceBase> {
private static final Logger s_logger = Logger.getLogger(GetVmStatsCommandWrapper.class);
@Override
public Answer execute(final GetVmStatsCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final List<String> vmNames = command.getVmNames();
final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
if (vmNames.size() == 0) {
return new GetVmStatsAnswer(command, vmStatsNameMap);
}
try {
// Determine the UUIDs of the requested VMs
final List<String> vmUUIDs = new ArrayList<String>();
for (final String vmName : vmNames) {
final VM vm = citrixResourceBase.getVM(conn, vmName);
vmUUIDs.add(vm.getUuid(conn));
}
final HashMap<String, VmStatsEntry> vmStatsUUIDMap = citrixResourceBase.getVmStats(conn, command, vmUUIDs, command.getHostGuid());
if (vmStatsUUIDMap == null) {
return new GetVmStatsAnswer(command, vmStatsNameMap);
}
for (final Map.Entry<String,VmStatsEntry>entry : vmStatsUUIDMap.entrySet()) {
vmStatsNameMap.put(vmNames.get(vmUUIDs.indexOf(entry.getKey())), entry.getValue());
}
return new GetVmStatsAnswer(command, vmStatsNameMap);
} catch (final XenAPIException e) {
final String msg = "Unable to get VM stats" + e.toString();
s_logger.warn(msg, e);
return new GetVmStatsAnswer(command, vmStatsNameMap);
} catch (final XmlRpcException e) {
final String msg = "Unable to get VM stats" + e.getMessage();
s_logger.warn(msg, e);
return new GetVmStatsAnswer(command, vmStatsNameMap);
}
}
}

View File

@ -8,6 +8,8 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -17,6 +19,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.RebootAnswer;
import com.cloud.agent.api.RebootCommand;
@ -136,6 +139,18 @@ public class CitrixRequestWrapperTest {
assertTrue(answer.getResult());
}
@Test
public void testGetVmStatsCommandCommand() {
final GetVmStatsCommand statsCommand = new GetVmStatsCommand(new ArrayList<String>(), null, null);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(statsCommand, citrixResourceBase);
assertTrue(answer.getResult());
}
}
class NotAValidCommand extends Command {