Search in sources :

Example 56 with KafkaTopic

use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi-kafka-operator by strimzi.

the class TopicOperatorTest method testReconcile_withResource_withKafka_noPrivate_matching.

/**
 * Test reconciliation when a resource has been added both in kafka and in k8s while the operator was down, and both
 * topics are identical.
 */
@Test
public void testReconcile_withResource_withKafka_noPrivate_matching(VertxTestContext context) throws InterruptedException {
    Topic kubeTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("cleanup.policy", "bar"), metadata).build();
    Topic kafkaTopic = kubeTopic;
    Topic privateTopic = null;
    CountDownLatch topicCreatedinKafkaAndK8s = new CountDownLatch(2);
    mockKafka.setCreateTopicResponse(topicName -> Future.succeededFuture());
    mockKafka.createTopic(Reconciliation.DUMMY_RECONCILIATION, kafkaTopic).onComplete(ar -> topicCreatedinKafkaAndK8s.countDown());
    mockK8s.setCreateResponse(topicName.asKubeName(), null);
    KafkaTopic topicResource = TopicSerialization.toTopicResource(kubeTopic, labels);
    LogContext logContext = LogContext.periodic(topicName.toString(), topicOperator.getNamespace(), topicName.toString());
    mockK8s.createResource(topicResource).onComplete(ar -> topicCreatedinKafkaAndK8s.countDown());
    mockTopicStore.setCreateTopicResponse(topicName, null);
    topicCreatedinKafkaAndK8s.await();
    Checkpoint async = context.checkpoint();
    topicOperator.reconcile(reconciliation(logContext), logContext, null, kubeTopic, kafkaTopic, privateTopic).onComplete(reconcileResult -> {
        assertSucceeded(context, reconcileResult);
        mockTopicStore.assertExists(context, topicName);
        mockK8s.assertExists(context, topicName.asKubeName());
        mockK8s.assertNoEvents(context);
        mockKafka.assertExists(context, topicName);
        mockTopicStore.read(topicName).onComplete(readResult -> {
            assertSucceeded(context, readResult);
            context.verify(() -> assertThat(readResult.result(), is(kubeTopic)));
            async.flag();
        });
        context.verify(() -> assertThat(mockKafka.getTopicState(topicName), is(kafkaTopic)));
        context.verify(() -> {
            MeterRegistry registry = metrics.meterRegistry();
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations").tag("kind", "KafkaTopic").counter().count(), is(1.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.successful").tag("kind", "KafkaTopic").counter().count(), is(1.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.failed").tag("kind", "KafkaTopic").counter().count(), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().count(), is(1L));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().totalTime(TimeUnit.MILLISECONDS), greaterThan(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "resource.state").tag("kind", "KafkaTopic").tag("name", topicName.toString()).tag("resource-namespace", "default-namespace").gauge().value(), is(0.0));
        });
    });
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) CountDownLatch(java.util.concurrent.CountDownLatch) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 57 with KafkaTopic

use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi-kafka-operator by strimzi.

the class TopicOperatorTest method testReconcile_withResource_noKafka_withPrivate.

/**
 * Test reconciliation when a topic has been deleted while the operator
 * wasn't running
 */
@Test
public void testReconcile_withResource_noKafka_withPrivate(VertxTestContext context) {
    Topic kubeTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("cleanup.policy", "bar"), new ObjectMeta()).build();
    Topic kafkaTopic = null;
    Topic privateTopic = kubeTopic;
    Checkpoint topicCreatedInK8sAndStored = context.checkpoint(2);
    // Must create all checkpoints used before flagging any to prevent premature test success
    Checkpoint completeTest = context.checkpoint();
    KafkaTopic topicResource = TopicSerialization.toTopicResource(kubeTopic, labels);
    LogContext logContext = LogContext.kubeWatch(Watcher.Action.DELETED, topicResource);
    mockK8s.setCreateResponse(resourceName, null).createResource(topicResource).onComplete(ar -> topicCreatedInK8sAndStored.flag());
    mockK8s.setDeleteResponse(resourceName, null);
    mockTopicStore.setCreateTopicResponse(topicName, null).create(privateTopic).onComplete(ar -> topicCreatedInK8sAndStored.flag());
    mockTopicStore.setDeleteTopicResponse(topicName, null);
    topicOperator.reconcile(reconciliation(logContext), logContext, null, kubeTopic, kafkaTopic, privateTopic).onComplete(reconcileResult -> {
        assertSucceeded(context, reconcileResult);
        mockKafka.assertNotExists(context, kubeTopic.getTopicName());
        mockTopicStore.assertNotExists(context, kubeTopic.getTopicName());
        mockK8s.assertNotExists(context, kubeTopic.getResourceName());
        mockK8s.assertNoEvents(context);
        completeTest.flag();
    });
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) Test(org.junit.jupiter.api.Test)

