Class: Testcontainers::RedisContainer

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

Overview

RedisContainer class is used to manage containers that run a Redis database

Constant Summary collapse

REDIS_DEFAULT_PORT =

Default port used by the container

6379
REDIS_DEFAULT_IMAGE =

Default image used by the container

"redis:latest"

Instance Method Summary collapse

Constructor Details

#initialize(image = REDIS_DEFAULT_IMAGE, **kwargs) ⇒ RedisContainer

Initializes a new instance of RedisContainer

Parameters:

  • image (String) (defaults to: REDIS_DEFAULT_IMAGE)

    the image to use

  • port (String)

    the port to use

  • kwargs (Hash)

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



19
20
21
22
# File 'lib/testcontainers/redis.rb', line 19

def initialize(image = REDIS_DEFAULT_IMAGE, **kwargs)
  super(image, **kwargs)
  @wait_for ||= add_wait_for(:logs, /Ready to accept connections/)
end

Instance Method Details

#portInteger

Returns the port used by the container

Returns:

  • (Integer)

    the port used by the container



35
36
37
# File 'lib/testcontainers/redis.rb', line 35

def port
  REDIS_DEFAULT_PORT
end

#redis_url(protocol: "redis", db: 0) ⇒ String Also known as: database_url

Returns the Redis connection url (e.g. redis://:password@localhost:6379/0)

Parameters:

  • protocol (String) (defaults to: "redis")

    the protocol to use in the string (default: “redis”)

Returns:

  • (String)

    the Redis connection url

Raises:

  • (ConnectionError)

    If the connection to the Docker daemon fails.

  • (ContainerNotStartedError)

    If the container has not been started.



45
46
47
48
49
50
51
# File 'lib/testcontainers/redis.rb', line 45

def redis_url(protocol: "redis", db: 0)
  if @password.nil? || @password.empty?
    "#{protocol}://#{host}:#{mapped_port(port)}/#{db}"
  else
    "#{protocol}://:#{password}@#{host}:#{mapped_port(port)}/#{db}"
  end
end

#startRedisContainer

Starts the container

Returns:



27
28
29
30
# File 'lib/testcontainers/redis.rb', line 27

def start
  with_exposed_ports(port)
  super
end