Skip to content

Commit

Permalink
Stacks support no options
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffgrunewald committed Apr 1, 2019
1 parent a81c096 commit 35722dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
8 changes: 5 additions & 3 deletions lib/divo/stack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ defmodule Divo.Stack do

services =
config
|> Enum.map(fn {module, envars} ->
apply(module, :gen_stack, [envars])
end)
|> Enum.map(&configure_stack/1)
|> Enum.reduce(%{}, fn service, acc -> Map.merge(service, acc) end)

Map.put(compose_file, :services, services)
end

defp configure_stack(module) when is_atom(module), do: apply(module, :gen_stack, [[]])

defp configure_stack({module, envars}), do: apply(module, :gen_stack, [envars])
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Divo.MixProject do
def project do
[
app: :divo,
version: "1.1.1",
version: "1.1.2",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
33 changes: 16 additions & 17 deletions test/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,32 @@ defmodule Divo.FileTest do
assert Divo.File.ensure_file(config) == "/var/tmp/foo/divo.compose"
end

test "generates compose file from a behaviour implementation" do
test "generates compose file from a behaviour implementation of a single service" do
allow(File.write!(any(), any()), return: :ok)
allow(System.get_env("TMPDIR"), return: "/var/tmp/bar")

services = [{DivoBarbaz, []}]
services = [{DivoFoobar, [db_password: "we-are-divo", db_name: "foobar-db", something: "else"]}]

TemporaryEnv.put :divo, :divo, services do
config = Divo.Helper.fetch_config()

assert Divo.File.ensure_file(config) == "/var/tmp/bar/divo.compose"
end
end
end

defmodule DivoBarbaz do
@behaviour Divo.Stack

@impl Divo.Stack
def gen_stack(_envars) do
%{
barbaz: %{
image: "library/barbaz",
healthcheck: %{
test: ["CMD-SHELL", "/bin/true || exit 1"]
},
ports: ["2345:2345", "7777:7777"]
}
}
test "concatenates compose file from multiple implementations of the behaviour" do
allow(File.write!(any(), any()), return: :ok)
allow(System.get_env("TMPDIR"), return: "/var/tmp/bar")

services = [
{DivoFoobar, [db_password: "we-are-divo", db_name: "foobar-db", something: "else"]},
DivoBarbaz
]

TemporaryEnv.put :divo, :divo, services do
config = Divo.Helper.fetch_config()

assert Divo.File.ensure_file(config) == "/var/tmp/bar/divo.compose"
end
end
end
42 changes: 41 additions & 1 deletion test/stack_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Divo.StackTest do
use ExUnit.Case
require TemporaryEnv

test "behaviour returns the service definition" do
test "behaviour returns the service definition of a single stack" do
services = [
{DivoFoobar, [db_password: "we-are-divo", db_name: "foobar-db", something: "else"]}
]
Expand All @@ -29,6 +29,29 @@ defmodule Divo.StackTest do

assert expected == actual
end

test "behaviour returns the service definition of a stack with no parameters" do
services = [
DivoBarbaz
]

expected = %{
version: "3.4",
services: %{
barbaz: %{
image: "library/barbaz",
ports: ["2345:2345", "7777:7777"],
healthcheck: %{
test: ["CMD-SHELL", "/bin/true || exit 1"]
}
}
}
}

actual = Divo.Stack.concat_compose(services)

assert expected == actual
end
end

defmodule DivoFoobar do
Expand All @@ -55,3 +78,20 @@ defmodule DivoFoobar do
}
end
end

defmodule DivoBarbaz do
@behaviour Divo.Stack

@impl Divo.Stack
def gen_stack(_envars) do
%{
barbaz: %{
image: "library/barbaz",
healthcheck: %{
test: ["CMD-SHELL", "/bin/true || exit 1"]
},
ports: ["2345:2345", "7777:7777"]
}
}
end
end

0 comments on commit 35722dd

Please sign in to comment.