Skip to content

Commit 17439e4

Browse files
committed
Check for backup descriptor in order to backup applications
1 parent 3ee6b29 commit 17439e4

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/steps/BuildCloudUndeployModelStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private List<CloudApplication> computeExistingAppsToBackup(ProcessContext contex
367367
}
368368
String mtaVersionOfCurrentDescriptor = context.getVariable(Variables.COMPLETE_DEPLOYMENT_DESCRIPTOR)
369369
.getVersion();
370-
return existingAppsToBackupCalculator.calculateExistingAppsToBackup(appsToUndeploy, mtaVersionOfCurrentDescriptor);
370+
return existingAppsToBackupCalculator.calculateExistingAppsToBackup(context, appsToUndeploy, mtaVersionOfCurrentDescriptor);
371371
}
372372

373373
private List<CloudApplication> computeAppsToUndeploy(List<DeployedMtaApplication> modulesToUndeploy, CloudControllerClient client) {

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/util/ExistingAppsToBackupCalculator.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ExistingAppsToBackupCalculator(DeployedMta deployedMta, DeployedMta backu
3737
this.descriptorBackupService = descriptorBackupService;
3838
}
3939

40-
public List<CloudApplication> calculateExistingAppsToBackup(List<CloudApplication> appsToUndeploy,
40+
public List<CloudApplication> calculateExistingAppsToBackup(ProcessContext context, List<CloudApplication> appsToUndeploy,
4141
String mtaVersionOfCurrentDescriptor) {
4242
if (doesDeployedMtaVersionMatchToCurrentDeployment(deployedMta, mtaVersionOfCurrentDescriptor)) {
4343
return Collections.emptyList();
@@ -47,6 +47,10 @@ public List<CloudApplication> calculateExistingAppsToBackup(List<CloudApplicatio
4747
return Collections.emptyList();
4848
}
4949

50+
if (doesDeployedMtaBackupDescriptorIsMissing(context, deployedMta)) {
51+
return Collections.emptyList();
52+
}
53+
5054
return getAppsWithLiveProductizationState(appsToUndeploy);
5155
}
5256

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

73+
private boolean doesDeployedMtaBackupDescriptorIsMissing(ProcessContext context, DeployedMta deployedMta) {
74+
if (deployedMta == null) {
75+
return true;
76+
}
77+
String deployedMtaVersion = deployedMta.getMetadata()
78+
.getVersion()
79+
.toString();
80+
return descriptorBackupService.createQuery()
81+
.mtaId(context.getVariable(Variables.MTA_ID))
82+
.spaceId(context.getVariable(Variables.SPACE_GUID))
83+
.namespace(context.getVariable(Variables.MTA_NAMESPACE))
84+
.mtaVersion(deployedMtaVersion)
85+
.list()
86+
.isEmpty();
87+
}
88+
6989
private ProductizationState getProductizationStateOfApplication(CloudApplication appToUndeploy) {
7090
return deployedMta.getApplications()
7191
.stream()

multiapps-controller-process/src/test/java/org/cloudfoundry/multiapps/controller/process/util/ExistingAppsToBackupCalculatorTest.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ void setUp() throws Exception {
5959
private static Stream<Arguments> testCalculateExistingAppsToBackup() {
6060
return Stream.of(
6161
// (1) Already deployed application match version of current deployment descriptor
62-
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1")), Collections.emptyList(), "1",
62+
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1")), Collections.emptyList(), "1", true,
6363
List.of("app-1-live"), List.of()),
6464
// (2) Current deployment descriptor version has different value than deployed mta
6565
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1"),
6666
new TestApplication("app-1", "app-1-idle", "2", ProductizationState.IDLE)),
67-
Collections.emptyList(), "2", List.of("app-1-live", "app-1-idle"),
67+
Collections.emptyList(), "2", true, List.of("app-1-live", "app-1-idle"),
6868
List.of(ImmutableCloudApplication.builder()
6969
.name("app-1-live")
7070
.v3Metadata(Metadata.builder()
@@ -74,41 +74,48 @@ private static Stream<Arguments> testCalculateExistingAppsToBackup() {
7474
.build())),
7575
// (3) Current deployment descriptor match version of deployed and backup mta
7676
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "1")),
77-
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1", List.of("app-1-live"),
77+
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1", true, List.of("app-1-live"),
7878
Collections.emptyList()),
7979
// (4) Current deployment descriptor version has different value of deployed and backup mta
8080
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2")),
81-
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "3", List.of("app-1-live",
82-
"mta-backup-app-1"),
83-
List.of(ImmutableCloudApplication.builder()
84-
.name("app-1-live")
85-
.v3Metadata(Metadata.builder()
86-
.annotation(MtaMetadataAnnotations.MTA_VERSION,
87-
"2")
88-
.build())
89-
.build())),
81+
List.of(new TestApplication("app-1",
82+
"mta-backup-app-1",
83+
"1")),
84+
"3", true, List.of("app-1-live", "mta-backup-app-1"), List.of(ImmutableCloudApplication.builder()
85+
.name("app-1-live")
86+
.v3Metadata(Metadata.builder()
87+
.annotation(MtaMetadataAnnotations.MTA_VERSION,
88+
"2")
89+
.build())
90+
.build())),
9091
// (5) Current deployment descriptor match version of deployed mta only
9192
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2")),
92-
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", List.of("app-1-live"),
93+
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", true, List.of("app-1-live"),
9394
Collections.emptyList()),
9495
// (6) Current deployment descriptor version match value of backup mta
9596
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2")),
96-
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1",
97-
List.of("app-1-live", "app-1-idle"), Collections.emptyList()));
97+
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "1", true,
98+
List.of("app-1-live", "app-1-idle"), Collections.emptyList()),
99+
// (7) Deployed mta does not have backup descriptor in db and won't be preserved
100+
Arguments.of(List.of(new TestApplication("app-1", "app-1", "1"), new TestApplication("app-2", "app-2", "1")),
101+
Collections.emptyList(), "2", false, Collections.emptyList(), Collections.emptyList()));
98102
}
99103

