Search in sources :

Example 16 with StackGresClusterSpec

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec 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));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) ClusterFinder(io.stackgres.common.resource.ClusterFinder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InjectMock(io.quarkus.test.junit.mockito.InjectMock) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) JsonUtil(io.stackgres.testutil.JsonUtil) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) BackupConfigFinder(io.stackgres.common.resource.BackupConfigFinder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 17 with StackGresClusterSpec

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec 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());
}
Also used : StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 18 with StackGresClusterSpec

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec 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));
}
Also used : StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 19 with StackGresClusterSpec

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec 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();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StackGresBackupList(io.stackgres.common.crd.sgbackup.StackGresBackupList) BeforeEach(org.junit.jupiter.api.BeforeEach) PoolingConfigFinder(io.stackgres.common.resource.PoolingConfigFinder) StackGresPostgresConfigStatus(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfigStatus) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresPoolingConfigStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigStatus) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) PrometheusConfigList(io.stackgres.operator.customresource.prometheus.PrometheusConfigList) QuarkusTest(io.quarkus.test.junit.QuarkusTest) ServiceMonitor(io.stackgres.operator.customresource.prometheus.ServiceMonitor) Inject(javax.inject.Inject) StackGresClusterRestoreFromBackup(io.stackgres.common.crd.sgcluster.StackGresClusterRestoreFromBackup) ProfileConfigFinder(io.stackgres.common.resource.ProfileConfigFinder) SecretFinder(io.stackgres.common.resource.SecretFinder) BackupScanner(io.stackgres.common.resource.BackupScanner) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InjectMock(io.quarkus.test.junit.mockito.InjectMock) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) PgBouncerDefaultValues(io.stackgres.operator.conciliation.factory.cluster.sidecars.pooling.parameters.PgBouncerDefaultValues) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) JsonUtil(io.stackgres.testutil.JsonUtil) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) Mockito.never(org.mockito.Mockito.never) List(java.util.List) BackupConfigFinder(io.stackgres.common.resource.BackupConfigFinder) StackGresPoolingConfigPgBouncerStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerStatus) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PostgresDefaultValues(io.stackgres.operator.conciliation.factory.cluster.patroni.parameters.PostgresDefaultValues) Optional(java.util.Optional) Secret(io.fabric8.kubernetes.api.model.Secret) PostgresConfigFinder(io.stackgres.common.resource.PostgresConfigFinder) PrometheusScanner(io.stackgres.operator.resource.PrometheusScanner) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 20 with StackGresClusterSpec

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec 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();
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterRestoreFromBackup(io.stackgres.common.crd.sgcluster.StackGresClusterRestoreFromBackup) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)46 Test (org.junit.jupiter.api.Test)26 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)23 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)18 QuarkusTest (io.quarkus.test.junit.QuarkusTest)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)15 List (java.util.List)11 Optional (java.util.Optional)11 Inject (javax.inject.Inject)10 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)7 Collectors (java.util.stream.Collectors)7 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)6 StackGresClusterPostgres (io.stackgres.common.crd.sgcluster.StackGresClusterPostgres)6 StackGresComponent (io.stackgres.common.StackGresComponent)5 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)5 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)5 ValidationFailed (io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed)5