cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject [8/8] git commit: updated refs/heads/master to 096d1b9
Date Sun, 05 Apr 2015 11:25:15 GMT
This closes #146

Refactoring the NetworkUsageCommand wrapper in order to cope with new design
   - Unit tests added: 100% coverage


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/096d1b93
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/096d1b93
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/096d1b93

Branch: refs/heads/master
Commit: 096d1b93b019d5232c2478ca63fc16eb6794fbe2
Parents: 02d1cdd
Author: wilderrodrigues <wrodrigues@schubergphilis.com>
Authored: Fri Apr 3 17:23:16 2015 +0200
Committer: wilderrodrigues <wrodrigues@schubergphilis.com>
Committed: Sun Apr 5 13:24:45 2015 +0200

----------------------------------------------------------------------
 .../xenserver/resource/XcpServerResource.java   |  30 -----
 .../xenserver/resource/XenServer56Resource.java |  18 ---
 .../resource/wrapper/CitrixRequestWrapper.java  |   6 +
 .../XcpServerNetworkUsageCommandWrapper.java    |  52 +++++++++
 .../resource/wrapper/XcpServerWrapperTest.java  | 116 +++++++++++++++++++
 5 files changed, 174 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/096d1b93/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XcpServerResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XcpServerResource.java
b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XcpServerResource.java
index 0cb4206..03d9bf1 100644
--- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XcpServerResource.java
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XcpServerResource.java
@@ -25,10 +25,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.NetworkUsageAnswer;
-import com.cloud.agent.api.NetworkUsageCommand;
 import com.cloud.resource.ServerResource;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.script.Script;
