Skip to content

Commit

Permalink
Correct query generation for availability
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jan 9, 2025
1 parent f473489 commit 94ee75c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/uplink/availability/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ defmodule Uplink.Availability.Query do
Map.values(@metrics_mappings)
end

@spec build([Node.t()] | Member.t(), [String.t()]) :: String.t()
def build(nodes, indices) when is_list(members) do
@spec build([Node.t()] | Node.t(), [String.t()]) :: String.t()
def build(nodes, indices) when is_list(nodes) do
nodes
|> Enum.flat_map(fn node ->
|> Enum.map(fn node ->
build(node, indices)
end)
|> Enum.join("\n")
Expand Down
1 change: 1 addition & 0 deletions lib/uplink/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Uplink.Metrics do
def query!(%{"attributes" => attributes} = monitor, query) do
headers = headers(monitor)
endpoint = Map.fetch!(attributes, "endpoint")
query = query <> "\n"

request = request(endpoint, headers)

Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/instellar/monitors/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"data": [
{
"attributes": {
"current_state": "active",
"endpoint": "https://elastic:9200",
"expires_at": "2024-11-21T03:14:17Z",
"id": 1,
"token": "some-token",
"type": "metrics",
"uid": "some-uid"
},
"id": "1",
"links": {
"self": "http://localhost:4000/uplink/self/monitors/1"
},
"relationships": {},
"type": "monitors"
}
]
}
28 changes: 24 additions & 4 deletions test/uplink/availability_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,50 @@ defmodule Uplink.AvailabilityTest do
setup do
bypass = Bypass.open()

Application.put_env(
:uplink,
Uplink.Clients.Instellar,
endpoint: "http://localhost:#{bypass.port}/uplink"
)

Cache.put(:self, %{
"credential" => %{
"endpoint" => "http://localhost:#{bypass.port}"
}
})

response = File.read!("test/fixtures/lxd/cluster/members/list.json")
cluster_members_response =
File.read!("test/fixtures/lxd/cluster/members/list.json")

monitors_list_response =
File.read!("test/fixtures/instellar/monitors/list.json")

Cache.delete(:cluster_members)

{:ok, bypass: bypass}
{:ok,
bypass: bypass,
cluster_members_response: cluster_members_response,
monitors_list_response: monitors_list_response}
end

describe "check availability of the nodes in the cluster" do
test "return availability check result", %{
bypass: bypass,
response: response
cluster_members_response: cluster_members_response,
monitors_list_response: monitors_list_response
} do
Bypass.expect_once(bypass, "GET", "/uplink/self/monitors", fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(200, monitors_list_response)
end)

Bypass.expect_once(bypass, "GET", "/1.0/cluster/members", fn conn ->
assert %{"recursion" => "1"} = conn.query_params

conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(200, response)
|> Plug.Conn.resp(200, cluster_members_response)
end)

assert {:ok, result} = Availability.check!()
Expand Down

0 comments on commit 94ee75c

Please sign in to comment.