Skip to content

Commit

Permalink
added container tests + fixed bug in with_fixed_port
Browse files Browse the repository at this point in the history
  • Loading branch information
tw00 committed May 11, 2024
1 parent 659ef70 commit e68bd40
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,28 @@ defmodule Testcontainers.Container do
%__MODULE__{config | exposed_ports: [port | filtered_ports]}
end

@doc """
Adds multiple _ports_ to be exposed on the _container_.
"""
def with_exposed_ports(%__MODULE__{} = config, ports) when is_list(ports) do
filtered_ports = config.exposed_ports |> Enum.reject(fn port -> port in ports end)

%__MODULE__{config | exposed_ports: ports ++ filtered_ports}
end

@doc """
Adds a fixed _port_ to be exposed on the _container_.
This approach to managing ports is not recommended by Testcontainers.
Use at your own risk.
"""
def with_fixed_port(%__MODULE__{} = config, port, host_port \\ nil)
when is_integer(port) and (is_nil(host_port) or is_integer(host_port)) do
filtered_ports = config.exposed_ports |> Enum.reject(fn p -> p == port end)
filtered_ports =
config.exposed_ports
|> Enum.reject(fn
{p, _} -> p == port
p -> p == port
end)

%__MODULE__{
config
Expand All @@ -78,15 +92,6 @@ defmodule Testcontainers.Container do
}
end

@doc """
Adds multiple _ports_ to be exposed on the _container_.
"""
def with_exposed_ports(%__MODULE__{} = config, ports) when is_list(ports) do
filtered_ports = config.exposed_ports |> Enum.reject(fn port -> port in ports end)

%__MODULE__{config | exposed_ports: ports ++ filtered_ports}
end

@doc """
Sets a file or the directory on the _host machine_ to be mounted into a _container_.
"""
Expand Down
70 changes: 70 additions & 0 deletions test/container_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,76 @@ defmodule Testcontainers.ContainerTest do

alias Testcontainers.Container

describe "with_exposed_port/2" do
test "adds an exposed port to the container" do
container =
Container.new("my-image")
|> Container.with_exposed_port(80)

assert container.exposed_ports == [80]
end

test "removes duplicate exposed ports" do
container =
Container.new("my-image")
|> Container.with_exposed_port(80)
|> Container.with_exposed_port(80)

assert container.exposed_ports == [80]
end
end

describe "with_exposed_ports/2" do
test "adds multiple exposed ports to the container" do
container =
Container.new("my-image")
|> Container.with_exposed_ports([80, 443])

assert container.exposed_ports == [80, 443]
end

test "removes duplicate exposed ports" do
container =
Container.new("my-image")
|> Container.with_exposed_ports([80, 443])
|> Container.with_exposed_ports([80])

assert container.exposed_ports == [80, 443]
end
end

describe "with_fixed_port/3" do
test "adds a fixed exposed port to the container" do
container =
Container.new("my-image")
|> Container.with_fixed_port(80, 8080)

assert container.exposed_ports == [{80, 8080}]
end

test "removes and overwrites duplicate fixed ports" do
container =
Container.new("my-image")
|> Container.with_fixed_port(80)
|> Container.with_fixed_port(80, 8080)
|> Container.with_fixed_port(80, 8081)

assert container.exposed_ports == [{80, 8081}]
end
end

describe "mapped_port/2" do
test "returns the mapped host port for the given exposed port" do
container = Container.new("my-image") |> Container.with_fixed_port(80, 8080)
assert Container.mapped_port(container, 80) == 8080
end

test "returns nil if the exposed port is not found" do
container = Container.new("my-image")
assert Container.mapped_port(container, 80) == nil
end
end

describe "with_auth/3" do
test "sets the authentication token for the container" do
container = Container.new("my-image")
Expand Down

0 comments on commit e68bd40

Please sign in to comment.