Skip to content

Commit

Permalink
add doc and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 14, 2023
1 parent 828cada commit 69a2c14
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
5 changes: 3 additions & 2 deletions lib/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ defmodule Testcontainers.Container do
Sets a file or the directory on the _host machine_ to be mounted into a _container_.
"""
def with_bind_mount(%__MODULE__{} = config, host_src, container_dest, options \\ "ro")
when is_binary(host_src) and is_binary(container_dest) do
when is_binary(host_src) and is_binary(container_dest) and is_binary(options) do
new_bind_mount = %{host_src: host_src, container_dest: container_dest, options: options}
%__MODULE__{config | bind_mounts: [new_bind_mount | config.bind_mounts]}
end

@doc """
Sets a volume to be mounted into a container on target path
"""
def with_bind_volume(%__MODULE__{} = config, volume, container_dest, read_only \\ false) do
def with_bind_volume(%__MODULE__{} = config, volume, container_dest, read_only \\ false)
when is_binary(volume) and is_binary(container_dest) and is_boolean(read_only) do
new_bind_volume = %{
volume: volume,
container_dest: container_dest,
Expand Down
37 changes: 20 additions & 17 deletions lib/container/mysql_container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,33 +194,36 @@ defmodule Testcontainers.MySqlContainer do
"Image #{config.image} is not compatible with #{MySqlContainer.default_image()}"
end

port_fn =
case config.port do
{exposed_port, host_port} ->
fn container -> with_fixed_port(container, exposed_port, host_port) end

port ->
fn container -> with_exposed_port(container, port) end
end

maybe_persisntent_volume_fn =
case config.persistent_volume do
nil -> fn container -> container end
volume -> fn container -> container |> with_bind_volume(volume, "/var/lib/mysql") end
end

new(config.image)
|> Kernel.then(port_fn)
|> then(MySqlContainer.container_port_fun(config.port))
|> with_environment(:MYSQL_USER, config.user)
|> with_environment(:MYSQL_PASSWORD, config.password)
|> with_environment(:MYSQL_DATABASE, config.database)
|> Kernel.then(maybe_persisntent_volume_fn)
|> then(MySqlContainer.container_volume_fun(config.persistent_volume))
|> with_environment(:MYSQL_RANDOM_ROOT_PASSWORD, "yes")
|> with_waiting_strategy(
LogWaitStrategy.new(~r/.*port: 3306 MySQL Community Server.*/, config.wait_timeout)
)
end
end

@doc false
def container_port_fun(nil), do: &Function.identity/1

def container_port_fun({exposed_port, host_port}) do
fn container -> Container.with_fixed_port(container, exposed_port, host_port) end
end

def container_port_fun(port) do
fn container -> Container.with_exposed_port(container, port) end
end

@doc false
def container_volume_fun(nil), do: &Function.identity/1

def container_volume_fun(volume) when is_binary(volume) do
fn container -> Container.with_bind_volume(container, volume, "/var/lib/mysql") end
end
end

defmodule Testcontainers.Container.MySqlContainer do
Expand Down
40 changes: 20 additions & 20 deletions lib/container/postgres_container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,12 @@ defmodule Testcontainers.PostgresContainer do
"Image #{config.image} is not compatible with #{PostgresContainer.default_image()}"
end

port_fn =
case config.port do
{exposed_port, host_port} ->
fn container -> with_fixed_port(container, exposed_port, host_port) end

port ->
fn container -> with_exposed_port(container, port) end
end

maybe_persisntent_volume_fn =
case config.persistent_volume do
nil ->
fn container -> container end

volume ->
fn container -> container |> with_bind_volume(volume, "/var/lib/postgresql/data") end
end

new(config.image)
|> Kernel.then(port_fn)
|> then(PostgresContainer.container_port_fun(config.port))
|> with_environment(:POSTGRES_USER, config.user)
|> with_environment(:POSTGRES_PASSWORD, config.password)
|> with_environment(:POSTGRES_DB, config.database)
|> Kernel.then(maybe_persisntent_volume_fn)
|> then(PostgresContainer.container_volume_fun(config.persistent_volume))
|> with_waiting_strategy(
CommandWaitStrategy.new(
[
Expand All @@ -230,6 +212,24 @@ defmodule Testcontainers.PostgresContainer do
)
end
end

@doc false
def container_port_fun(nil), do: &Function.identity/1

def container_port_fun({exposed_port, host_port}) do
fn container -> Container.with_fixed_port(container, exposed_port, host_port) end
end

def container_port_fun(port) do
fn container -> Container.with_exposed_port(container, port) end
end

@doc false
def container_volume_fun(nil), do: &Function.identity/1

def container_volume_fun(volume) when is_binary(volume) do
fn container -> Container.with_bind_volume(container, volume, "/var/lib/postgresql/data") end
end
end

defmodule Testcontainers.Container.PostgresContainer do
Expand Down
3 changes: 2 additions & 1 deletion lib/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule Testcontainers.Ecto do
- `:password` (optional) - Determines the password for the Postgres user (defaults to "postgres").
- `:database` (optional) - Specifies the name of the database to be created within the Postgres instance. If not provided, the default behavior is to create a database with the name derived from the application's atom, appended with "_test".
- `:migrations_path` (optional) - Indicates the path to the migrations folder (defaults to "priv/repo/migrations").
- `:persistent_volume_name` (optional, EXPERIMENTAL) - Sets a named volume for the data in the database. This is an experimental option, and changes in database container image or other things that could invalidate the data, would make the container not start properly.
## Database Lifecycle in Testing
Expand Down Expand Up @@ -117,7 +118,7 @@ defmodule Testcontainers.Ecto do
- `:password` (optional) - Determines the password for the Mysql user (defaults to "test").
- `:database` (optional) - Specifies the name of the database to be created within the Mysql instance. If not provided, the default behavior is to create a database with the name derived from the application's atom, appended with "_test".
- `:migrations_path` (optional) - Indicates the path to the migrations folder (defaults to "priv/repo/migrations").
- `:persistent_volume_name` (optional, EXPERIMENTAL) - Sets a named volume for the data in the database. This is an experimental option, and changes in database container image or other things that could invalidate the data, would make the container not start properly.
## Database Lifecycle in Testing
It's important to note that the Mysql database initiated by this function will remain operational for the duration of the test process and is not explicitly shut down by the function. The database and its corresponding data are ephemeral, lasting only for the scope of the test session.
Expand Down

0 comments on commit 69a2c14

Please sign in to comment.