Class: Testcontainers::MysqlContainer

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

Overview

MysqlContainer class is used to manage containers that runs a MySQL database

Constant Summary collapse

MYSQL_DEFAULT_PORT =

Default port used by the container

3306
MYSQL_DEFAULT_IMAGE =

Default image used by the container

"mysql:latest"
MYSQL_DEFAULT_USERNAME =
"test"
MYSQL_DEFAULT_PASSWORD =
"test"
MYSQL_DEFAULT_ROOT_USERNAME =
"root"
MYSQL_DEFAULT_DATABASE =
"test"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image = MYSQL_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) ⇒ MysqlContainer

Initializes a new instance of MysqlContainer

Parameters:

  • image (String) (defaults to: MYSQL_DEFAULT_IMAGE)

    the image to use

  • username (String) (defaults to: nil)

    the username to use

  • password (String) (defaults to: nil)

    the password to use

  • database (String) (defaults to: nil)

    the database to use

  • port (String) (defaults to: nil)

    the port to use

  • kwargs (Hash)

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



34
35
36
37
38
39
40
41
# File 'lib/testcontainers/mysql.rb', line 34

def initialize(image = MYSQL_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs)
  super(image, **kwargs)
  @username = username || ENV.fetch("MYSQL_USER", MYSQL_DEFAULT_USERNAME)
  @password = password || ENV.fetch("MYSQL_PASSWORD", MYSQL_DEFAULT_PASSWORD)
  @database = database || ENV.fetch("MYSQL_DATABASE", MYSQL_DEFAULT_DATABASE)
  @healthcheck ||= add_healthcheck(_default_healthcheck_options)
  @wait_for ||= add_wait_for(:healthcheck)
end

Instance Attribute Details

#databaseString (readonly)

used by the container

Returns:

  • (String)

    the current value of database



11
12
13
# File 'lib/testcontainers/mysql.rb', line 11

def database
  @database
end

#passwordString (readonly)

used by the container

Returns:

  • (String)

    the current value of password



11
12
13
# File 'lib/testcontainers/mysql.rb', line 11

def password
  @password
end

#usernameString (readonly)

used by the container

Returns:

  • (String)

    the current value of username



11
12
13
# File 'lib/testcontainers/mysql.rb', line 11

def username
  @username
end

Instance Method Details

#database_url(protocol: "mysql", username: nil, password: nil, database: nil, options: {}) ⇒ String

Returns the database url (e.g. mysql://user:password@host:port/database)

Parameters:

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

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

  • database (String) (defaults to: nil)

    the database to use in the string (default: @database)

  • options (Hash) (defaults to: {})

    the options to use in the query string (default: {})

Returns:

  • (String)

    the database url

Raises:

  • (ConnectionError)

    If the connection to the Docker daemon fails.

  • (ContainerNotStartedError)

    If the container has not been started.



77
78
79
80
81
82
83
84
# File 'lib/testcontainers/mysql.rb', line 77

def database_url(protocol: "mysql", username: nil, password: nil, database: nil, options: {})
  database ||= @database
  username ||= @username
  password ||= @password
  query_string = options.empty? ? "" : "?#{URI.encode_www_form(options)}"

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

#hostString

Returns the host used to connect to the container If the host is “localhost”, it is replaced by “127.0.0.1” since MySQL fallbacks to a socket connection with “localhost”

Returns:

  • (String)

    the host used to connect to the container



57
58
59
60
# File 'lib/testcontainers/mysql.rb', line 57

def host
  host = super
  (host == "localhost") ? "127.0.0.1" : host
end

#portInteger

Returns the port used by the container

Returns:

  • (Integer)

    the port used by the container



65
66
67
# File 'lib/testcontainers/mysql.rb', line 65

def port
  MYSQL_DEFAULT_PORT
end

#startMysqlContainer

Starts the container

Returns:



46
47
48
49
50
# File 'lib/testcontainers/mysql.rb', line 46

def start
  with_exposed_ports(port)
  _configure
  super
end

#with_database(database) ⇒ MysqlContainer

Sets the database to use

Parameters:

  • database (String)

    the database to use

Returns:



90
91
92
93
# File 'lib/testcontainers/mysql.rb', line 90

def with_database(database)
  @database = database
  self
end

#with_password(password) ⇒ MysqlContainer

Sets the password to use

Parameters:

  • password (String)

    the password to use

Returns:



108
109
110
111
# File 'lib/testcontainers/mysql.rb', line 108

def with_password(password)
  @password = password
  self
end

#with_username(username) ⇒ MysqlContainer

Sets the username to use

Parameters:

  • username (String)

    the username to use

Returns:



99
100
101
102
# File 'lib/testcontainers/mysql.rb', line 99

def with_username(username)
  @username = username
  self
end