Search in sources :

Example 11 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi-kafka-operator by strimzi.

the class JbodStorageTest method init.

@BeforeEach
private void init() {
    this.volumes = new ArrayList<>(2);
    volumes.add(new PersistentClaimStorageBuilder().withId(0).withDeleteClaim(true).withSize("100Gi").build());
    volumes.add(new PersistentClaimStorageBuilder().withId(1).withDeleteClaim(false).withSize("100Gi").build());
    this.kafka = new KafkaBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(NAME).endMetadata().withNewSpec().withNewKafka().withReplicas(3).withListeners(new GenericKafkaListenerBuilder().withName("plain").withPort(9092).withType(KafkaListenerType.INTERNAL).withTls(false).build()).withNewJbodStorage().withVolumes(volumes).endJbodStorage().endKafka().withNewZookeeper().withReplicas(1).endZookeeper().endSpec().build();
    // setting up the Kafka CRD
    CustomResourceDefinition kafkaAssemblyCrd = Crds.kafka();
    // setting up a mock Kubernetes client
    this.mockClient = new MockKube().withCustomResourceDefinition(kafkaAssemblyCrd, Kafka.class, KafkaList.class).end().withCustomResourceDefinition(Crds.strimziPodSet(), StrimziPodSet.class, StrimziPodSetList.class).end().build();
    // initialize a Kafka in MockKube
    Crds.kafkaOperation(this.mockClient).inNamespace(NAMESPACE).withName(NAME).create(this.kafka);
    PlatformFeaturesAvailability pfa = new PlatformFeaturesAvailability(false, KubernetesVersion.V1_16);
    // creating the Kafka operator
    ResourceOperatorSupplier ros = new ResourceOperatorSupplier(this.vertx, this.mockClient, ResourceUtils.zookeeperLeaderFinder(this.vertx, this.mockClient), ResourceUtils.adminClientProvider(), ResourceUtils.zookeeperScalerProvider(), ResourceUtils.metricsProvider(), pfa, FeatureGates.NONE, 60_000L);
    this.operator = new KafkaAssemblyOperator(this.vertx, pfa, new MockCertManager(), new PasswordGenerator(10, "a", "a"), ros, ResourceUtils.dummyClusterOperatorConfig(VERSIONS, 2_000));
}
Also used : StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Kafka(io.strimzi.api.kafka.model.Kafka) PersistentClaimStorageBuilder(io.strimzi.api.kafka.model.storage.PersistentClaimStorageBuilder) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) KafkaList(io.strimzi.api.kafka.KafkaList) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) MockKube(io.strimzi.test.mockkube.MockKube) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi-kafka-operator by strimzi.

the class KafkaConnectAssemblyOperatorMockTest method setConnectResource.

private void setConnectResource(KafkaConnect connectResource) {
    mockKube = new MockKube();
    mockClient = mockKube.withCustomResourceDefinition(Crds.kafkaConnect(), KafkaConnect.class, KafkaConnectList.class, KafkaConnect::getStatus, KafkaConnect::setStatus).withInitialInstances(Collections.singleton(connectResource)).end().withCustomResourceDefinition(Crds.kafkaConnector(), KafkaConnector.class, KafkaConnectorList.class, KafkaConnector::getStatus, KafkaConnector::setStatus).end().build();
}
Also used : MockKube(io.strimzi.test.mockkube.MockKube) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect)

Example 13 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi-kafka-operator by strimzi.

the class KafkaConnectorIT method test.

