Skip to content

Commit

Permalink
refactor name and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 15, 2023
1 parent 03091f0 commit 595ed97
Show file tree
Hide file tree
Showing 24 changed files with 106 additions and 72 deletions.
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@

> Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Current in planning phase. Feedback is appreciated.
## Usage

The library will use
For automatic cleanup of docker containers created in tests, for example if testcontainers fails to stop and remove the container, its suggested to register a reaper genserver in test_helper.exs like this:

https://github.com/jarlah/docker-engine-api-elixir
test/test_helper.exs
```elixir
{:ok, _} = Testcontainers.Reaper.start_link()
ExUnit.start()
```

to communicate with docker engine api. The latter is an autogenerated elixir client library from openapi spec with some adjustments for proper usage.
test/myswql_container_test.exs
```elixir
defmodule MySqlContainerTest do
use ExUnit.Case, async: true

import Testcontainers.ExUnit

alias Testcontainers.Container.MySqlContainer

describe "with default configuration" do
container(:mysql, MySqlContainer.new())

test "provides a ready-to-use mysql container", %{mysql: mysql} do
assert true

# if you want to test like below, add
# {:myxql, "~> 0.6.0", only: [:dev, :test]},
# to mix.exs and run mix deps.get

#{:ok, pid} = MyXQL.start_link(MySqlContainer.connection_parameters(mysql))
#assert %{num_rows: 1} = MyXQL.query!(pid, "SELECT 1", [])
end
end
```

## Contribution

Do you want to contribute? Find spots to improve on, fire up an issue and get the discussion going.
2 changes: 1 addition & 1 deletion lib/container.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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 do
defmodule Testcontainers.Container do
@enforce_keys [:image]

