testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.03k stars 1.65k forks source link

Support for Kafka Schema registry #3806

Open gaetancollaud opened 3 years ago

gaetancollaud commented 3 years ago

I use the Kafka test container which work really well. The only issue that I have is that I use protobuf/avro encoding for my messages. This means I need to run the schema registry container.

Would it be possible to provide the schema registry out of the box and document it in the docs ? I'm willing to create a PR but I prefer to ask first.

See below the definition that I use in my unit test:


public class SchemaRegistryContainer extends GenericContainer<SchemaRegistryContainer> {

  public SchemaRegistryContainer() {
    this("5.5.1");
  }

  public SchemaRegistryContainer(String version) {
    super("confluentinc/cp-schema-registry:" + version);
    withExposedPorts(8081);
  }

  public SchemaRegistryContainer withKafka(KafkaContainer kafka) {
    withEnv("SCHEMA_REGISTRY_HOST_NAME", "schema-registry");
    withEnv("SCHEMA_REGISTRY_LISTENERS", "http://0.0.0.0:8081");
    withEnv(
        "SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS",
        "PLAINTEXT://" + kafka.getNetworkAliases().get(0) + ":9092");
    return self();
  }

  public String getSchemaRegistryUrl() {
    return "http://" + getContainerIpAddress() + ":" + getMappedPort(8081);
  }
}
cgraving commented 3 years ago

If you are using the confluent avro serde, you can set the schema registry url to mock:// to use a built in mock schema registry which just runs in memory and works well if you use auto schema registration.

gaetancollaud commented 3 years ago

I'm aware of this mock feature, but for me, the goal of testcontainer is to run your tests against a real system, not a mock. Why not mocking kafka altogether then?

krickert commented 2 years ago

I'd love to use one for Protobufs - right now I'll need to mock all that code and just assume the serializers work in prod or write my own serialization so the tests are the same in both environments. I'm trying to think of a nicer way - but likely may need to replicate the functionality in the avro stream serde.

PPrydorozhnyi commented 9 months ago

Hi @eddumelendez. Will you accept contribution for this task or you think it’s not needed? Asking because of tag “somedaymaybe”

eddumelendez commented 9 months ago

Hi, for further reference you can check this snippet to configure the confluent stack.

@PPrydorozhnyi, I don't think this is the best place to host the module. I hope to have news regarding to this in the upcoming weeks.