From 35722dd71e54d039f3429d9263afaf17a5c64ccc Mon Sep 17 00:00:00 2001 From: jeffgrunewald Date: Mon, 1 Apr 2019 13:40:51 -0400 Subject: [PATCH] Stacks support no options --- lib/divo/stack.ex | 8 +++++--- mix.exs | 2 +- test/file_test.exs | 33 ++++++++++++++++----------------- test/stack_test.exs | 42 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/lib/divo/stack.ex b/lib/divo/stack.ex index 1828e51..2923c36 100644 --- a/lib/divo/stack.ex +++ b/lib/divo/stack.ex @@ -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 diff --git a/mix.exs b/mix.exs index d5cddbd..2f70c12 100644 --- a/mix.exs +++ b/mix.exs @@ -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(), diff --git a/test/file_test.exs b/test/file_test.exs index c895676..7073adc 100644 --- a/test/file_test.exs +++ b/test/file_test.exs @@ -31,11 +31,11 @@ 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() @@ -43,21 +43,20 @@ defmodule Divo.FileTest do 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 diff --git a/test/stack_test.exs b/test/stack_test.exs index 2df7132..f0a5275 100644 --- a/test/stack_test.exs +++ b/test/stack_test.exs @@ -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"]} ] @@ -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 @@ -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