Search in sources :

Example 6 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi by strimzi.

the class TopicOperatorMockTest method testCreatedWithoutTopicNameInKube.

@Test
public void testCreatedWithoutTopicNameInKube(VertxTestContext context) throws InterruptedException, ExecutionException {
    LOGGER.info("Test started");
    int retention = 100_000_000;
    KafkaTopic kt = new KafkaTopicBuilder().withNewMetadata().withName("my-topic").withNamespace(NAMESPACE).addToLabels(Labels.STRIMZI_KIND_LABEL, "topic").addToLabels(Labels.KUBERNETES_NAME_LABEL, "topic-operator").endMetadata().withNewSpec().withPartitions(1).withReplicas(1).addToConfig("retention.bytes", retention).endSpec().build();
    testCreatedInKube(context, kt);
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 7 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi by strimzi.

the class TopicOperatorReplicationIT method testKafkaTopicModifiedChangedReplication.

@Test
public void testKafkaTopicModifiedChangedReplication() throws Exception {
    // create the topicResource
    String topicName = "test-kafkatopic-modified-with-changed-replication";
    String resourceName = createTopic(topicName, asList(1));
    // now change the topicResource
    KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().withReplicas(2).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).patch(changedTopic);
    assertStatusNotReady(topicName, "Changing 'spec.replicas' is not supported. " + "This KafkaTopic's 'spec.replicas' should be reverted to 1 and then " + "the replication should be changed directly in Kafka.");
    // Now do the revert
    changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().withReplicas(1).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).patch(changedTopic);
    assertStatusReady(topicName);
    File file = File.createTempFile(getClass().getSimpleName(), ".json");
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = new ObjectNode(mapper.getNodeFactory());
    root.put("version", 1).putArray("partitions").addObject().put("topic", topicName).put("partition", 0).putArray("replicas").add(0).add(1);
    mapper.writeValue(file, root);
    LOGGER.info("Creating 2nd replica: {}", mapper.writeValueAsString(root));
    // Now change it in Kafka
    String reassignmentOutput = doReassignmentCommand("--bootstrap-server", kafkaCluster.getBootstrapServers(), "--reassignment-json-file", file.getAbsolutePath(), "--execute");
    LOGGER.info(reassignmentOutput);
    LOGGER.info("Waiting for reassignment completion");
    waitFor(() -> {
        String output = doReassignmentCommand("--bootstrap-server", kafkaCluster.getBootstrapServers(), "--reassignment-json-file", file.getAbsolutePath(), "--verify");
        LOGGER.info(output);
        if (output.contains("Reassignment of partition test-kafkatopic-modified-with-changed-replication-0 is still in progress")) {
            return false;
        } else {
            assertThat("Reassignment is no longer in progress, but wasn't successful: " + output, output.contains("Reassignment of partition test-kafkatopic-modified-with-changed-replication-0 is complete"), is(true));
            return true;
        }
    }, "reassignment completion");
    // wait for reconciliation and that now replicas=2.
    waitFor(() -> {
        KafkaTopic kafkaTopic = Crds.topicOperation(kubeClient).inNamespace(NAMESPACE).withName(resourceName).get();
        LOGGER.info(kafkaTopic == null ? "Null topic" : kafkaTopic.toString());
        return kafkaTopic.getSpec().getReplicas() == 2;
    }, "KafkaTopic.spec.replicas=2");
    // And check that the status is ready
    assertStatusReady(topicName);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 8 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi by strimzi.

the class TopicOperatorTest method testOnKafkaTopicAdded_invalidResource.

/**
 * Test what happens when a non-topic KafkaTopic gets created in kubernetes
 */
@Test
public void testOnKafkaTopicAdded_invalidResource(VertxTestContext context) {
    KafkaTopic kafkaTopic = new KafkaTopicBuilder().withMetadata(new ObjectMetaBuilder().withName("invalid").withLabels(labels.labels()).build()).withNewSpec().withReplicas(1).withPartitions(1).withConfig(singletonMap(null, null)).endSpec().build();
    String errorMessage = "KafkaTopic's spec.config has invalid entry: The key 'null' of the topic config is invalid: The value corresponding to the key must have a string, number or boolean value but the value was null";
    mockK8s.setGetFromNameResponse(new ResourceName(kafkaTopic), Future.succeededFuture(kafkaTopic));
    LogContext logContext = LogContext.kubeWatch(Watcher.Action.ADDED, kafkaTopic);
    Checkpoint async = context.checkpoint();
    topicOperator.onResourceEvent(logContext, kafkaTopic, ADDED).onComplete(ar -> {
        assertFailed(context, ar);
        context.verify(() -> assertThat(ar.cause(), instanceOf(InvalidTopicException.class)));
        context.verify(() -> assertThat(ar.cause().getMessage(), is(errorMessage)));
        mockKafka.assertEmpty(context);
        mockTopicStore.assertEmpty(context);
        assertNotReadyStatus(context, new InvalidTopicException(null, ar.cause().getMessage()));
        context.verify(() -> {
            MeterRegistry registry = metrics.meterRegistry();
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations").tag("kind", "KafkaTopic").counter().count(), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.successful").tag("kind", "KafkaTopic").counter().count(), is(0.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(0L));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().totalTime(TimeUnit.MILLISECONDS), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "resource.state").tag("kind", "KafkaTopic").tag("name", "invalid").tag("resource-namespace", "default-namespace").tag("reason", errorMessage).gauge().value(), is(0.0));
        });
        async.flag();
    });
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 9 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi by strimzi.

