141 lines
4.3 KiB
Elixir
141 lines
4.3 KiB
Elixir
defmodule Towerops.MonitoringTest do
|
|
use Towerops.DataCase
|
|
|
|
alias Towerops.Monitoring
|
|
|
|
describe "monitoring_checks" do
|
|
import Towerops.AccountsFixtures
|
|
|
|
alias Towerops.Monitoring.Check
|
|
|
|
setup do
|
|
user = user_fixture()
|
|
{:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)
|
|
|
|
{:ok, site} =
|
|
Towerops.Sites.create_site(%{
|
|
name: "Test Site",
|
|
organization_id: organization.id
|
|
})
|
|
|
|
{:ok, equipment} =
|
|
Towerops.Equipment.create_equipment(%{
|
|
name: "Router 1",
|
|
ip_address: "192.168.1.1",
|
|
site_id: site.id
|
|
})
|
|
|
|
%{equipment: equipment}
|
|
end
|
|
|
|
@valid_attrs %{
|
|
status: :success,
|
|
response_time_ms: 50,
|
|
checked_at: ~U[2025-12-21 12:00:00Z]
|
|
}
|
|
|
|
test "create_check/1 with valid data creates a check", %{equipment: equipment} do
|
|
attrs = Map.put(@valid_attrs, :equipment_id, equipment.id)
|
|
assert {:ok, %Check{} = check} = Monitoring.create_check(attrs)
|
|
assert check.status == :success
|
|
assert check.response_time_ms == 50
|
|
end
|
|
|
|
test "create_check/1 with invalid data returns error changeset" do
|
|
assert {:error, %Ecto.Changeset{}} = Monitoring.create_check(%{})
|
|
end
|
|
|
|
test "list_equipment_checks/2 returns checks for equipment", %{equipment: equipment} do
|
|
attrs = Map.put(@valid_attrs, :equipment_id, equipment.id)
|
|
{:ok, check} = Monitoring.create_check(attrs)
|
|
assert Monitoring.list_equipment_checks(equipment.id) == [check]
|
|
end
|
|
|
|
test "list_equipment_checks/2 limits results", %{equipment: equipment} do
|
|
# Create 10 checks
|
|
for i <- 1..10 do
|
|
attrs =
|
|
@valid_attrs
|
|
|> Map.put(:equipment_id, equipment.id)
|
|
|> Map.put(:checked_at, DateTime.add(~U[2025-12-21 12:00:00Z], i, :second))
|
|
|
|
{:ok, _check} = Monitoring.create_check(attrs)
|
|
end
|
|
|
|
assert length(Monitoring.list_equipment_checks(equipment.id, 5)) == 5
|
|
end
|
|
|
|
test "get_latest_check/1 returns most recent check", %{equipment: equipment} do
|
|
attrs1 =
|
|
@valid_attrs
|
|
|> Map.put(:equipment_id, equipment.id)
|
|
|> Map.put(:checked_at, ~U[2025-12-21 12:00:00Z])
|
|
|
|
attrs2 =
|
|
@valid_attrs
|
|
|> Map.put(:equipment_id, equipment.id)
|
|
|> Map.put(:checked_at, ~U[2025-12-21 13:00:00Z])
|
|
|
|
{:ok, _check1} = Monitoring.create_check(attrs1)
|
|
{:ok, check2} = Monitoring.create_check(attrs2)
|
|
|
|
latest = Monitoring.get_latest_check(equipment.id)
|
|
assert latest.id == check2.id
|
|
end
|
|
|
|
test "get_latest_check/1 returns nil when no checks exist", %{equipment: equipment} do
|
|
assert Monitoring.get_latest_check(equipment.id) == nil
|
|
end
|
|
|
|
test "delete_old_checks/1 deletes checks older than date", %{equipment: equipment} do
|
|
old_attrs =
|
|
@valid_attrs
|
|
|> Map.put(:equipment_id, equipment.id)
|
|
|> Map.put(:checked_at, ~U[2025-01-01 12:00:00Z])
|
|
|
|
new_attrs =
|
|
@valid_attrs
|
|
|> Map.put(:equipment_id, equipment.id)
|
|
|> Map.put(:checked_at, ~U[2025-12-21 12:00:00Z])
|
|
|
|
{:ok, _old_check} = Monitoring.create_check(old_attrs)
|
|
{:ok, _new_check} = Monitoring.create_check(new_attrs)
|
|
|
|
{deleted, _} = Monitoring.delete_old_checks(~U[2025-06-01 00:00:00Z])
|
|
assert deleted == 1
|
|
assert length(Monitoring.list_equipment_checks(equipment.id)) == 1
|
|
end
|
|
end
|
|
|
|
describe "ping" do
|
|
alias Towerops.Monitoring.Ping
|
|
|
|
test "ping/1 returns ok tuple with response time for localhost" do
|
|
# Test with localhost which should always be reachable
|
|
case Ping.ping("127.0.0.1", 2000) do
|
|
{:ok, response_time} ->
|
|
assert is_integer(response_time)
|
|
assert response_time >= 0
|
|
|
|
{:error, _reason} ->
|
|
# Ping might fail in some test environments
|
|
assert true
|
|
end
|
|
end
|
|
|
|
test "ping/1 returns error tuple for unreachable host" do
|
|
# Use a non-routable IP
|
|
case Ping.ping("192.0.2.1", 1000) do
|
|
{:error, _reason} -> assert true
|
|
{:ok, _} -> assert true
|
|
end
|
|
end
|
|
|
|
test "ping/1 with custom timeout" do
|
|
result = Ping.ping("127.0.0.1", 5000)
|
|
# Result should be either {:ok, time} or {:error, reason}
|
|
assert is_tuple(result)
|
|
assert elem(result, 0) in [:ok, :error]
|
|
end
|
|
end
|
|
end
|