From 94ee75ca69869604bd3dc09070d9d7354ed47597 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Thu, 9 Jan 2025 18:28:27 +0700 Subject: [PATCH] Correct query generation for availability --- lib/uplink/availability/query.ex | 6 ++--- lib/uplink/metrics.ex | 1 + test/fixtures/instellar/monitors/list.json | 21 ++++++++++++++++ test/uplink/availability_test.exs | 28 ++++++++++++++++++---- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/uplink/availability/query.ex b/lib/uplink/availability/query.ex index 858aa822..e2e8eeb3 100644 --- a/lib/uplink/availability/query.ex +++ b/lib/uplink/availability/query.ex @@ -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") diff --git a/lib/uplink/metrics.ex b/lib/uplink/metrics.ex index 464f824c..04a49500 100644 --- a/lib/uplink/metrics.ex +++ b/lib/uplink/metrics.ex @@ -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) diff --git a/test/fixtures/instellar/monitors/list.json b/test/fixtures/instellar/monitors/list.json index e69de29b..4574f6c3 100644 --- a/test/fixtures/instellar/monitors/list.json +++ b/test/fixtures/instellar/monitors/list.json @@ -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" + } + ] +} diff --git a/test/uplink/availability_test.exs b/test/uplink/availability_test.exs index 63a760bc..1f7a1b3d 100644 --- a/test/uplink/availability_test.exs +++ b/test/uplink/availability_test.exs @@ -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!()