Example 58 with KafkaTopic

use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi-kafka-operator by strimzi.

the class TopicOperatorTest method testOnTopicCreated.

// TODO ^^ but a disconnected/loss of session error
/**
 * 1. operator is notified that a topic is created
 * 2. operator successfully queries kafka to get topic metadata
 * 3. operator successfully creates KafkaTopic
 * 4. operator successfully creates in topic store
 */
@Test
public void testOnTopicCreated(VertxTestContext context) {
    TopicMetadata topicMetadata = Utils.getTopicMetadata(topicName.toString(), new org.apache.kafka.clients.admin.Config(Collections.emptyList()));
    mockTopicStore.setCreateTopicResponse(topicName, null);
    mockKafka.setTopicExistsResult(t -> Future.succeededFuture(true));
    mockKafka.setTopicMetadataResponse(topicName, topicMetadata, null);
    mockK8s.setCreateResponse(resourceName, null);
    LogContext logContext = LogContext.zkWatch("///", topicName.toString(), topicOperator.getNamespace(), topicName.toString());
    Checkpoint async = context.checkpoint();
    topicOperator.onTopicCreated(logContext, topicName).onComplete(ar -> {
        assertSucceeded(context, ar);
        mockK8s.assertExists(context, resourceName);
        Topic t = TopicSerialization.fromTopicMetadata(topicMetadata);
        mockTopicStore.assertContains(context, t);
        context.verify(() -> {
            MeterRegistry registry = metrics.meterRegistry();
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "resource.state").tag("kind", "KafkaTopic").tag("name", topicName.toString()).tag("resource-namespace", "default-namespace").gauge().value(), is(1.0));
        });
        async.flag();
    });
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 59 with KafkaTopic

use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi-kafka-operator by strimzi.

the class TopicOperatorTest method testReconcile_withResource_withKafka_noPrivate_configsReconcilable.

/**
 * Test reconciliation when a resource has been added both in kafka and in k8s while the operator was down, and
 * the topics are irreconcilably different: Kafka wins
 */
