testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

Support for Kafka Schema registry

Open gaetancollaud opened this issue 4 years ago • 5 comments

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);
  }
}

gaetancollaud avatar Feb 15 '21 14:02 gaetancollaud

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.

cgraving avatar May 12 '21 14:05 cgraving

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?

gaetancollaud avatar May 12 '21 15:05 gaetancollaud

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.

krickert avatar Nov 04 '22 09:11 krickert

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

PPrydorozhnyi avatar Jan 28 '24 16:01 PPrydorozhnyi

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.

eddumelendez avatar Jan 29 '24 14:01 eddumelendez