Skip to content

Commit

Permalink
Merge pull request #95 from upmaru/feature/ops-639-use-balancer-addre…
Browse files Browse the repository at this point in the history
…ss-if-exists

Add test for LXD client creation
  • Loading branch information
zacksiri authored Apr 2, 2024
2 parents f2bdb1e + 2a86868 commit b9f3fe8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/uplink/clients/lxd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,25 @@ defmodule Uplink.Clients.LXD do
def client do
%{
"credential" => credential
} = Instellar.get_self()
} = params = Instellar.get_self()

balancer = Map.get(params, "balancer")

endpoint =
case Map.get(params, "balancer") do
%{"address" => address, "current_state" => "active"} ->
uri = URI.parse(credential["endpoint"])

uri = %{uri | host: address}

to_string(uri)

nil ->
credential["endpoint"]
end

Lexdee.create_client(
credential["endpoint"],
endpoint,
credential["certificate"],
credential["private_key"]
)
Expand Down
75 changes: 75 additions & 0 deletions test/uplink/clients/lxd_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Uplink.Clients.LXDTest do
use ExUnit.Case

alias Uplink.Cache

alias Uplink.Clients.LXD

describe "balancer is nil" do
setup do
Cache.put(:self, %{
"balancer" => nil,
"credential" => %{
"endpoint" => "http://localhost:8443"
}
})
end

test "use credential endpoint" do
assert %Tesla.Client{pre: pre} = LXD.client()

{_, _, [base_url]} =
Enum.find(pre, fn {middleware, _, _} ->
middleware == Tesla.Middleware.BaseUrl
end)

assert base_url == "http://localhost:8443"
end
end

describe "client without balancer" do
setup do
Cache.put(:self, %{
"credential" => %{
"endpoint" => "http://localhost:8443"
}
})
end

test "use credential endpoint" do
assert %Tesla.Client{pre: pre} = LXD.client()

{_, _, [base_url]} =
Enum.find(pre, fn {middleware, _, _} ->
middleware == Tesla.Middleware.BaseUrl
end)

assert base_url == "http://localhost:8443"
end
end

describe "client with blaancer" do
setup do
Cache.put(:self, %{
"balancer" => %{
"address" => "some.address.com",
"current_state" => "active"
},
"credential" => %{
"endpoint" => "http://localhost:8443"
}
})
end

test "use balancer address" do
assert %Tesla.Client{pre: pre} = LXD.client()

{_, _, [base_url]} =
Enum.find(pre, fn {middleware, _, _} ->
middleware == Tesla.Middleware.BaseUrl
end)

assert base_url == "http://some.address.com:8443"
end
end
end

0 comments on commit b9f3fe8

Please sign in to comment.