Class: Testcontainers::MysqlContainer
- Inherits:
-
DockerContainer
- Object
- DockerContainer
- Testcontainers::MysqlContainer
- 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
-
#database ⇒ String
readonly
used by the container.
-
#password ⇒ String
readonly
used by the container.
-
#username ⇒ String
readonly
used by the container.
Instance Method Summary collapse
-
#database_url(protocol: "mysql", username: nil, password: nil, database: nil, options: {}) ⇒ String
Returns the database url (e.g. mysql://user:password@host:port/database).
-
#host ⇒ String
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”.
-
#initialize(image = MYSQL_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) ⇒ MysqlContainer
constructor
Initializes a new instance of MysqlContainer.
-
#port ⇒ Integer
Returns the port used by the container.
-
#start ⇒ MysqlContainer
Starts the container.
-
#with_database(database) ⇒ MysqlContainer
Sets the database to use.
-
#with_password(password) ⇒ MysqlContainer
Sets the password to use.
-
#with_username(username) ⇒ MysqlContainer
Sets the username to use.
Constructor Details
#initialize(image = MYSQL_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) ⇒ MysqlContainer
Initializes a new instance of MysqlContainer
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() @wait_for ||= add_wait_for(:healthcheck) end |
Instance Attribute Details
#database ⇒ String (readonly)
used by the container
11 12 13 |
# File 'lib/testcontainers/mysql.rb', line 11 def database @database end |
#password ⇒ String (readonly)
used by the container
11 12 13 |
# File 'lib/testcontainers/mysql.rb', line 11 def password @password end |
#username ⇒ String (readonly)
used by the container
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)
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 = .empty? ? "" : "?#{URI.encode_www_form()}" "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}/#{database}#{query_string}" end |
#host ⇒ String
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”
57 58 59 60 |
# File 'lib/testcontainers/mysql.rb', line 57 def host host = super (host == "localhost") ? "127.0.0.1" : host end |
#port ⇒ Integer
Returns the port used by the container
65 66 67 |
# File 'lib/testcontainers/mysql.rb', line 65 def port MYSQL_DEFAULT_PORT end |
#start ⇒ MysqlContainer
Starts the container
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
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
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
99 100 101 102 |
# File 'lib/testcontainers/mysql.rb', line 99 def with_username(username) @username = username self end |