@Test
public void testReconcile_withResource_withKafka_noPrivate_configsReconcilable(VertxTestContext context) throws InterruptedException {
    Topic kubeTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("cleanup.policy", "bar"), metadata).build();
    Topic kafkaTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("unclean.leader.election.enable", "true"), metadata).build();
    Topic privateTopic = null;
    Topic mergedTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("unclean.leader.election.enable", "true", "cleanup.policy", "bar"), metadata).build();
    CountDownLatch topicCreatedInKafkaAndK8s = new CountDownLatch(2);
    mockKafka.setCreateTopicResponse(topicName -> Future.succeededFuture());
    mockKafka.createTopic(Reconciliation.DUMMY_RECONCILIATION, kafkaTopic).onComplete(ar -> topicCreatedInKafkaAndK8s.countDown());
    mockKafka.setUpdateTopicResponse(topicName -> Future.succeededFuture());
    KafkaTopic topic = TopicSerialization.toTopicResource(kubeTopic, labels);
    LogContext logContext = LogContext.periodic(topicName.toString(), topicOperator.getNamespace(), topicName.toString());
    mockK8s.setCreateResponse(topicName.asKubeName(), null);
    mockK8s.createResource(topic).onComplete(ar -> topicCreatedInKafkaAndK8s.countDown());
    mockK8s.setModifyResponse(topicName.asKubeName(), null);
    mockTopicStore.setCreateTopicResponse(topicName, null);
    topicCreatedInKafkaAndK8s.await();
    CountDownLatch async = new CountDownLatch(2);
    topicOperator.reconcile(reconciliation(logContext), logContext, topic, kubeTopic, kafkaTopic, privateTopic).onComplete(reconcileResult -> {
        assertSucceeded(context, reconcileResult);
        mockTopicStore.assertExists(context, topicName);
        mockK8s.assertExists(context, topicName.asKubeName());
        mockKafka.assertExists(context, topicName);
        mockTopicStore.read(topicName).onComplete(readResult -> {
            assertSucceeded(context, readResult);
            context.verify(() -> assertThat(readResult.result(), is(mergedTopic)));
            async.countDown();
        });
        mockK8s.getFromName(topicName.asKubeName()).onComplete(readResult -> {
            assertSucceeded(context, readResult);
            context.verify(() -> assertThat(TopicSerialization.fromTopicResource(readResult.result()), is(mergedTopic)));
            async.countDown();
        });
        try {
            async.await(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        context.verify(() -> assertThat(mockKafka.getTopicState(topicName), is(mergedTopic)));
        context.verify(() -> {
            MeterRegistry registry = metrics.meterRegistry();
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations").tag("kind", "KafkaTopic").counter().count(), is(1.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.successful").tag("kind", "KafkaTopic").counter().count(), is(1.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.failed").tag("kind", "KafkaTopic").counter().count(), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().count(), is(1L));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().totalTime(TimeUnit.MILLISECONDS), greaterThan(0.0));
        });
        context.completeNow();
    });
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) CountDownLatch(java.util.concurrent.CountDownLatch) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 60 with KafkaTopic

use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi-kafka-operator by strimzi.

the class TopicOperatorTest method testOnKafkaTopicChanged.

@Test
public void testOnKafkaTopicChanged(VertxTestContext context) {
    Topic kubeTopic = new Topic.Builder(topicName, resourceName, 10, (short) 2, map("cleanup.policy", "baz"), null).build();
    Topic kafkaTopic = new Topic.Builder(topicName, resourceName, 10, (short) 2, map("cleanup.policy", "bar"), null).build();
    Topic privateTopic = kafkaTopic;
    KafkaTopic resource = TopicSerialization.toTopicResource(kubeTopic, labels);
    LogContext logContext = LogContext.zkWatch("///", topicName.toString(), topicOperator.getNamespace(), topicName.toString());
    mockKafka.setCreateTopicResponse(topicName.toString(), null).createTopic(Reconciliation.DUMMY_RECONCILIATION, kafkaTopic);
    mockKafka.setTopicMetadataResponse(topicName, Utils.getTopicMetadata(kafkaTopic), null);
    mockKafka.setUpdateTopicResponse(topicName -> Future.succeededFuture());
    mockTopicStore.setCreateTopicResponse(topicName, null).create(privateTopic);
    mockTopicStore.setUpdateTopicResponse(topicName, null);
    mockK8s.setCreateResponse(resourceName, null);
    mockK8s.createResource(resource).onComplete(ar -> {
        assertSucceeded(context, ar);
    });
    mockK8s.setModifyResponse(resourceName, null);
    CountDownLatch async = new CountDownLatch(3);
    topicOperator.onResourceEvent(logContext, resource, MODIFIED).onComplete(ar -> {
        assertSucceeded(context, ar);
        context.verify(() -> assertThat(mockKafka.getTopicState(topicName).getConfig().get("cleanup.policy"), is("baz")));
        mockTopicStore.read(topicName).onComplete(ar2 -> {
            assertSucceeded(context, ar2);
            context.verify(() -> assertThat(ar2.result().getConfig().get("cleanup.policy"), is("baz")));
            async.countDown();
        });
        mockK8s.getFromName(resourceName).onComplete(ar2 -> {
            assertSucceeded(context, ar2);
            context.verify(() -> assertThat(ar2.result(), is(notNullValue())));
            context.verify(() -> assertThat(TopicSerialization.fromTopicResource(ar2.result()).getConfig().get("cleanup.policy"), is("baz")));
            async.countDown();
        });
        context.verify(() -> {
            MeterRegistry registry = metrics.meterRegistry();
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "resource.state").tag("kind", "KafkaTopic").tag("name", topicName.toString()).tag("resource-namespace", "default-namespace").gauge().value(), is(1.0));
        });
        async.countDown();
        try {
            async.await(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        context.completeNow();
    });
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) CountDownLatch(java.util.concurrent.CountDownLatch) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaTopic (io.strimzi.api.kafka.model.KafkaTopic)187 Test (org.junit.jupiter.api.Test)92 KafkaTopicBuilder (io.strimzi.api.kafka.model.KafkaTopicBuilder)80 Checkpoint (io.vertx.junit5.Checkpoint)46 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)38 HashMap (java.util.HashMap)32 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)30 CountDownLatch (java.util.concurrent.CountDownLatch)28 NewTopic (org.apache.kafka.clients.admin.NewTopic)28 List (java.util.List)26 Map (java.util.Map)26 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)22 KafkaTopicStatus (io.strimzi.api.kafka.model.status.KafkaTopicStatus)22 AsyncResult (io.vertx.core.AsyncResult)22 MaxAttemptsExceededException (io.strimzi.operator.common.MaxAttemptsExceededException)20 Vertx (io.vertx.core.Vertx)20 Matchers.containsString (org.hamcrest.Matchers.containsString)20 Watcher (io.fabric8.kubernetes.client.Watcher)18 KafkaClients (io.strimzi.systemtest.kafkaclients.internalClients.KafkaClients)18 KafkaClientsBuilder (io.strimzi.systemtest.kafkaclients.internalClients.KafkaClientsBuilder)18