100104
@ParameterizedTest
101105
@MethodSource
102106
void testCalculateExistingAppsToBackup(List<TestApplication> deployedApplications, List<TestApplication> backupApplications,
103-
String mtaVersionOfCurrentDescriptor, List<String> appNamesToUndeploy,
104-
List<CloudApplication> expectedAppsToBackup) {
107+
String mtaVersionOfCurrentDescriptor, boolean isDescriptorAvailableInDb,
108+
List<String> appNamesToUndeploy, List<CloudApplication> expectedAppsToBackup) {
105109
DeployedMta deployedMta = getDeployedMta(deployedApplications);
106110
DeployedMta backupMta = getDeployedMta(backupApplications);
107111

112+
prepareContext(isDescriptorAvailableInDb);
113+
108114
ExistingAppsToBackupCalculator calculator = new ExistingAppsToBackupCalculator(deployedMta, backupMta, descriptorBackupService);
109115

110116
List<CloudApplication> appsToUndeploy = getAppsToUndeploy(deployedMta.getApplications(), appNamesToUndeploy);
111-
List<CloudApplication> appsToBackup = calculator.calculateExistingAppsToBackup(appsToUndeploy, mtaVersionOfCurrentDescriptor);
117+
List<CloudApplication> appsToBackup = calculator.calculateExistingAppsToBackup(context, appsToUndeploy,
118+
mtaVersionOfCurrentDescriptor);
112119

113120
assertEquals(expectedAppsToBackup, appsToBackup);
114121
}
@@ -159,15 +166,7 @@ void testCalculateAppsToUndeploy(List<TestApplication> deployedBackupApps, List<
159166
boolean isDescriptorAvailableInDb, List<CloudApplication> expectedAppsToUndeploy) {
160167
DeployedMta backupMta = getDeployedMta(deployedBackupApps);
161168

162-
when(context.getVariable(Variables.MTA_ID)).thenReturn(MTA_ID);
163-
when(context.getVariable(Variables.SPACE_GUID)).thenReturn(SPACE_GUID);
164-
when(descriptorBackupService.createQuery()).thenReturn(descriptorBackupQuery);
165-
when(descriptorBackupQuery.mtaId(anyString())).thenReturn(descriptorBackupQuery);
166-
when(descriptorBackupQuery.spaceId(anyString())).thenReturn(descriptorBackupQuery);
167-
when(descriptorBackupQuery.namespace(any())).thenReturn(descriptorBackupQuery);
168-
when(descriptorBackupQuery.mtaVersion(anyString())).thenReturn(descriptorBackupQuery);
169-
when(descriptorBackupQuery.list()).thenReturn(isDescriptorAvailableInDb ? List.of(Mockito.mock(BackupDescriptor.class))
170-
: Collections.emptyList());
169+
prepareContext(isDescriptorAvailableInDb);
171170

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

@@ -210,6 +209,18 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
210209
.build();
211210
}
212211

212+
private void prepareContext(boolean isDescriptorAvailableInDb) {
213+
when(context.getVariable(Variables.MTA_ID)).thenReturn(MTA_ID);
214+
when(context.getVariable(Variables.SPACE_GUID)).thenReturn(SPACE_GUID);
215+
when(descriptorBackupService.createQuery()).thenReturn(descriptorBackupQuery);
216+
when(descriptorBackupQuery.mtaId(anyString())).thenReturn(descriptorBackupQuery);
217+
when(descriptorBackupQuery.spaceId(anyString())).thenReturn(descriptorBackupQuery);
218+
when(descriptorBackupQuery.namespace(any())).thenReturn(descriptorBackupQuery);
219+
when(descriptorBackupQuery.mtaVersion(anyString())).thenReturn(descriptorBackupQuery);
220+
when(descriptorBackupQuery.list()).thenReturn(isDescriptorAvailableInDb ? List.of(Mockito.mock(BackupDescriptor.class))
221+
: Collections.emptyList());
222+
}
223+
213224
private List<CloudApplication> getAppsToUndeploy(List<DeployedMtaApplication> deployedApplications, List<String> appNamesToUndeploy) {
214225
return deployedApplications.stream()
215226
.filter(deployedApplication -> appNamesToUndeploy.contains(deployedApplication.getName()))

0 commit comments

Comments
 (0)