Class: Testcontainers::ElasticsearchContainer

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

Overview

ElasticsearchContainer class is used to manage Docker containers running Elasticsearch. It extends the generic DockerContainer class and provides Elasticsearch-specific functionality.

Constant Summary collapse

ELASTICSEARCH_DEFAULT_HTTP_PORT =
9200
ELASTICSEARCH_DEFAULT_TCP_PORT =
9300
ELASTICSEARCH_DEFAULT_IMAGE =
"docker.elastic.co/elasticsearch/elasticsearch:8.7.1"
ELASTICSEARCH_DEFAULT_USERNAME =
"elastic"
ELASTICSEARCH_DEFAULT_PASSWORD =
"elastic"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image = ELASTICSEARCH_DEFAULT_IMAGE, http_port: nil, tcp_port: nil, username: nil, password: nil, **kwargs) ⇒ ElasticsearchContainer

Initializes a new ElasticsearchContainer instance.

Parameters:

  • image (String) (defaults to: ELASTICSEARCH_DEFAULT_IMAGE)

    the Elasticsearch Docker image to use

  • http_port (Integer) (defaults to: nil)

    the port to expose for HTTP traffic, defaults to 9200

  • tcp_port (Integer) (defaults to: nil)

    the port to expose for TCP traffic, defaults to 9300

  • username (String) (defaults to: nil)

    the Elasticsearch username, defaults to ‘elastic’

  • password (String) (defaults to: nil)

    the Elasticsearch password, defaults to ‘elastic’

  • kwargs (Hash)

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



28
29
30
31
32
33
34
# File 'lib/testcontainers/elasticsearch.rb', line 28

def initialize(image = ELASTICSEARCH_DEFAULT_IMAGE, http_port: nil, tcp_port: nil, username: nil, password: nil, **kwargs)
  super(image, **kwargs)
  @username = username || ELASTICSEARCH_DEFAULT_USERNAME
  @password = password || ELASTICSEARCH_DEFAULT_PASSWORD
  @healthcheck ||= add_healthcheck(_default_healthcheck_options)
  @wait_for ||= add_wait_for(:healthcheck)
end

Instance Attribute Details

#passwordString (readonly)

the Elasticsearch password

Returns:

  • (String)

    the current value of password



10
11
12
# File 'lib/testcontainers/elasticsearch.rb', line 10

def password
  @password
end

#usernameString (readonly)

the Elasticsearch username

Returns:

  • (String)

    the current value of username



10
11
12
# File 'lib/testcontainers/elasticsearch.rb', line 10

def username
  @username
end

Instance Method Details

#elasticsearch_url(protocol: nil, port: nil, username: nil, password: nil) ⇒ String

Returns the URL to access Elasticsearch

Parameters:

  • protocol (String) (defaults to: nil)

    the protocol (http or https), defaults to ‘http’

  • port (Integer) (defaults to: nil)

    the port to use, defaults to the HTTP port

  • username (String) (defaults to: nil)

    the username to use, defaults to the container username

  • password (String) (defaults to: nil)

    the password to use, defaults to the container password

Returns:

  • (String)

    the URL to access Elasticsearch



66
67
68
69
70
71
72
73
74
75
# File 'lib/testcontainers/elasticsearch.rb', line 66

def elasticsearch_url(protocol: nil, port: nil, username: nil, password: nil)
  if protocol.nil?
    protocol = (get_env("xpack.security.enabled") == "true") ? "https" : "http"
  end
  username ||= @username
  password ||= @password
  port ||= http_port

  "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}"
end

#http_portInteger

Returns the HTTP port used by the container

Returns:

  • (Integer)

    the port used by the container



48
49
50
# File 'lib/testcontainers/elasticsearch.rb', line 48

def http_port
  ELASTICSEARCH_DEFAULT_HTTP_PORT
end

#startElasticsearchContainer

Starts the container

Returns:



39
40
41
42
43
# File 'lib/testcontainers/elasticsearch.rb', line 39

def start
  with_exposed_ports(http_port, tcp_port)
  _configure
  super
end

#tcp_portInteger

Returns the TCP (transport) port used by the container

Returns:

  • (Integer)

    the port used by the container



55
56
57
# File 'lib/testcontainers/elasticsearch.rb', line 55

def tcp_port
  ELASTICSEARCH_DEFAULT_TCP_PORT
end