-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remote store tests for kafka ingestion
Signed-off-by: Varun Bharadwaj <[email protected]>
- Loading branch information
1 parent
fdb1316
commit b758603
Showing
3 changed files
with
230 additions
and
160 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
104 changes: 104 additions & 0 deletions
104
...-kafka/src/internalClusterTest/java/org/opensearch/plugin/kafka/KafkaIngestionBaseIT.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,104 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.kafka; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; | ||
|
||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.test.OpenSearchIntegTestCase; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.testcontainers.containers.KafkaContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
/** | ||
* Base test class for Kafka ingestion tests | ||
*/ | ||
@ThreadLeakFilters(filters = TestContainerThreadLeakFilter.class) | ||
public class KafkaIngestionBaseIT extends OpenSearchIntegTestCase { | ||
static final String topicName = "test"; | ||
static final String indexName = "testindex"; | ||
static final String mapping = "{\"properties\":{\"name\":{\"type\": \"text\"},\"age\":{\"type\": \"integer\"}}}}"; | ||
|
||
protected KafkaContainer kafka; | ||
protected Producer<String, String> producer; | ||
|
||
@Override | ||
protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
return Arrays.asList(KafkaPlugin.class); | ||
} | ||
|
||
@Before | ||
private void setup() { | ||
setupKafka(); | ||
} | ||
|
||
@After | ||
private void cleanup() { | ||
stopKafka(); | ||
} | ||
|
||
private void setupKafka() { | ||
kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1")) | ||
// disable topic auto creation | ||
.withEnv("KAFKA_AUTO_CREATE_TOPICS_ENABLE", "false"); | ||
kafka.start(); | ||
|
||
// setup producer | ||
String boostrapServers = kafka.getBootstrapServers(); | ||
KafkaUtils.createTopic(topicName, 1, boostrapServers); | ||
Properties props = new Properties(); | ||
props.put("bootstrap.servers", kafka.getBootstrapServers()); | ||
producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer()); | ||
} | ||
|
||
private void stopKafka() { | ||
if (producer != null) { | ||
producer.close(); | ||
} | ||
|
||
if (kafka != null) { | ||
kafka.stop(); | ||
} | ||
} | ||
|
||
protected void produceData(String id, String name, String age) { | ||
String payload = String.format( | ||
"{\"_id\":\"%s\", \"_op_type:\":\"index\",\"_source\":{\"name\":\"%s\", \"age\": %s}}", | ||
id, | ||
name, | ||
age | ||
); | ||
producer.send(new ProducerRecord<>(topicName, "null", payload)); | ||
} | ||
|
||
protected void waitForSearchableDocs(long docCount, List<String> nodes) throws Exception { | ||
assertBusy(() -> { | ||
for (String node : nodes) { | ||
final SearchResponse response = client(node).prepareSearch(indexName).setSize(0).setPreference("_only_local").get(); | ||
final long hits = response.getHits().getTotalHits().value(); | ||
if (hits < docCount) { | ||
fail("Expected search hits on node: " + node + " to be at least " + docCount + " but was: " + hits); | ||
} | ||
} | ||
}, 1, TimeUnit.MINUTES); | ||
} | ||
} |
Oops, something went wrong.