From 26423aa50129ef06a8d79f73b8e0e2315941a4b5 Mon Sep 17 00:00:00 2001 From: "Michael B. Klein" Date: Mon, 11 Mar 2024 21:02:42 +0000 Subject: [PATCH] Don't start index pipeline name with "-" in release --- app/lib/env.ex | 15 +++++++--- app/test/env_test.exs | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 app/test/env_test.exs diff --git a/app/lib/env.ex b/app/lib/env.ex index aab614eda..8bd91fc6b 100644 --- a/app/lib/env.ex +++ b/app/lib/env.ex @@ -6,12 +6,17 @@ if !function_exported?(:Env, :prefix, 0) do alias Hush.Provider.{AwsSecretsManager, SystemEnvironment} def prefix do - with env <- if(function_exported?(Mix, :env, 0), do: Mix.env(), else: nil) do - [System.get_env("DEV_PREFIX"), env] |> Enum.reject(&is_nil/1) |> Enum.join("-") - end + env = + cond do + System.get_env("RELEASE_NAME") -> nil + function_exported?(Mix, :env, 0) -> Mix.env() + true -> nil + end + + [System.get_env("DEV_PREFIX"), env] |> Enum.reject(&is_nil/1) |> Enum.join("-") end - def prefix(val), do: [prefix(), to_string(val)] |> Enum.reject(&is_nil/1) |> Enum.join("-") + def prefix(val), do: [prefix(), to_string(val)] |> reject_empty() |> Enum.join("-") def atom_prefix(val), do: prefix(val) |> String.to_atom() def aws_secret(name, opts \\ []), @@ -23,5 +28,7 @@ if !function_exported?(:Env, :prefix, 0) do defp hush_secret(provider, name, opts), do: {:hush, provider, name, opts} defp secrets_path, do: System.get_env("SECRETS_PATH", "config") + + defp reject_empty(list), do: Enum.reject(list, &(is_nil(&1) or &1 == "")) end end diff --git a/app/test/env_test.exs b/app/test/env_test.exs new file mode 100644 index 000000000..6e7563aa6 --- /dev/null +++ b/app/test/env_test.exs @@ -0,0 +1,67 @@ +defmodule EnvTest do + use ExUnit.Case + + import Env + + setup %{environment: environment} do + set_env = fn + {key, nil} -> System.delete_env(key) + {key, value} -> System.put_env(key, value) + end + + saved = Enum.map(environment, fn {key, _} -> {key, System.get_env(key)} end) + Enum.each(environment, set_env) + on_exit(fn -> Enum.each(saved, set_env) end) + end + + describe "hush" do + @describetag environment: [{"SECRETS_PATH", "foo/config"}] + + test "aws_secret/2" do + assert aws_secret("meadow", test: :value) == + {:hush, Hush.Provider.AwsSecretsManager, "foo/config/meadow", [test: :value]} + end + + test "meadow_secret/2" do + assert meadow_secret(test: :value) == + {:hush, Hush.Provider.AwsSecretsManager, "foo/config/meadow", [test: :value]} + end + + test "environment_secret/2" do + assert environment_secret("DEV_PREFIX", test: :value) == + {:hush, Hush.Provider.SystemEnvironment, "DEV_PREFIX", [test: :value]} + end + end + + describe "dev environment" do + @describetag environment: [{"DEV_PREFIX", "env"}] + + test "prefix/0" do + assert prefix() == "env-test" + end + + test "prefix/1" do + assert prefix("database") == "env-test-database" + end + + test "atom_prefix/1" do + assert atom_prefix("database") == :"env-test-database" + end + end + + describe "release environment" do + @describetag environment: [{"DEV_PREFIX", nil}, {"RELEASE_NAME", "meadow"}] + + test "prefix/0" do + assert prefix() == "" + end + + test "prefix/1" do + assert prefix("database") == "database" + end + + test "atom_prefix/1" do + assert atom_prefix("database") == :database + end + end +end