diff --git a/config/dev.exs b/config/dev.exs index d1c43e4deb..df83c79898 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -92,6 +92,7 @@ config :trento, Trento.Scheduler, ] config :trento, Trento.Integration.Telemetry, adapter: Trento.Integration.Telemetry.ToLogger + config :trento, Trento.Integration.Checks, adapter: Trento.Integration.Checks.MockRunner, runner_url: "" diff --git a/config/prod.exs b/config/prod.exs index b8358c36b3..3ccedb6eb9 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -5,8 +5,7 @@ config :trento, TrentoWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json", server: true - config :trento, Trento.Integration.Checks, - runner_url: System.get_env("RUNNER_URL") +config :trento, Trento.Integration.Checks, runner_url: System.get_env("RUNNER_URL") # Do not print debug messages in production # config :logger, level: :info diff --git a/config/runtime.exs b/config/runtime.exs index 9e34db8e6d..9f57593b7e 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -61,8 +61,7 @@ if config_env() == :prod do For example: http://localhost:8080 """ - config :trento, Trento.Integration.Checks, - runner_url: runner_url + config :trento, Trento.Integration.Checks, runner_url: runner_url # ## Using releases # diff --git a/lib/trento/application/integration/checks/checks.ex b/lib/trento/application/integration/checks/checks.ex index 3290dfff7f..8a5459ee14 100644 --- a/lib/trento/application/integration/checks/checks.ex +++ b/lib/trento/application/integration/checks/checks.ex @@ -1,5 +1,4 @@ defmodule Trento.Integration.Checks do - alias Trento.Integration.Checks.Models.Catalog @moduledoc """ @@ -16,6 +15,7 @@ defmodule Trento.Integration.Checks do case is_catalog_ready(runner_url()) do :ok -> get_catalog_content(runner_url()) + {:error, reason} -> {:error, reason} end @@ -49,12 +49,14 @@ defmodule Trento.Integration.Checks do defp handle_catalog_ready(%{"ready" => true}), do: :ok - defp handle_catalog_ready(%{"ready" => false}), do: {:error, "The catalog is still being built."} + defp handle_catalog_ready(%{"ready" => false}), + do: {:error, "The catalog is still being built."} defp normalize_catalog(catalog_raw) do - normalized_catalog = catalog_raw - |> Enum.group_by(&Map.take(&1, ["provider"]), &Map.drop(&1, ["provider"])) - |> Enum.map(fn {key, value} -> Map.put(key, "groups", group_by_groups(value)) end) + normalized_catalog = + catalog_raw + |> Enum.group_by(&Map.take(&1, ["provider"]), &Map.drop(&1, ["provider"])) + |> Enum.map(fn {key, value} -> Map.put(key, "groups", group_by_groups(value)) end) Catalog.new(%{providers: normalized_catalog}) end @@ -70,5 +72,4 @@ defmodule Trento.Integration.Checks do defp runner_url, do: Application.fetch_env!(:trento, __MODULE__)[:runner_url] - end diff --git a/lib/trento/application/integration/checks/models/group.ex b/lib/trento/application/integration/checks/models/group.ex index 312ea2aeab..c585f2390f 100644 --- a/lib/trento/application/integration/checks/models/group.ex +++ b/lib/trento/application/integration/checks/models/group.ex @@ -15,6 +15,6 @@ defmodule Trento.Integration.Checks.Models.Group do deftype do field :group, :string - embeds_many :checks, Check + embeds_many :checks, Check end end diff --git a/lib/trento/application/integration/checks/models/provider.ex b/lib/trento/application/integration/checks/models/provider.ex index dc020690ef..7ff44abe00 100644 --- a/lib/trento/application/integration/checks/models/provider.ex +++ b/lib/trento/application/integration/checks/models/provider.ex @@ -15,6 +15,6 @@ defmodule Trento.Integration.Checks.Models.Provider do deftype do field :provider, Ecto.Enum, values: [:azure, :aws, :gcp, :dev, :unknown] - embeds_many :groups, Group + embeds_many :groups, Group end end diff --git a/lib/trento_web/controllers/catalog_controller.ex b/lib/trento_web/controllers/catalog_controller.ex index 45b59c7dac..bc2984cedb 100644 --- a/lib/trento_web/controllers/catalog_controller.ex +++ b/lib/trento_web/controllers/catalog_controller.ex @@ -13,6 +13,6 @@ defmodule TrentoWeb.CatalogController do conn |> put_status(:bad_request) |> json(%{error: reason}) - end + end end end diff --git a/test/test_helper.exs b/test/test_helper.exs index 2fc07a0cfe..d56615754c 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -5,9 +5,7 @@ Application.put_env(:trento, Trento.Integration.Telemetry, adapter: Trento.Integration.Telemetry.Mock ) -Application.put_env(:trento, Trento.Integration.Checks, - adapter: Trento.Integration.Checks.Mock -) +Application.put_env(:trento, Trento.Integration.Checks, adapter: Trento.Integration.Checks.Mock) ExUnit.start() Ecto.Adapters.SQL.Sandbox.mode(Trento.Repo, :manual) diff --git a/test/trento/application/integration/checks/checks_test.exs b/test/trento/application/integration/checks/checks_test.exs index e8fa05b5e1..3adf3c84d3 100644 --- a/test/trento/application/integration/checks/checks_test.exs +++ b/test/trento/application/integration/checks/checks_test.exs @@ -5,6 +5,7 @@ defmodule Trento.Integration.ChecksTest do import Mox alias Trento.Integration.Checks + alias Trento.Integration.Checks.Models.{ Catalog, Provider, @@ -52,132 +53,131 @@ defmodule Trento.Integration.ChecksTest do |> expect(:get_runner_ready_content, fn _runner_url -> {:ok, %{"ready" => true}} end) |> expect(:get_catalog_content, fn _runner_url -> {:ok, raw_catalog} end) - normalized_catalog = - %Catalog{ - providers: [ - %Provider{ - groups: [ - %Group{ - checks: [ - %Check{ - description: "description 1", - id: "1", - implementation: "implementation 1", - labels: "labels", - name: "test 1", - remediation: "remediation 1" - }, - %Check{ - description: "description 2", - id: "2", - implementation: "implementation 2", - labels: "labels", - name: "test 2", - remediation: "remediation 2" - } - ], - group: "Group 1" - }, - %Group{ - checks: [ - %Check{ - description: "description 3", - id: "3", - implementation: "implementation 3", - labels: "labels", - name: "test 3", - remediation: "remediation 3" - }, - %Check{ - description: "description 4", - id: "4", - implementation: "implementation 4", - labels: "labels", - name: "test 4", - remediation: "remediation 4" - } - ], - group: "Group 2" - }, - %Group{ - checks: [ - %Check{ - description: "description 5", - id: "5", - implementation: "implementation 5", - labels: "labels", - name: "test 5", - remediation: "remediation 5" - } - ], - group: "Group 3" - } + normalized_catalog = %Catalog{ + providers: [ + %Provider{ + groups: [ + %Group{ + checks: [ + %Check{ + description: "description 1", + id: "1", + implementation: "implementation 1", + labels: "labels", + name: "test 1", + remediation: "remediation 1" + }, + %Check{ + description: "description 2", + id: "2", + implementation: "implementation 2", + labels: "labels", + name: "test 2", + remediation: "remediation 2" + } + ], + group: "Group 1" + }, + %Group{ + checks: [ + %Check{ + description: "description 3", + id: "3", + implementation: "implementation 3", + labels: "labels", + name: "test 3", + remediation: "remediation 3" + }, + %Check{ + description: "description 4", + id: "4", + implementation: "implementation 4", + labels: "labels", + name: "test 4", + remediation: "remediation 4" + } ], - provider: :aws + group: "Group 2" }, - %Provider{ - groups: [ - %Group{ - checks: [ - %Check{ - description: "description 1", - id: "1", - implementation: "implementation 1", - labels: "labels", - name: "test 1", - remediation: "remediation 1" - }, - %Check{ - description: "description 2", - id: "2", - implementation: "implementation 2", - labels: "labels", - name: "test 2", - remediation: "remediation 2" - } - ], - group: "Group 1" + %Group{ + checks: [ + %Check{ + description: "description 5", + id: "5", + implementation: "implementation 5", + labels: "labels", + name: "test 5", + remediation: "remediation 5" + } + ], + group: "Group 3" + } + ], + provider: :aws + }, + %Provider{ + groups: [ + %Group{ + checks: [ + %Check{ + description: "description 1", + id: "1", + implementation: "implementation 1", + labels: "labels", + name: "test 1", + remediation: "remediation 1" }, - %Group{ - checks: [ - %Check{ - description: "description 3", - id: "3", - implementation: "implementation 3", - labels: "labels", - name: "test 3", - remediation: "remediation 3" - }, - %Check{ - description: "description 4", - id: "4", - implementation: "implementation 4", - labels: "labels", - name: "test 4", - remediation: "remediation 4" - } - ], - group: "Group 2" + %Check{ + description: "description 2", + id: "2", + implementation: "implementation 2", + labels: "labels", + name: "test 2", + remediation: "remediation 2" + } + ], + group: "Group 1" + }, + %Group{ + checks: [ + %Check{ + description: "description 3", + id: "3", + implementation: "implementation 3", + labels: "labels", + name: "test 3", + remediation: "remediation 3" }, - %Group{ - checks: [ - %Check{ - description: "description 5", - id: "5", - implementation: "implementation 5", - labels: "labels", - name: "test 5", - remediation: "remediation 5" - } - ], - group: "Group 3" + %Check{ + description: "description 4", + id: "4", + implementation: "implementation 4", + labels: "labels", + name: "test 4", + remediation: "remediation 4" + } + ], + group: "Group 2" + }, + %Group{ + checks: [ + %Check{ + description: "description 5", + id: "5", + implementation: "implementation 5", + labels: "labels", + name: "test 5", + remediation: "remediation 5" } ], - provider: :azure + group: "Group 3" } - ] + ], + provider: :azure } + ] + } - assert {:ok, normalized_catalog} == Checks.get_catalog() + assert {:ok, normalized_catalog} == Checks.get_catalog() end end