Merge release branch 4.19 to main

* 4.19:
  server: fix duplicated records for templates if there are multiple zones (#8933)
This commit is contained in:
Daan Hoogland 2024-04-23 12:22:53 +02:00
commit c31aebc188
2 changed files with 12 additions and 15 deletions

View File

@ -304,12 +304,12 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
zonesIds = _dcDao.listAllZones().stream().map(DataCenterVO::getId).collect(Collectors.toList());
}
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zonesIds, profile);
for (long zoneId : zonesIds) {
DataStore imageStore = verifyHeuristicRulesForZone(template, zoneId);
if (imageStore == null) {
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile);
standardImageStoreAllocation(imageStores, template);
} else {
validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, null);
@ -317,8 +317,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
}
}
protected List<DataStore> getImageStoresThrowsExceptionIfNotFound(List<Long> zonesIds, TemplateProfile profile) {
List<DataStore> imageStores = storeMgr.getImageStoresByZoneIds(zonesIds.toArray(new Long[0]));
protected List<DataStore> getImageStoresThrowsExceptionIfNotFound(long zoneId, TemplateProfile profile) {
List<DataStore> imageStores = storeMgr.getImageStoresByZoneIds(zoneId);
if (imageStores == null || imageStores.size() == 0) {
throw new CloudRuntimeException(String.format("Unable to find image store to download the template [%s].", profile.getTemplate()));
}
@ -424,7 +424,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
List<TemplateOrVolumePostUploadCommand> payloads = new LinkedList<>();
if (imageStore == null) {
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(List.of(zoneId), profile);
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile);
postUploadAllocation(imageStores, template, payloads);
} else {
postUploadAllocation(List.of(imageStore), template, payloads);

View File

@ -153,6 +153,8 @@ public class HypervisorTemplateAdapterTest {
private AutoCloseable closeable;
private static final long zoneId = 1L;
@Before
public void before() {
closeable = MockitoAnnotations.openMocks(this);
@ -325,7 +327,6 @@ public class HypervisorTemplateAdapterTest {
VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class);
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(null);
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
_adapter.createTemplateWithinZones(templateProfileMock, vmTemplateVOMock);
@ -339,7 +340,7 @@ public class HypervisorTemplateAdapterTest {
List<Long> zoneIds = List.of(1L);
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds);
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(Long.class), Mockito.any(TemplateProfile.class));
Mockito.doReturn(null).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong());
Mockito.doNothing().when(_adapter).standardImageStoreAllocation(Mockito.isNull(), Mockito.any(VMTemplateVO.class));
@ -352,10 +353,10 @@ public class HypervisorTemplateAdapterTest {
public void createTemplateWithinZonesTestZoneDoesNotHaveActiveHeuristicRulesShouldCallStandardImageStoreAllocation() {
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class);
List<Long> zoneIds = List.of(1L);
List<Long> zoneIds = List.of(zoneId);
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds);
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(Long.class), Mockito.any(TemplateProfile.class));
Mockito.doReturn(null).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong());
Mockito.doNothing().when(_adapter).standardImageStoreAllocation(Mockito.isNull(), Mockito.any(VMTemplateVO.class));
@ -372,7 +373,6 @@ public class HypervisorTemplateAdapterTest {
List<Long> zoneIds = List.of(1L);
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds);
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
Mockito.doReturn(dataStoreMock).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong());
Mockito.doNothing().when(_adapter).validateSecondaryStorageAndCreateTemplate(Mockito.any(List.class), Mockito.any(VMTemplateVO.class), Mockito.isNull());
@ -384,34 +384,31 @@ public class HypervisorTemplateAdapterTest {
@Test(expected = CloudRuntimeException.class)
public void getImageStoresThrowsExceptionIfNotFoundTestNullImageStoreShouldThrowCloudRuntimeException() {
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
List<Long> zoneIds = List.of(1L);
Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(null);
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock);
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock);
}
@Test(expected = CloudRuntimeException.class)
public void getImageStoresThrowsExceptionIfNotFoundTestEmptyImageStoreShouldThrowCloudRuntimeException() {
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
List<Long> zoneIds = List.of(1L);
List<DataStore> imageStoresList = new ArrayList<>();
Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(imageStoresList);
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock);
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock);
}
@Test
public void getImageStoresThrowsExceptionIfNotFoundTestNonEmptyImageStoreShouldNotThrowCloudRuntimeException() {
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
List<Long> zoneIds = List.of(1L);
DataStore dataStoreMock = Mockito.mock(DataStore.class);
List<DataStore> imageStoresList = List.of(dataStoreMock);
Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(imageStoresList);
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock);
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock);
}
@Test