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:56 UTC
[15/39] git commit: updated refs/heads/master to 3e28747
Refactoring DestroyCommand and CreateStoragePoolCommand wrappers to cope with the new design
- Basic tests added
s Please enter the commit message for your changes. Lines starting
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/a06958f4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/a06958f4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/a06958f4
Branch: refs/heads/master
Commit: a06958f46927cc5e1555f45d0678977b37c9261e
Parents: a70d80d
Author: wilderrodrigues <wr...@schubergphilis.com>
Authored: Tue Mar 24 14:31:19 2015 +0100
Committer: wilderrodrigues <wr...@schubergphilis.com>
Committed: Tue Mar 31 10:38:27 2015 +0200
----------------------------------------------------------------------
.../xenserver/resource/CitrixResourceBase.java | 10 +--
.../CitrixCreateStoragePoolCommandWrapper.java | 57 ++++++++++++++
.../wrapper/CitrixDestroyCommandWrapper.java | 83 ++++++++++++++++++++
.../resource/wrapper/CitrixRequestWrapper.java | 4 +
.../wrapper/CitrixRequestWrapperTest.java | 49 +++++++++++-
5 files changed, 192 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a06958f4/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 2f7ba8b..e935303 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
@@ -441,12 +441,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
if (cmd instanceof NetworkElementCommand) {
return _vrResource.executeRequest((NetworkElementCommand)cmd);
- } else if (clazz == PrepareForMigrationCommand.class) {
- return execute((PrepareForMigrationCommand)cmd);
- } else if (clazz == MigrateCommand.class) {
- return execute((MigrateCommand)cmd);
- } else if (clazz == DestroyCommand.class) {
- return execute((DestroyCommand)cmd);
} else if (clazz == CreateStoragePoolCommand.class) {
return execute((CreateStoragePoolCommand)cmd);
} else if (clazz == ModifyStoragePoolCommand.class) {
@@ -5863,7 +5857,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
- protected SR getIscsiSR(final Connection conn, final String srNameLabel, final String target, String path, final String chapInitiatorUsername, final String chapInitiatorPassword,
+ public SR getIscsiSR(final Connection conn, final String srNameLabel, final String target, String path, final String chapInitiatorUsername, final String chapInitiatorPassword,
final boolean ignoreIntroduceException) {
synchronized (srNameLabel.intern()) {
final Map<String, String> deviceConfig = new HashMap<String, String>();
@@ -6007,7 +6001,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
- protected SR getNfsSR(final Connection conn, final String poolid, final String uuid, final String server, String serverpath, final String pooldesc) {
+ public SR getNfsSR(final Connection conn, final String poolid, final String uuid, final String server, String serverpath, final String pooldesc) {
final Map<String, String> deviceConfig = new HashMap<String, String>();
try {
serverpath = serverpath.replace("//", "/");
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a06958f4/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixCreateStoragePoolCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixCreateStoragePoolCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixCreateStoragePoolCommandWrapper.java
new file mode 100644
index 0000000..f749abb
--- /dev/null
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixCreateStoragePoolCommandWrapper.java
@@ -0,0 +1,57 @@
+//
+// 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.CreateStoragePoolCommand;
+import com.cloud.agent.api.to.StorageFilerTO;
+import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
+import com.cloud.resource.CommandWrapper;
+import com.cloud.storage.Storage.StoragePoolType;
+import com.xensource.xenapi.Connection;
+
+public final class CitrixCreateStoragePoolCommandWrapper extends CommandWrapper<CreateStoragePoolCommand, Answer, CitrixResourceBase> {
+
+ private static final Logger s_logger = Logger.getLogger(CitrixCreateStoragePoolCommandWrapper.class);
+
+ @Override
+ public Answer execute(final CreateStoragePoolCommand command, final CitrixResourceBase citrixResourceBase) {
+ final Connection conn = citrixResourceBase.getConnection();
+ final StorageFilerTO pool = command.getPool();
+ try {
+ if (pool.getType() == StoragePoolType.NetworkFilesystem) {
+ citrixResourceBase.getNfsSR(conn, Long.toString(pool.getId()), pool.getUuid(), pool.getHost(), pool.getPath(), pool.toString());
+ } else if (pool.getType() == StoragePoolType.IscsiLUN) {
+ citrixResourceBase.getIscsiSR(conn, pool.getUuid(), pool.getHost(), pool.getPath(), null, null, false);
+ } else if (pool.getType() == StoragePoolType.PreSetup) {
+ } else {
+ return new Answer(command, false, "The pool type: " + pool.getType().name() + " is not supported.");
+ }
+ return new Answer(command, true, "success");
+ } catch (final Exception e) {
+ final String msg = "Catch Exception " + e.getClass().getName() + ", create StoragePool failed due to " + e.toString() + " on host:"
+ + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
+ s_logger.warn(msg, e);
+ return new Answer(command, false, msg);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a06958f4/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixDestroyCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixDestroyCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixDestroyCommandWrapper.java
new file mode 100644
index 0000000..0685a1b
--- /dev/null
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixDestroyCommandWrapper.java
@@ -0,0 +1,83 @@
+//
+// 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.Set;
+
+import org.apache.log4j.Logger;
+
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.storage.DestroyCommand;
+import com.cloud.agent.api.to.VolumeTO;
+import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
+import com.cloud.resource.CommandWrapper;
+import com.xensource.xenapi.Connection;
+import com.xensource.xenapi.VBD;
+import com.xensource.xenapi.VDI;
+
+public final class CitrixDestroyCommandWrapper extends CommandWrapper<DestroyCommand, Answer, CitrixResourceBase> {
+
+ private static final Logger s_logger = Logger.getLogger(CitrixDestroyCommandWrapper.class);
+
+ @Override
+ public Answer execute(final DestroyCommand command, final CitrixResourceBase citrixResourceBase) {
+ final Connection conn = citrixResourceBase.getConnection();
+ final VolumeTO vol = command.getVolume();
+ // Look up the VDI
+ final String volumeUUID = vol.getPath();
+ VDI vdi = null;
+ try {
+ vdi = citrixResourceBase.getVDIbyUuid(conn, volumeUUID);
+ } catch (final Exception e) {
+ return new Answer(command, true, "Success");
+ }
+ Set<VBD> vbds = null;
+ try {
+ vbds = vdi.getVBDs(conn);
+ } catch (final Exception e) {
+ final String msg = "VDI getVBDS for " + volumeUUID + " failed due to " + e.toString();
+ s_logger.warn(msg, e);
+ return new Answer(command, false, msg);
+ }
+ for (final VBD vbd : vbds) {
+ try {
+ vbd.unplug(conn);
+ vbd.destroy(conn);
+ } catch (final Exception e) {
+ final String msg = "VM destroy for " + volumeUUID + " failed due to " + e.toString();
+ s_logger.warn(msg, e);
+ return new Answer(command, false, msg);
+ }
+ }
+ try {
+ final Set<VDI> snapshots = vdi.getSnapshots(conn);
+ for (final VDI snapshot : snapshots) {
+ snapshot.destroy(conn);
+ }
+ vdi.destroy(conn);
+ } catch (final Exception e) {
+ final String msg = "VDI destroy for " + volumeUUID + " failed due to " + e.toString();
+ s_logger.warn(msg, e);
+ return new Answer(command, false, msg);
+ }
+
+ return new Answer(command, true, "Success");
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a06958f4/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 ca0b7d0..af5e5b4 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
@@ -25,6 +25,7 @@ import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CheckHealthCommand;
import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.agent.api.Command;
+import com.cloud.agent.api.CreateStoragePoolCommand;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetVmDiskStatsCommand;
import com.cloud.agent.api.GetVmStatsCommand;
@@ -37,6 +38,7 @@ import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand;
import com.cloud.agent.api.storage.CreateCommand;
+import com.cloud.agent.api.storage.DestroyCommand;
import com.cloud.resource.CommandWrapper;
import com.cloud.resource.RequestWrapper;
import com.cloud.resource.ServerResource;
@@ -73,6 +75,8 @@ public class CitrixRequestWrapper extends RequestWrapper {
map.put(CheckVirtualMachineCommand.class, new CitrixCheckVirtualMachineCommandWrapper());
map.put(PrepareForMigrationCommand.class, new CitrixPrepareForMigrationCommandWrapper());
map.put(MigrateCommand.class, new CitrixMigrateCommandWrapper());
+ map.put(DestroyCommand.class, new CitrixDestroyCommandWrapper());
+ map.put(CreateStoragePoolCommand.class, new CitrixCreateStoragePoolCommandWrapper());
}
public static CitrixRequestWrapper getInstance() {
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a06958f4/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 e895581..baf7a55 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
@@ -5,6 +5,7 @@ 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 java.util.ArrayList;
@@ -12,12 +13,14 @@ import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CheckHealthCommand;
import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.agent.api.Command;
+import com.cloud.agent.api.CreateStoragePoolCommand;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetVmDiskStatsCommand;
import com.cloud.agent.api.GetVmStatsCommand;
@@ -32,8 +35,11 @@ import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand;
import com.cloud.agent.api.storage.CreateAnswer;
import com.cloud.agent.api.storage.CreateCommand;
+import com.cloud.agent.api.storage.DestroyCommand;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
+import com.cloud.hypervisor.xenserver.resource.XsHost;
+import com.cloud.storage.VMTemplateStorageResourceAssoc;
import com.cloud.vm.DiskProfile;
@RunWith(MockitoJUnitRunner.class)
@@ -80,7 +86,9 @@ public class CitrixRequestWrapperTest {
@Test
public void testExecuteCreateCommand() {
- final CreateCommand createCommand = new CreateCommand(new DiskProfile(null), "", new StoragePoolVO(), false);
+ final StoragePoolVO poolVO = Mockito.mock(StoragePoolVO.class);
+ final DiskProfile diskProfile = Mockito.mock(DiskProfile.class);
+ final CreateCommand createCommand = new CreateCommand(diskProfile, "", poolVO, false);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
@@ -216,7 +224,8 @@ public class CitrixRequestWrapperTest {
@Test
public void testPrepareForMigrationCommand() {
- final PrepareForMigrationCommand prepareCommand = new PrepareForMigrationCommand(new VirtualMachineTO(0, "Test", null, 2, 200, 256, 512, null, "CentOS", true, false, "123"));
+ final VirtualMachineTO machineTO = Mockito.mock(VirtualMachineTO.class);
+ final PrepareForMigrationCommand prepareCommand = new PrepareForMigrationCommand(machineTO);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
@@ -228,7 +237,8 @@ public class CitrixRequestWrapperTest {
@Test
public void testMigrateCommand() {
- final MigrateCommand migrateCommand = new MigrateCommand("Test", "127.0.0.1", false, new VirtualMachineTO(0, "Test", null, 2, 200, 256, 512, null, "CentOS", true, false, "123"), false);
+ final VirtualMachineTO machineTO = Mockito.mock(VirtualMachineTO.class);
+ final MigrateCommand migrateCommand = new MigrateCommand("Test", "127.0.0.1", false, machineTO, false);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
@@ -237,6 +247,39 @@ public class CitrixRequestWrapperTest {
assertFalse(answer.getResult());
}
+
+ @Test
+ public void testDestroyCommand() {
+
+ final VMTemplateStorageResourceAssoc templateStorage = Mockito.mock(VMTemplateStorageResourceAssoc.class);
+ final StoragePoolVO poolVO = Mockito.mock(StoragePoolVO.class);
+
+ final DestroyCommand destroyCommand = new DestroyCommand(poolVO, templateStorage);
+
+ final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+ assertNotNull(wrapper);
+
+ final Answer answer = wrapper.execute(destroyCommand, citrixResourceBase);
+
+ assertFalse(answer.getResult());
+ }
+
+ @Test
+ public void testCreateStoragePoolCommand() {
+ final StoragePoolVO poolVO = Mockito.mock(StoragePoolVO.class);
+ final XsHost xsHost = Mockito.mock(XsHost.class);
+
+ final CreateStoragePoolCommand destroyCommand = new CreateStoragePoolCommand(false, poolVO);
+
+ final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
+ assertNotNull(wrapper);
+
+ when(citrixResourceBase.getHost()).thenReturn(xsHost);
+
+ final Answer answer = wrapper.execute(destroyCommand, citrixResourceBase);
+
+ assertFalse(answer.getResult());
+ }
}
class NotAValidCommand extends Command {