Skip to content

Redpanda

Install

1
npm install @testcontainers/redpanda --save-dev

Examples

These examples use the following libraries:

Choose an image from the container registry and substitute IMAGE.

Produce/consume a message

1
2
3
await using container = await new RedpandaContainer(IMAGE).start();

await assertMessageProducedAndConsumed(container);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
export async function assertMessageProducedAndConsumed(container: StartedRedpandaContainer) {
  const kafka = new Kafka({ logLevel: logLevel.NOTHING, brokers: [container.getBootstrapServers()] });

  const producer = kafka.producer();
  await producer.connect();
  const consumer = kafka.consumer({ groupId: "test-group" });
  await consumer.connect();

  await producer.send({ topic: "test-topic", messages: [{ value: "test message" }] });
  await consumer.subscribe({ topic: "test-topic", fromBeginning: true });

  const consumedMessage = await new Promise((resolve) =>
    consumer.run({
      eachMessage: async ({ message }) => resolve(message.value?.toString()),
    })
  );
  expect(consumedMessage).toBe("test message");

  await consumer.disconnect();
  await producer.disconnect();
}

Connect to schema registry

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
await using container = await new RedpandaContainer(IMAGE).start();
const schemaRegistryUrl = container.getSchemaRegistryAddress();

const response = await fetch(`${schemaRegistryUrl}/subjects`, {
  method: "GET",
  headers: {
    "Content-Type": "application/vnd.schemaregistry.v1+json",
  },
});

expect(response.status).toBe(200);

Connect to admin

1
2
3
4
5
6
await using container = await new RedpandaContainer(IMAGE).start();
const adminUrl = `${container.getAdminAddress()}/v1`;

const response = await fetch(adminUrl);

expect(response.status).toBe(200);

Connect to REST proxy

1
2
3
4
5
6
await using container = await new RedpandaContainer(IMAGE).start();
const restProxyUrl = `${container.getRestProxyAddress()}/topics`;

const response = await fetch(restProxyUrl);

expect(response.status).toBe(200);