the class TopicOperatorTest method resourceAdded.

/**
 * Trigger {@link TopicOperator#onResourceEvent(LogContext, KafkaTopic, io.fabric8.kubernetes.client.Watcher.Action)}
 * and have the Kafka and TopicStore respond with the given exceptions.
 */
private TopicOperator resourceAdded(VertxTestContext context, CountDownLatch latch, Exception createException, Exception storeException) throws InterruptedException {
    mockKafka.setCreateTopicResponse(topicName.toString(), createException);
    mockKafka.setTopicExistsResult(t -> Future.succeededFuture(false));
    mockTopicStore.setCreateTopicResponse(topicName, storeException);
    KafkaTopic kafkaTopic = new KafkaTopicBuilder().withMetadata(metadata).withNewSpec().withReplicas(2).withPartitions(10).endSpec().build();
    mockKafka.setTopicMetadataResponses(topicName -> Future.succeededFuture(), topicName -> Future.succeededFuture(Utils.getTopicMetadata(TopicSerialization.fromTopicResource(kafkaTopic))));
    LogContext logContext = LogContext.kubeWatch(Watcher.Action.ADDED, kafkaTopic);
    Checkpoint async = context.checkpoint();
    mockK8s.setGetFromNameResponse(new ResourceName(kafkaTopic), Future.succeededFuture(kafkaTopic));
    topicOperator.onResourceEvent(logContext, kafkaTopic, ADDED).onComplete(ar -> {
        if (createException != null || storeException != null) {
            assertFailed(context, ar);
            Class<? extends Exception> expectedExceptionType;
            if (createException != null) {
                expectedExceptionType = createException.getClass();
            } else {
                expectedExceptionType = storeException.getClass();
            }
            if (!expectedExceptionType.equals(ar.cause().getClass())) {
                ar.cause().printStackTrace();
            }
            context.verify(() -> assertThat(ar.cause().getMessage(), ar.cause().getClass().getName(), is(expectedExceptionType.getName())));
            TopicName topicName = TopicSerialization.fromTopicResource(kafkaTopic).getTopicName();
            if (createException != null) {
                mockKafka.assertNotExists(context, topicName);
            } else {
                mockKafka.assertExists(context, topicName);
            }
            mockTopicStore.assertNotExists(context, topicName);
        // TODO mockK8s.assertContainsEvent(context, e -> "Error".equals(e.getKind()));
        } else {
            assertSucceeded(context, ar);
            Topic expectedTopic = TopicSerialization.fromTopicResource(kafkaTopic);
            mockKafka.assertContains(context, expectedTopic);
            mockTopicStore.assertContains(context, expectedTopic);
            mockK8s.assertNoEvents(context);
        }
        if (latch != null) {
            latch.countDown();
        }
        async.flag();
    });
    if (!context.awaitCompletion(60, TimeUnit.SECONDS)) {
        context.failNow(new Throwable("Test timeout"));
    }
    return topicOperator;
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic)

Example 10 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi by strimzi.

the class TopicSerializationTest method testErrorInConfigInvalidValueWrongType.

@Test
public void testErrorInConfigInvalidValueWrongType() {
    KafkaTopic kafkaTopic = new KafkaTopicBuilder().withMetadata(new ObjectMetaBuilder().withName("my-topic").build()).withNewSpec().withReplicas(1).withPartitions(1).withConfig(singletonMap("foo", new Object())).endSpec().build();
    try {
        TopicSerialization.fromTopicResource(kafkaTopic);
        fail("Should throw");
    } catch (InvalidTopicException e) {
        assertThat(e.getMessage(), is("KafkaTopic's spec.config has invalid entry: The key 'foo' of the topic config is invalid: The value corresponding to the key must have a string, number or boolean value but was of type java.lang.Object"));
    }
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaTopicBuilder (io.strimzi.api.kafka.model.KafkaTopicBuilder)56 KafkaTopic (io.strimzi.api.kafka.model.KafkaTopic)54 Test (org.junit.jupiter.api.Test)40 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)22 Checkpoint (io.vertx.junit5.Checkpoint)22 NewTopic (org.apache.kafka.clients.admin.NewTopic)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)2 KafkaTopicList (io.strimzi.api.kafka.KafkaTopicList)2 KafkaTopicStatusBuilder (io.strimzi.api.kafka.model.status.KafkaTopicStatusBuilder)2 Vertx (io.vertx.core.Vertx)2 VertxExtension (io.vertx.junit5.VertxExtension)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 File (java.io.File)2