You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by bh...@apache.org on 2015/04/02 20:22:46 UTC
[05/39] git commit: updated refs/heads/master to 3e28747
Refactoring the GetHostStatsCommand
- Added basic tests
- Added test for when the key (Command) does not exist in the Map
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/0e51379d
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/0e51379d
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/0e51379d
Branch: refs/heads/master
Commit: 0e51379da72257e34fd24ae8d46df64981458347
Parents: 30e72e4
Author: wilderrodrigues <wr...@schubergphilis.com>
Authored: Tue Mar 24 10:16:16 2015 +0100
Committer: wilderrodrigues <wr...@schubergphilis.com>
Committed: Tue Mar 31 10:38:25 2015 +0200
----------------------------------------------------------------------
.../xenserver/resource/CitrixResourceBase.java | 2 +-
.../resource/wrapper/CitrixRequestWrapper.java | 6 +++
.../wrapper/GetHostStatsCommandWrapper.java | 48 ++++++++++++++++++++
.../wrapper/CitrixRequestWrapperTest.java | 36 +++++++++++++++
4 files changed, 91 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0e51379d/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
index 850cbb3..42d37d5 100644
--- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
@@ -2250,7 +2250,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
- protected HostStatsEntry getHostStats(final Connection conn, final GetHostStatsCommand cmd, final String hostGuid, final long hostId) {
+ public HostStatsEntry getHostStats(final Connection conn, final GetHostStatsCommand cmd, final String hostGuid, final long hostId) {
final HostStatsEntry hostStats = new HostStatsEntry(hostId, 0, 0, 0, "host", 0, 0, 0, 0);
final Object[] rrdData = getRRDData(conn, 1); // call rrd method with 1 for host
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0e51379d/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java
index f2a921c..98989ab 100644
--- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java
@@ -23,6 +23,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.ReadyCommand;
import com.cloud.agent.api.RebootRouterCommand;
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
@@ -55,6 +56,7 @@ public class CitrixRequestWrapper extends RequestWrapper {
map.put(CheckConsoleProxyLoadCommand.class, new CitrixCheckConsoleProxyLoadCommandWrapper());
map.put(WatchConsoleProxyLoadCommand.class, new CitrixWatchConsoleProxyLoadCommandWrapper());
map.put(ReadyCommand.class, new CitrixReadyCommandWrapper());
+ map.put(GetHostStatsCommand.class, new GetHostStatsCommandWrapper());
}
public static CitrixRequestWrapper getInstance() {
@@ -66,6 +68,10 @@ public class CitrixRequestWrapper extends RequestWrapper {
@SuppressWarnings("unchecked")
final CommandWrapper<Command, Answer, ServerResource> commandWrapper = map.get(command.getClass());
+ if (commandWrapper == null) {
+ throw new NullPointerException("No key found for '" + command.getClass() + "' in the Map!");
+ }
+
return commandWrapper.execute(command, serverResource);
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0e51379d/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetHostStatsCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetHostStatsCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetHostStatsCommandWrapper.java
new file mode 100644
index 0000000..d605367
--- /dev/null
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetHostStatsCommandWrapper.java
@@ -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.GetHostStatsAnswer;
+import com.cloud.agent.api.GetHostStatsCommand;
+import com.cloud.agent.api.HostStatsEntry;
+import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
+import com.cloud.resource.CommandWrapper;
+import com.xensource.xenapi.Connection;
+
+public final class GetHostStatsCommandWrapper extends CommandWrapper<GetHostStatsCommand, Answer, CitrixResourceBase> {
+
+ private static final Logger s_logger = Logger.getLogger(GetHostStatsCommandWrapper.class);
+
+ @Override
+ public Answer execute(final GetHostStatsCommand command, final CitrixResourceBase citrixResourceBase) {
+ final Connection conn = citrixResourceBase.getConnection();
+ try {
+ final HostStatsEntry hostStats = citrixResourceBase.getHostStats(conn, command, command.getHostGuid(), command.getHostId());
+ return new GetHostStatsAnswer(command, hostStats);
+ } catch (final Exception e) {
+ final String msg = "Unable to get Host stats" + e.toString();
+ s_logger.warn(msg, e);
+ return new GetHostStatsAnswer(command, null);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0e51379d/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java
index 3e7fb57..027a5fd 100644
--- a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java
+++ b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java
@@ -3,6 +3,7 @@ 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;
@@ -14,6 +15,8 @@ import org.mockito.Mock;
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.ReadyCommand;
import com.cloud.agent.api.RebootAnswer;
import com.cloud.agent.api.RebootCommand;
@@ -42,6 +45,18 @@ public class CitrixRequestWrapperTest {
}
@Test
+ public void testUnknownCommand() {
+ final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+ assertNotNull(wrapper);
+
+ try {
+ wrapper.execute(new NotAValidCommand(), citrixResourceBase);
+ } catch (final Exception e) {
+ assertTrue(e instanceof NullPointerException);
+ }
+ }
+
+ @Test
public void testExecuteRebootRouterCommand() {
final RebootRouterCommand rebootCommand = new RebootRouterCommand("", "");
@@ -109,4 +124,25 @@ public class CitrixRequestWrapperTest {
assertFalse(answer.getResult());
}
+
+ @Test
+ public void testGetHostStatsCommandCommand() {
+ final GetHostStatsCommand statsCommand = new GetHostStatsCommand(null, null, 0);
+
+ final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+ assertNotNull(wrapper);
+
+ final Answer answer = wrapper.execute(statsCommand, citrixResourceBase);
+
+ assertTrue(answer.getResult());
+ }
+}
+
+class NotAValidCommand extends Command {
+
+ @Override
+ public boolean executeInSequence() {
+ return false;
+ }
+
}
\ No newline at end of file