defstruct [
Expand Down
6 changes: 3 additions & 3 deletions lib/container/ceph_container.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT
defmodule TestcontainersElixir.Container.CephContainer do
alias TestcontainersElixir.WaitStrategy.LogWaitStrategy
alias TestcontainersElixir.Container
defmodule Testcontainers.Container.CephContainer do
alias Testcontainers.WaitStrategy.LogWaitStrategy
alias Testcontainers.Container

def new(options \\ []) do
image = Keyword.get(options, :image, "quay.io/ceph/demo:latest-quincy")
Expand Down
6 changes: 3 additions & 3 deletions lib/container/mysql_container.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 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
defmodule Testcontainers.Container.MySqlContainer do
@moduledoc """
Functions to build and interact with MySql containers.
"""

alias TestcontainersElixir.Container
alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy
alias Testcontainers.Container
alias Testcontainers.WaitStrategy.CommandWaitStrategy

@mysql_port 3306

Expand Down
6 changes: 3 additions & 3 deletions lib/container/postgres_container.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 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
defmodule Testcontainers.Container.PostgresContainer do
@moduledoc """
Functions to build and interact with PostgreSql containers.
"""

alias TestcontainersElixir.Container
alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy
alias Testcontainers.Container
alias Testcontainers.WaitStrategy.CommandWaitStrategy

@postgres_port 5432

Expand Down
6 changes: 3 additions & 3 deletions lib/container/redis_container.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 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
defmodule Testcontainers.Container.RedisContainer do
@moduledoc """
Functions to build and interact with Redis containers.
"""

alias TestcontainersElixir.Container
alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy
alias Testcontainers.Container
alias Testcontainers.WaitStrategy.CommandWaitStrategy

@redis_port 6379
@wait_strategy CommandWaitStrategy.new(["redis-cli", "PING"])
Expand Down
10 changes: 5 additions & 5 deletions lib/docker/api.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-License-Identifier: MIT
defmodule TestcontainersElixir.Docker.Api do
alias TestcontainersElixir.WaitStrategy
defmodule Testcontainers.Docker.Api do
alias Testcontainers.WaitStrategy
alias DockerEngineAPI.Model.ContainerCreateRequest
alias DockerEngineAPI.Api
alias TestcontainersElixir.Container
alias TestcontainersElixir.ReaperWorker
alias TestcontainersElixir.Connection
alias Testcontainers.Container
alias Testcontainers.ReaperWorker
alias Testcontainers.Connection

def run(%Container{} = container_config, options \\ []) do
on_exit = Keyword.get(options, :on_exit, nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/connection.ex → lib/docker/connection.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
defmodule TestcontainersElixir.Connection do
defmodule Testcontainers.Connection do
alias DockerEngineAPI.Connection

@default_host "unix:///var/run/docker.sock"
Expand Down
4 changes: 2 additions & 2 deletions lib/docker/exec.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule TestcontainersElixir.Docker.Exec do
defmodule Testcontainers.Docker.Exec do
@moduledoc false

alias TestcontainersElixir.Connection
alias Testcontainers.Connection

def inspect(exec_id, options \\ []) do
conn = Connection.get_connection(options)
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_unit.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# SPDX-License-Identifier: MIT
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers
# Modified by: Jarl André Hübenthal @ 2023
defmodule TestcontainersElixir.ExUnit do
defmodule Testcontainers.ExUnit do
@moduledoc """
Convenient macros to run containers within ExUnit tests.
"""
import ExUnit.Callbacks

alias TestcontainersElixir.Docker
alias Testcontainers.Docker

@doc """
Sets a container to be created anew for each test in the module.
Expand Down
14 changes: 7 additions & 7 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TestcontainersElixir.Reaper do
defmodule Testcontainers.Reaper do
use Supervisor

def start_link(opts \\ []) do
Expand All @@ -10,20 +10,20 @@ defmodule TestcontainersElixir.Reaper do
Process.flag(:trap_exit, true)

children = [
{TestcontainersElixir.ReaperWorker, []},
{Testcontainers.ReaperWorker, []},
]

Supervisor.init(children, strategy: :one_for_one)
end
end

defmodule TestcontainersElixir.ReaperWorker do
defmodule Testcontainers.ReaperWorker do
use GenServer

require Logger

alias TestcontainersElixir.Docker
alias TestcontainersElixir.Container
alias Testcontainers.Docker
alias Testcontainers.Container

@ryuk_image "testcontainers/ryuk:0.5.1"
@ryuk_port 8080
Expand All @@ -37,10 +37,10 @@ defmodule TestcontainersElixir.ReaperWorker do
GenServer.cast(__MODULE__, {:register, filter})
else
Logger.warning("""
Reaper is not running! Ensure that TestcontainersElixir.Reaper
Reaper is not running! Ensure that Testcontainers.Reaper
is started in your test_helper.exs.
e.g.,
TestcontainersElixir.Reaper.start_link()
Testcontainers.Reaper.start_link()
""")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wait_strategy.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers
# Modified by: Jarl André Hübenthal @ 2023
defprotocol TestcontainersElixir.WaitStrategy do
defprotocol Testcontainers.WaitStrategy do
@moduledoc false

@spec wait_until_container_is_ready(t, String.t()) :: :ok | {:error, atom()}
Expand Down
8 changes: 4 additions & 4 deletions lib/wait_strategy/command_wait_strategy.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers
# Modified by: Jarl André Hübenthal @ 2023
defmodule TestcontainersElixir.WaitStrategy.CommandWaitStrategy do
defmodule Testcontainers.WaitStrategy.CommandWaitStrategy do
@moduledoc """
Considers container as ready as soon as a command runs successfully inside the container.
"""
Expand All @@ -13,9 +13,9 @@ defmodule TestcontainersElixir.WaitStrategy.CommandWaitStrategy do
def new(command), do: %__MODULE__{command: command}
end

defimpl TestcontainersElixir.WaitStrategy,
for: TestcontainersElixir.WaitStrategy.CommandWaitStrategy do
alias TestcontainersElixir.Docker.Exec
defimpl Testcontainers.WaitStrategy,
for: Testcontainers.WaitStrategy.CommandWaitStrategy do
alias Testcontainers.Docker.Exec

def wait_until_container_is_ready(wait_strategy, id_or_name) do
case exec_and_wait(id_or_name, wait_strategy.command) do
Expand Down
8 changes: 4 additions & 4 deletions lib/wait_strategy/http_wait_strategy.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
defmodule TestcontainersElixir.WaitStrategy.HttpWaitStrategy do
defmodule Testcontainers.WaitStrategy.HttpWaitStrategy do
@moduledoc """
Considers container as ready as soon as a command runs successfully inside the container.
"""
Expand All @@ -12,9 +12,9 @@ defmodule TestcontainersElixir.WaitStrategy.HttpWaitStrategy do
do: %__MODULE__{ip: ip, port: port, path: path, timeout: timeout}
end

defimpl TestcontainersElixir.WaitStrategy, for: TestcontainersElixir.WaitStrategy.HttpWaitStrategy do
alias TestcontainersElixir.Container
alias TestcontainersElixir.Docker
defimpl Testcontainers.WaitStrategy, for: Testcontainers.WaitStrategy.HttpWaitStrategy do
alias Testcontainers.Container
alias Testcontainers.Docker

@impl true
def wait_until_container_is_ready(wait_strategy, id_or_name) do
Expand Down
6 changes: 3 additions & 3 deletions lib/wait_strategy/log_wait_strategy.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
defmodule TestcontainersElixir.WaitStrategy.LogWaitStrategy do
defmodule Testcontainers.WaitStrategy.LogWaitStrategy do
@moduledoc """
Considers container as ready as soon as a command runs successfully inside the container.
"""
Expand All @@ -12,8 +12,8 @@ defmodule TestcontainersElixir.WaitStrategy.LogWaitStrategy do
do: %__MODULE__{log_regex: log_regex, timeout: timeout}
end

defimpl TestcontainersElixir.WaitStrategy, for: TestcontainersElixir.WaitStrategy.LogWaitStrategy do
alias TestcontainersElixir.Docker
defimpl Testcontainers.WaitStrategy, for: Testcontainers.WaitStrategy.LogWaitStrategy do
alias Testcontainers.Docker

@impl true
def wait_until_container_is_ready(wait_strategy, id_or_name) do
Expand Down
8 changes: 4 additions & 4 deletions lib/wait_strategy/port_wait_strategy.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
defmodule TestcontainersElixir.WaitStrategy.PortWaitStrategy do
defmodule Testcontainers.WaitStrategy.PortWaitStrategy do
@moduledoc """
Considers container as ready as soon as a command runs successfully inside the container.
"""
Expand All @@ -12,9 +12,9 @@ defmodule TestcontainersElixir.WaitStrategy.PortWaitStrategy do
do: %__MODULE__{ip: ip, port: port, timeout: timeout}
end

defimpl TestcontainersElixir.WaitStrategy, for: TestcontainersElixir.WaitStrategy.PortWaitStrategy do
alias TestcontainersElixir.Docker
alias TestcontainersElixir.Container
defimpl Testcontainers.WaitStrategy, for: Testcontainers.WaitStrategy.PortWaitStrategy do
alias Testcontainers.Docker
alias Testcontainers.Container

@impl true
def wait_until_container_is_ready(wait_strategy, id_or_name) do
Expand Down
10 changes: 7 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
defmodule TestcontainersElixir.MixProject do
use Mix.Project

@app :testcontainers_elixir
@version "0.1.0"
@app :testcontainers
@version "1.0.0"

def project do
[
app: @app,
name: "#{@app}",
version: @version,
description: "Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.",
elixir: "~> 1.15",
name: "#{@app}",
licenses: ["MIT"],
source_url: "https://github.com/jarlah/testcontainers-elixir",
homepage_url: "https://github.com/jarlah/testcontainers-elixir",
aliases: aliases(),
deps: deps()
]
Expand Down
6 changes: 3 additions & 3 deletions test/ceph_container_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule CephContainerTest do
use ExUnit.Case, async: true

import TestcontainersElixir.ExUnit
alias TestcontainersElixir.Container
alias TestcontainersElixir.Container.CephContainer
import Testcontainers.ExUnit
alias Testcontainers.Container
alias Testcontainers.Container.CephContainer

@tag timeout: 300_000

Expand Down
6 changes: 3 additions & 3 deletions test/httpd_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule AnotherTest do
use ExUnit.Case, async: true

import TestcontainersElixir.ExUnit
import Testcontainers.ExUnit

alias TestcontainersElixir.Container
alias TestcontainersElixir.WaitStrategy.HttpWaitStrategy
alias Testcontainers.Container
alias Testcontainers.WaitStrategy.HttpWaitStrategy

test "creates and uses container" do
exposed_port = 80
Expand Down
6 changes: 3 additions & 3 deletions test/mysql_container_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule TestcontainersElixir.MySqlContainerTest do
defmodule Testcontainers.MySqlContainerTest do
use ExUnit.Case, async: true
import TestcontainersElixir.ExUnit

alias TestcontainersElixir.Container.MySqlContainer
import Testcontainers.ExUnit
alias Testcontainers.Container.MySqlContainer

describe "with default configuration" do
container(:mysql, MySqlContainer.new())
Expand Down
6 changes: 3 additions & 3 deletions test/nginx_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule SimpleTest do
use ExUnit.Case, async: true

import TestcontainersElixir.ExUnit
import Testcontainers.ExUnit

alias TestcontainersElixir.WaitStrategy.HttpWaitStrategy
alias TestcontainersElixir.Container
alias Testcontainers.WaitStrategy.HttpWaitStrategy
alias Testcontainers.Container

test "creates and uses container" do
exposed_port = 80
Expand Down
6 changes: 3 additions & 3 deletions test/postgres_container_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule TestcontainersElixir.PostgresContainerTest do
defmodule Testcontainers.PostgresContainerTest do
use ExUnit.Case, async: true
import TestcontainersElixir.ExUnit
import Testcontainers.ExUnit

alias TestcontainersElixir.Container.PostgresContainer
alias Testcontainers.Container.PostgresContainer

describe "with default configuration" do
container(:postgres, PostgresContainer.new())
Expand Down
Loading

0 comments on commit 595ed97

Please sign in to comment.