Search in sources :

Example 1 with ClusterFinder

use of io.stackgres.common.resource.ClusterFinder 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)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 InjectMock (io.quarkus.test.junit.mockito.InjectMock)1 StackGresComponent (io.stackgres.common.StackGresComponent)1 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)1 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)1 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)1 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)1 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)1 BackupConfigFinder (io.stackgres.common.resource.BackupConfigFinder)1 ClusterFinder (io.stackgres.common.resource.ClusterFinder)1 JsonUtil (io.stackgres.testutil.JsonUtil)1 List (java.util.List)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Assertions.fail (org.junit.jupiter.api.Assertions.fail)1