|
19 | 19 | import static org.junit.Assert.assertFalse; |
20 | 20 | import static org.junit.Assert.assertNull; |
21 | 21 | import static org.junit.Assert.assertTrue; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
22 | 24 |
|
23 | 25 | import java.io.IOException; |
24 | 26 | import java.util.ArrayList; |
|
33 | 35 |
|
34 | 36 | import com.cloud.dc.ClusterDetailsVO; |
35 | 37 | import com.cloud.dc.DataCenter; |
| 38 | +import com.cloud.dc.HostPodVO; |
36 | 39 | import com.cloud.gpu.GPU; |
37 | 40 | import com.cloud.host.Host; |
38 | 41 | import com.cloud.host.HostVO; |
39 | 42 | import com.cloud.host.Status; |
40 | 43 | import com.cloud.storage.DiskOfferingVO; |
41 | 44 | import com.cloud.storage.Storage; |
42 | 45 | import com.cloud.storage.StoragePool; |
| 46 | +import com.cloud.storage.StoragePoolStatus; |
43 | 47 | import com.cloud.storage.VMTemplateVO; |
44 | 48 | import com.cloud.storage.Volume; |
45 | 49 | import com.cloud.storage.VolumeVO; |
46 | 50 | import com.cloud.storage.dao.VMTemplateDao; |
| 51 | +import com.cloud.template.VirtualMachineTemplate; |
| 52 | +import com.cloud.user.Account; |
47 | 53 | import com.cloud.user.AccountVO; |
48 | 54 | import com.cloud.user.dao.AccountDao; |
49 | 55 | import com.cloud.utils.Pair; |
| 56 | +import com.cloud.vm.DiskProfile; |
50 | 57 | import com.cloud.vm.VMInstanceVO; |
51 | 58 | import com.cloud.vm.VirtualMachine; |
52 | 59 | import com.cloud.vm.VirtualMachine.Type; |
53 | 60 | import com.cloud.vm.VirtualMachineProfile; |
54 | 61 | import com.cloud.vm.VirtualMachineProfileImpl; |
55 | 62 | import org.apache.cloudstack.affinity.dao.AffinityGroupDomainMapDao; |
| 63 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
| 64 | +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore; |
| 65 | +import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator; |
56 | 66 | import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
57 | 67 | import org.apache.commons.collections.CollectionUtils; |
58 | 68 | import org.junit.Assert; |
|
67 | 77 | import org.mockito.Mockito; |
68 | 78 | import org.mockito.MockitoAnnotations; |
69 | 79 | import org.mockito.Spy; |
| 80 | +import org.powermock.api.mockito.PowerMockito; |
70 | 81 | import org.springframework.context.annotation.Bean; |
71 | 82 | import org.springframework.context.annotation.ComponentScan; |
72 | 83 | import org.springframework.context.annotation.ComponentScan.Filter; |
@@ -197,6 +208,18 @@ public class DeploymentPlanningManagerImplTest { |
197 | 208 | @Mock |
198 | 209 | ConfigurationDao configDao; |
199 | 210 |
|
| 211 | + @Mock |
| 212 | + AccountManager _accountMgr; |
| 213 | + |
| 214 | + @Inject |
| 215 | + DiskOfferingDao _diskOfferingDao; |
| 216 | + |
| 217 | + @Mock |
| 218 | + DataStoreManager _dataStoreManager; |
| 219 | + |
| 220 | + @Inject |
| 221 | + HostPodDao _podDao; |
| 222 | + |
200 | 223 | private static final long dataCenterId = 1L; |
201 | 224 | private static final long instanceId = 123L; |
202 | 225 | private static final long hostId = 0L; |
@@ -242,6 +265,8 @@ public void testSetUp() { |
242 | 265 | List<DeploymentPlanner> planners = new ArrayList<DeploymentPlanner>(); |
243 | 266 | planners.add(_planner); |
244 | 267 | _dpm.setPlanners(planners); |
| 268 | + StoragePoolAllocator allocator = Mockito.mock(StoragePoolAllocator.class); |
| 269 | + _dpm.setStoragePoolAllocators(Arrays.asList(allocator)); |
245 | 270 |
|
246 | 271 | Mockito.when(host.getId()).thenReturn(hostId); |
247 | 272 | Mockito.doNothing().when(_dpm).avoidDisabledResources(vmProfile, dc, avoids); |
@@ -702,6 +727,84 @@ public void failEncRootPlannerHostSupportingEncryptionTest() { |
702 | 727 | } |
703 | 728 | } |
704 | 729 |
|
| 730 | + @Test |
| 731 | + public void findSuitablePoolsForVolumesTest() throws Exception { |
| 732 | + Long diskOfferingId = 1L; |
| 733 | + HostVO host = Mockito.spy(new HostVO("host")); |
| 734 | + Map<String, String> hostDetails = new HashMap<>() { |
| 735 | + { |
| 736 | + put(Host.HOST_VOLUME_ENCRYPTION, "true"); |
| 737 | + } |
| 738 | + }; |
| 739 | + host.setDetails(hostDetails); |
| 740 | + Mockito.when(host.getStatus()).thenReturn(Status.Up); |
| 741 | + |
| 742 | + VolumeVO vol1 = Mockito.spy(new VolumeVO("vol1", dataCenterId, podId, 1L, 1L, instanceId, "folder", "path", |
| 743 | + Storage.ProvisioningType.THIN, (long) 10 << 30, Volume.Type.ROOT)); |
| 744 | + Mockito.when(vol1.getId()).thenReturn(1L); |
| 745 | + vol1.setState(Volume.State.Allocated); |
| 746 | + vol1.setPassphraseId(1L); |
| 747 | + vol1.setPoolId(1L); |
| 748 | + vol1.setDiskOfferingId(diskOfferingId); |
| 749 | + |
| 750 | + StoragePoolVO storagePool = new StoragePoolVO(); |
| 751 | + storagePool.setStatus(StoragePoolStatus.Maintenance); |
| 752 | + storagePool.setId(vol1.getPoolId()); |
| 753 | + storagePool.setDataCenterId(dataCenterId); |
| 754 | + storagePool.setPodId(podId); |
| 755 | + storagePool.setClusterId(clusterId); |
| 756 | + |
| 757 | + DiskProfile diskProfile = Mockito.mock(DiskProfile.class); |
| 758 | + |
| 759 | + StoragePoolAllocator allocator = Mockito.mock(StoragePoolAllocator.class); |
| 760 | + |
| 761 | + DataCenterDeployment plan = new DataCenterDeployment(dataCenterId, podId, clusterId, null, null, null); |
| 762 | + |
| 763 | + Account account = Mockito.mock(Account.class); |
| 764 | + Mockito.when(account.getId()).thenReturn(1L); |
| 765 | + Mockito.when(vmProfile.getOwner()).thenReturn(account); |
| 766 | + Mockito.when(_accountMgr.isRootAdmin(account.getId())).thenReturn(Boolean.FALSE); |
| 767 | + |
| 768 | + Mockito.when(_dcDao.findById(dataCenterId)).thenReturn(dc); |
| 769 | + Mockito.when(dc.getAllocationState()).thenReturn(AllocationState.Enabled); |
| 770 | + |
| 771 | + HostPodVO podVo = Mockito.mock(HostPodVO.class); |
| 772 | + Mockito.when(podVo.getAllocationState()).thenReturn(AllocationState.Enabled); |
| 773 | + Mockito.doReturn(podVo).when(_podDao).findById(podId); |
| 774 | + |
| 775 | + ClusterVO cluster = Mockito.mock(ClusterVO.class); |
| 776 | + Mockito.when(cluster.getAllocationState()).thenReturn(AllocationState.Enabled); |
| 777 | + Mockito.when(_clusterDao.findById(clusterId)).thenReturn(cluster); |
| 778 | + |
| 779 | + DiskOfferingVO diskOffering = Mockito.mock(DiskOfferingVO.class); |
| 780 | + |
| 781 | + Mockito.when(_diskOfferingDao.findById(vol1.getDiskOfferingId())).thenReturn(diskOffering); |
| 782 | + VirtualMachineTemplate vmt = Mockito.mock(VirtualMachineTemplate.class); |
| 783 | + |
| 784 | + ServiceOfferingVO serviceOffering = Mockito.mock(ServiceOfferingVO.class); |
| 785 | + Mockito.when(vmProfile.getServiceOffering()).thenReturn(serviceOffering); |
| 786 | + |
| 787 | + PrimaryDataStore primaryDataStore = Mockito.mock(PrimaryDataStore.class); |
| 788 | + |
| 789 | + Mockito.when(vmt.getFormat()).thenReturn(Storage.ImageFormat.ISO); |
| 790 | + Mockito.when(vmProfile.getTemplate()).thenReturn(vmt); |
| 791 | + |
| 792 | + Mockito.when(vmProfile.getId()).thenReturn(1L); |
| 793 | + Mockito.when(vmProfile.getType()).thenReturn(VirtualMachine.Type.User); |
| 794 | + Mockito.when(volDao.findUsableVolumesForInstance(1L)).thenReturn(Arrays.asList(vol1)); |
| 795 | + Mockito.when(volDao.findByInstanceAndType(1L, Volume.Type.ROOT)).thenReturn(Arrays.asList(vol1)); |
| 796 | + Mockito.when(_dataStoreManager.getPrimaryDataStore(vol1.getPoolId())).thenReturn((DataStore) primaryDataStore); |
| 797 | + Mockito.when(avoids.shouldAvoid(storagePool)).thenReturn(Boolean.FALSE); |
| 798 | + PowerMockito.whenNew(DiskProfile.class).withAnyArguments().thenReturn(diskProfile); |
| 799 | + |
| 800 | + Mockito.doReturn(Arrays.asList(storagePool)).when(allocator).allocateToPool(diskProfile, vmProfile, plan, |
| 801 | + avoids, 10); |
| 802 | + Mockito.when(volDao.update(vol1.getId(), vol1)).thenReturn(true); |
| 803 | + _dpm.findSuitablePoolsForVolumes(vmProfile, plan, avoids, 10); |
| 804 | + verify(vol1, times(1)).setPoolId(null); |
| 805 | + assertTrue(vol1.getPoolId() == null); |
| 806 | + |
| 807 | + } |
705 | 808 | // This is so ugly but everything is so intertwined... |
706 | 809 | private DeploymentClusterPlanner setupMocksForPlanDeploymentHostTests(HostVO host, VolumeVO vol1) { |
707 | 810 | long diskOfferingId = 345L; |
|
0 commit comments