Search in sources :

Example 71 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ExtensionManagerTest method testIsExtensionPending.

@Test
void testIsExtensionPending() throws Exception {
    StackGresCluster cluster = getCluster();
    when(webClientFactory.create(anyBoolean())).thenReturn(webClient);
    when(webClient.getJson(any(), any())).thenReturn(getExtensions());
    final String extensionPackageName = ExtensionUtil.getExtensionPackageName(getInstalledExtension());
    when(fileSystemHandler.exists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.PENDING_SUFFIX)))).thenReturn(true);
    StackGresClusterInstalledExtension extension = getInstalledExtension();
    Assertions.assertTrue(extensionManager.getExtensionInstaller(context(cluster), extension).isExtensionPendingOverwrite());
    verify(webClientFactory, times(1)).create(anyBoolean());
    verify(webClient, times(1)).getJson(any(), any());
    verify(webClient, times(1)).getJson(eq(ExtensionUtil.getIndexUri(REPOSITORY)), eq(StackGresExtensions.class));
    verify(webClient, times(0)).getInputStream(any());
    verify(fileSystemHandler, times(0)).newInputStream(any());
    verify(fileSystemHandler, times(0)).createOrReplaceFile(any());
    verify(fileSystemHandler, times(0)).createDirectories(any());
    verify(fileSystemHandler, times(0)).createOrReplaceSymbolicLink(any(), any());
    verify(fileSystemHandler, times(0)).copyOrReplace(any(), any());
    verify(fileSystemHandler, times(0)).setPosixFilePermissions(any(), any());
    verify(fileSystemHandler, times(0)).deleteIfExists(any());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) Test(org.junit.jupiter.api.Test)

Example 72 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ExtensionManagerTest method testIsExtensionNotPending.

@Test
void testIsExtensionNotPending() throws Exception {
    when(webClientFactory.create(anyBoolean())).thenReturn(webClient);
    when(webClient.getJson(any(), any())).thenReturn(getExtensions());
    StackGresCluster cluster = getCluster();
    StackGresClusterInstalledExtension extension = getInstalledExtension();
    Assertions.assertFalse(extensionManager.getExtensionInstaller(context(cluster), extension).isExtensionPendingOverwrite());
    verify(webClientFactory, times(1)).create(anyBoolean());
    verify(webClient, times(1)).getJson(any(), any());
    verify(webClient, times(1)).getJson(eq(ExtensionUtil.getIndexUri(REPOSITORY)), eq(StackGresExtensions.class));
    verify(webClient, times(0)).getInputStream(any());
    verify(fileSystemHandler, times(0)).newInputStream(any());
    verify(fileSystemHandler, times(0)).createOrReplaceFile(any());
    verify(fileSystemHandler, times(0)).createDirectories(any());
    verify(fileSystemHandler, times(0)).createOrReplaceSymbolicLink(any(), any());
    verify(fileSystemHandler, times(0)).copyOrReplace(any(), any());
    verify(fileSystemHandler, times(0)).setPosixFilePermissions(any(), any());
    verify(fileSystemHandler, times(0)).deleteIfExists(any());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) Test(org.junit.jupiter.api.Test)

Example 73 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ExtensionReconciliationTest method getContext.

private ExtensionReconciliatorContext getContext(Consumer<StackGresCluster> consumer) {
    StackGresCluster cluster = JsonUtil.readFromJson("stackgres_cluster/list.json", StackGresClusterList.class).getItems().get(0);
    cluster.getSpec().getPostgres().setVersion(POSTGRES_VERSION);
    consumer.accept(cluster);
    when(context.getCluster()).thenReturn(cluster);
    when(context.getExtensions()).thenReturn(Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getToInstallPostgresExtensions).map(ImmutableList::copyOf).orElse(ImmutableList.of()));
    return context;
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ImmutableList(com.google.common.collect.ImmutableList)

Example 74 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ExtensionReconciliationTest method testInitReconciliationWithExtensionPending_installIsPerformed.

@Test
void testInitReconciliationWithExtensionPending_installIsPerformed() throws Exception {
    StackGresClusterInstalledExtension installedExtension = getInstalledExtension();
    ExtensionReconciliatorContext context = getContext(cluster -> {
        cluster.getSpec().getPostgres().setExtensions(null);
        cluster.getSpec().setToInstallPostgresExtensions(new ArrayList<>());
        cluster.getSpec().getToInstallPostgresExtensions().add(installedExtension);
        cluster.setStatus(new StackGresClusterStatus());
        cluster.getStatus().setPodStatuses(new ArrayList<>());
        StackGresClusterPodStatus podStatus = new StackGresClusterPodStatus();
        podStatus.setName("test-0");
        podStatus.setPendingRestart(true);
        podStatus.setInstalledPostgresExtensions(new ArrayList<>());
        podStatus.getInstalledPostgresExtensions().add(installedExtension);
        cluster.getStatus().getPodStatuses().add(podStatus);
    });
    when(extensionManager.getExtensionInstaller(any(), any(StackGresClusterInstalledExtension.class))).thenReturn(extensionInstaller);
    when(extensionInstaller.isExtensionInstalled()).thenReturn(false);
    doNothing().when(eventEmitter).emitExtensionDeployed(installedExtension);
    Assertions.assertTrue(initReconciliator.reconcile(null, context).result().get());
    Assertions.assertTrue(Optional.of(context.getCluster()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getPodStatuses).stream().flatMap(List::stream).anyMatch(podStatus -> podStatus.getName().equals("test-0")));
    Assertions.assertFalse(context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getPendingRestart).orElse(false));
    Assertions.assertIterableEquals(ImmutableList.of(installedExtension), context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getInstalledPostgresExtensions).stream().flatMap(List::stream).collect(ImmutableList.toImmutableList()));
    verify(extensionInstaller, times(1)).isExtensionInstalled();
    verify(extensionInstaller, times(0)).isLinksCreated();
    verify(extensionInstaller, times(0)).isExtensionPendingOverwrite();
    verify(extensionUninstaller, times(0)).isExtensionInstalled();
    verify(extensionInstaller, times(1)).downloadAndExtract();
    verify(extensionInstaller, times(1)).verify();
    verify(extensionInstaller, times(1)).installExtension();
    verify(extensionInstaller, times(0)).createExtensionLinks();
    verify(extensionInstaller, times(0)).doesInstallOverwriteAnySharedLibrary();
    verify(extensionInstaller, times(0)).setExtensionAsPending();
    verify(extensionUninstaller, times(0)).uninstallExtension();
    verify(eventEmitter).emitExtensionDeployed(installedExtension);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) ArrayList(java.util.ArrayList) ExtensionUninstaller(io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) StackGresClusterPodStatus(io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) ExtensionInstaller(io.stackgres.common.extension.ExtensionManager.ExtensionInstaller) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mockito.times(org.mockito.Mockito.times) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) JsonUtil(io.stackgres.testutil.JsonUtil) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) StackGresClusterPodStatus(io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) Test(org.junit.jupiter.api.Test)

