cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kous...@apache.org
Subject [10/50] [abbrv] git commit: updated refs/heads/CLOUDSTACK-8301 to 8ff9000
Date Tue, 19 May 2015 16:20:56 GMT
Refactoring the LibvirtComputingResource
  - Adding LibvirtPrimaryStorageDownloadCommandWrapper
  - 4 unit tests added
  - KVM hypervisor plugin with 13.3% coverage


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

Branch: refs/heads/CLOUDSTACK-8301
Commit: 8268d353a2a5ffb032017687f7df82c4a6871812
Parents: 4472cad
Author: wilderrodrigues <wrodrigues@schubergphilis.com>
Authored: Thu Apr 30 14:53:17 2015 +0200
Committer: wilderrodrigues <wrodrigues@schubergphilis.com>
Committed: Wed May 6 19:24:11 2015 +0200

----------------------------------------------------------------------
 .../kvm/resource/LibvirtComputingResource.java  |  55 +------
 ...irtPrimaryStorageDownloadCommandWrapper.java |  88 +++++++++++
 .../resource/wrapper/LibvirtRequestWrapper.java |   2 +
 .../resource/LibvirtComputingResourceTest.java  | 157 ++++++++++++++++++-
 4 files changed, 247 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8268d353/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index 4abc048..8e9cbc1 100644
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@ -146,8 +146,6 @@ import com.cloud.agent.api.routing.SetSourceNatCommand;
 import com.cloud.agent.api.storage.CopyVolumeAnswer;
 import com.cloud.agent.api.storage.CopyVolumeCommand;
 import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer;
-import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
-import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
 import com.cloud.agent.api.storage.ResizeVolumeAnswer;
 import com.cloud.agent.api.storage.ResizeVolumeCommand;
 import com.cloud.agent.api.to.DataStoreTO;
@@ -1298,9 +1296,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements
Serv
         }
 
         try {
-            if (cmd instanceof PrimaryStorageDownloadCommand) {
-                return execute((PrimaryStorageDownloadCommand)cmd);
-            } else if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) {
+            if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) {
                 return execute((CreatePrivateTemplateFromVolumeCommand)cmd);
             } else if (cmd instanceof GetStorageStatsCommand) {
                 return execute((GetStorageStatsCommand)cmd);
@@ -2704,55 +2700,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements
Serv
         }
     }
 
