Search in sources :

Example 1 with KafkaTopicBuilder

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

the class K8sImplTest method testList.

@Test
public void testList(VertxTestContext context) {
    Checkpoint async = context.checkpoint();
    List<KafkaTopic> mockKafkaTopicsList = Collections.singletonList(new KafkaTopicBuilder().withMetadata(new ObjectMetaBuilder().withName("unrelated").withLabels(Collections.singletonMap("foo", "bar")).build()).build());
    KubernetesClient mockClient = mock(KubernetesClient.class);
    MixedOperation<KafkaTopic, KafkaTopicList, Resource<KafkaTopic>> mockResources = mock(MixedOperation.class);
    when(mockClient.resources(any(Class.class), any(Class.class))).thenReturn(mockResources);
    when(mockClient.resources(any(Class.class), any(Class.class))).thenReturn(mockResources);
    when(mockResources.withLabels(any())).thenReturn(mockResources);
    when(mockResources.inNamespace(any())).thenReturn(mockResources);
    when(mockResources.list()).thenAnswer(invocation -> {
        KafkaTopicList ktl = new KafkaTopicList();
        ktl.setItems(mockKafkaTopicsList);
        return ktl;
    });
    K8sImpl k8s = new K8sImpl(vertx, mockClient, new Labels("foo", "bar"), "default");
    k8s.listResources().onComplete(context.succeeding(kafkaTopics -> context.verify(() -> {
        assertThat(kafkaTopics, is(mockKafkaTopicsList));
        async.flag();
    })));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) Resource(io.fabric8.kubernetes.client.dsl.Resource) List(java.util.List) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) KafkaTopicList(io.strimzi.api.kafka.KafkaTopicList) BeforeAll(org.junit.jupiter.api.BeforeAll) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Mockito.mock(org.mockito.Mockito.mock) KafkaTopicList(io.strimzi.api.kafka.KafkaTopicList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Resource(io.fabric8.kubernetes.client.dsl.Resource) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with KafkaTopicBuilder

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

the class TopicOperatorBaseIT method alterTopicConfigInKube.

protected String alterTopicConfigInKube(String resourceName, String key, Function<String, String> mutator) {
    // now change the topic resource
    Object retention = operation().inNamespace(NAMESPACE).withName(resourceName).get().getSpec().getConfig().getOrDefault(key, "12341233");
    String currentValue = retention instanceof Integer ? retention.toString() : (String) retention;
    String newValue = mutator.apply(currentValue);
    KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().addToConfig(key, newValue).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).replace(changedTopic);
    return newValue;
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder)

Example 3 with KafkaTopicBuilder

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

the class TopicOperatorIT method testKafkaTopicModifiedNameChanged.

@Test
public void testKafkaTopicModifiedNameChanged() throws Exception {
    // create the topicResource
    String topicName = "test-kafkatopic-modified-name-changed";
    KafkaTopic topicResource = createKafkaTopicResource(topicName);
    // now change the topicResource
    String changedName = topicName.toUpperCase(Locale.ENGLISH);
    LOGGER.info("Changing Topic Resource spec.topicName from {} to {}", topicName, changedName);
    KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(topicResource.getMetadata().getName()).get()).editOrNewSpec().withTopicName(changedName).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(topicResource.getMetadata().getName()).replace(changedTopic);
    // We expect this to cause a warning event
    waitForEvent(topicResource, "Kafka topics cannot be renamed, but KafkaTopic's spec.topicName has changed.", TopicOperator.EventType.WARNING);
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with KafkaTopicBuilder

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

the class TopicOperatorIT method testInvalidConfig.

@Test
public void testInvalidConfig() throws Exception {
    String topicName = "topic-invalid-config";
    String expectedMessage = "Invalid config value for resource ConfigResource(type=TOPIC, name='" + topicName + "'): Invalid value x for configuration min.insync.replicas: Not a number of type INT";
    String resourceName = createTopic(topicName, new NewTopic(topicName, 2, (short) 1));
    KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().addToConfig("min.insync.replicas", "x").endSpec().build();
    KafkaTopic replaced = operation().inNamespace(NAMESPACE).withName(resourceName).replace(changedTopic);
    assertStatusNotReady(topicName, InvalidRequestException.class, expectedMessage);
    // Now modify Kafka-side to cause another reconciliation: We want the same status.
    alterTopicConfigInKafka(topicName, "compression.type", value -> "snappy".equals(value) ? "lz4" : "snappy");
    // Wait for a periodic reconciliation
    Thread.sleep(RECONCILIATION_INTERVAL + 10_000);
    assertStatusNotReady(topicName, InvalidRequestException.class, expectedMessage);
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.jupiter.api.Test)

Example 5 with KafkaTopicBuilder

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

the class TopicOperatorMockTest method testCreatedWithDefaultsInKube.

@Test
public void testCreatedWithDefaultsInKube(VertxTestContext context) throws InterruptedException, ExecutionException {
    int retention = 100_000_000;
    KafkaTopic kt = new KafkaTopicBuilder().withNewMetadata().withName("my-topic").withNamespace(NAMESPACE).addToLabels(Labels.STRIMZI_KIND_LABEL, "topic").endMetadata().withNewSpec().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)

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