Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for backup descriptor in order to backup applications #1583

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private List<CloudApplication> computeExistingAppsToBackup(ProcessContext contex
}
String mtaVersionOfCurrentDescriptor = context.getVariable(Variables.COMPLETE_DEPLOYMENT_DESCRIPTOR)
.getVersion();
return existingAppsToBackupCalculator.calculateExistingAppsToBackup(appsToUndeploy, mtaVersionOfCurrentDescriptor);
return existingAppsToBackupCalculator.calculateExistingAppsToBackup(context, appsToUndeploy, mtaVersionOfCurrentDescriptor);
}

private List<CloudApplication> computeAppsToUndeploy(List<DeployedMtaApplication> modulesToUndeploy, CloudControllerClient client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ExistingAppsToBackupCalculator(DeployedMta deployedMta, DeployedMta backu
this.descriptorBackupService = descriptorBackupService;
}

public List<CloudApplication> calculateExistingAppsToBackup(List<CloudApplication> appsToUndeploy,
public List<CloudApplication> calculateExistingAppsToBackup(ProcessContext context, List<CloudApplication> appsToUndeploy,
String mtaVersionOfCurrentDescriptor) {
if (doesDeployedMtaVersionMatchToCurrentDeployment(deployedMta, mtaVersionOfCurrentDescriptor)) {
return Collections.emptyList();
Expand All @@ -47,6 +47,10 @@ public List<CloudApplication> calculateExistingAppsToBackup(List<CloudApplicatio
return Collections.emptyList();
}

if (isDeployedMtaBackupDescriptorMissing(context, deployedMta)) {
return Collections.emptyList();
}

return getAppsWithLiveProductizationState(appsToUndeploy);
}

Expand All @@ -66,6 +70,22 @@ private boolean doesMtaVersionMatchToCurrentDeployment(DeployedMtaApplication de
return mtaVersionOfDeployedApplication != null && mtaVersionOfDeployedApplication.equals(mtaVersionOfCurrentDescriptor);
}

private boolean isDeployedMtaBackupDescriptorMissing(ProcessContext context, DeployedMta deployedMta) {
if (deployedMta == null) {
return true;
}
String deployedMtaVersion = deployedMta.getMetadata()
.getVersion()
.toString();
return descriptorBackupService.createQuery()
.mtaId(context.getVariable(Variables.MTA_ID))
.spaceId(context.getVariable(Variables.SPACE_GUID))
.namespace(context.getVariable(Variables.MTA_NAMESPACE))
.mtaVersion(deployedMtaVersion)
.list()
.isEmpty();
}

private ProductizationState getProductizationStateOfApplication(CloudApplication appToUndeploy) {
return deployedMta.getApplications()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ void setUp() throws Exception {
private static Stream<Arguments> testCalculateExistingAppsToBackup() {
return Stream.of(
// (1) Already deployed application match version of current deployment descriptor
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1")), Collections.emptyList(), "1",
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1")), Collections.emptyList(), "1", true,
List.of("app-1-live"), List.of()),
// (2) Current deployment descriptor version has different value than deployed mta
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1"),
new TestApplication("app-1", "app-1-idle", "2", ProductizationState.IDLE)),
Collections.emptyList(), "2", List.of("app-1-live", "app-1-idle"),
Collections.emptyList(), "2", true, List.of("app-1-live", "app-1-idle"),
List.of(ImmutableCloudApplication.builder()
.name("app-1-live")
.v3Metadata(Metadata.builder()
Expand All @@ -74,41 +74,48 @@ private static Stream<Arguments> testCalculateExistingAppsToBackup() {
.build())),
// (3) Current deployment descriptor match version of deployed and backup mta
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1")),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1", List.of("app-1-live"),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1", true, List.of("app-1-live"),
Collections.emptyList()),
// (4) Current deployment descriptor version has different value of deployed and backup mta
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2")),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "3", List.of("app-1-live",
"mta-backup-app-1"),
List.of(ImmutableCloudApplication.builder()
.name("app-1-live")
.v3Metadata(Metadata.builder()
.annotation(MtaMetadataAnnotations.MTA_VERSION,
"2")
.build())
.build())),
List.of(new TestApplication("app-1",
"mta-backup-app-1",
"1")),
"3", true, List.of("app-1-live", "mta-backup-app-1"), List.of(ImmutableCloudApplication.builder()
.name("app-1-live")
.v3Metadata(Metadata.builder()
.annotation(MtaMetadataAnnotations.MTA_VERSION,
"2")
.build())
.build())),
// (5) Current deployment descriptor match version of deployed mta only
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2")),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", List.of("app-1-live"),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", true, List.of("app-1-live"),
Collections.emptyList()),
// (6) Current deployment descriptor version match value of backup mta
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2")),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1",
List.of("app-1-live", "app-1-idle"), Collections.emptyList()));
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1", true,
List.of("app-1-live", "app-1-idle"), Collections.emptyList()),
// (7) Deployed mta does not have backup descriptor in db and won't be preserved
Arguments.of(List.of(new TestApplication("app-1", "app-1", "1"), new TestApplication("app-2", "app-2", "1")),
Collections.emptyList(), "2", false, Collections.emptyList(), Collections.emptyList()));
}

