|
| 1 | +defmodule Preview.ApplicationTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Preview.Application |
| 5 | + |
| 6 | + test "queue children can be disabled without removing the web endpoint" do |
| 7 | + enabled = child_ids(Application.children(true)) |
| 8 | + disabled = child_ids(Application.children(false)) |
| 9 | + |
| 10 | + assert Preview.Queue in enabled |
| 11 | + assert Preview.Debouncer in enabled |
| 12 | + refute Preview.Queue in disabled |
| 13 | + refute Preview.Debouncer in disabled |
| 14 | + assert PreviewWeb.Endpoint in enabled |
| 15 | + assert PreviewWeb.Endpoint in disabled |
| 16 | + end |
| 17 | + |
| 18 | + test "disabled production configuration does not require queue credentials" do |
| 19 | + elixir = System.find_executable("elixir") |
| 20 | + erl = System.find_executable("erl") |
| 21 | + path = [Path.dirname(elixir), Path.dirname(erl), "/usr/bin", "/bin"] |> Enum.uniq() |
| 22 | + |
| 23 | + env = [ |
| 24 | + "PATH=#{Enum.join(path, ":")}", |
| 25 | + "PREVIEW_QUEUE_ENABLED=false", |
| 26 | + "PREVIEW_HOST=preview.hex.pm", |
| 27 | + "PREVIEW_REPO_URL=https://repo.hex.pm", |
| 28 | + "PREVIEW_REPO_PUBLIC_KEY=public-key", |
| 29 | + "PREVIEW_BUCKET=preview-bucket", |
| 30 | + "PREVIEW_PORT=4005", |
| 31 | + "PREVIEW_SECRET_KEY_BASE=secret-key-base", |
| 32 | + "PREVIEW_SENTRY_DSN=sentry-dsn", |
| 33 | + "PREVIEW_ENV=prod", |
| 34 | + "BEAM_PORT=14005" |
| 35 | + ] |
| 36 | + |
| 37 | + expression = ~S|Config.Reader.read!("config/runtime.exs", env: :prod); IO.write("configured")| |
| 38 | + |
| 39 | + assert {"configured", 0} = |
| 40 | + System.cmd(System.find_executable("env"), ["-i" | env] ++ [elixir, "-e", expression]) |
| 41 | + end |
| 42 | + |
| 43 | + test "production configuration defaults to an enabled queue" do |
| 44 | + env = runtime_env() ++ queue_env() |
| 45 | + |
| 46 | + expression = |
| 47 | + ~S""" |
| 48 | + config = Config.Reader.read!("config/runtime.exs", env: :prod) |
| 49 | + preview = config[:preview] |
| 50 | + ex_aws = config[:ex_aws] |
| 51 | +
|
| 52 | + IO.write( |
| 53 | + Enum.join( |
| 54 | + [ |
| 55 | + preview[:queue_id], |
| 56 | + preview[:queue_concurrency], |
| 57 | + preview[:fastly_key], |
| 58 | + preview[:fastly_repo], |
| 59 | + preview[:repo_bucket][:name], |
| 60 | + ex_aws[:access_key_id], |
| 61 | + ex_aws[:secret_access_key] |
| 62 | + ], |
| 63 | + "|" |
| 64 | + ) |
| 65 | + ) |
| 66 | + """ |
| 67 | + |
| 68 | + assert {"queue|10|fastly-key|fastly-repo|repo-bucket|aws-key|aws-secret", 0} = |
| 69 | + System.cmd( |
| 70 | + System.find_executable("env"), |
| 71 | + ["-i" | env] ++ [System.find_executable("elixir"), "-e", expression] |
| 72 | + ) |
| 73 | + end |
| 74 | + |
| 75 | + test "production configuration rejects invalid queue settings" do |
| 76 | + env = ["PREVIEW_QUEUE_ENABLED=invalid" | runtime_env()] |
| 77 | + expression = ~S|Config.Reader.read!("config/runtime.exs", env: :prod)| |
| 78 | + |
| 79 | + assert {output, status} = |
| 80 | + System.cmd( |
| 81 | + System.find_executable("env"), |
| 82 | + ["-i" | env] ++ [System.find_executable("elixir"), "-e", expression], |
| 83 | + stderr_to_stdout: true |
| 84 | + ) |
| 85 | + |
| 86 | + assert status != 0 |
| 87 | + assert output =~ "invalid PREVIEW_QUEUE_ENABLED" |
| 88 | + end |
| 89 | + |
| 90 | + defp runtime_env do |
| 91 | + elixir = System.find_executable("elixir") |
| 92 | + erl = System.find_executable("erl") |
| 93 | + path = [Path.dirname(elixir), Path.dirname(erl), "/usr/bin", "/bin"] |> Enum.uniq() |
| 94 | + |
| 95 | + [ |
| 96 | + "PATH=#{Enum.join(path, ":")}", |
| 97 | + "PREVIEW_HOST=preview.hex.pm", |
| 98 | + "PREVIEW_REPO_URL=https://repo.hex.pm", |
| 99 | + "PREVIEW_REPO_PUBLIC_KEY=public-key", |
| 100 | + "PREVIEW_BUCKET=preview-bucket", |
| 101 | + "PREVIEW_PORT=4005", |
| 102 | + "PREVIEW_SECRET_KEY_BASE=secret-key-base", |
| 103 | + "PREVIEW_SENTRY_DSN=sentry-dsn", |
| 104 | + "PREVIEW_ENV=prod", |
| 105 | + "BEAM_PORT=14005" |
| 106 | + ] |
| 107 | + end |
| 108 | + |
| 109 | + defp queue_env do |
| 110 | + [ |
| 111 | + "PREVIEW_QUEUE_ID=queue", |
| 112 | + "PREVIEW_QUEUE_CONCURRENCY=10", |
| 113 | + "PREVIEW_FASTLY_KEY=fastly-key", |
| 114 | + "PREVIEW_FASTLY_REPO=fastly-repo", |
| 115 | + "PREVIEW_REPO_BUCKET=repo-bucket", |
| 116 | + "PREVIEW_AWS_ACCESS_KEY_ID=aws-key", |
| 117 | + "PREVIEW_AWS_ACCESS_KEY_SECRET=aws-secret" |
| 118 | + ] |
| 119 | + end |
| 120 | + |
| 121 | + defp child_ids(children) do |
| 122 | + MapSet.new(children, fn child -> Supervisor.child_spec(child, []).id end) |
| 123 | + end |
| 124 | +end |
0 commit comments