Skip to content

Commit 7fd43f3

Browse files
Refactoring the LibvirtComputingResource
- 5 unit tests added - KVM hypervisor plugin with 20.1% coverage
1 parent 0b1b2b6 commit 7fd43f3

2 files changed

Lines changed: 216 additions & 9 deletions

File tree

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ public final class LibvirtCopyVolumeCommandWrapper extends CommandWrapper<CopyVo
3737
@Override
3838
public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
3939
/**
40-
This method is only used for copying files from Primary Storage TO Secondary Storage
41-
42-
It COULD also do it the other way around, but the code in the ManagementServerImpl shows
43-
that it always sets copyToSecondary to true
44-
40+
* This method is only used for copying files from Primary Storage TO
41+
* Secondary Storage
42+
*
43+
* It COULD also do it the other way around, but the code in the
44+
* ManagementServerImpl shows that it always sets copyToSecondary to
45+
* true
4546
*/
4647
final boolean copyToSecondary = command.toSecondaryStorage();
4748
String volumePath = command.getVolumePath();
@@ -56,9 +57,7 @@ public Answer execute(final CopyVolumeCommand command, final LibvirtComputingRes
5657
primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
5758
} catch (final CloudRuntimeException e) {
5859
if (e.getMessage().contains("not found")) {
59-
primaryPool =
60-
storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(),
61-
command.getPool().getUserInfo(), command.getPool().getType());
60+
primaryPool = storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType());
6261
} else {
6362
return new CopyVolumeAnswer(command, false, e.getMessage(), null, null);
6463
}