@ParameterizedTest
@MethodSource
void testCalculateExistingAppsToBackup(List<TestApplication> deployedApplications, List<TestApplication> backupApplications,
String mtaVersionOfCurrentDescriptor, List<String> appNamesToUndeploy,
List<CloudApplication> expectedAppsToBackup) {
String mtaVersionOfCurrentDescriptor, boolean isDescriptorAvailableInDb,
List<String> appNamesToUndeploy, List<CloudApplication> expectedAppsToBackup) {
DeployedMta deployedMta = getDeployedMta(deployedApplications);
DeployedMta backupMta = getDeployedMta(backupApplications);

prepareContext(isDescriptorAvailableInDb);

ExistingAppsToBackupCalculator calculator = new ExistingAppsToBackupCalculator(deployedMta, backupMta, descriptorBackupService);

List<CloudApplication> appsToUndeploy = getAppsToUndeploy(deployedMta.getApplications(), appNamesToUndeploy);
List<CloudApplication> appsToBackup = calculator.calculateExistingAppsToBackup(appsToUndeploy, mtaVersionOfCurrentDescriptor);
List<CloudApplication> appsToBackup = calculator.calculateExistingAppsToBackup(context, appsToUndeploy,
mtaVersionOfCurrentDescriptor);

assertEquals(expectedAppsToBackup, appsToBackup);
}
Expand Down Expand Up @@ -159,15 +166,7 @@ void testCalculateAppsToUndeploy(List<TestApplication> deployedBackupApps, List<
boolean isDescriptorAvailableInDb, List<CloudApplication> expectedAppsToUndeploy) {
DeployedMta backupMta = getDeployedMta(deployedBackupApps);

when(context.getVariable(Variables.MTA_ID)).thenReturn(MTA_ID);
when(context.getVariable(Variables.SPACE_GUID)).thenReturn(SPACE_GUID);
when(descriptorBackupService.createQuery()).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.mtaId(anyString())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.spaceId(anyString())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.namespace(any())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.mtaVersion(anyString())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.list()).thenReturn(isDescriptorAvailableInDb ? List.of(Mockito.mock(BackupDescriptor.class))
: Collections.emptyList());
prepareContext(isDescriptorAvailableInDb);

ExistingAppsToBackupCalculator calculator = new ExistingAppsToBackupCalculator(null, backupMta, descriptorBackupService);

Expand Down Expand Up @@ -210,6 +209,18 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
.build();
}

private void prepareContext(boolean isDescriptorAvailableInDb) {
when(context.getVariable(Variables.MTA_ID)).thenReturn(MTA_ID);
when(context.getVariable(Variables.SPACE_GUID)).thenReturn(SPACE_GUID);
when(descriptorBackupService.createQuery()).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.mtaId(anyString())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.spaceId(anyString())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.namespace(any())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.mtaVersion(anyString())).thenReturn(descriptorBackupQuery);
when(descriptorBackupQuery.list()).thenReturn(isDescriptorAvailableInDb ? List.of(Mockito.mock(BackupDescriptor.class))
: Collections.emptyList());
}

private List<CloudApplication> getAppsToUndeploy(List<DeployedMtaApplication> deployedApplications, List<String> appNamesToUndeploy) {
return deployedApplications.stream()
.filter(deployedApplication -> appNamesToUndeploy.contains(deployedApplication.getName()))
Expand Down
Loading