Search in sources :

Example 6 with Deletable

use of io.fabric8.kubernetes.client.dsl.Deletable in project strimzi by strimzi.

the class StatefulSetOperatorTest method testInternalReplace.

@Test
public void testInternalReplace(VertxTestContext context) {
    StatefulSet sts1 = new StatefulSetBuilder().withNewMetadata().withNamespace(AbstractResourceOperatorTest.NAMESPACE).withName(AbstractResourceOperatorTest.RESOURCE_NAME).endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().endTemplate().endSpec().build();
    Map<String, Quantity> requests = new HashMap<>();
    requests.put("storage", new Quantity("100Gi"));
    PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder().withNewMetadata().withName("data").endMetadata().withNewSpec().withAccessModes("ReadWriteOnce").withNewResources().withRequests(requests).endResources().withStorageClassName("gp2").endSpec().build();
    StatefulSet sts2 = new StatefulSetBuilder().withNewMetadata().withNamespace(AbstractResourceOperatorTest.NAMESPACE).withName(AbstractResourceOperatorTest.RESOURCE_NAME).endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().endTemplate().withVolumeClaimTemplates(pvc).endSpec().build();
    Deletable mockDeletable = mock(Deletable.class);
    when(mockDeletable.delete()).thenReturn(Boolean.TRUE);
    Resource mockERPD = mock(resourceType());
    when(mockERPD.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockDeletable);
    when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(sts1);
    when(mockResource.withPropagationPolicy(eq(DeletionPropagation.ORPHAN))).thenReturn(mockERPD);
    when(mockResource.create(any(StatefulSet.class))).thenReturn(sts1);
    PodOperator podOperator = mock(PodOperator.class);
    when(podOperator.waitFor(any(), anyString(), anyString(), anyLong(), anyLong(), any(BiPredicate.class))).thenReturn(Future.succeededFuture());
    when(podOperator.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(podOperator.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    when(podOperator.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(new PodBuilder().withNewMetadata().withName("my-pod-0").endMetadata().build()));
    PvcOperator pvcOperator = mock(PvcOperator.class);
    when(pvcOperator.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    KubernetesClient mockClient = mock(KubernetesClient.class);
    mocker(mockClient, mockCms);
    StatefulSetOperator op = new StatefulSetOperator(AbstractResourceOperatorTest.vertx, mockClient, 5_000L, podOperator, pvcOperator) {

        @Override
        protected boolean shouldIncrementGeneration(Reconciliation reconciliation, StatefulSetDiff diff) {
            return true;
        }

        @Override
        public Future<Void> waitFor(Reconciliation reconciliation, String namespace, String name, String logState, long pollIntervalMs, final long timeoutMs, BiPredicate<String, String> predicate) {
            return Future.succeededFuture();
        }
    };
    Checkpoint async = context.checkpoint();
    op.reconcile(new Reconciliation("test", "kind", "namespace", "name"), sts1.getMetadata().getNamespace(), sts1.getMetadata().getName(), sts2).onComplete(context.succeeding(rrState -> {
        verify(mockDeletable).delete();
        async.flag();
    }));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) AbstractScalableResourceOperator(io.strimzi.operator.common.operator.resource.AbstractScalableResourceOperator) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) MockitoException(org.mockito.exceptions.base.MockitoException) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BiPredicate(java.util.function.BiPredicate) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) StatefulSetList(io.fabric8.kubernetes.api.model.apps.StatefulSetList) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Checkpoint(io.vertx.junit5.Checkpoint) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Quantity(io.fabric8.kubernetes.api.model.Quantity) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Reconciliation(io.strimzi.operator.common.Reconciliation) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) BiPredicate(java.util.function.BiPredicate) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Test(org.junit.jupiter.api.Test)

Example 7 with Deletable

use of io.fabric8.kubernetes.client.dsl.Deletable in project strimzi by strimzi.

the class StatefulSetOperatorTest method testCascadingDeleteAsync.

