Search in sources :

Example 6 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi by strimzi.

the class ConfigTest method testInvalidSaslConfig.

@Test
public void testInvalidSaslConfig() {
    Map<String, String> map = new HashMap<>(MANDATORY);
    map.put(Config.SASL_ENABLED.key, "true");
    MockKube mockKube = new MockKube();
    KubernetesClient kubeClient = mockKube.build();
    Config config = new Config(map);
    Session session = new Session(kubeClient, config);
    assertThrows(InvalidConfigurationException.class, () -> session.adminClientProperties());
    String username = "admin";
    String password = "password";
    map.put(Config.SASL_USERNAME.key, username);
    map.put(Config.SASL_PASSWORD.key, password);
    Config configWithCredentials = new Config(map);
    Session sessionWithCredentials = new Session(kubeClient, configWithCredentials);
    assertThrows(IllegalArgumentException.class, () -> sessionWithCredentials.adminClientProperties());
}
Also used : MockKube(io.strimzi.test.mockkube.MockKube) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) Test(org.junit.jupiter.api.Test)

Example 7 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi by strimzi.

the class ConfigTest method testInvalidTlsSecurityProtocolConfig.

@Test
public void testInvalidTlsSecurityProtocolConfig() {
    Map<String, String> map = new HashMap<>(MANDATORY);
    map.put(Config.SECURITY_PROTOCOL.key, "PLAINTEXT");
    map.put(Config.TLS_ENABLED.key, "true");
    MockKube mockKube = new MockKube();
    KubernetesClient kubeClient = mockKube.build();
    Config config = new Config(map);
    Session session = new Session(kubeClient, config);
    assertThrows(InvalidConfigurationException.class, () -> session.adminClientProperties());
}
Also used : MockKube(io.strimzi.test.mockkube.MockKube) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) Test(org.junit.jupiter.api.Test)

Example 8 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi by strimzi.

the class ConfigTest method testDefaultConfig.

@Test
public void testDefaultConfig() {
    Map<String, String> map = new HashMap<>(MANDATORY);
    MockKube mockKube = new MockKube();
    KubernetesClient kubeClient = mockKube.build();
    Config config = new Config(map);
    Session session = new Session(kubeClient, config);
    Properties adminClientProps = session.adminClientProperties();
    assertThat(adminClientProps.getProperty(AdminClientConfig.SECURITY_PROTOCOL_CONFIG), is("PLAINTEXT"));
    assertNull(adminClientProps.getProperty(SaslConfigs.SASL_MECHANISM));
    assertNull(adminClientProps.getProperty(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG));
}
Also used : MockKube(io.strimzi.test.mockkube.MockKube) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 9 with MockKube

use of io.strimzi.test.mockkube.MockKube in project strimzi by strimzi.

the class TopicOperatorMockTest method setup.

@BeforeEach
public void setup(VertxTestContext context) throws Exception {
    // Create cluster in @BeforeEach instead of @BeforeAll as once the checkpoints causing premature success were fixed,
    // tests were failing due to topic "my-topic" already existing, and trying to delete the topics at the end of the test was timing out occasionally.
    // So works best when the cluster is recreated for each test to avoid shared state
    Map<String, String> config = new HashMap<>();
    config.put("zookeeper.connect", "zookeeper:2181");
    kafkaCluster = new StrimziKafkaCluster(1, 1, config);
    kafkaCluster.start();
    MockKube mockKube = new MockKube();
    mockKube.withCustomResourceDefinition(Crds.kafkaTopic(), KafkaTopic.class, KafkaTopicList.class, KafkaTopic::getStatus, KafkaTopic::setStatus);
    kubeClient = mockKube.build();
    adminClient = AdminClient.create(Map.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaCluster.getBootstrapServers()));
    Config topicConfig = new Config(Map.of(Config.KAFKA_BOOTSTRAP_SERVERS.key, kafkaCluster.getBootstrapServers(), Config.ZOOKEEPER_CONNECT.key, kafkaCluster.getZookeeper().getHost() + ":" + kafkaCluster.getZookeeper().getFirstMappedPort(), Config.ZOOKEEPER_CONNECTION_TIMEOUT_MS.key, "30000", Config.NAMESPACE.key, "myproject", Config.CLIENT_ID.key, "myproject-client-id", Config.FULL_RECONCILIATION_INTERVAL_MS.key, "10000"));
    session = new Session(kubeClient, topicConfig);
    Checkpoint async = context.checkpoint();
    vertx.deployVerticle(session, ar -> {
        if (ar.succeeded()) {
            deploymentId = ar.result();
            topicsConfigWatcher = session.topicConfigsWatcher;
            topicWatcher = session.topicWatcher;
            topicsWatcher = session.topicsWatcher;
            metrics = session.metricsRegistry;
            metrics.forEachMeter(meter -> metrics.remove(meter));
            async.flag();
        } else {
            ar.cause().printStackTrace();
            context.failNow(new Throwable("Failed to deploy session"));
        }
    });
    if (!context.awaitCompletion(60, TimeUnit.SECONDS)) {
        context.failNow(new Throwable("Test timeout"));
    }
    int timeout = 30_000;
    waitFor("Topic watcher not started", 1_000, timeout, () -> this.topicWatcher.started());
    waitFor("Topic configs watcher not started", 1_000, timeout, () -> this.topicsConfigWatcher.started());
    waitFor("Topic watcher not started", 1_000, timeout, () -> this.topicsWatcher.started());
}
Also used : MockKube(io.strimzi.test.mockkube.MockKube) Checkpoint(io.vertx.junit5.Checkpoint) HashMap(java.util.HashMap) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) StrimziKafkaCluster(io.strimzi.test.container.StrimziKafkaCluster) Checkpoint(io.vertx.junit5.Checkpoint) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with MockKube

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