‎plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java‎

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
import com.cloud.agent.api.check.CheckSshCommand;
120120
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
121121
import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand;
122+
import com.cloud.agent.api.storage.CopyVolumeCommand;
122123
import com.cloud.agent.api.storage.CreateCommand;
123124
import com.cloud.agent.api.storage.DestroyCommand;
124125
import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
@@ -133,8 +134,8 @@
133134
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
134135
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
135136
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
136-
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper;
137137
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper;
138+
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper;
138139
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
139140
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
140141
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
@@ -3890,4 +3891,211 @@ public void testCreatePrivateTemplateFromSnapshotCommandCloudRuntime() {
38903891
verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
38913892
verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl());
38923893
}
3894+
3895+
@Test
3896+
public void testCopyVolumeCommand() {
3897+
final StoragePool storagePool = Mockito.mock(StoragePool.class);
3898+
final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary";
3899+
final Long volumeId = 1l;
3900+
final int wait = 0;
3901+
final String volumePath = "/vol/path";
3902+
final boolean toSecondaryStorage = true;
3903+
final boolean executeInSequence = false;
3904+
3905+
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence );
3906+
3907+
final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
3908+
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
3909+
3910+
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
3911+
final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class);
3912+
final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class);
3913+
3914+
final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class);
3915+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
3916+
3917+
final StorageFilerTO pool = command.getPool();
3918+
3919+
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
3920+
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
3921+
3922+
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
3923+
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
3924+
when(primary.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
3925+
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL)).thenReturn(secondary);
3926+
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
3927+
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
3928+
when(secondary.createFolder(volumeDestPath)).thenReturn(true);
3929+
when(storagePoolMgr.deleteStoragePool(secondary.getType(), secondary.getUuid())).thenReturn(true);
3930+
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
3931+
when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, secondary, 0)).thenReturn(disk);
3932+
3933+
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
3934+
assertNotNull(wrapper);
3935+
3936+
final Answer answer = wrapper.execute(command, libvirtComputingResource);
3937+
assertTrue(answer.getResult());
3938+
3939+
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
3940+
}
3941+
3942+
@Test
3943+
public void testCopyVolumeCommandToSecFalse() {
3944+
final StoragePool storagePool = Mockito.mock(StoragePool.class);
3945+
final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary";
3946+
final Long volumeId = 1l;
3947+
final int wait = 0;
3948+
final String volumePath = "/vol/path";
3949+
final boolean toSecondaryStorage = false;
3950+
final boolean executeInSequence = false;
3951+
3952+
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence );
3953+
3954+
final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
3955+
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
3956+
3957+
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
3958+
final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class);
3959+
final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class);
3960+
3961+
final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class);
3962+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
3963+
3964+
final StorageFilerTO pool = command.getPool();
3965+
3966+
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
3967+
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
3968+
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
3969+
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
3970+
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
3971+
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
3972+
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
3973+
when(primary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenReturn(disk);
3974+
when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, primary, 0)).thenReturn(disk);
3975+
3976+
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
3977+
assertNotNull(wrapper);
3978+
3979+
final Answer answer = wrapper.execute(command, libvirtComputingResource);
3980+
assertTrue(answer.getResult());
3981+
3982+
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
3983+
}
3984+
3985+
@SuppressWarnings("unchecked")
3986+
@Test
3987+
public void testCopyVolumeCommandCloudRuntime() {
3988+
final StoragePool storagePool = Mockito.mock(StoragePool.class);
3989+
final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary";
3990+
final Long volumeId = 1l;
3991+
final int wait = 0;
3992+
final String volumePath = "/vol/path";
3993+
final boolean toSecondaryStorage = false;
3994+
final boolean executeInSequence = false;
3995+
3996+
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence );
3997+
3998+
final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
3999+
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
4000+
4001+
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
4002+
final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class);
4003+
final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class);
4004+
4005+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
4006+
4007+
final StorageFilerTO pool = command.getPool();
4008+
4009+
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
4010+
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
4011+
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4012+
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
4013+
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
4014+
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
4015+
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
4016+
when(secondary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenThrow(CloudRuntimeException.class);
4017+
4018+
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
4019+
assertNotNull(wrapper);
4020+
4021+
final Answer answer = wrapper.execute(command, libvirtComputingResource);
4022+
assertFalse(answer.getResult());
4023+
4024+
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
4025+
}
4026+
4027+
@Test
4028+
public void testCopyVolumeCommandCloudRuntime2() {
4029+
final StoragePool storagePool = Mockito.mock(StoragePool.class);
4030+
final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary";
4031+
final Long volumeId = 1l;
4032+
final int wait = 0;
4033+
final String volumePath = "/vol/path";
4034+
final boolean toSecondaryStorage = false;
4035+
final boolean executeInSequence = false;
4036+
4037+
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence );
4038+
4039+
final StorageFilerTO pool = command.getPool();
4040+
4041+
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
4042+
4043+
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
4044+
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenThrow(new CloudRuntimeException("error"));
4045+
4046+
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
4047+
assertNotNull(wrapper);
4048+
4049+
final Answer answer = wrapper.execute(command, libvirtComputingResource);
4050+
assertFalse(answer.getResult());
4051+
4052+
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
4053+
}
4054+
4055+
@Test
4056+
public void testCopyVolumeCommandPrimaryNotFound() {
4057+
final StoragePool storagePool = Mockito.mock(StoragePool.class);
4058+
final String secondaryStoragePoolURL = "nfs:/192.168.2.2/storage/secondary";
4059+
final Long volumeId = 1l;
4060+
final int wait = 0;
4061+
final String volumePath = "/vol/path";
4062+
final boolean toSecondaryStorage = false;
4063+
final boolean executeInSequence = false;
4064+
4065+
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence );
4066+
4067+
final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
4068+
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
4069+
4070+
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
4071+
final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class);
4072+
final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class);
4073+
4074+
final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class);
4075+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
4076+
4077+
final StorageFilerTO pool = command.getPool();
4078+
4079+
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
4080+
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenThrow(new CloudRuntimeException("not found"));
4081+
4082+
when(storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(),
4083+
pool.getUserInfo(), pool.getType())).thenReturn(primary);
4084+
4085+
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4086+
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
4087+
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
4088+
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
4089+
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
4090+
when(primary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenReturn(disk);
4091+
when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, primary, 0)).thenReturn(disk);
4092+
4093+
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
4094+
assertNotNull(wrapper);
4095+
4096+
final Answer answer = wrapper.execute(command, libvirtComputingResource);
4097+
assertTrue(answer.getResult());
4098+
4099+
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
4100+
}
38934101
}

0 commit comments

Comments
 (0)