@Test
public void testCascadingDeleteAsync(VertxTestContext context) {
    Deletable mockDeletable = mock(Deletable.class);
    when(mockDeletable.delete()).thenReturn(Boolean.TRUE);
    Resource mockERPD = mock(resourceType());
    when(mockERPD.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockDeletable);
    when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
    RollableScalableResource mockRSR = mock(RollableScalableResource.class);
    ArgumentCaptor<DeletionPropagation> cascadingCaptor = ArgumentCaptor.forClass(DeletionPropagation.class);
    when(mockRSR.withPropagationPolicy(cascadingCaptor.capture())).thenReturn(mockERPD);
    ArgumentCaptor<Watcher> watcherCaptor = ArgumentCaptor.forClass(Watcher.class);
    when(mockRSR.watch(watcherCaptor.capture())).thenReturn(mock(Watch.class));
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockRSR);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    PodOperator podOperator = mock(PodOperator.class);
    PvcOperator pvcOperator = mock(PvcOperator.class);
    KubernetesClient mockClient = mock(KubernetesClient.class);
    mocker(mockClient, mockCms);
    StatefulSetOperator op = new StatefulSetOperator(AbstractResourceOperatorTest.vertx, mockClient, 5_000L, podOperator, pvcOperator) {

        @Override
        protected boolean shouldIncrementGeneration(Reconciliation reconciliation, StatefulSetDiff diff) {
            return true;
        }
    };
    Checkpoint async = context.checkpoint();
    op.deleteAsync(new Reconciliation("test", "kind", "namespace", "name"), NAMESPACE, RESOURCE_NAME, true).onComplete(context.succeeding(v -> context.verify(() -> {
        assertThat(cascadingCaptor.getValue(), is(DeletionPropagation.FOREGROUND));
        async.flag();
    })));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) AbstractScalableResourceOperator(io.strimzi.operator.common.operator.resource.AbstractScalableResourceOperator) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) MockitoException(org.mockito.exceptions.base.MockitoException) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BiPredicate(java.util.function.BiPredicate) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) StatefulSetList(io.fabric8.kubernetes.api.model.apps.StatefulSetList) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Checkpoint(io.vertx.junit5.Checkpoint) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) Watcher(io.fabric8.kubernetes.client.Watcher) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Checkpoint(io.vertx.junit5.Checkpoint) Reconciliation(io.strimzi.operator.common.Reconciliation) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Watch(io.fabric8.kubernetes.client.Watch) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Test(org.junit.jupiter.api.Test)

Example 8 with Deletable

use of io.fabric8.kubernetes.client.dsl.Deletable in project strimzi-kafka-operator by strimzi.

the class StatefulSetOperatorTest method testDeleteAsyncFailing.

@Test
public void testDeleteAsyncFailing(VertxTestContext context) {
    Deletable mockDeletable = mock(Deletable.class);
    when(mockDeletable.delete()).thenThrow(new MockitoException("Something failed"));
    Resource mockERPD = mock(resourceType());
    when(mockERPD.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockDeletable);
    when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
    RollableScalableResource mockRSR = mock(RollableScalableResource.class);
    when(mockRSR.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockERPD);
    when(mockRSR.watch(any())).thenReturn(mock(Watch.class));
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockRSR);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    PodOperator podOperator = mock(PodOperator.class);
    PvcOperator pvcOperator = mock(PvcOperator.class);
    KubernetesClient mockClient = mock(KubernetesClient.class);
    mocker(mockClient, mockCms);
    StatefulSetOperator op = new StatefulSetOperator(AbstractResourceOperatorTest.vertx, mockClient, 5_000L, podOperator, pvcOperator) {

        @Override
        protected boolean shouldIncrementGeneration(Reconciliation reconciliation, StatefulSetDiff diff) {
            return true;
        }
    };
    Checkpoint async = context.checkpoint();
    op.deleteAsync(new Reconciliation("test", "kind", "namespace", "name"), NAMESPACE, RESOURCE_NAME, false).onComplete(context.failing(e -> context.verify(() -> {
        assertThat(e, instanceOf(MockitoException.class));
        assertThat(e.getMessage(), is("Something failed"));
        async.flag();
    })));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) AbstractScalableResourceOperator(io.strimzi.operator.common.operator.resource.AbstractScalableResourceOperator) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) MockitoException(org.mockito.exceptions.base.MockitoException) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BiPredicate(java.util.function.BiPredicate) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) StatefulSetList(io.fabric8.kubernetes.api.model.apps.StatefulSetList) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Checkpoint(io.vertx.junit5.Checkpoint) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Checkpoint(io.vertx.junit5.Checkpoint) MockitoException(org.mockito.exceptions.base.MockitoException) Reconciliation(io.strimzi.operator.common.Reconciliation) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Watch(io.fabric8.kubernetes.client.Watch) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Test(org.junit.jupiter.api.Test)

