diff --git a/config/test.exs b/config/test.exs index 53a52d7..dc38920 100644 --- a/config/test.exs +++ b/config/test.exs @@ -23,7 +23,9 @@ config :polar, PolarWeb.Endpoint, secret_key_base: "6KFZ6zNwk0UXHww8AnEmReHHKixN5GmuKJLFVB+/YvfcvgVaKMwM3G4SvSNz5Z8s", server: false -config :polar, Oban, testing: :inline +config :polar, Oban, testing: :manual + +config :polar, :lexdee, Polar.LexdeeMock # In test we don't send emails. config :polar, Polar.Mailer, adapter: Swoosh.Adapters.Test diff --git a/lib/polar/machines/cluster/connect.ex b/lib/polar/machines/cluster/connect.ex index d51f8d0..b089127 100644 --- a/lib/polar/machines/cluster/connect.ex +++ b/lib/polar/machines/cluster/connect.ex @@ -5,7 +5,7 @@ defmodule Polar.Machines.Cluster.Connect do alias Polar.Accounts.User alias Polar.Machines.Cluster - @lexdee Application.compile_env(:instellar, :lexdee) || Lexdee + @lexdee Application.compile_env(:polar, :lexdee) || Lexdee def perform(%Job{args: %{"user_id" => user_id, "cluster_id" => cluster_id}}) do user = Repo.get(User, user_id) diff --git a/lib/polar/machines/cluster/transitions.ex b/lib/polar/machines/cluster/transitions.ex index a285e91..c1f10d8 100644 --- a/lib/polar/machines/cluster/transitions.ex +++ b/lib/polar/machines/cluster/transitions.ex @@ -7,7 +7,7 @@ defmodule Polar.Machines.Cluster.Transitions do Cluster |> transition( [from: "created", to: "connecting", via: "connect"], - fn changes -> transit(changes) end + fn changes -> transit(changes, Cluster.Triggers) end ) Cluster @@ -15,4 +15,10 @@ defmodule Polar.Machines.Cluster.Transitions do [from: "connecting", to: "healthy", via: "healthy"], fn changes -> transit(changes) end ) + + Cluster + |> transition( + [from: "connecting", to: "created", via: "revert"], + fn changes -> transit(changes) end + ) end diff --git a/mix.exs b/mix.exs index ddeb3bf..6e76f03 100644 --- a/mix.exs +++ b/mix.exs @@ -73,7 +73,7 @@ defmodule Polar.MixProject do # LXD client {:lexdee, "~> 2.3"}, - + # Cert {:x509, "~> 0.8"}, @@ -81,7 +81,8 @@ defmodule Polar.MixProject do {:cloak_ecto, "~> 1.3"}, # Dev / Test - {:dialyxir, "~> 1.0", only: [:dev], runtime: false} + {:dialyxir, "~> 1.0", only: [:dev], runtime: false}, + {:mox, "~> 1.0", only: :test} ] end diff --git a/mix.lock b/mix.lock index dececbc..742c8fd 100644 --- a/mix.lock +++ b/mix.lock @@ -29,6 +29,7 @@ "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mint": {:hex, :mint, "1.6.1", "065e8a5bc9bbd46a41099dfea3e0656436c5cbcb6e741c80bd2bad5cd872446f", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "4fc518dcc191d02f433393a72a7ba3f6f94b101d094cb6bf532ea54c89423780"}, "mint_web_socket": {:hex, :mint_web_socket, "1.0.4", "0b539116dbb3d3f861cdf5e15e269a933cb501c113a14db7001a3157d96ffafd", [:mix], [{:mint, ">= 1.4.1 and < 2.0.0-0", [hex: :mint, repo: "hexpm", optional: false]}], "hexpm", "027d4c5529c45a4ba0ce27a01c0f35f284a5468519c045ca15f43decb360a991"}, + "mox": {:hex, :mox, "1.1.0", "0f5e399649ce9ab7602f72e718305c0f9cdc351190f72844599545e4996af73c", [:mix], [], "hexpm", "d44474c50be02d5b72131070281a5d3895c0e7a95c780e90bc0cfe712f633a13"}, "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, "oban": {:hex, :oban, "2.17.10", "c3e5bd739b5c3fdc38eba1d43ab270a8c6ca4463bb779b7705c69400b0d87678", [:mix], [{:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ecto_sqlite3, "~> 0.9", [hex: :ecto_sqlite3, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4afd027b8e2bc3c399b54318b4f46ee8c40251fb55a285cb4e38b5363f0ee7c4"}, diff --git a/test/polar/machines/cluster/connect_test.exs b/test/polar/machines/cluster/connect_test.exs new file mode 100644 index 0000000..66d4834 --- /dev/null +++ b/test/polar/machines/cluster/connect_test.exs @@ -0,0 +1,50 @@ +defmodule Polar.Machines.Cluster.ConnectTest do + use Polar.DataCase, async: true + use Oban.Testing, repo: Polar.Repo + + alias Polar.Machines + alias Polar.Machines.Cluster.Connect + + import Polar.AccountsFixtures + + import Mox + + setup :verify_on_exit! + + setup do + user = user_fixture() + + {:ok, cluster} = + Machines.create_cluster(%{ + name: "example", + type: "lxd", + arch: "amd64", + credential_endpoint: "some.cluster.com:8443", + credential_password: "sometoken", + credential_password_confirmation: "sometoken" + }) + + {:ok, cluster: cluster, user: user} + end + + describe "perform" do + setup %{cluster: cluster, user: user} do + {:ok, %{resource: connecting_cluster}} = + Eventful.Transit.perform(cluster, user, "connect") + + {:ok, cluster: connecting_cluster} + end + + test "connect to the cluster", %{cluster: cluster, user: user} do + Polar.LexdeeMock + |> expect(:create_certificate, fn _, _ -> + {:ok, nil} + end) + + assert {:ok, %{resource: healthy_cluster}} = + perform_job(Connect, %{cluster_id: cluster.id, user_id: user.id}) + + assert healthy_cluster.current_state == "healthy" + end + end +end diff --git a/test/polar/machines/cluster/transitions_test.exs b/test/polar/machines/cluster/transitions_test.exs new file mode 100644 index 0000000..3c4638f --- /dev/null +++ b/test/polar/machines/cluster/transitions_test.exs @@ -0,0 +1,34 @@ +defmodule Polar.Machines.Cluster.TransitionsTest do + use Polar.DataCase, async: true + + alias Polar.Machines + + import Polar.AccountsFixtures + + setup do + user = user_fixture() + + {:ok, cluster} = + Machines.create_cluster(%{ + name: "example", + type: "lxd", + arch: "amd64", + credential_endpoint: "some.cluster.com:8443", + credential_password: "sometoken", + credential_password_confirmation: "sometoken" + }) + + {:ok, cluster: cluster, user: user} + end + + describe "connect" do + test "can transition", %{user: user, cluster: cluster} do + assert {:ok, %{resource: connecting_cluster, trigger: trigger}} = + Eventful.Transit.perform(cluster, user, "connect") + + assert %Oban.Job{worker: "Polar.Machines.Cluster.Connect"} = trigger + + assert connecting_cluster.current_state == "connecting" + end + end +end diff --git a/test/support/mocks.ex b/test/support/mocks.ex new file mode 100644 index 0000000..575bd64 --- /dev/null +++ b/test/support/mocks.ex @@ -0,0 +1 @@ +Mox.defmock(Polar.LexdeeMock, for: Lexdee.Behaviour)