-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
importing containers from excontainers
- Loading branch information
Showing
15 changed files
with
392 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# SPDX-License-Identifier: MIT | ||
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers | ||
# Modified by: Jarl André Hübenthal @ 2023 | ||
defmodule TestcontainersElixir.Container.MySqlContainer do | ||
@moduledoc """ | ||
Functions to build and interact with MySql containers. | ||
""" | ||
|
||
alias TestcontainersElixir.Container | ||
alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy | ||
|
||
@mysql_port 3306 | ||
|
||
@doc """ | ||
Builds a MySql container. | ||
Uses MySql 8.0 by default, but a custom image can also be set. | ||
## Options | ||
- `username` sets the username for the user | ||
- `password` sets the password for the user | ||
- `database` sets the name of the database | ||
""" | ||
def new(image \\ "mysql:8.0", opts \\ []) do | ||
username = Keyword.get(opts, :username, "test") | ||
password = Keyword.get(opts, :password, "test") | ||
|
||
Container.new( | ||
image, | ||
exposed_ports: [@mysql_port], | ||
environment: %{ | ||
MYSQL_USER: username, | ||
MYSQL_PASSWORD: password, | ||
MYSQL_DATABASE: Keyword.get(opts, :database, "test"), | ||
MYSQL_RANDOM_ROOT_PASSWORD: "yes" | ||
}, | ||
wait_strategy: wait_strategy(username, password) | ||
) | ||
end | ||
|
||
@doc """ | ||
Returns the port on the _host machine_ where the MySql container is listening. | ||
""" | ||
def port(container), do: Container.mapped_port(container, @mysql_port) | ||
|
||
@doc """ | ||
Returns the connection parameters to connect to the database from the _host machine_. | ||
""" | ||
def connection_parameters(%Container{} = container) do | ||
[ | ||
hostname: "localhost", | ||
port: port(container), | ||
username: container.environment[:MYSQL_USER], | ||
password: container.environment[:MYSQL_PASSWORD], | ||
database: container.environment[:MYSQL_DATABASE] | ||
] | ||
end | ||
|
||
defp wait_strategy(username, password) do | ||
CommandWaitStrategy.new([ | ||
"sh", | ||
"-c", | ||
"mysqladmin ping --user='#{username}' --password='#{password}' -h localhost | grep 'mysqld is alive'" | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# SPDX-License-Identifier: MIT | ||
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers | ||
# Modified by: Jarl André Hübenthal @ 2023 | ||
defmodule TestcontainersElixir.Container.PostgresContainer do | ||
@moduledoc """ | ||
Functions to build and interact with PostgreSql containers. | ||
""" | ||
|
||
alias TestcontainersElixir.Container | ||
alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy | ||
|
||
@postgres_port 5432 | ||
@wait_strategy CommandWaitStrategy.new([ | ||
"pg_isready", | ||
"-U", | ||
"test", | ||
"-d", | ||
"test", | ||
"-h", | ||
"localhost" | ||
]) | ||
|
||
@doc """ | ||
Builds a PostgreSql container. | ||
Uses PostgreSql 13.1 by default, but a custom image can also be set. | ||
## Options | ||
- `username` sets the username for the user | ||
- `password` sets the password for the user | ||
- `database` sets the name of the database | ||
""" | ||
def new(image \\ "postgres:13.1", opts \\ []) do | ||
Container.new( | ||
image, | ||
exposed_ports: [@postgres_port], | ||
environment: %{ | ||
POSTGRES_USER: Keyword.get(opts, :username, "test"), | ||
POSTGRES_PASSWORD: Keyword.get(opts, :password, "test"), | ||
POSTGRES_DB: Keyword.get(opts, :database, "test") | ||
}, | ||
wait_strategy: @wait_strategy | ||
) | ||
end | ||
|
||
@doc """ | ||
Returns the port on the _host machine_ where the MySql container is listening. | ||
""" | ||
def port(%Container{} = container), | ||
do: with({:ok, port} <- Container.mapped_port(container, @postgres_port), do: port) | ||
|
||
@doc """ | ||
Returns the connection parameters to connect to the database from the _host machine_. | ||
""" | ||
def connection_parameters(%Container{} = container) do | ||
[ | ||
hostname: "localhost", | ||
port: port(container), | ||
username: container.environment[:POSTGRES_USER], | ||
password: container.environment[:POSTGRES_PASSWORD], | ||
database: container.environment[:POSTGRES_DB] | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# SPDX-License-Identifier: MIT | ||
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers | ||
# Modified by: Jarl André Hübenthal @ 2023 | ||
defmodule TestcontainersElixir.Container.RedisContainer do | ||
@moduledoc """ | ||
Functions to build and interact with Redis containers. | ||
""" | ||
|
||
alias TestcontainersElixir.Container | ||
alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy | ||
|
||
@redis_port 6379 | ||
@wait_strategy CommandWaitStrategy.new(["redis-cli", "PING"]) | ||
|
||
@doc """ | ||
Creates a Redis container. | ||
Runs Redis 6.0 by default, but a custom image can also be set. | ||
""" | ||
def new(image \\ "redis:6.0-alpine", _opts \\ []) do | ||
Container.new( | ||
image, | ||
exposed_ports: [@redis_port], | ||
environment: %{}, | ||
wait_strategy: @wait_strategy | ||
) | ||
end | ||
|
||
@doc """ | ||
Returns the port on the _host machine_ where the Redis container is listening. | ||
""" | ||
def port(%Container{} = container), do: Container.mapped_port(container, @redis_port) | ||
|
||
@doc """ | ||
Returns the connection url to connect to Redis from the _host machine_. | ||
""" | ||
def connection_url(%Container{} = container), do: "redis://localhost:#{port(container)}/" | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.