Example 9 with Deletable

use of io.fabric8.kubernetes.client.dsl.Deletable in project strimzi-kafka-operator by strimzi.

the class StatefulSetOperatorTest method testInternalReplace.

@Test
public void testInternalReplace(VertxTestContext context) {
    StatefulSet sts1 = new StatefulSetBuilder().withNewMetadata().withNamespace(AbstractResourceOperatorTest.NAMESPACE).withName(AbstractResourceOperatorTest.RESOURCE_NAME).endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().endTemplate().endSpec().build();
    Map<String, Quantity> requests = new HashMap<>();
    requests.put("storage", new Quantity("100Gi"));
    PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder().withNewMetadata().withName("data").endMetadata().withNewSpec().withAccessModes("ReadWriteOnce").withNewResources().withRequests(requests).endResources().withStorageClassName("gp2").endSpec().build();
    StatefulSet sts2 = new StatefulSetBuilder().withNewMetadata().withNamespace(AbstractResourceOperatorTest.NAMESPACE).withName(AbstractResourceOperatorTest.RESOURCE_NAME).endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().endTemplate().withVolumeClaimTemplates(pvc).endSpec().build();
    Deletable mockDeletable = mock(Deletable.class);
    when(mockDeletable.delete()).thenReturn(Boolean.TRUE);
    Resource mockERPD = mock(resourceType());
    when(mockERPD.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockDeletable);
    when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(sts1);
    when(mockResource.withPropagationPolicy(eq(DeletionPropagation.ORPHAN))).thenReturn(mockERPD);
    when(mockResource.create(any(StatefulSet.class))).thenReturn(sts1);
    PodOperator podOperator = mock(PodOperator.class);
    when(podOperator.waitFor(any(), anyString(), anyString(), anyLong(), anyLong(), any(BiPredicate.class))).thenReturn(Future.succeededFuture());
    when(podOperator.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(podOperator.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    when(podOperator.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(new PodBuilder().withNewMetadata().withName("my-pod-0").endMetadata().build()));
    PvcOperator pvcOperator = mock(PvcOperator.class);
    when(pvcOperator.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    KubernetesClient mockClient = mock(KubernetesClient.class);
    mocker(mockClient, mockCms);
    StatefulSetOperator op = new StatefulSetOperator(AbstractResourceOperatorTest.vertx, mockClient, 5_000L, podOperator, pvcOperator) {

        @Override
        protected boolean shouldIncrementGeneration(Reconciliation reconciliation, StatefulSetDiff diff) {
            return true;
        }

        @Override
        public Future<Void> waitFor(Reconciliation reconciliation, String namespace, String name, String logState, long pollIntervalMs, final long timeoutMs, BiPredicate<String, String> predicate) {
            return Future.succeededFuture();
        }
    };
    Checkpoint async = context.checkpoint();
    op.reconcile(new Reconciliation("test", "kind", "namespace", "name"), sts1.getMetadata().getNamespace(), sts1.getMetadata().getName(), sts2).onComplete(context.succeeding(rrState -> {
        verify(mockDeletable).delete();
        async.flag();
    }));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) AbstractScalableResourceOperator(io.strimzi.operator.common.operator.resource.AbstractScalableResourceOperator) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) MockitoException(org.mockito.exceptions.base.MockitoException) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BiPredicate(java.util.function.BiPredicate) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) StatefulSetList(io.fabric8.kubernetes.api.model.apps.StatefulSetList) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Checkpoint(io.vertx.junit5.Checkpoint) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Quantity(io.fabric8.kubernetes.api.model.Quantity) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Reconciliation(io.strimzi.operator.common.Reconciliation) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) BiPredicate(java.util.function.BiPredicate) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Test(org.junit.jupiter.api.Test)

