Search in sources :

Example 76 with Context

use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.

the class KafkaAssemblyOperatorMockIT method testKafkaScaleUp.

/**
 * Create a cluster from a Kafka Cluster CM
 */
@Test
public void testKafkaScaleUp(TestContext context) {
    KafkaAssemblyOperator kco = createCluster(context);
    Async updateAsync = context.async();
    int newScale = kafkaReplicas + 1;
    String newPod = KafkaCluster.kafkaPodName(CLUSTER_NAME, kafkaReplicas);
    context.assertNull(mockClient.pods().inNamespace(NAMESPACE).withName(newPod).get());
    HashMap<String, String> data = new HashMap<>(cluster.getData());
    data.put(KafkaCluster.KEY_REPLICAS, String.valueOf(newScale));
    ConfigMap changedClusterCm = new ConfigMapBuilder(cluster).withData(data).build();
    mockClient.configMaps().inNamespace(NAMESPACE).withName(CLUSTER_NAME).patch(changedClusterCm);
    LOGGER.info("Scaling up to {} Kafka pods", newScale);
    kco.reconcileAssembly(new Reconciliation("test-trigger", AssemblyType.KAFKA, NAMESPACE, CLUSTER_NAME), ar -> {
        if (ar.failed())
            ar.cause().printStackTrace();
        context.assertTrue(ar.succeeded());
        context.assertEquals(newScale, mockClient.apps().statefulSets().inNamespace(NAMESPACE).withName(KafkaCluster.kafkaClusterName(CLUSTER_NAME)).get().getSpec().getReplicas());
        context.assertNotNull(mockClient.pods().inNamespace(NAMESPACE).withName(newPod).get(), "Expected pod " + newPod + " to have been created");
        // TODO assert no rolling update
        updateAsync.complete();
    });
    updateAsync.await();
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Reconciliation(io.strimzi.controller.cluster.Reconciliation) Test(org.junit.Test)

Example 77 with Context

use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.

the class KafkaAssemblyOperatorMockIT method testKafkaScaleDown.

/**
 * Create a cluster from a Kafka Cluster CM
 */
@Test
public void testKafkaScaleDown(TestContext context) {
    if (kafkaReplicas <= 1) {
        LOGGER.info("Skipping scale down test because there's only 1 broker");
        return;
    }
    KafkaAssemblyOperator kco = createCluster(context);
    Async updateAsync = context.async();
    int newScale = kafkaReplicas - 1;
    String deletedPod = KafkaCluster.kafkaPodName(CLUSTER_NAME, newScale);
    context.assertNotNull(mockClient.pods().inNamespace(NAMESPACE).withName(deletedPod).get());
    HashMap<String, String> data = new HashMap<>(cluster.getData());
    data.put(KafkaCluster.KEY_REPLICAS, String.valueOf(newScale));
    ConfigMap changedClusterCm = new ConfigMapBuilder(cluster).withData(data).build();
    mockClient.configMaps().inNamespace(NAMESPACE).withName(CLUSTER_NAME).patch(changedClusterCm);
    LOGGER.info("Scaling down to {} Kafka pods", newScale);
    kco.reconcileAssembly(new Reconciliation("test-trigger", AssemblyType.KAFKA, NAMESPACE, CLUSTER_NAME), ar -> {
        if (ar.failed())
            ar.cause().printStackTrace();
        context.assertTrue(ar.succeeded());
        context.assertEquals(newScale, mockClient.apps().statefulSets().inNamespace(NAMESPACE).withName(KafkaCluster.kafkaClusterName(CLUSTER_NAME)).get().getSpec().getReplicas());
        context.assertNull(mockClient.pods().inNamespace(NAMESPACE).withName(deletedPod).get(), "Expected pod " + deletedPod + " to have been deleted");
        // TODO assert no rolling update
        updateAsync.complete();
    });
    updateAsync.await();
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Reconciliation(io.strimzi.controller.cluster.Reconciliation) Test(org.junit.Test)

Example 78 with Context

use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.

the class KafkaConnectAssemblyOperatorTest method testUpdateCluster.

@Test
public void testUpdateCluster(TestContext context) {
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
    String clusterCmName = "foo";
    String clusterCmNamespace = "test";
    ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, clusterCmName);
    KafkaConnectCluster connect = KafkaConnectCluster.fromConfigMap(clusterCm);
    // Change the image to generate some diff
    clusterCm.getData().put("image", "some/different:image");
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    when(mockServiceOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateService());
    when(mockDcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateDeployment());
    ArgumentCaptor<String> serviceNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(eq(clusterCmNamespace), serviceNameCaptor.capture(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Deployment> dcCaptor = ArgumentCaptor.forClass(Deployment.class);
    when(mockDcOps.reconcile(eq(clusterCmNamespace), dcNameCaptor.capture(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleUpNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleUpReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleUp(eq(clusterCmNamespace), dcScaleUpNameCaptor.capture(), dcScaleUpReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleDownNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleDownReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleDown(eq(clusterCmNamespace), dcScaleDownNameCaptor.capture(), dcScaleDownReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps);
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        context.assertTrue(createResult.succeeded());
        KafkaConnectCluster compareTo = KafkaConnectCluster.fromConfigMap(clusterCm);
        // Vertify service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(1, capturedServices.size());
        Service service = capturedServices.get(0);
        context.assertEquals(compareTo.getName(), service.getMetadata().getName());
        context.assertEquals(compareTo.generateService(), service, "Services are not equal");
        // Verify Deployment
        List<Deployment> capturedDc = dcCaptor.getAllValues();
        context.assertEquals(1, capturedDc.size());
        Deployment dc = capturedDc.get(0);
        context.assertEquals(compareTo.getName(), dc.getMetadata().getName());
        context.assertEquals(compareTo.generateDeployment(), dc, "Deployments are not equal");
        // Verify scaleDown / scaleUp were not called
        context.assertEquals(1, dcScaleDownNameCaptor.getAllValues().size());
        context.assertEquals(1, dcScaleUpNameCaptor.getAllValues().size());
        async.complete();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) KafkaConnectCluster(io.strimzi.controller.cluster.model.KafkaConnectCluster) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) Test(org.junit.Test)

Example 79 with Context

use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.

the class KafkaConnectAssemblyOperatorTest method testReconcile.

@Test
public void testReconcile(TestContext context) {
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
    String clusterCmNamespace = "test";
    ConfigMap foo = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, "foo");
    ConfigMap bar = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, "bar");
    ConfigMap baz = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, "baz");
    when(mockCmOps.list(eq(clusterCmNamespace), any())).thenReturn(asList(foo, bar));
    // when requested ConfigMap for a specific Kafka Connect cluster
    when(mockCmOps.get(eq(clusterCmNamespace), eq("foo"))).thenReturn(foo);
    when(mockCmOps.get(eq(clusterCmNamespace), eq("bar"))).thenReturn(bar);
    // providing the list of ALL Deployments for all the Kafka Connect clusters
    Labels newLabels = Labels.forType(AssemblyType.CONNECT);
    when(mockDcOps.list(eq(clusterCmNamespace), eq(newLabels))).thenReturn(asList(KafkaConnectCluster.fromConfigMap(bar).generateDeployment(), KafkaConnectCluster.fromConfigMap(baz).generateDeployment()));
    // providing the list Deployments for already "existing" Kafka Connect clusters
    Labels barLabels = Labels.forCluster("bar");
    when(mockDcOps.list(eq(clusterCmNamespace), eq(barLabels))).thenReturn(asList(KafkaConnectCluster.fromConfigMap(bar).generateDeployment()));
    Labels bazLabels = Labels.forCluster("baz");
    when(mockDcOps.list(eq(clusterCmNamespace), eq(bazLabels))).thenReturn(asList(KafkaConnectCluster.fromConfigMap(baz).generateDeployment()));
    Set<String> createdOrUpdated = new HashSet<>();
    Set<String> deleted = new HashSet<>();
    Async async = context.async(3);
    KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps) {

        @Override
        public void createOrUpdate(Reconciliation reconciliation, ConfigMap assemblyCm, Handler h) {
            createdOrUpdated.add(assemblyCm.getMetadata().getName());
            async.countDown();
            h.handle(Future.succeededFuture());
        }

        @Override
        public void delete(Reconciliation reconciliation, Handler h) {
            deleted.add(reconciliation.assemblyName());
            async.countDown();
            h.handle(Future.succeededFuture());
        }
    };
    // Now try to reconcile all the Kafka Connect clusters
    ops.reconcileAll("test", clusterCmNamespace, Labels.EMPTY);
    async.await();
    context.assertEquals(new HashSet(asList("foo", "bar")), createdOrUpdated);
    context.assertEquals(singleton("baz"), deleted);
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) Handler(io.vertx.core.Handler) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) Labels(io.strimzi.controller.cluster.model.Labels) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with Context

use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.

the class KafkaConnectAssemblyOperatorTest method testUpdateClusterScaleDown.

@Test
public void testUpdateClusterScaleDown(TestContext context) {
    int scaleTo = 2;
    String newReplicas = String.valueOf(scaleTo);
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
    String clusterCmName = "foo";
    String clusterCmNamespace = "test";
    ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, clusterCmName);
    KafkaConnectCluster connect = KafkaConnectCluster.fromConfigMap(clusterCm);
    // Change replicas to create ScaleDown
    clusterCm.getData().put(KafkaConnectCluster.KEY_REPLICAS, newReplicas);
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    when(mockServiceOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateService());
    when(mockDcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateDeployment());
    when(mockServiceOps.reconcile(eq(clusterCmNamespace), any(), any())).thenReturn(Future.succeededFuture());
    when(mockDcOps.reconcile(eq(clusterCmNamespace), any(), any())).thenReturn(Future.succeededFuture());
    doAnswer(i -> Future.succeededFuture(scaleTo)).when(mockDcOps).scaleUp(clusterCmNamespace, connect.getName(), scaleTo);
    doAnswer(i -> Future.succeededFuture(scaleTo)).when(mockDcOps).scaleDown(clusterCmNamespace, connect.getName(), scaleTo);
    KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps);
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        context.assertTrue(createResult.succeeded());
        verify(mockDcOps).scaleUp(clusterCmNamespace, connect.getName(), scaleTo);
        async.complete();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) KafkaConnectCluster(io.strimzi.controller.cluster.model.KafkaConnectCluster) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)135 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)58 Async (io.vertx.ext.unit.Async)52 Expectations (mockit.Expectations)34 Probe (io.fabric8.kubernetes.api.model.Probe)33 Reconciliation (io.strimzi.controller.cluster.Reconciliation)24 File (java.io.File)23 Git (org.eclipse.jgit.api.Git)23 GitContext (io.fabric8.api.GitContext)21 IOException (java.io.IOException)21 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)20 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)20 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 HashMap (java.util.HashMap)17 LockHandle (io.fabric8.api.LockHandle)15 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)15 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)15 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)14 Resource (io.fabric8.kubernetes.client.dsl.Resource)14