Class: Testcontainers::RedpandaContainer

Inherits:
DockerContainer
  • Object
show all
Defined in:
lib/testcontainers/redpanda.rb

Overview

RedpandaContainer class is used to manage containers that run a Redpanda

Constant Summary collapse

REDPANDA_DEFAULT_PORT =

Default port used by the container

9092
REDPANDA_DEFAULT_SCHEMA_REGISTRY_PORT =

Default port used for schema registry

8081
REDPANDA_DEFAULT_IMAGE =

Default image used by the container

"redpandadata/redpanda:latest"
STARTUP_SCRIPT_PATH =

Default path to the startup script

"/testcontainers.sh"

Instance Method Summary collapse

Constructor Details

#initialize(image = REDPANDA_DEFAULT_IMAGE, **kwargs) ⇒ RedpandaContainer

Initializes a new instance of RedpandaContainer

Parameters:

  • image (String) (defaults to: REDPANDA_DEFAULT_IMAGE)

    the image to use

  • kwargs (Hash)

    the options to pass to the container. See DockerContainer#initialize



24
25
26
# File 'lib/testcontainers/redpanda.rb', line 24

def initialize(image = REDPANDA_DEFAULT_IMAGE, **kwargs)
  super(image, **kwargs)
end

Instance Method Details

#bootstrap_serversObject



64
65
66
# File 'lib/testcontainers/redpanda.rb', line 64

def bootstrap_servers
  "PLAINTEXT://#{host}:#{mapped_port(port)}"
end

#connection_urlString

Returns the Redpanda connection url (e.g. localhost:9092)

Returns:

  • (String)

    the Redpanda connection url

Raises:

  • (ConnectionError)

    If the connection to the Docker daemon fails.

  • (ContainerNotStartedError)

    If the container has not been started.



60
61
62
# File 'lib/testcontainers/redpanda.rb', line 60

def connection_url
  "#{host}:#{mapped_port(port)}"
end

#portObject



51
52
53
# File 'lib/testcontainers/redpanda.rb', line 51

def port
  REDPANDA_DEFAULT_PORT
end

#schema_registry_addressObject



68
69
70
# File 'lib/testcontainers/redpanda.rb', line 68

def schema_registry_address
  "http://#{host}:#{mapped_port(REDPANDA_DEFAULT_SCHEMA_REGISTRY_PORT)}"
end

#startRedpandaContainer

Starts the container

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/testcontainers/redpanda.rb', line 31

def start
  with_exposed_ports(REDPANDA_DEFAULT_PORT, REDPANDA_DEFAULT_SCHEMA_REGISTRY_PORT)
  with_entrypoint(%w[/bin/sh])
  with_command(["-c", "while [ ! -f #{STARTUP_SCRIPT_PATH} ]; do sleep 0.1; done; #{STARTUP_SCRIPT_PATH}"])
  super

  # Copy the startup script to the container
  copy_file_to_container("/tmp" + STARTUP_SCRIPT_PATH, _startup_script)

  # File is copied with root owner and permissions, so we need to change them
  exec_as_root(%w[chmod 777] + ["/tmp" + STARTUP_SCRIPT_PATH])
  exec_as_root(%w[chown redpanda:redpanda] + ["/tmp" + STARTUP_SCRIPT_PATH])

  # Copy the startup script to expected location
  exec_as_root(%w[cp] + ["/tmp" + STARTUP_SCRIPT_PATH] + [STARTUP_SCRIPT_PATH])

  wait_for_logs(/Successfully started Redpanda!/)
  self
end