Skip to content

Commit

Permalink
Add test for transitions and connect cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jun 21, 2024
1 parent 98f74f2 commit 83be729
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 5 deletions.
4 changes: 3 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/polar/machines/cluster/connect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/polar/machines/cluster/transitions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ 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
|> transition(
[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
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ defmodule Polar.MixProject do

# LXD client
{:lexdee, "~> 2.3"},

# Cert
{:x509, "~> 0.8"},

# Encryption
{: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

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
50 changes: 50 additions & 0 deletions test/polar/machines/cluster/connect_test.exs
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions test/polar/machines/cluster/transitions_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/support/mocks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mox.defmock(Polar.LexdeeMock, for: Lexdee.Behaviour)

0 comments on commit 83be729

Please sign in to comment.