Example 75 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ExtensionReconciliationTest method testReconciliationWithExtensionPending_installIsSkipped.

@Test
void testReconciliationWithExtensionPending_installIsSkipped() throws Exception {
    StackGresClusterInstalledExtension installedExtension = getInstalledExtension();
    ExtensionReconciliatorContext context = getContext(cluster -> {
        cluster.getSpec().getPostgres().setExtensions(null);
        cluster.getSpec().setToInstallPostgresExtensions(new ArrayList<>());
        cluster.getSpec().getToInstallPostgresExtensions().add(installedExtension);
        cluster.setStatus(new StackGresClusterStatus());
        cluster.getStatus().setPodStatuses(new ArrayList<>());
        StackGresClusterPodStatus podStatus = new StackGresClusterPodStatus();
        podStatus.setName("test-0");
        podStatus.setPendingRestart(true);
        podStatus.setInstalledPostgresExtensions(new ArrayList<>());
        podStatus.getInstalledPostgresExtensions().add(installedExtension);
        cluster.getStatus().getPodStatuses().add(podStatus);
    });
    when(extensionManager.getExtensionInstaller(any(), any(StackGresClusterInstalledExtension.class))).thenReturn(extensionInstaller);
    when(extensionInstaller.isExtensionInstalled()).thenReturn(false);
    when(extensionInstaller.isLinksCreated()).thenReturn(true);
    when(extensionInstaller.isExtensionPendingOverwrite()).thenReturn(true);
    Assertions.assertFalse(reconciliator.reconcile(null, context).result().get());
    Assertions.assertTrue(Optional.of(context.getCluster()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getPodStatuses).stream().flatMap(List::stream).anyMatch(podStatus -> podStatus.getName().equals("test-0")));
    Assertions.assertTrue(context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getPendingRestart).orElse(false));
    Assertions.assertIterableEquals(ImmutableList.of(installedExtension), context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getInstalledPostgresExtensions).stream().flatMap(List::stream).collect(ImmutableList.toImmutableList()));
    verify(extensionInstaller, times(1)).isExtensionInstalled();
    verify(extensionInstaller, times(1)).isLinksCreated();
    verify(extensionInstaller, times(1)).isExtensionPendingOverwrite();
    verify(extensionUninstaller, times(0)).isExtensionInstalled();
    verify(extensionInstaller, times(0)).downloadAndExtract();
    verify(extensionInstaller, times(0)).verify();
    verify(extensionInstaller, times(0)).installExtension();
    verify(extensionInstaller, times(0)).createExtensionLinks();
    verify(extensionInstaller, times(0)).doesInstallOverwriteAnySharedLibrary();
    verify(extensionInstaller, times(0)).setExtensionAsPending();
    verify(extensionUninstaller, times(0)).uninstallExtension();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) ArrayList(java.util.ArrayList) ExtensionUninstaller(io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) StackGresClusterPodStatus(io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) ExtensionInstaller(io.stackgres.common.extension.ExtensionManager.ExtensionInstaller) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mockito.times(org.mockito.Mockito.times) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) JsonUtil(io.stackgres.testutil.JsonUtil) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) StackGresClusterPodStatus(io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)127 Test (org.junit.jupiter.api.Test)48 Optional (java.util.Optional)32 List (java.util.List)31 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)27 StackGresClusterInstalledExtension (io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)25 ImmutableList (com.google.common.collect.ImmutableList)24 Inject (javax.inject.Inject)22 ArrayList (java.util.ArrayList)19 StackGresClusterStatus (io.stackgres.common.crd.sgcluster.StackGresClusterStatus)18 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)15 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 StackGresComponent (io.stackgres.common.StackGresComponent)15 StackGresClusterPodStatus (io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus)14 ExtensionInstaller (io.stackgres.common.extension.ExtensionManager.ExtensionInstaller)14 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)13 Collectors (java.util.stream.Collectors)13 BeforeEach (org.junit.jupiter.api.BeforeEach)13 ExtensionUninstaller (io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller)12 JsonUtil (io.stackgres.testutil.JsonUtil)12