@Test
public void test(VertxTestContext context) {
    KafkaConnectApiImpl connectClient = new KafkaConnectApiImpl(vertx);
    KubernetesClient client = new MockKube().withCustomResourceDefinition(Crds.kafkaConnector(), KafkaConnector.class, KafkaConnectorList.class).end().build();
    PlatformFeaturesAvailability pfa = new PlatformFeaturesAvailability(false, KubernetesVersion.V1_20);
    String namespace = "ns";
    String connectorName = "my-connector";
    LinkedHashMap<String, Object> config = new LinkedHashMap<>();
    config.put(TestingConnector.START_TIME_MS, 1_000);
    config.put(TestingConnector.STOP_TIME_MS, 0);
    config.put(TestingConnector.TASK_START_TIME_MS, 1_000);
    config.put(TestingConnector.TASK_STOP_TIME_MS, 0);
    config.put(TestingConnector.TASK_POLL_TIME_MS, 1_000);
    config.put(TestingConnector.TASK_POLL_RECORDS, 100);
    config.put(TestingConnector.NUM_PARTITIONS, 1);
    config.put(TestingConnector.TOPIC_NAME, "my-topic");
    KafkaConnector connector = createKafkaConnector(namespace, connectorName, config);
    Crds.kafkaConnectorOperation(client).inNamespace(namespace).create(connector);
    // Intercept status updates at CrdOperator level
    // This is to bridge limitations between MockKube and the CrdOperator, as there are currently no Fabric8 APIs for status update
    CrdOperator connectCrdOperator = mock(CrdOperator.class);
    when(connectCrdOperator.updateStatusAsync(any(), any())).thenAnswer(invocation -> {
        try {
            return Future.succeededFuture(Crds.kafkaConnectorOperation(client).inNamespace(namespace).withName(connectorName).patch((KafkaConnector) invocation.getArgument(1)));
        } catch (Exception e) {
            return Future.failedFuture(e);
        }
    });
    when(connectCrdOperator.getAsync(any(), any())).thenAnswer(invocationOnMock -> {
        try {
            return Future.succeededFuture(Crds.kafkaConnectorOperation(client).inNamespace(namespace).withName(connectorName).get());
        } catch (Exception e) {
            return Future.failedFuture(e);
        }
    });
    MetricsProvider metrics = new MicrometerMetricsProvider();
    KafkaConnectAssemblyOperator operator = new KafkaConnectAssemblyOperator(vertx, pfa, new ResourceOperatorSupplier(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, connectCrdOperator, null, null, null, null, null, null, metrics, null, null), ClusterOperatorConfig.fromMap(Collections.emptyMap(), KafkaVersionTestUtils.getKafkaVersionLookup()), connect -> new KafkaConnectApiImpl(vertx), connectCluster.getPort(2)) {
    };
    Checkpoint async = context.checkpoint();
    operator.reconcileConnectorAndHandleResult(new Reconciliation("test", "KafkaConnect", namespace, "bogus"), "localhost", connectClient, true, connectorName, connector).onComplete(context.succeeding(v -> assertConnectorIsRunning(context, client, namespace, connectorName))).compose(v -> {
        config.remove(TestingConnector.START_TIME_MS, 1_000);
        config.put(TestingConnector.START_TIME_MS, 1_000);
        Crds.kafkaConnectorOperation(client).inNamespace(namespace).withName(connectorName).patch(createKafkaConnector(namespace, connectorName, config));
        return operator.reconcileConnectorAndHandleResult(new Reconciliation("test", "KafkaConnect", namespace, "bogus"), "localhost", connectClient, true, connectorName, connector);
    }).onComplete(context.succeeding(v -> context.verify(() -> {
        assertConnectorIsRunning(context, client, namespace, connectorName);
        // Assert metrics from Connector Operator
        MeterRegistry registry = metrics.meterRegistry();
        assertThat(registry.get(AbstractOperator.METRICS_PREFIX + "reconciliations").tag("kind", KafkaConnector.RESOURCE_KIND).counter().count(), CoreMatchers.is(2.0));
        assertThat(registry.get(AbstractOperator.METRICS_PREFIX + "reconciliations.successful").tag("kind", KafkaConnector.RESOURCE_KIND).counter().count(), CoreMatchers.is(2.0));
        assertThat(registry.get(AbstractOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", KafkaConnector.RESOURCE_KIND).timer().count(), CoreMatchers.is(2L));
        assertThat(registry.get(AbstractOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", KafkaConnector.RESOURCE_KIND).timer().totalTime(TimeUnit.MILLISECONDS), greaterThan(0.0));
        async.flag();
    })));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) HashMap(java.util.HashMap) Crds(io.strimzi.api.kafka.Crds) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) MockKube(io.strimzi.test.mockkube.MockKube) AfterAll(org.junit.jupiter.api.AfterAll) LinkedHashMap(java.util.LinkedHashMap) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) MicrometerMetricsOptions(io.vertx.micrometer.MicrometerMetricsOptions) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Map(java.util.Map) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StrimziKafkaCluster(io.strimzi.test.container.StrimziKafkaCluster) KafkaConnectorBuilder(io.strimzi.api.kafka.model.KafkaConnectorBuilder) AbstractOperator(io.strimzi.operator.common.AbstractOperator) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) KubernetesVersion(io.strimzi.operator.KubernetesVersion) MetricsProvider(io.strimzi.operator.common.MetricsProvider) MicrometerMetricsProvider(io.strimzi.operator.common.MicrometerMetricsProvider) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) IOException(java.io.IOException) VertxPrometheusOptions(io.vertx.micrometer.VertxPrometheusOptions) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Reconciliation(io.strimzi.operator.common.Reconciliation) AfterEach(org.junit.jupiter.api.AfterEach) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Collections(java.util.Collections) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) MockKube(io.strimzi.test.mockkube.MockKube) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Reconciliation(io.strimzi.operator.common.Reconciliation) MicrometerMetricsProvider(io.strimzi.operator.common.MicrometerMetricsProvider) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) MetricsProvider(io.strimzi.operator.common.MetricsProvider) MicrometerMetricsProvider(io.strimzi.operator.common.MicrometerMetricsProvider) Test(org.junit.jupiter.api.Test)

