use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class BackupRequiredResourcesGeneratorTest method givenValidCluster_getRequiredResourcesAllReturnedResourcesShouldHaveTheOwnerReference.
@Test
void givenValidCluster_getRequiredResourcesAllReturnedResourcesShouldHaveTheOwnerReference() {
final String backupNamespace = backup.getMetadata().getNamespace();
final String clusterName = backup.getSpec().getSgCluster();
final StackGresClusterSpec clusterSpec = cluster.getSpec();
final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
final String backupConfigName = clusterConfiguration.getBackupConfig();
when(clusterFinder.findByNameAndNamespace(any(), any())).thenReturn(Optional.of(cluster));
when(backupConfigFinder.findByNameAndNamespace(backupConfigName, backupNamespace)).thenReturn(Optional.of(this.backupConfig));
List<HasMetadata> resources = generator.getRequiredResources(backup);
resources.forEach(resource -> {
assertNotNull(resource.getMetadata().getOwnerReferences(), "Resource " + resource.getMetadata().getName() + " doesn't owner references");
if (resource.getMetadata().getOwnerReferences().size() == 0) {
fail("Resource " + resource.getMetadata().getName() + " doesn't have any owner");
}
assertTrue(resource.getMetadata().getOwnerReferences().stream().anyMatch(ownerReference -> ownerReference.getApiVersion().equals(HasMetadata.getApiVersion(StackGresBackup.class)) && ownerReference.getKind().equals(HasMetadata.getKind(StackGresBackup.class)) && ownerReference.getName().equals(backup.getMetadata().getName()) && ownerReference.getUid().equals(backup.getMetadata().getUid()) && Optional.ofNullable(ownerReference.getBlockOwnerDeletion()).orElse(Boolean.FALSE).equals(Boolean.FALSE)));
});
verify(clusterFinder, times(1)).findByNameAndNamespace(any(), any());
verify(clusterFinder).findByNameAndNamespace(eq(clusterName), eq(backupNamespace));
verify(backupConfigFinder, times(1)).findByNameAndNamespace(any(), any());
verify(backupConfigFinder).findByNameAndNamespace(eq(backupConfigName), eq(backupNamespace));
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class BackupRequiredResourcesGeneratorTest method givenABackupWithClusterWithoutBackupConfig_getRequiredResourcesShouldFail.
@Test
void givenABackupWithClusterWithoutBackupConfig_getRequiredResourcesShouldFail() {
final String backupNamespace = backup.getMetadata().getNamespace();
final String backupName = backup.getMetadata().getName();
final String clusterName = backup.getSpec().getSgCluster();
final StackGresClusterSpec clusterSpec = cluster.getSpec();
final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
clusterConfiguration.setBackupConfig(null);
when(clusterFinder.findByNameAndNamespace(any(), any())).thenReturn(Optional.of(cluster));
assertException("SGBackup " + backupNamespace + "/" + backupName + " target SGCluster " + clusterName + " without a SGBackupConfig");
verify(clusterFinder, times(1)).findByNameAndNamespace(any(), any());
verify(clusterFinder).findByNameAndNamespace(eq(clusterName), eq(backupNamespace));
verify(backupConfigFinder, times(0)).findByNameAndNamespace(any(), any());
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class BackupRequiredResourcesGeneratorTest method givenABackupWithClusterWithInvalidBackupConfig_getRequiredResourcesShouldFail.
@Test
void givenABackupWithClusterWithInvalidBackupConfig_getRequiredResourcesShouldFail() {
final String backupNamespace = backup.getMetadata().getNamespace();
final String backupName = backup.getMetadata().getName();
final String clusterName = backup.getSpec().getSgCluster();
final StackGresClusterSpec clusterSpec = cluster.getSpec();
final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
final String backupConfigName = clusterConfiguration.getBackupConfig();
when(clusterFinder.findByNameAndNamespace(any(), any())).thenReturn(Optional.of(cluster));
when(backupConfigFinder.findByNameAndNamespace(backupConfigName, backupNamespace)).thenReturn(Optional.empty());
assertException("SGBackup " + backupNamespace + "/" + backupName + " target SGCluster " + clusterName + " with a non existent SGBackupConfig " + backupConfigName);
verify(clusterFinder, times(1)).findByNameAndNamespace(any(), any());
verify(clusterFinder).findByNameAndNamespace(eq(clusterName), eq(backupNamespace));
verify(backupConfigFinder, times(1)).findByNameAndNamespace(any(), any());
verify(backupConfigFinder).findByNameAndNamespace(eq(backupConfigName), eq(backupNamespace));
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class ClusterRequiredResourcesGeneratorTest method givenValidCluster_getRequiredResourcesAllReturnedResourcesShouldHaveTheOwnerReference.
@Test
void givenValidCluster_getRequiredResourcesAllReturnedResourcesShouldHaveTheOwnerReference() {
final ObjectMeta metadata = cluster.getMetadata();
final String clusterNamespace = metadata.getNamespace();
final StackGresClusterSpec clusterSpec = cluster.getSpec();
final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
final String backupConfigName = clusterConfiguration.getBackupConfig();
mockBackupConfig(clusterNamespace, backupConfigName);
final String postgresConfigName = clusterConfiguration.getPostgresConfig();
mockPgConfig(clusterNamespace, postgresConfigName);
final String connectionPoolingConfig = clusterConfiguration.getConnectionPoolingConfig();
mockPoolingConfig(clusterNamespace, connectionPoolingConfig);
final String resourceProfile = clusterSpec.getResourceProfile();
when(profileConfigFinder.findByNameAndNamespace(resourceProfile, clusterNamespace)).thenReturn(Optional.of(instanceProfile));
when(backupScanner.getResources()).thenReturn(backups);
mockSecrets(clusterNamespace);
List<HasMetadata> resources = generator.getRequiredResources(cluster);
resources.forEach(resource -> {
assertNotNull(resource.getMetadata().getOwnerReferences(), "Resource " + resource.getMetadata().getName() + " doesn't owner references");
if (resource.getMetadata().getOwnerReferences().size() == 0) {
fail("Resource " + resource.getMetadata().getName() + " doesn't have any owner");
}
assertTrue(resource.getMetadata().getOwnerReferences().stream().anyMatch(ownerReference -> ownerReference.getApiVersion().equals(HasMetadata.getApiVersion(StackGresCluster.class)) && ownerReference.getKind().equals(HasMetadata.getKind(StackGresCluster.class)) && ownerReference.getName().equals(cluster.getMetadata().getName()) && ownerReference.getUid().equals(cluster.getMetadata().getUid()) && Optional.ofNullable(ownerReference.getBlockOwnerDeletion()).orElse(Boolean.FALSE).equals(Boolean.FALSE)));
});
verify(backupConfigFinder).findByNameAndNamespace(backupConfigName, clusterNamespace);
verify(postgresConfigFinder).findByNameAndNamespace(postgresConfigName, clusterNamespace);
verify(poolingConfigFinder).findByNameAndNamespace(connectionPoolingConfig, clusterNamespace);
verify(profileConfigFinder).findByNameAndNamespace(resourceProfile, clusterNamespace);
verify(backupScanner, atLeastOnce()).getResources();
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class ClusterRequiredResourcesGeneratorTest method givenAClusterInvalidRestoreData_getRequiredResourcesShouldNotScanForBackups.
@Test
void givenAClusterInvalidRestoreData_getRequiredResourcesShouldNotScanForBackups() {
cluster.getSpec().getInitData().getRestore().setFromBackup(new StackGresClusterRestoreFromBackup());
cluster.getSpec().getInitData().getRestore().getFromBackup().setUid(UUID.randomUUID().toString());
final ObjectMeta metadata = cluster.getMetadata();
final String clusterName = metadata.getName();
final String clusterNamespace = metadata.getNamespace();
final StackGresClusterSpec clusterSpec = cluster.getSpec();
final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
final String backupConfigName = clusterConfiguration.getBackupConfig();
mockBackupConfig(clusterNamespace, backupConfigName);
final String postgresConfigName = clusterConfiguration.getPostgresConfig();
mockPgConfig(clusterNamespace, postgresConfigName);
final String connectionPoolingConfig = clusterConfiguration.getConnectionPoolingConfig();
mockPoolingConfig(clusterNamespace, connectionPoolingConfig);
final String resourceProfile = clusterSpec.getResourceProfile();
when(profileConfigFinder.findByNameAndNamespace(resourceProfile, clusterNamespace)).thenReturn(Optional.of(instanceProfile));
when(backupScanner.getResources()).thenReturn(backups);
assertException("SGCluster " + clusterNamespace + "/" + clusterName + " have an invalid restore backup Uid");
verify(backupConfigFinder).findByNameAndNamespace(backupConfigName, clusterNamespace);
verify(postgresConfigFinder).findByNameAndNamespace(postgresConfigName, clusterNamespace);
verify(poolingConfigFinder).findByNameAndNamespace(connectionPoolingConfig, clusterNamespace);
verify(profileConfigFinder).findByNameAndNamespace(resourceProfile, clusterNamespace);
verify(backupScanner, atLeastOnce()).getResources();
}
Aggregations