@@ -44,15 +40,6 @@ public class XcpServerResource extends CitrixResourceBase {
     private final static long mem_32m = 33554432L;
 
     @Override
-    public Answer executeRequest(final Command cmd) {
-        if (cmd instanceof NetworkUsageCommand) {
-            return execute((NetworkUsageCommand)cmd);
-        } else {
-            return super.executeRequest(cmd);
-        }
-    }
-
-    @Override
     protected List<File> getPatchFiles() {
         final List<File> files = new ArrayList<File>();
         final String patch = "scripts/vm/hypervisor/xenserver/xcpserver/patch";
@@ -65,23 +52,6 @@ public class XcpServerResource extends CitrixResourceBase {
         return files;
     }
 
-    protected NetworkUsageAnswer execute(final NetworkUsageCommand cmd) {
-        try {
-            final Connection conn = getConnection();
-            if (cmd.getOption() != null && cmd.getOption().equals("create")) {
-                final String result = networkUsage(conn, cmd.getPrivateIP(), "create", null);
-                final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L,
0L);
-                return answer;
-            }
-            final long[] stats = getNetworkStats(conn, cmd.getPrivateIP());
-            final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]);
-            return answer;
-        } catch (final Exception ex) {
-            s_logger.warn("Failed to get network usage stats due to ", ex);
-            return new NetworkUsageAnswer(cmd, ex);
-        }
-    }
-
     /**
      XCP provides four memory configuration fields through which
      administrators can control this behaviour:

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/096d1b93/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServer56Resource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServer56Resource.java
b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServer56Resource.java
index 3a30dae..12e4fc5 100644
--- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServer56Resource.java
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServer56Resource.java
@@ -25,10 +25,7 @@ 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.StartupCommand;
-import com.cloud.hypervisor.xenserver.resource.wrapper.CitrixRequestWrapper;
 import com.cloud.resource.ServerResource;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.script.Script;
@@ -46,17 +43,6 @@ public class XenServer56Resource extends CitrixResourceBase {
     private final static Logger s_logger = Logger.getLogger(XenServer56Resource.class);
 
     @Override
-    public Answer executeRequest(final Command cmd) {
-
-        final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
-        try {
-            return wrapper.execute(cmd, this);
-        } catch (final Exception e) {
-            return super.executeRequest(cmd);
-        }
-    }
-
-    @Override
     protected List<File> getPatchFiles() {
         final List<File> files = new ArrayList<File>();
         final String patch = "scripts/vm/hypervisor/xenserver/xenserver56/patch";
@@ -193,8 +179,4 @@ public class XenServer56Resource extends CitrixResourceBase {
         final StartupCommand[] cmds = super.initialize();
         return cmds;
     }
-
-    public XenServer56Resource() {
-        super();
-    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/096d1b93/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 8128f23..4537b2a 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
@@ -90,6 +90,7 @@ import com.cloud.agent.api.storage.MigrateVolumeCommand;
 import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
 import com.cloud.agent.api.storage.ResizeVolumeCommand;
 import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
+import com.cloud.hypervisor.xenserver.resource.XcpServerResource;
 import com.cloud.hypervisor.xenserver.resource.XenServer56FP1Resource;
 import com.cloud.hypervisor.xenserver.resource.XenServer56Resource;
 import com.cloud.hypervisor.xenserver.resource.XenServer610Resource;
@@ -204,6 +205,11 @@ public class CitrixRequestWrapper extends RequestWrapper {
         xenServer610Commands.put(MigrateWithStorageCompleteCommand.class, new XenServer610MigrateWithStorageCompleteCommandWrapper());
         xenServer610Commands.put(MigrateVolumeCommand.class, new XenServer610MigrateVolumeCommandWrapper());
         resources.put(XenServer610Resource.class, xenServer610Commands);
+
+        // XcpServerResource commands
+        final Hashtable<Class<? extends Command>, CommandWrapper> xcpServerResourceCommand
= new Hashtable<Class<? extends Command>, CommandWrapper>();
+        xcpServerResourceCommand.put(NetworkUsageCommand.class, new XcpServerNetworkUsageCommandWrapper());
+        resources.put(XcpServerResource.class, xcpServerResourceCommand);
     }
 
     public static CitrixRequestWrapper getInstance() {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/096d1b93/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerNetworkUsageCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerNetworkUsageCommandWrapper.java
b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerNetworkUsageCommandWrapper.java
new file mode 100644
index 0000000..d22c608
--- /dev/null
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerNetworkUsageCommandWrapper.java
@@ -0,0 +1,52 @@
+//
+// 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.NetworkUsageAnswer;
+import com.cloud.agent.api.NetworkUsageCommand;
+import com.cloud.hypervisor.xenserver.resource.XcpServerResource;
+import com.cloud.resource.CommandWrapper;
+import com.xensource.xenapi.Connection;
+
+public final class XcpServerNetworkUsageCommandWrapper extends CommandWrapper<NetworkUsageCommand,
Answer, XcpServerResource> {
+
+    private static final Logger s_logger = Logger.getLogger(XcpServerNetworkUsageCommandWrapper.class);
+
+    @Override
+    public Answer execute(final NetworkUsageCommand command, final XcpServerResource xcpServerResource)
{
+        try {
+            final Connection conn = xcpServerResource.getConnection();
+            if (command.getOption() != null && command.getOption().equals("create"))
{
+                final String result = xcpServerResource.networkUsage(conn, command.getPrivateIP(),
"create", null);
+                final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result,
0L, 0L);
+                return answer;
+            }
+            final long[] stats = xcpServerResource.getNetworkStats(conn, command.getPrivateIP());
+            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0],
stats[1]);
+            return answer;
+        } catch (final Exception ex) {
+            s_logger.warn("Failed to get network usage stats due to ", ex);
+            return new NetworkUsageAnswer(command, ex);
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/096d1b93/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerWrapperTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerWrapperTest.java
b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerWrapperTest.java
new file mode 100644
index 0000000..358d670
--- /dev/null
+++ b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/XcpServerWrapperTest.java
@@ -0,0 +1,116 @@
+// 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.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+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.NetworkUsageCommand;
+import com.cloud.hypervisor.xenserver.resource.XcpServerResource;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.xensource.xenapi.Connection;
+
+@RunWith(PowerMockRunner.class)
+public class XcpServerWrapperTest {
+
+    @Mock
+    protected XcpServerResource XcpServerResource;
+
+
+    @Test
+    public void testNetworkUsageCommandCreate() {
+        final Connection conn = Mockito.mock(Connection.class);
+
+        final String privateIP = "192.168.0.10";
+        final String domRName = "dom";
+        final String option = "create";
+        final boolean forVpc = true;
+
+        final NetworkUsageCommand usageCommand = new NetworkUsageCommand(privateIP, domRName,
option, forVpc);
+
+        final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        when(XcpServerResource.getConnection()).thenReturn(conn);
+        when(XcpServerResource.networkUsage(conn, usageCommand.getPrivateIP(), "create",
null)).thenReturn("success");
+
+        final Answer answer = wrapper.execute(usageCommand, XcpServerResource);
+
+        verify(XcpServerResource, times(1)).getConnection();
+
+        assertTrue(answer.getResult());
+    }
+
+    @Test
+    public void testNetworkUsageCommandGet() {
+        final Connection conn = Mockito.mock(Connection.class);
+
+        final String privateIP = "192.168.0.10";
+        final String domRName = "dom";
+        final boolean forVpc = true;
+        final String gatewayIp = "172.16.0.10";
+
+        final NetworkUsageCommand usageCommand = new NetworkUsageCommand(privateIP, domRName,
forVpc, gatewayIp);
+
+        final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        when(XcpServerResource.getConnection()).thenReturn(conn);
+        when(XcpServerResource.getNetworkStats(conn, usageCommand.getPrivateIP())).thenReturn(new
long[]{1l, 1l});
+
+        final Answer answer = wrapper.execute(usageCommand, XcpServerResource);
+
+        verify(XcpServerResource, times(1)).getConnection();
+
+        assertTrue(answer.getResult());
+    }
+
+    @Test
+    public void testNetworkUsageCommandExceptiopn() {
+        final Connection conn = Mockito.mock(Connection.class);
+
+        final String privateIP = "192.168.0.10";
+        final String domRName = "dom";
+        final String option = null;
+        final boolean forVpc = true;
+
+        final NetworkUsageCommand usageCommand = new NetworkUsageCommand(privateIP, domRName,
option, forVpc);
+
+        final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        when(XcpServerResource.getConnection()).thenReturn(conn);
+        when(XcpServerResource.networkUsage(conn, usageCommand.getPrivateIP(), "create",
null)).thenThrow(new CloudRuntimeException("FAILED"));
+
+        final Answer answer = wrapper.execute(usageCommand, XcpServerResource);
+
+        verify(XcpServerResource, times(1)).getConnection();
+
+        assertFalse(answer.getResult());
+    }
+}
\ No newline at end of file


Mime
View raw message