Example 14 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi-kafka-operator by strimzi.

the class KafkaRebalanceAssemblyOperatorTest method beforeEach.

@BeforeEach
public void beforeEach(Vertx vertx) {
    ccServer.reset();
    kubernetesClient = new MockKube().withCustomResourceDefinition(Crds.kafkaRebalance(), KafkaRebalance.class, KafkaRebalanceList.class).end().build();
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
    PlatformFeaturesAvailability pfa = new PlatformFeaturesAvailability(true, kubernetesVersion);
    // Override to inject mocked cruise control address so real cruise control not required
    kcrao = new KafkaRebalanceAssemblyOperator(vertx, pfa, supplier, ResourceUtils.dummyClusterOperatorConfig()) {

        @Override
        public String cruiseControlHost(String clusterName, String clusterNamespace) {
            return HOST;
        }

        @Override
        public CruiseControlApi cruiseControlClientProvider(Secret ccSecret, Secret ccApiSecret, boolean apiAuthEnabled, boolean apiSslEnabled) {
            return new CruiseControlApiImpl(vertx, 1, ccSecret, ccApiSecret, true, true);
        }
    };
    mockRebalanceOps = supplier.kafkaRebalanceOperator;
    mockKafkaOps = supplier.kafkaOperator;
    mockCmOps = supplier.configMapOperations;
    mockSecretOps = supplier.secretOperations;
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) MockKube(io.strimzi.test.mockkube.MockKube) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) KafkaRebalanceList(io.strimzi.api.kafka.KafkaRebalanceList) CruiseControlApi(io.strimzi.operator.cluster.operator.resource.cruisecontrol.CruiseControlApi) CruiseControlApiImpl(io.strimzi.operator.cluster.operator.resource.cruisecontrol.CruiseControlApiImpl) KafkaRebalance(io.strimzi.api.kafka.model.KafkaRebalance) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi-kafka-operator by strimzi.

the class KafkaMirrorMaker2AssemblyOperatorMockTest method setMirrorMaker2Resource.

private void setMirrorMaker2Resource(KafkaMirrorMaker2 mirrorMaker2Resource) {
    if (mockClient != null) {
        mockClient.close();
    }
    MockKube mockKube = new MockKube();
    mockClient = mockKube.withCustomResourceDefinition(Crds.kafkaMirrorMaker2(), KafkaMirrorMaker2.class, KafkaMirrorMaker2List.class, KafkaMirrorMaker2::getStatus, KafkaMirrorMaker2::setStatus).withInitialInstances(Collections.singleton(mirrorMaker2Resource)).end().build();
}
Also used : MockKube(io.strimzi.test.mockkube.MockKube) KafkaMirrorMaker2(io.strimzi.api.kafka.model.KafkaMirrorMaker2)

Aggregations

MockKube (io.strimzi.test.mockkube.MockKube)42 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)22 HashMap (java.util.HashMap)20 Test (org.junit.jupiter.api.Test)20 ResourceOperatorSupplier (io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier)18 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)16 AdminClientConfig (org.apache.kafka.clients.admin.AdminClientConfig)16 BeforeEach (org.junit.jupiter.api.BeforeEach)14 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)10 PasswordGenerator (io.strimzi.operator.common.PasswordGenerator)10 Reconciliation (io.strimzi.operator.common.Reconciliation)10 MockCertManager (io.strimzi.operator.common.operator.MockCertManager)10 KafkaList (io.strimzi.api.kafka.KafkaList)8 StrimziPodSetList (io.strimzi.api.kafka.StrimziPodSetList)8 Kafka (io.strimzi.api.kafka.model.Kafka)8 StrimziPodSet (io.strimzi.api.kafka.model.StrimziPodSet)8 ClusterOperatorConfig (io.strimzi.operator.cluster.ClusterOperatorConfig)8 Checkpoint (io.vertx.junit5.Checkpoint)8 Properties (java.util.Properties)7 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)6