Uptime exit test against a real HTTP server

1542c63 · gmorell · 2026-12-02 03:07

3 files +87 -0

Files changed

added test/vigil/uptime_live_server_test.exs
+87 −0
@@ -0,0 +1,87 @@
1 +defmodule Vigil.UptimeLiveServerTest do
2 + @moduledoc """
3 + Slice 8's exit against a real HTTP server on loopback (no stubs): the
4 + check chain sees it go down and come back within one interval each, and
5 + each change alerts exactly once.
6 + """
7 + use Vigil.DataCase, async: false
8 + use Oban.Testing, repo: Vigil.Repo
9 +
10 + import Vigil.AccountsFixtures
11 + import Vigil.OrganizationsFixtures
12 +
13 + alias Vigil.{Alerts, Uptime}
14 + alias Vigil.Alerts.Notification
15 + alias Vigil.Uptime.{CheckWorker, Monitor}
16 +
17 + defmodule Toggle do
18 + @moduledoc false
19 + @behaviour Plug
20 + def init(agent), do: agent
21 +
22 + def call(conn, agent) do
23 + if Agent.get(agent, & &1),
24 + do: Plug.Conn.send_resp(conn, 200, "ok"),
25 + else: Plug.Conn.send_resp(conn, 503, "maintenance")
26 + end
27 + end
28 +
29 + setup do
30 + previous = Application.get_env(:vigil, :uptime_opts)
31 + # Real requests to loopback: allowed for this test only.
32 + Application.put_env(:vigil, :uptime_opts, allow_private: true)
33 + on_exit(fn -> Application.put_env(:vigil, :uptime_opts, previous) end)
34 +
35 + agent = start_supervised!({Agent, fn -> true end})
36 + server = start_supervised!({Bandit, plug: {Toggle, agent}, port: 0, ip: :loopback})
37 + {:ok, {_ip, port}} = ThousandIsland.listener_info(server)
38 +
39 + owner = user_fixture()
40 + org = organization_fixture(owner)
41 + scope = org_scope(owner, org)
42 + project = project_fixture(scope)
43 + {:ok, _rule} = Alerts.create_rule(scope, project, %{name: "Uptime", kind: :uptime})
44 +
45 + %{agent: agent, url: "http://127.0.0.1:#{port}/health", scope: scope, project: project}
46 + end
47 +
48 + # Runs the queued check (as Oban would when its time comes), and returns
49 + # the next one it queued.
50 + defp run_next_check(monitor) do
51 + [job] = all_enqueued(worker: CheckWorker, args: %{monitor_id: monitor.id})
52 + Repo.delete!(job)
53 + assert :ok = perform_job(CheckWorker, job.args)
54 + [next] = all_enqueued(worker: CheckWorker, args: %{monitor_id: monitor.id})
55 + next
56 + end
57 +
58 + defp titles, do: Repo.all(from n in Notification, order_by: n.id, select: n.message["title"])
59 +
60 + test "down and back up, one interval each, one alert per change", ctx do
61 + {:ok, m} =
62 + Uptime.create_monitor(ctx.scope, ctx.project, %{
63 + "name" => "API",
64 + "type" => "get",
65 + "url" => ctx.url,
66 + "interval_seconds" => 30
67 + })
68 +
69 + next = run_next_check(m)
70 + assert %{is_up: true} = Repo.get!(Monitor, m.id)
71 + # The next check is one interval out.
72 + assert DateTime.diff(next.scheduled_at, DateTime.utc_now()) in 25..31
73 + assert titles() == []
74 +
75 + Agent.update(ctx.agent, fn _ -> false end)
76 + run_next_check(m)
77 + assert %{is_up: false, reason: "status"} = Repo.get!(Monitor, m.id)
78 + run_next_check(m)
79 + assert titles() == ["API is down"]
80 +
81 + Agent.update(ctx.agent, fn _ -> true end)
82 + run_next_check(m)
83 + assert %{is_up: true} = Repo.get!(Monitor, m.id)
84 + run_next_check(m)
85 + assert titles() == ["API is down", "API is back up"]
86 + end
87 +end

Parents: e47e04f