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, **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)

  • 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.

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