-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integation tests for PgSQL connector
- Loading branch information
1 parent
c682109
commit 5ea08e4
Showing
8 changed files
with
814 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/integration-test/java/io/aiven/connect/jdbc/AbstractIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright 2020 Aiven Oy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.aiven.connect.jdbc; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.Future; | ||
|
||
import cloud.localstack.docker.LocalstackDockerExtension; | ||
import cloud.localstack.docker.annotation.LocalstackDockerProperties; | ||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.AdminClientConfig; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
|
||
import org.apache.avro.generic.GenericRecord; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.KafkaContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@ExtendWith(LocalstackDockerExtension.class) | ||
@LocalstackDockerProperties(services = {"jdbc"}) | ||
@Testcontainers | ||
public abstract class AbstractIT { | ||
|
||
static final Logger LOGGER = LoggerFactory.getLogger(AbstractIT.class); | ||
|
||
protected static final String TEST_TOPIC_NAME = "test_pg_topic"; | ||
|
||
protected static KafkaProducer<String, GenericRecord> producer; | ||
|
||
@Container | ||
protected KafkaContainer kafkaContainer = | ||
new KafkaContainer() | ||
.withEnv("KAFKA_AUTO_CREATE_TOPICS_ENABLE", "false"); | ||
|
||
@Container | ||
protected SchemaRegistryContainer schemaRegistryContainer = | ||
new SchemaRegistryContainer(kafkaContainer); | ||
|
||
protected JdbcConnectService jdbcConnectService; | ||
|
||
@BeforeEach | ||
void startKafka() throws Exception { | ||
LOGGER.info("Configure Kafka connect plugins"); | ||
final var pluginDir = setupPluginDir(); | ||
setupKafka(); | ||
setupKafkaConnect(pluginDir); | ||
createProducer(); | ||
} | ||
|
||
protected AdminClient adminClient() { | ||
final Properties adminClientConfig = new Properties(); | ||
adminClientConfig.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers()); | ||
return AdminClient.create(adminClientConfig); | ||
} | ||
|
||
private static Path setupPluginDir() throws Exception { | ||
final var testDir = Files.createTempDirectory("aiven-kafka-connect-jdbc-test-"); | ||
final var distFile = Paths.get(System.getProperty("integration-test.distribution.file.path")); | ||
assert Files.exists(distFile); | ||
|
||
final var pluginDir = Paths.get(testDir.toString(), "plugins/aiven-kafka-connect-jdbc/"); | ||
Files.createDirectories(pluginDir); | ||
|
||
final String cmd = String.format("tar -xf %s --strip-components=1 -C %s", | ||
distFile.toString(), pluginDir.toString()); | ||
final Process p = Runtime.getRuntime().exec(cmd); | ||
assert p.waitFor() == 0; | ||
return pluginDir; | ||
} | ||
|
||
private void setupKafka() throws Exception { | ||
LOGGER.info("Setup Kafka"); | ||
try (final var adminClient = adminClient()) { | ||
LOGGER.info("Create topic {}", TEST_TOPIC_NAME); | ||
final NewTopic newTopic = new NewTopic(TEST_TOPIC_NAME, 4, (short) 1); | ||
adminClient.createTopics(List.of(newTopic)).all().get(); | ||
} | ||
} | ||
|
||
private void setupKafkaConnect(final Path pluginDir) throws Exception { | ||
LOGGER.info("Start Kafka Connect"); | ||
jdbcConnectService = | ||
new JdbcConnectService(kafkaContainer.getBootstrapServers(), pluginDir); | ||
jdbcConnectService.start(); | ||
} | ||
|
||
private void createProducer() { | ||
LOGGER.info("Create kafka producer"); | ||
final Map<String, Object> producerProps = new HashMap<>(); | ||
producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers()); | ||
producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, | ||
"io.confluent.kafka.serializers.KafkaAvroSerializer"); | ||
producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, | ||
"io.confluent.kafka.serializers.KafkaAvroSerializer"); | ||
producerProps.put("schema.registry.url", schemaRegistryContainer.getSchemaRegistryUrl()); | ||
producer = new KafkaProducer<>(producerProps); | ||
} | ||
|
||
@AfterEach | ||
void stopKafka() throws Exception { | ||
jdbcConnectService.stop(); | ||
} | ||
|
||
protected Future<RecordMetadata> sendMessageAsync(final int partition, | ||
final String key, | ||
final GenericRecord value) { | ||
final var msg = new ProducerRecord<>( | ||
TEST_TOPIC_NAME, partition, | ||
key == null ? null : key, | ||
value == null ? null : value); | ||
return producer.send(msg); | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
src/integration-test/java/io/aiven/connect/jdbc/JdbcConnectService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2020 Aiven Oy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.aiven.connect.jdbc; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.apache.kafka.common.utils.Time; | ||
import org.apache.kafka.connect.runtime.Connect; | ||
import org.apache.kafka.connect.runtime.ConnectorConfig; | ||
import org.apache.kafka.connect.runtime.Herder; | ||
import org.apache.kafka.connect.runtime.Worker; | ||
import org.apache.kafka.connect.runtime.isolation.Plugins; | ||
import org.apache.kafka.connect.runtime.rest.RestServer; | ||
import org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo; | ||
import org.apache.kafka.connect.runtime.standalone.StandaloneConfig; | ||
import org.apache.kafka.connect.runtime.standalone.StandaloneHerder; | ||
import org.apache.kafka.connect.storage.MemoryOffsetBackingStore; | ||
import org.apache.kafka.connect.util.FutureCallback; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class JdbcConnectService { | ||
|
||
static final Logger LOGGER = LoggerFactory.getLogger(JdbcConnectService.class); | ||
|
||
private final String bootstrapServers; | ||
|
||
private final int offsetFlushInterval = 5000; | ||
|
||
private final Path pluginDir; | ||
|
||
private Herder herder; | ||
|
||
private Connect connect; | ||
|
||
public JdbcConnectService(final String bootstrapServers, final Path pluginDir) { | ||
this.bootstrapServers = bootstrapServers; | ||
this.pluginDir = pluginDir; | ||
} | ||
|
||
void start() { | ||
final Map<String, String> workerProps = Map.of( | ||
"bootstrap.servers", bootstrapServers, | ||
"offset.flush.interval.ms", Integer.toString(offsetFlushInterval), | ||
// These don't matter much (each connector sets its own converters), | ||
// but need to be filled with valid classes. | ||
"key.converter", "org.apache.kafka.connect.converters.ByteArrayConverter", | ||
"value.converter", "org.apache.kafka.connect.converters.ByteArrayConverter", | ||
"internal.key.converter", "org.apache.kafka.connect.json.JsonConverter", | ||
"internal.key.converter.schemas.enable", "false", | ||
"internal.value.converter", "org.apache.kafka.connect.json.JsonConverter", | ||
"internal.value.converter.schemas.enable", "false", | ||
// Don't need it since we'll memory MemoryOffsetBackingStore. | ||
"offset.storage.file.filename", "", | ||
"plugin.path", pluginDir.toString()); | ||
|
||
final Time time = Time.SYSTEM; | ||
final String workerId = "test-worker"; | ||
final String kafkaClusterId = "test-cluster"; | ||
|
||
final var plugins = new Plugins(workerProps); | ||
final var config = new StandaloneConfig(workerProps); | ||
|
||
final var worker = new Worker(workerId, time, plugins, config, new MemoryOffsetBackingStore()); | ||
herder = new StandaloneHerder(worker, kafkaClusterId); | ||
connect = new Connect(herder, new RestServer(config)); | ||
|
||
connect.start(); | ||
} | ||
|
||
public void createConnector(final Map<String, String> config) throws ExecutionException, InterruptedException { | ||
assert herder != null; | ||
|
||
final FutureCallback<Herder.Created<ConnectorInfo>> cb = | ||
new FutureCallback<>((error, info) -> { | ||
if (error != null) { | ||
LOGGER.error("Failed to create job"); | ||
} else { | ||
LOGGER.info("Created connector {}", info.result().name()); | ||
} | ||
}); | ||
herder.putConnectorConfig( | ||
config.get(ConnectorConfig.NAME_CONFIG), | ||
config, false, cb | ||
); | ||
|
||
final Herder.Created<ConnectorInfo> connectorInfoCreated = cb.get(); | ||
assert connectorInfoCreated.created(); | ||
} | ||
|
||
void stop() { | ||
herder.stop(); | ||
connect.stop(); | ||
} | ||
|
||
} |
Oops, something went wrong.