fix tests

This commit is contained in:
Graham McIntire 2026-01-11 14:58:55 -06:00
parent f16b0ebabd
commit 5e1a97fcb3
No known key found for this signature in database
4 changed files with 21 additions and 14 deletions

View file

@ -47,4 +47,5 @@ config :towerops, ToweropsWeb.Endpoint,
config :towerops, config :towerops,
ping_module: Towerops.Monitoring.PingMock, ping_module: Towerops.Monitoring.PingMock,
poller_module: Towerops.Snmp.PollerMock, poller_module: Towerops.Snmp.PollerMock,
snmp_adapter: Towerops.Snmp.SnmpMock snmp_adapter: Towerops.Snmp.SnmpMock,
sql_sandbox: true

View file

@ -83,18 +83,20 @@ defmodule Towerops.Agents do
{1, nil} {1, nil}
""" """
def update_agent_token_heartbeat(agent_token_id, ip_address, metadata \\ %{}) do def update_agent_token_heartbeat(agent_token_id, ip_address, metadata \\ nil) do
now = DateTime.truncate(DateTime.utc_now(), :second) now = DateTime.truncate(DateTime.utc_now(), :second)
# Only update metadata if explicitly provided (not nil and not empty)
updates =
case metadata do
nil -> [last_seen_at: now, last_ip: ip_address]
meta when map_size(meta) > 0 -> [last_seen_at: now, last_ip: ip_address, metadata: meta]
_ -> [last_seen_at: now, last_ip: ip_address]
end
AgentToken AgentToken
|> where([t], t.id == ^agent_token_id) |> where([t], t.id == ^agent_token_id)
|> Repo.update_all( |> Repo.update_all(set: updates)
set: [
last_seen_at: now,
last_ip: ip_address,
metadata: metadata
]
)
end end
@doc """ @doc """

View file

@ -77,11 +77,7 @@ defmodule ToweropsWeb.Api.AgentController do
} }
# Update synchronously in the heartbeat endpoint # Update synchronously in the heartbeat endpoint
fn -> Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata)
Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata)
end
|> Task.async()
|> Task.await()
json(conn, %{status: "ok"}) json(conn, %{status: "ok"})
end end

View file

@ -9,6 +9,7 @@ defmodule ToweropsWeb.Plugs.AgentAuth do
import Phoenix.Controller import Phoenix.Controller
import Plug.Conn import Plug.Conn
alias Ecto.Adapters.SQL.Sandbox
alias Towerops.Agents alias Towerops.Agents
@doc """ @doc """
@ -43,7 +44,14 @@ defmodule ToweropsWeb.Plugs.AgentAuth do
defp update_heartbeat(conn, agent_token) do defp update_heartbeat(conn, agent_token) do
ip = to_string(:inet_parse.ntoa(conn.remote_ip)) ip = to_string(:inet_parse.ntoa(conn.remote_ip))
parent = self()
Task.start(fn -> Task.start(fn ->
# Allow this task to use the test database sandbox
if Application.get_env(:towerops, :sql_sandbox) do
Sandbox.allow(Towerops.Repo, parent, self())
end
Agents.update_agent_token_heartbeat(agent_token.id, ip) Agents.update_agent_token_heartbeat(agent_token.id, ip)
end) end)