Example 10 with Deletable

use of io.fabric8.kubernetes.client.dsl.Deletable in project strimzi-kafka-operator by strimzi.

the class StatefulSetOperatorTest method testCascadingDeleteAsync.

@Test
public void testCascadingDeleteAsync(VertxTestContext context) {
    Deletable mockDeletable = mock(Deletable.class);
    when(mockDeletable.delete()).thenReturn(Boolean.TRUE);
    Resource mockERPD = mock(resourceType());
    when(mockERPD.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockDeletable);
    when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
    RollableScalableResource mockRSR = mock(RollableScalableResource.class);
    ArgumentCaptor<DeletionPropagation> cascadingCaptor = ArgumentCaptor.forClass(DeletionPropagation.class);
    when(mockRSR.withPropagationPolicy(cascadingCaptor.capture())).thenReturn(mockERPD);
    ArgumentCaptor<Watcher> watcherCaptor = ArgumentCaptor.forClass(Watcher.class);
    when(mockRSR.watch(watcherCaptor.capture())).thenReturn(mock(Watch.class));
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockRSR);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    PodOperator podOperator = mock(PodOperator.class);
    PvcOperator pvcOperator = mock(PvcOperator.class);
    KubernetesClient mockClient = mock(KubernetesClient.class);
    mocker(mockClient, mockCms);
    StatefulSetOperator op = new StatefulSetOperator(AbstractResourceOperatorTest.vertx, mockClient, 5_000L, podOperator, pvcOperator) {

        @Override
        protected boolean shouldIncrementGeneration(Reconciliation reconciliation, StatefulSetDiff diff) {
            return true;
        }
    };
    Checkpoint async = context.checkpoint();
    op.deleteAsync(new Reconciliation("test", "kind", "namespace", "name"), NAMESPACE, RESOURCE_NAME, true).onComplete(context.succeeding(v -> context.verify(() -> {
        assertThat(cascadingCaptor.getValue(), is(DeletionPropagation.FOREGROUND));
        async.flag();
    })));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) AbstractScalableResourceOperator(io.strimzi.operator.common.operator.resource.AbstractScalableResourceOperator) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) MockitoException(org.mockito.exceptions.base.MockitoException) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BiPredicate(java.util.function.BiPredicate) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) StatefulSetList(io.fabric8.kubernetes.api.model.apps.StatefulSetList) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Checkpoint(io.vertx.junit5.Checkpoint) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) Watcher(io.fabric8.kubernetes.client.Watcher) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Checkpoint(io.vertx.junit5.Checkpoint) Reconciliation(io.strimzi.operator.common.Reconciliation) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Watch(io.fabric8.kubernetes.client.Watch) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Test(org.junit.jupiter.api.Test)

Aggregations

Watch (io.fabric8.kubernetes.client.Watch)12 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)9 Deletable (io.fabric8.kubernetes.client.dsl.Deletable)9 Test (org.junit.jupiter.api.Test)9 DeletionPropagation (io.fabric8.kubernetes.api.model.DeletionPropagation)8 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)8 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)8 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)8 Quantity (io.fabric8.kubernetes.api.model.Quantity)8 StatefulSetBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)8 StatefulSetList (io.fabric8.kubernetes.api.model.apps.StatefulSetList)8 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)8 Watcher (io.fabric8.kubernetes.client.Watcher)8 AppsAPIGroupDSL (io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL)8 EditReplacePatchDeletable (io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable)8 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)8 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)8 Resource (io.fabric8.kubernetes.client.dsl.Resource)8 RollableScalableResource (io.fabric8.kubernetes.client.dsl.RollableScalableResource)8 Reconciliation (io.strimzi.operator.common.Reconciliation)8