private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDeployment plan) { DataCenterVO mockDc = mock(DataCenterVO.class); VMInstanceVO vm = mock(VMInstanceVO.class); UserVmVO userVm = mock(UserVmVO.class); ServiceOfferingVO offering = mock(ServiceOfferingVO.class); AccountVO account = mock(AccountVO.class); when(account.getId()).thenReturn(accountId); when(account.getAccountId()).thenReturn(accountId); when(vmProfile.getOwner()).thenReturn(account); when(vmProfile.getVirtualMachine()).thenReturn(vm); when(vmProfile.getId()).thenReturn(12L); when(vmDao.findById(12L)).thenReturn(userVm); when(userVm.getAccountId()).thenReturn(accountId); when(vm.getDataCenterId()).thenReturn(dataCenterId); when(dcDao.findById(1L)).thenReturn(mockDc); when(plan.getDataCenterId()).thenReturn(dataCenterId); when(plan.getClusterId()).thenReturn(null); when(plan.getPodId()).thenReturn(null); when(configDao.getValue(anyString())).thenReturn("false").thenReturn("CPU"); // Mock offering details. when(vmProfile.getServiceOffering()).thenReturn(offering); when(offering.getId()).thenReturn(offeringId); when(vmProfile.getServiceOfferingId()).thenReturn(offeringId); when(offering.getCpu()).thenReturn(noOfCpusInOffering); when(offering.getSpeed()).thenReturn(cpuSpeedInOffering); when(offering.getRamSize()).thenReturn(ramInOffering); List<Long> clustersWithEnoughCapacity = new ArrayList<Long>(); clustersWithEnoughCapacity.add(1L); clustersWithEnoughCapacity.add(2L); clustersWithEnoughCapacity.add(3L); when(capacityDao.listClustersInZoneOrPodByHostCapacities( dataCenterId, noOfCpusInOffering * cpuSpeedInOffering, ramInOffering * 1024L * 1024L, CapacityVO.CAPACITY_TYPE_CPU, true)) .thenReturn(clustersWithEnoughCapacity); Map<Long, Double> clusterCapacityMap = new HashMap<Long, Double>(); clusterCapacityMap.put(1L, 2048D); clusterCapacityMap.put(2L, 2048D); clusterCapacityMap.put(3L, 2048D); Pair<List<Long>, Map<Long, Double>> clustersOrderedByCapacity = new Pair<List<Long>, Map<Long, Double>>(clustersWithEnoughCapacity, clusterCapacityMap); when(capacityDao.orderClustersByAggregateCapacity( dataCenterId, CapacityVO.CAPACITY_TYPE_CPU, true)) .thenReturn(clustersOrderedByCapacity); List<Long> disabledClusters = new ArrayList<Long>(); List<Long> clustersWithDisabledPods = new ArrayList<Long>(); when(clusterDao.listDisabledClusters(dataCenterId, null)).thenReturn(disabledClusters); when(clusterDao.listClustersWithDisabledPods(dataCenterId)) .thenReturn(clustersWithDisabledPods); }
@Test public void checkWhenDcInAvoidList() throws InsufficientServerCapacityException { DataCenterVO mockDc = mock(DataCenterVO.class); ExcludeList avoids = mock(ExcludeList.class); VirtualMachineProfileImpl vmProfile = mock(VirtualMachineProfileImpl.class); VMInstanceVO vm = mock(VMInstanceVO.class); DataCenterDeployment plan = mock(DataCenterDeployment.class); when(avoids.shouldAvoid(mockDc)).thenReturn(true); when(vmProfile.getVirtualMachine()).thenReturn(vm); when(vm.getDataCenterId()).thenReturn(1L); when(dcDao.findById(1L)).thenReturn(mockDc); List<Long> clusterList = planner.orderClusters(vmProfile, plan, avoids); assertTrue( "Cluster list should be null/empty if the dc is in avoid list", (clusterList == null || clusterList.isEmpty())); }