Skip to content

Commit 05e5d11

Browse files
committed
refactor: improve testability of context construction
1 parent defb9da commit 05e5d11

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/backup/context/BackupClusterContextAppender.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,27 @@ public BackupClusterContextAppender(
3737
public void appendContext(StackGresBackup backup, Builder contextBuilder) {
3838
final String clusterNamespace = StackGresUtil.getNamespaceFromRelativeId(
3939
backup.getSpec().getSgCluster(), backup.getMetadata().getNamespace());
40-
if (!clusterNamespace.equals(backup.getMetadata().getNamespace())) {
41-
contextBuilder.foundCluster(Optional.empty());
42-
return;
43-
}
44-
45-
if (BackupStatus.isFinished(backup)) {
40+
final String clusterName = StackGresUtil.getNameFromRelativeId(
41+
backup.getSpec().getSgCluster());
42+
if (BackupStatus.isFinished(backup)
43+
&& !StackGresUtil.isRelativeIdNotInSameNamespace(backup.getSpec().getSgCluster())) {
4644
contextBuilder.foundCluster(Optional.empty());
4745
return;
4846
}
4947

5048
final Optional<StackGresCluster> foundCluster = clusterFinder
5149
.findByNameAndNamespace(
52-
backup.getSpec().getSgCluster(),
53-
backup.getMetadata().getNamespace());
50+
clusterName,
51+
clusterNamespace);
5452

5553
contextBuilder.foundCluster(foundCluster);
5654

5755
if (foundCluster.isEmpty()) {
56+
if (StackGresUtil.isRelativeIdNotInSameNamespace(backup.getSpec().getSgCluster())) {
57+
return;
58+
}
5859
throw new IllegalArgumentException(
59-
StackGresCluster.KIND + " " + backup.getSpec().getSgCluster() + " was not found");
60+
StackGresCluster.KIND + " " + clusterName + " was not found");
6061
}
6162
final StackGresCluster cluster = foundCluster.get();
6263
backupClusterInstanceProfileContextAppender.appendContext(cluster, contextBuilder);

stackgres-k8s/src/operator/src/test/java/io/stackgres/operator/conciliation/backup/context/BackupClusterContextAppenderTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
99
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
import static org.mockito.ArgumentMatchers.any;
1011
import static org.mockito.Mockito.verify;
1112
import static org.mockito.Mockito.when;
1213

@@ -70,6 +71,19 @@ void givenBackupWithCluster_shouldPass() {
7071
verify(backupClusterObjectStorageContextAppender).appendContext(cluster, contextBuilder);
7172
}
7273

74+
@Test
75+
void givenBackupCopyWithCluster_shouldPass() {
76+
backup.getSpec().setSgCluster("test.test");
77+
when(clusterFinder.findByNameAndNamespace(
78+
"test",
79+
"test"))
80+
.thenReturn(Optional.of(cluster));
81+
contextAppender.appendContext(backup, contextBuilder);
82+
verify(contextBuilder).foundCluster(Optional.of(cluster));
83+
verify(backupClusterInstanceProfileContextAppender).appendContext(cluster, contextBuilder);
84+
verify(backupClusterObjectStorageContextAppender).appendContext(cluster, contextBuilder);
85+
}
86+
7387
@Test
7488
void givenBackupWithoutCluster_shouldFail() {
7589
when(clusterFinder.findByNameAndNamespace(
@@ -81,6 +95,17 @@ void givenBackupWithoutCluster_shouldFail() {
8195
assertEquals("SGCluster backup-with-default-storage was not found", ex.getMessage());
8296
}
8397

98+
@Test
99+
void givenBackupCopyWithoutCluster_shouldPass() {
100+
backup.getSpec().setSgCluster("test.test");
101+
when(clusterFinder.findByNameAndNamespace("test", "test"))
102+
.thenReturn(Optional.empty());
103+
contextAppender.appendContext(backup, contextBuilder);
104+
verify(contextBuilder).foundCluster(Optional.empty());
105+
verify(backupClusterInstanceProfileContextAppender, Mockito.never()).appendContext(Mockito.any(), Mockito.any());
106+
verify(backupClusterObjectStorageContextAppender, Mockito.never()).appendContext(Mockito.any(), Mockito.any());
107+
}
108+
84109
@Test
85110
void givenCompletedBackupWithoutCluster_shouldPass() {
86111
backup.setStatus(
@@ -98,13 +123,4 @@ void givenCompletedBackupWithoutCluster_shouldPass() {
98123
verify(backupClusterObjectStorageContextAppender, Mockito.never()).appendContext(Mockito.any(), Mockito.any());
99124
}
100125

101-
@Test
102-
void givenBackupInAnotherNamespaceWithoutCluster_shouldPass() {
103-
backup.getSpec().setSgCluster("test.test");
104-
contextAppender.appendContext(backup, contextBuilder);
105-
verify(contextBuilder).foundCluster(Optional.empty());
106-
verify(backupClusterInstanceProfileContextAppender, Mockito.never()).appendContext(Mockito.any(), Mockito.any());
107-
verify(backupClusterObjectStorageContextAppender, Mockito.never()).appendContext(Mockito.any(), Mockito.any());
108-
}
109-
110126
}

0 commit comments

Comments
 (0)