FullKafkaContainer

class safir.testing.containers.FullKafkaContainer(image='confluentinc/cp-kafka:7.6.0', port=9093, ssl_port=29093, sasl_plaintext_port=29094, sasl_ssl_port=29095, *, constant_host_ports=False, starting_host_port=50000, **kwargs)

Bases: DockerContainer

Kafka container.

Examples

from testcontainers.kafka import KafkaContainer

with KafkaContainer() as kafka:
    connection = kafka.get_bootstrap_server()
Parameters:
  • image (str, default: 'confluentinc/cp-kafka:7.6.0')

  • port (int, default: 9093)

  • ssl_port (int, default: 29093)

  • sasl_plaintext_port (int, default: 29094)

  • sasl_ssl_port (int, default: 29095)

  • constant_host_ports (bool, default: False)

  • starting_host_port (int, default: 50000)

  • kwargs (Any)

Methods Summary

create_file(content, path)

Create a file inside the container.

get_bootstrap_server()

Get the bootstrap server for a no TLS, no SASL connection.

get_sasl_password()

Get the configured SASL password for the Kafka listener.

get_sasl_plaintext_bootstrap_server()

Get the bootstrap server for a SASL PLAINTEXT connection.

get_sasl_ssl_bootstrap_server()

Get the bootstrap server for a SASL TLS connection.

get_sasl_username()

Get the configured SASL username for the Kafka listener.

get_secret_file_contents(name)

Get the contents of a file from /etc/kafka/secrets.

get_ssl_bootstrap_server()

Get the bootstrap server for a TLS connection.

reset()

Reset all Kafka topics.

start([timeout])

Start the container.

start_container_again()

Start this container again.

stop_container()

Stop the container, but don't remove it.

wait_for_ready([timeout])

Wait until Kafka is ready to serve requests.

wait_for_ready_again([timeout])

Wait until Kafka is ready to serve requests.

Methods Documentation

create_file(content, path)

Create a file inside the container.

Parameters:
  • content (bytes) – Content of the file.

  • path (str) – Path to the file inside the container.

Return type:

None

get_bootstrap_server()

Get the bootstrap server for a no TLS, no SASL connection.

Return type:

str

get_sasl_password()

Get the configured SASL password for the Kafka listener.

Return type:

str

get_sasl_plaintext_bootstrap_server()

Get the bootstrap server for a SASL PLAINTEXT connection.

Return type:

str

get_sasl_ssl_bootstrap_server()

Get the bootstrap server for a SASL TLS connection.

Return type:

str

get_sasl_username()

Get the configured SASL username for the Kafka listener.

Return type:

str

get_secret_file_contents(name)

Get the contents of a file from /etc/kafka/secrets.

Useful for getting TLS certs and keys for connecting with the SSL security protocol.

We can’t just mount a host volume over /etc/kafka/secrets because the OS user that owns that directory could be different than that appuser user that runs kafka in the container.

Parameters:

name (str) – Name of the secret to retrieve. This should normally be one of ca.crt, client.crt, or client.key.

Raises:

FileNotFoundError – Raised if the requested file could not be found in the /etc/kafka/secrets directory.

Return type:

str

get_ssl_bootstrap_server()

Get the bootstrap server for a TLS connection.

Return type:

str

reset()

Reset all Kafka topics.

Return type:

None

start(timeout=30)

Start the container.

Parameters:

timeout (int, default: 30) – How long to wait for the container to start before raising an error if Kafka still isn’t running.

Return type:

Self

start_container_again()

Start this container again.

Can only be called after the container is stopped with stop_container.

Return type:

None

stop_container()

Stop the container, but don’t remove it.

This is useful in tests for stopping the container and starting it up again with the same ports mapped. The default stop method removes the container too, so if you call start again, it hass different ports mapped.

Return type:

None

wait_for_ready(timeout=30)

Wait until Kafka is ready to serve requests.

Parameters:

timeout (int, default: 30)

Return type:

None

wait_for_ready_again(timeout=30)

Wait until Kafka is ready to serve requests.

Parameters:

timeout (int, default: 30)

Return type:

None