-    protected PrimaryStorageDownloadAnswer execute(final PrimaryStorageDownloadCommand cmd)
{
-        final String tmplturl = cmd.getUrl();
-        final int index = tmplturl.lastIndexOf("/");
-        final String mountpoint = tmplturl.substring(0, index);
-        String tmpltname = null;
-        if (index < tmplturl.length() - 1) {
-            tmpltname = tmplturl.substring(index + 1);
-        }
-
-        KVMPhysicalDisk tmplVol = null;
-        KVMStoragePool secondaryPool = null;
-        try {
-            secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
-
-            /* Get template vol */
-            if (tmpltname == null) {
-                secondaryPool.refresh();
-                final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
-                if (disks == null || disks.isEmpty()) {
-                    return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool:
" + secondaryPool.getUuid());
-                }
-                for (final KVMPhysicalDisk disk : disks) {
-                    if (disk.getName().endsWith("qcow2")) {
-                        tmplVol = disk;
-                        break;
-                    }
-                }
-                if (tmplVol == null) {
-                    return new PrimaryStorageDownloadAnswer("Failed to get template from
pool: " + secondaryPool.getUuid());
-                }
-            } else {
-                tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
-            }
-
-            /* Copy volume to primary storage */
-            final KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPool().getType(),
cmd.getPoolUuid());
-
-            final KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(tmplVol,
UUID.randomUUID().toString(), primaryPool, 0);
-
-            return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
-        } catch (final CloudRuntimeException e) {
-            return new PrimaryStorageDownloadAnswer(e.toString());
-        } finally {
-            if (secondaryPool != null) {
-                _storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
-            }
-        }
-    }
-
     protected Answer execute(final CreateStoragePoolCommand cmd) {
         return new Answer(cmd, true, "success");
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8268d353/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java
new file mode 100644
index 0000000..391ab27
--- /dev/null
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java
@@ -0,0 +1,88 @@
+//
+// 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.kvm.resource.wrapper;
+
+import java.util.List;
+import java.util.UUID;
+
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
+import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
+import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
+import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
+import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
+import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
+import com.cloud.resource.CommandWrapper;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public final class LibvirtPrimaryStorageDownloadCommandWrapper extends CommandWrapper<PrimaryStorageDownloadCommand,
Answer, LibvirtComputingResource> {
+
+    @Override
+    public Answer execute(final PrimaryStorageDownloadCommand command, final LibvirtComputingResource
libvirtComputingResource) {
+        final String tmplturl = command.getUrl();
+        final int index = tmplturl.lastIndexOf("/");
+        final String mountpoint = tmplturl.substring(0, index);
+        String tmpltname = null;
+
+        if (index < tmplturl.length() - 1) {
+            tmpltname = tmplturl.substring(index + 1);
+        }
+
+        KVMPhysicalDisk tmplVol = null;
+        KVMStoragePool secondaryPool = null;
+        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
+        try {
+            secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint);
+
+            /* Get template vol */
+            if (tmpltname == null) {
+                secondaryPool.refresh();
+                final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
+                if (disks == null || disks.isEmpty()) {
+                    return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool:
" + secondaryPool.getUuid());
+                }
+                for (final KVMPhysicalDisk disk : disks) {
+                    if (disk.getName().endsWith("qcow2")) {
+                        tmplVol = disk;
+                        break;
+                    }
+                }
+                if (tmplVol == null) {
+                    return new PrimaryStorageDownloadAnswer("Failed to get template from
pool: " + secondaryPool.getUuid());
+                }
+            } else {
+                tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
+            }
+
+            /* Copy volume to primary storage */
+            final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(),
command.getPoolUuid());
+
+            final KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(),
primaryPool, 0);
+
+            return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
+        } catch (final CloudRuntimeException e) {
+            return new PrimaryStorageDownloadAnswer(e.toString());
+        } finally {
+            if (secondaryPool != null) {
+                storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
+            }
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8268d353/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java
index eb8d569..7c9b443 100644
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java
@@ -43,6 +43,7 @@ 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.agent.api.storage.PrimaryStorageDownloadCommand;
 import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
 import com.cloud.resource.CommandWrapper;
 import com.cloud.resource.RequestWrapper;
@@ -86,6 +87,7 @@ public class LibvirtRequestWrapper extends RequestWrapper {
         linbvirtCommands.put(MaintainCommand.class, new LibvirtMaintainCommandWrapper());
         linbvirtCommands.put(CreateCommand.class, new LibvirtCreateCommandWrapper());
         linbvirtCommands.put(DestroyCommand.class, new LibvirtDestroyCommandWrapper());
+        linbvirtCommands.put(PrimaryStorageDownloadCommand.class, new LibvirtPrimaryStorageDownloadCommandWrapper());
 
         resources.put(LibvirtComputingResource.class, linbvirtCommands);
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8268d353/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
index 0865fb5..946d871 100644
--- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
+++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
@@ -86,6 +86,7 @@ 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.agent.api.storage.PrimaryStorageDownloadCommand;
 import com.cloud.agent.api.to.DiskTO;
 import com.cloud.agent.api.to.NicTO;
 import com.cloud.agent.api.to.StorageFilerTO;
@@ -101,6 +102,7 @@ import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
 import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
 import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
 import com.cloud.network.Networks.TrafficType;
+import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.storage.Storage.StoragePoolType;
 import com.cloud.storage.StoragePool;
 import com.cloud.storage.Volume;
@@ -1378,4 +1380,157 @@ public class LibvirtComputingResourceTest {
         verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
         verify(poolManager, times(1)).getStoragePool(vol.getPoolType(), vol.getPoolUuid());
     }
-}
\ No newline at end of file
+
+    @Test(expected = NullPointerException.class)
+    public void testPrimaryStorageDownloadCommandNOTemplateDisk() {
+        final StoragePool pool = Mockito.mock(StoragePool.class);
+
+        final List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
+
+        final String name = "Test";
+        final String url = "http://template/";
+        final ImageFormat format = ImageFormat.QCOW2;
+        final long accountId = 1l;
+        final int wait = 0;
+        final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name,
url, format, accountId, pool, wait);
+
+        final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
+        final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
+        final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
+
+        final KVMPhysicalDisk disk = new KVMPhysicalDisk("/path", "disk.qcow2", primaryPool);
+        disks.add(disk);
+
+        final int index = url.lastIndexOf("/");
+        final String mountpoint = url.substring(0, index);
+
+        when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
+        when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
+        when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
+        when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
+        when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool,
0)).thenReturn(primaryVol);
+
+        final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        final Answer answer = wrapper.execute(command, libvirtComputingResource);
+        assertFalse(answer.getResult());
+
+        verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
+    }
+
+    @Test
+    public void testPrimaryStorageDownloadCommandNOTemplateNODisk() {
+        final StoragePool pool = Mockito.mock(StoragePool.class);
+
+        final List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
+
+        final String name = "Test";
+        final String url = "http://template/";
+        final ImageFormat format = ImageFormat.QCOW2;
+        final long accountId = 1l;
+        final int wait = 0;
+        final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name,
url, format, accountId, pool, wait);
+
+        final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
+        final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
+        final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
+
+        final int index = url.lastIndexOf("/");
+        final String mountpoint = url.substring(0, index);
+
+        when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
+        when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
+        when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
+        when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
+        when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool,
0)).thenReturn(primaryVol);
+
+        final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        final Answer answer = wrapper.execute(command, libvirtComputingResource);
+        assertFalse(answer.getResult());
+
+        verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
+    }
+
+    @Test
+    public void testPrimaryStorageDownloadCommandNOTemplateNOQcow2() {
+        final StoragePool pool = Mockito.mock(StoragePool.class);
+
+        final List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
+        final List<KVMPhysicalDisk> spiedDisks = Mockito.spy(disks);
+
+        final String name = "Test";
+        final String url = "http://template/";
+        final ImageFormat format = ImageFormat.QCOW2;
+        final long accountId = 1l;
+        final int wait = 0;
+        final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name,
url, format, accountId, pool, wait);
+
+        final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
+        final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
+        final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
+
+        final int index = url.lastIndexOf("/");
+        final String mountpoint = url.substring(0, index);
+
+        when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
+        when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
+        when(secondaryPool.listPhysicalDisks()).thenReturn(spiedDisks);
+        when(spiedDisks.isEmpty()).thenReturn(false);
+
+        when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
+        when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool,
0)).thenReturn(primaryVol);
+
+        final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        final Answer answer = wrapper.execute(command, libvirtComputingResource);
+        assertFalse(answer.getResult());
+
+        verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testPrimaryStorageDownloadCommandTemplateNoDisk() {
+        final StoragePool pool = Mockito.mock(StoragePool.class);
+
+        final String name = "Test";
+        final String url = "http://template/template.qcow2";
+        final ImageFormat format = ImageFormat.VHD;
+        final long accountId = 1l;
+        final int wait = 0;
+        final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name,
url, format, accountId, pool, wait);
+
+        final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
+        final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
+        final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
+        final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
+
+        final int index = url.lastIndexOf("/");
+        final String mountpoint = url.substring(0, index);
+
+        when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
+        when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
+        when(secondaryPool.getPhysicalDisk("template.qcow2")).thenReturn(tmplVol);
+        when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
+        when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool,
0)).thenReturn(primaryVol);
+
+        final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        final Answer answer = wrapper.execute(command, libvirtComputingResource);
+        assertTrue(answer.getResult());
+
+        verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
+        verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), command.getPoolUuid());
+    }
+}


Mime
View raw message