Refactoring the LibvirtComputingResource - 5 unit tests added - KVM hypervisor plugin with 20.1% coverage
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/7fd43f33 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/7fd43f33 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/7fd43f33 Branch: refs/heads/CLOUDSTACK-8301 Commit: 7fd43f33d35fc514cc480c0e15f934e58be2cfdc Parents: 0b1b2b6 Author: wilderrodrigues <[email protected]> Authored: Wed May 6 08:48:24 2015 +0200 Committer: wilderrodrigues <[email protected]> Committed: Wed May 6 19:24:15 2015 +0200 ---------------------------------------------------------------------- .../LibvirtCopyVolumeCommandWrapper.java | 15 +- .../resource/LibvirtComputingResourceTest.java | 210 ++++++++++++++++++- 2 files changed, 216 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/7fd43f33/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java index d14b045..348c421 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java @@ -37,11 +37,12 @@ public final class LibvirtCopyVolumeCommandWrapper extends CommandWrapper<CopyVo @Override public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) { /** - This method is only used for copying files from Primary Storage TO Secondary Storage - - It COULD also do it the other way around, but the code in the ManagementServerImpl shows - that it always sets copyToSecondary to true - + * This method is only used for copying files from Primary Storage TO + * Secondary Storage + * + * It COULD also do it the other way around, but the code in the + * ManagementServerImpl shows that it always sets copyToSecondary to + * true */ final boolean copyToSecondary = command.toSecondaryStorage(); String volumePath = command.getVolumePath(); @@ -56,9 +57,7 @@ public final class LibvirtCopyVolumeCommandWrapper extends CommandWrapper<CopyVo primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid()); } catch (final CloudRuntimeException e) { if (e.getMessage().contains("not found")) { - primaryPool = - storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), - command.getPool().getUserInfo(), command.getPool().getType()); + primaryPool = storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType()); } else { return new CopyVolumeAnswer(command, false, e.getMessage(), null, null); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/7fd43f33/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 c5c1553..86ff007 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 @@ -119,6 +119,7 @@ import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.check.CheckSshCommand; import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand; import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand; +import com.cloud.agent.api.storage.CopyVolumeCommand; import com.cloud.agent.api.storage.CreateCommand; import com.cloud.agent.api.storage.DestroyCommand; import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; @@ -133,8 +134,8 @@ import com.cloud.exception.InternalErrorException; import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef; -import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper; import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper; +import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper; import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; import com.cloud.hypervisor.kvm.storage.KVMStoragePool; import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; @@ -3890,4 +3891,211 @@ public class LibvirtComputingResourceTest { verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl()); } + + @Test + public void testCopyVolumeCommand() { + final StoragePool storagePool = Mockito.mock(StoragePool.class); + final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary"; + final Long volumeId = 1l; + final int wait = 0; + final String volumePath = "/vol/path"; + final boolean toSecondaryStorage = true; + final boolean executeInSequence = false; + + final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence ); + + final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; + final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator; + + final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); + final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class); + final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); + + final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + + final StorageFilerTO pool = command.getPool(); + + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary); + + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName); + when(primary.getPhysicalDisk(command.getVolumePath())).thenReturn(disk); + when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL)).thenReturn(secondary); + when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS); + when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6"); + when(secondary.createFolder(volumeDestPath)).thenReturn(true); + when(storagePoolMgr.deleteStoragePool(secondary.getType(), secondary.getUuid())).thenReturn(true); + when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary); + when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, secondary, 0)).thenReturn(disk); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getStoragePoolMgr(); + } + + @Test + public void testCopyVolumeCommandToSecFalse() { + final StoragePool storagePool = Mockito.mock(StoragePool.class); + final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary"; + final Long volumeId = 1l; + final int wait = 0; + final String volumePath = "/vol/path"; + final boolean toSecondaryStorage = false; + final boolean executeInSequence = false; + + final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence ); + + final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; + final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator; + + final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); + final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class); + final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); + + final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + + final StorageFilerTO pool = command.getPool(); + + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary); + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName); + when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS); + when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6"); + when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary); + when(primary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenReturn(disk); + when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, primary, 0)).thenReturn(disk); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getStoragePoolMgr(); + } + + @SuppressWarnings("unchecked") + @Test + public void testCopyVolumeCommandCloudRuntime() { + final StoragePool storagePool = Mockito.mock(StoragePool.class); + final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary"; + final Long volumeId = 1l; + final int wait = 0; + final String volumePath = "/vol/path"; + final boolean toSecondaryStorage = false; + final boolean executeInSequence = false; + + final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence ); + + final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; + final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator; + + final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); + final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class); + final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); + + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + + final StorageFilerTO pool = command.getPool(); + + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary); + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName); + when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS); + when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6"); + when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary); + when(secondary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenThrow(CloudRuntimeException.class); + + 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 testCopyVolumeCommandCloudRuntime2() { + final StoragePool storagePool = Mockito.mock(StoragePool.class); + final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary"; + final Long volumeId = 1l; + final int wait = 0; + final String volumePath = "/vol/path"; + final boolean toSecondaryStorage = false; + final boolean executeInSequence = false; + + final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence ); + + final StorageFilerTO pool = command.getPool(); + + final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); + + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenThrow(new CloudRuntimeException("error")); + + 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 testCopyVolumeCommandPrimaryNotFound() { + final StoragePool storagePool = Mockito.mock(StoragePool.class); + final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary"; + final Long volumeId = 1l; + final int wait = 0; + final String volumePath = "/vol/path"; + final boolean toSecondaryStorage = false; + final boolean executeInSequence = false; + + final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence ); + + final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; + final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator; + + final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); + final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class); + final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); + + final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + + final StorageFilerTO pool = command.getPool(); + + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenThrow(new CloudRuntimeException("not found")); + + when(storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), + pool.getUserInfo(), pool.getType())).thenReturn(primary); + + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName); + when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS); + when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6"); + when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary); + when(primary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenReturn(disk); + when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, primary, 0)).thenReturn(disk); + + final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, libvirtComputingResource); + assertTrue(answer.getResult()); + + verify(libvirtComputingResource, times(1)).getStoragePoolMgr(); + } } \ No newline at end of file