the class ConnectorMockTest method setup.

@SuppressWarnings({ "checkstyle:MethodLength" })
@BeforeEach
public void setup(VertxTestContext testContext) {
    vertx = Vertx.vertx();
    client = new MockKube().withCustomResourceDefinition(Crds.kafkaConnect(), KafkaConnect.class, KafkaConnectList.class, KafkaConnect::getStatus, KafkaConnect::setStatus).end().withCustomResourceDefinition(Crds.kafkaConnector(), KafkaConnector.class, KafkaConnectorList.class, KafkaConnector::getStatus, KafkaConnector::setStatus).end().build();
    PlatformFeaturesAvailability pfa = new PlatformFeaturesAvailability(true, KubernetesVersion.V1_18);
    setupMockConnectAPI();
    ResourceOperatorSupplier ros = new ResourceOperatorSupplier(vertx, client, new ZookeeperLeaderFinder(vertx, // Retry up to 3 times (4 attempts), with overall max delay of 35000ms
    () -> new BackOff(5_000, 2, 4)), new DefaultAdminClientProvider(), new DefaultZookeeperScalerProvider(), ResourceUtils.metricsProvider(), pfa, FeatureGates.NONE, 10_000);
    ClusterOperatorConfig config = ClusterOperatorConfig.fromMap(map(ClusterOperatorConfig.STRIMZI_KAFKA_IMAGES, KafkaVersionTestUtils.getKafkaImagesEnvVarString(), ClusterOperatorConfig.STRIMZI_KAFKA_CONNECT_IMAGES, KafkaVersionTestUtils.getKafkaConnectImagesEnvVarString(), ClusterOperatorConfig.STRIMZI_KAFKA_MIRROR_MAKER_2_IMAGES, KafkaVersionTestUtils.getKafkaMirrorMaker2ImagesEnvVarString(), ClusterOperatorConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS, Long.toString(Long.MAX_VALUE)), KafkaVersionTestUtils.getKafkaVersionLookup());
    kafkaConnectOperator = new KafkaConnectAssemblyOperator(vertx, pfa, ros, config, x -> api);
    Checkpoint async = testContext.checkpoint();
    // Fail test if watcher closes for any reason
    kafkaConnectOperator.createWatch(NAMESPACE, e -> testContext.failNow(e)).onComplete(testContext.succeeding()).compose(watch -> AbstractConnectOperator.createConnectorWatch(kafkaConnectOperator, NAMESPACE, null)).onComplete(testContext.succeeding(v -> async.flag()));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) BeforeEach(org.junit.jupiter.api.BeforeEach) ConnectorPluginBuilder(io.strimzi.api.kafka.model.connect.ConnectorPluginBuilder) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) TestUtils.waitFor(io.strimzi.test.TestUtils.waitFor) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Annotations(io.strimzi.operator.common.Annotations) TimeoutException(java.util.concurrent.TimeoutException) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) MockKube(io.strimzi.test.mockkube.MockKube) Collections.singletonList(java.util.Collections.singletonList) Resource(io.fabric8.kubernetes.client.dsl.Resource) DefaultAdminClientProvider(io.strimzi.operator.common.DefaultAdminClientProvider) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Mockito.atLeast(org.mockito.Mockito.atLeast) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) JsonObject(io.vertx.core.json.JsonObject) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) Predicate(java.util.function.Predicate) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) KafkaConnectBuilder(io.strimzi.api.kafka.model.KafkaConnectBuilder) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) KafkaConnectCluster(io.strimzi.operator.cluster.model.KafkaConnectCluster) List(java.util.List) Labels(io.strimzi.operator.common.model.Labels) Logger(org.apache.logging.log4j.Logger) Checkpoint(io.vertx.junit5.Checkpoint) Condition(io.strimzi.api.kafka.model.status.Condition) KafkaConnectList(io.strimzi.api.kafka.KafkaConnectList) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) CustomResource(io.fabric8.kubernetes.client.CustomResource) Mockito.mock(org.mockito.Mockito.mock) VertxTestContext(io.vertx.junit5.VertxTestContext) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BackOff(io.strimzi.operator.common.BackOff) HashMap(java.util.HashMap) Crds(io.strimzi.api.kafka.Crds) FeatureGates(io.strimzi.operator.cluster.FeatureGates) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) ZookeeperLeaderFinder(io.strimzi.operator.cluster.operator.resource.ZookeeperLeaderFinder) TestUtils(io.strimzi.test.TestUtils) Status(io.strimzi.api.kafka.model.status.Status) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) KafkaConnectorBuilder(io.strimzi.api.kafka.model.KafkaConnectorBuilder) TestUtils.map(io.strimzi.test.TestUtils.map) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.empty(org.hamcrest.Matchers.empty) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) ConnectorPlugin(io.strimzi.api.kafka.model.connect.ConnectorPlugin) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Reconciliation(io.strimzi.operator.common.Reconciliation) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultZookeeperScalerProvider(io.strimzi.operator.cluster.operator.resource.DefaultZookeeperScalerProvider) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) KafkaConnectResources(io.strimzi.api.kafka.model.KafkaConnectResources) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) BackOff(io.strimzi.operator.common.BackOff) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) MockKube(io.strimzi.test.mockkube.MockKube) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) KafkaConnectList(io.strimzi.api.kafka.KafkaConnectList) ZookeeperLeaderFinder(io.strimzi.operator.cluster.operator.resource.ZookeeperLeaderFinder) DefaultZookeeperScalerProvider(io.strimzi.operator.cluster.operator.resource.DefaultZookeeperScalerProvider) DefaultAdminClientProvider(io.strimzi.operator.common.DefaultAdminClientProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

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