|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.configuration; |
18 | 18 |
|
| 19 | +import com.cloud.capacity.dao.CapacityDao; |
| 20 | +import com.cloud.dc.DataCenterVO; |
| 21 | +import com.cloud.dc.VlanVO; |
| 22 | +import com.cloud.dc.dao.DataCenterDao; |
| 23 | +import com.cloud.dc.dao.DataCenterIpAddressDao; |
| 24 | +import com.cloud.dc.dao.DedicatedResourceDao; |
| 25 | +import com.cloud.dc.dao.HostPodDao; |
| 26 | +import com.cloud.dc.dao.VlanDao; |
19 | 27 | import com.cloud.exception.InvalidParameterValueException; |
| 28 | +import com.cloud.host.dao.HostDao; |
| 29 | +import com.cloud.network.Network; |
| 30 | +import com.cloud.network.NetworkModel; |
| 31 | +import com.cloud.network.NetworkService; |
| 32 | +import com.cloud.network.Networks; |
| 33 | +import com.cloud.network.dao.IPAddressDao; |
| 34 | +import com.cloud.network.dao.NsxProviderDao; |
| 35 | +import com.cloud.network.dao.PhysicalNetworkDao; |
| 36 | +import com.cloud.network.element.NsxProviderVO; |
| 37 | +import com.cloud.offering.NetworkOffering; |
| 38 | +import com.cloud.offerings.NetworkOfferingVO; |
| 39 | +import com.cloud.offerings.dao.NetworkOfferingDao; |
20 | 40 | import com.cloud.storage.StorageManager; |
| 41 | +import com.cloud.storage.dao.VMTemplateZoneDao; |
| 42 | +import com.cloud.storage.dao.VolumeDao; |
21 | 43 | import com.cloud.utils.net.NetUtils; |
| 44 | +import com.cloud.vm.dao.VMInstanceDao; |
| 45 | +import org.apache.cloudstack.annotation.dao.AnnotationDao; |
| 46 | +import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd; |
| 47 | +import org.apache.cloudstack.api.command.admin.zone.DeleteZoneCmd; |
| 48 | +import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope; |
22 | 49 | import org.apache.cloudstack.framework.config.ConfigDepot; |
23 | 50 | import org.apache.cloudstack.framework.config.ConfigKey; |
| 51 | +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
| 52 | +import org.apache.cloudstack.storage.datastore.db.ImageStoreDao; |
24 | 53 | import org.junit.Assert; |
25 | 54 | import org.junit.Before; |
26 | 55 | import org.junit.Test; |
|
29 | 58 | import org.mockito.MockedStatic; |
30 | 59 | import org.mockito.Mockito; |
31 | 60 | import org.mockito.junit.MockitoJUnitRunner; |
| 61 | +import org.springframework.test.util.ReflectionTestUtils; |
32 | 62 |
|
| 63 | +import java.util.Collections; |
33 | 64 | import java.util.List; |
34 | 65 |
|
| 66 | +import static org.mockito.ArgumentMatchers.any; |
| 67 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 68 | +import static org.mockito.Mockito.when; |
| 69 | +import static org.mockito.Mockito.lenient; |
| 70 | +import static org.mockito.Mockito.anyString; |
| 71 | +import static org.mockito.Mockito.anyBoolean; |
| 72 | +import static org.mockito.Mockito.doNothing; |
| 73 | +import static org.mockito.Mockito.nullable; |
| 74 | +import static org.mockito.Mockito.anyMap; |
| 75 | +import static org.mockito.Mockito.anyList; |
| 76 | +import static org.mockito.Mockito.anyInt; |
| 77 | +import static org.mockito.Mockito.verify; |
| 78 | +import static org.mockito.Mockito.times; |
35 | 79 |
|
36 | 80 | @RunWith(MockitoJUnitRunner.class) |
37 | 81 | public class ConfigurationManagerImplTest { |
38 | 82 | @Mock |
39 | 83 | ConfigDepot configDepot; |
| 84 | + @Mock |
| 85 | + NsxProviderDao nsxProviderDao; |
| 86 | + @Mock |
| 87 | + DataCenterDao zoneDao; |
| 88 | + @Mock |
| 89 | + HostDao hostDao; |
| 90 | + @Mock |
| 91 | + HostPodDao podDao; |
| 92 | + @Mock |
| 93 | + DataCenterIpAddressDao ipAddressDao; |
| 94 | + @Mock |
| 95 | + IPAddressDao publicIpAddressDao; |
| 96 | + @Mock |
| 97 | + VMInstanceDao vmInstanceDao; |
| 98 | + @Mock |
| 99 | + VolumeDao volumeDao; |
| 100 | + @Mock |
| 101 | + PhysicalNetworkDao physicalNetworkDao; |
| 102 | + @Mock |
| 103 | + ImageStoreDao imageStoreDao; |
| 104 | + @Mock |
| 105 | + VlanDao vlanDao; |
| 106 | + @Mock |
| 107 | + VMTemplateZoneDao vmTemplateZoneDao; |
| 108 | + @Mock |
| 109 | + CapacityDao capacityDao; |
| 110 | + @Mock |
| 111 | + DedicatedResourceDao dedicatedResourceDao; |
| 112 | + @Mock |
| 113 | + AnnotationDao annotationDao; |
| 114 | + @Mock |
| 115 | + ConfigurationDao configDao; |
| 116 | + @Mock |
| 117 | + NetworkOfferingDao networkOfferingDao; |
| 118 | + @Mock |
| 119 | + NetworkService networkService; |
| 120 | + @Mock |
| 121 | + NetworkModel networkModel; |
| 122 | + |
40 | 123 | ConfigurationManagerImpl configurationManagerImplSpy = Mockito.spy(new ConfigurationManagerImpl()); |
| 124 | + DeleteZoneCmd deleteZoneCmd; |
| 125 | + CreateNetworkOfferingCmd createNetworkOfferingCmd; |
41 | 126 |
|
42 | 127 | @Before |
43 | 128 | public void setUp() throws Exception { |
44 | 129 | configurationManagerImplSpy._configDepot = configDepot; |
| 130 | + configurationManagerImplSpy.nsxProviderDao = nsxProviderDao; |
| 131 | + configurationManagerImplSpy._zoneDao = zoneDao; |
| 132 | + configurationManagerImplSpy._hostDao = hostDao; |
| 133 | + configurationManagerImplSpy._podDao = podDao; |
| 134 | + configurationManagerImplSpy._privateIpAddressDao = ipAddressDao; |
| 135 | + configurationManagerImplSpy._publicIpAddressDao = publicIpAddressDao; |
| 136 | + configurationManagerImplSpy._vmInstanceDao = vmInstanceDao; |
| 137 | + configurationManagerImplSpy._volumeDao = volumeDao; |
| 138 | + configurationManagerImplSpy._physicalNetworkDao = physicalNetworkDao; |
| 139 | + configurationManagerImplSpy._imageStoreDao = imageStoreDao; |
| 140 | + configurationManagerImplSpy._vlanDao = vlanDao; |
| 141 | + configurationManagerImplSpy._capacityDao = capacityDao; |
| 142 | + configurationManagerImplSpy._dedicatedDao = dedicatedResourceDao; |
| 143 | + configurationManagerImplSpy._configDao = configDao; |
| 144 | + configurationManagerImplSpy._networkOfferingDao = networkOfferingDao; |
| 145 | + configurationManagerImplSpy._networkSvc = networkService; |
| 146 | + configurationManagerImplSpy._networkModel = networkModel; |
| 147 | + ReflectionTestUtils.setField(configurationManagerImplSpy, "templateZoneDao", vmTemplateZoneDao); |
| 148 | + ReflectionTestUtils.setField(configurationManagerImplSpy, "annotationDao", annotationDao); |
| 149 | + |
| 150 | + deleteZoneCmd = Mockito.mock(DeleteZoneCmd.class); |
| 151 | + createNetworkOfferingCmd = Mockito.mock(CreateNetworkOfferingCmd.class); |
45 | 152 | } |
46 | 153 |
|
47 | 154 | @Test |
@@ -250,4 +357,55 @@ public void testValidateIpAddressRelatedConfigValuesValidIpRange() { |
250 | 357 | Mockito.doReturn(key).when(configurationManagerImplSpy._configDepot).get("config.iprange"); |
251 | 358 | configurationManagerImplSpy.validateIpAddressRelatedConfigValues("config.iprange", "192.168.1.1-192.168.1.100"); |
252 | 359 | } |
| 360 | + |
| 361 | + @Test |
| 362 | + public void testDeleteZoneInvokesDeleteNsxProviderWhenNSXIsEnabled() { |
| 363 | + NsxProviderVO nsxProviderVO = Mockito.mock(NsxProviderVO.class); |
| 364 | + DataCenterVO dataCenterVO = Mockito.mock(DataCenterVO.class); |
| 365 | + |
| 366 | + when(nsxProviderDao.findByZoneId(anyLong())).thenReturn(nsxProviderVO); |
| 367 | + when(zoneDao.findById(anyLong())).thenReturn(dataCenterVO); |
| 368 | + lenient().when(hostDao.findByDataCenterId(anyLong())).thenReturn(Collections.emptyList()); |
| 369 | + when(podDao.listByDataCenterId(anyLong())).thenReturn(Collections.emptyList()); |
| 370 | + when(ipAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); |
| 371 | + when(publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); |
| 372 | + when(vmInstanceDao.listByZoneId(anyLong())).thenReturn(Collections.emptyList()); |
| 373 | + when(volumeDao.findByDc(anyLong())).thenReturn(Collections.emptyList()); |
| 374 | + when(physicalNetworkDao.listByZone(anyLong())).thenReturn(Collections.emptyList()); |
| 375 | + when(imageStoreDao.findByZone(any(ZoneScope.class), nullable(Boolean.class))).thenReturn(Collections.emptyList()); |
| 376 | + when(vlanDao.listByZone(anyLong())).thenReturn(List.of(Mockito.mock(VlanVO.class))); |
| 377 | + when(nsxProviderVO.getId()).thenReturn(1L); |
| 378 | + when(zoneDao.remove(anyLong())).thenReturn(true); |
| 379 | + when(capacityDao.removeBy(nullable(Short.class), anyLong(), nullable(Long.class), nullable(Long.class), nullable(Long.class))).thenReturn(true); |
| 380 | + when(dedicatedResourceDao.findByZoneId(anyLong())).thenReturn(null); |
| 381 | + lenient().when(annotationDao.removeByEntityType(anyString(), anyString())).thenReturn(true); |
| 382 | + |
| 383 | + configurationManagerImplSpy.deleteZone(deleteZoneCmd); |
| 384 | + |
| 385 | + verify(nsxProviderDao, times(1)).remove(anyLong()); |
| 386 | + } |
| 387 | + |
| 388 | + @Test |
| 389 | + public void testCreateNetworkOfferingForNsx() { |
| 390 | + NetworkOfferingVO offeringVO = Mockito.mock(NetworkOfferingVO.class); |
| 391 | + |
| 392 | + when(createNetworkOfferingCmd.isForNsx()).thenReturn(true); |
| 393 | + when(createNetworkOfferingCmd.getNsxMode()).thenReturn(NetworkOffering.NsxMode.NATTED.name()); |
| 394 | + when(createNetworkOfferingCmd.getTraffictype()).thenReturn(Networks.TrafficType.Guest.name()); |
| 395 | + when(createNetworkOfferingCmd.getGuestIpType()).thenReturn(Network.GuestType.Isolated.name()); |
| 396 | + when(createNetworkOfferingCmd.getAvailability()).thenReturn(NetworkOffering.Availability.Optional.name()); |
| 397 | + lenient().when(configurationManagerImplSpy.createNetworkOffering(anyString(), anyString(), any(Networks.TrafficType.class), anyString(), |
| 398 | + anyBoolean(), any(NetworkOffering.Availability.class), anyInt(), anyMap(), anyBoolean(), any(Network.GuestType.class), |
| 399 | + anyBoolean(), anyLong(), anyBoolean(), anyMap(), anyBoolean(), anyBoolean(), anyMap(), anyBoolean(), anyInt(), |
| 400 | + anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyString(), anyList(), anyList(), anyBoolean(), any(NetUtils.InternetProtocol.class))) |
| 401 | + .thenReturn(offeringVO); |
| 402 | + when(configDao.getValue(anyString())).thenReturn("1000"); |
| 403 | + lenient().when(networkOfferingDao.persist(any(NetworkOfferingVO.class), anyMap())).thenReturn(offeringVO); |
| 404 | + doNothing().when(networkService).validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter(anyLong()); |
| 405 | + doNothing().when(networkModel).canProviderSupportServices(anyMap()); |
| 406 | + |
| 407 | + NetworkOffering offering = configurationManagerImplSpy.createNetworkOffering(createNetworkOfferingCmd); |
| 408 | + |
| 409 | + Assert.assertNotNull(offering); |
| 410 | + } |
253 | 411 | } |
0 commit comments