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

View File

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