Skip to content

Commit

Permalink
Add test case for monitors task
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Oct 29, 2024
1 parent 4d0f688 commit 43f058c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/uplink/monitors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Uplink.Monitors do
end
end

def run(_options) do
def run(_options \\ []) do
Cache.put_new({:monitors, :metrics}, [])

Instellar.list_monitors()
Expand Down
23 changes: 23 additions & 0 deletions lib/uplink/monitors/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Uplink.Monitors.Router do
use Plug.Router
use Uplink.Web

alias Uplink.Secret

plug :match

plug Plug.Parsers,
parsers: [:urlencoded, :json],
body_reader: {Uplink.Web.CacheBodyReader, :read_body, []},
json_decoder: Jason

plug Secret.VerificationPlug

plug :dispatch

post "/" do
Uplink.Monitors.run()

json(conn, :ok, %{message: "monitors updated."})
end
end
2 changes: 2 additions & 0 deletions lib/uplink/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Uplink.Router do
alias Uplink.Components
alias Uplink.Installations
alias Uplink.Cache
alias Uplink.Monitors

alias Uplink.Packages.{
Instance,
Expand All @@ -23,6 +24,7 @@ defmodule Uplink.Router do
forward "/instances", to: Instance.Router
forward "/components", to: Components.Router
forward "/cache", to: Cache.Router
forward "/monitors", to: Monitors.Router

match _ do
send_resp(conn, 404, "not found")
Expand Down
47 changes: 47 additions & 0 deletions test/uplink/monitors_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Uplink.MonitorsTest do
use ExUnit.Case

@monitors_response %{
"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"
}
],
"included" => [],
"links" => %{"self" => "http://localhost:4000/uplink/self/monitors"}
}

setup do
bypass = Bypass.open()

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

{:ok, bypass: bypass}
end

test "run", %{bypass: bypass} do
Bypass.expect_once(bypass, "GET", "/uplink/self/monitors", fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.send_resp(200, Jason.encode!(@monitors_response))
end)

assert :ok == Uplink.Monitors.run()
end
end

0 comments on commit 43f058c

Please sign in to comment.