towerops/test/towerops_web/channels/agent_channel_cloud_poller_test.exs

82 lines
2.6 KiB
Elixir

defmodule ToweropsWeb.AgentChannelCloudPollerTest do
@moduledoc """
Drives the `is_cloud_poller` join branch + latency_probe_jobs subscribe
side-effect. The default AgentChannel test connects with a regular
(non-cloud) agent token; this file specifically tests the cloud-poller
path so the `Phoenix.PubSub.subscribe(... :latency_probe)` line + downstream
handler executes with cloud_poller=true.
"""
use Towerops.DataCase, async: false
import Phoenix.ChannelTest
alias Towerops.AccountsFixtures
alias Towerops.Agent.AgentJobList
alias Towerops.Agents
alias Towerops.OrganizationsFixtures
alias ToweropsWeb.AgentSocket
@endpoint ToweropsWeb.Endpoint
setup do
user = AccountsFixtures.user_fixture()
organization = OrganizationsFixtures.organization_fixture(user.id)
{:ok, agent_token, token_string} = Agents.create_cloud_poller("Test Cloud Poller")
{:ok, socket} = connect(AgentSocket, %{})
{:ok, _, socket} =
subscribe_and_join(socket, "agent:#{agent_token.id}", %{"token" => token_string})
assert_push("jobs", _initial_jobs, 1_000)
%{
socket: socket,
agent_token: agent_token,
token_string: token_string,
organization: organization,
user: user
}
end
describe "cloud poller join + latency_probe subscription" do
test "join with a cloud-poller token sets the cloud poller branch", %{
agent_token: agent_token,
organization: organization
} do
# Cloud-poller tokens are global (no organization_id), so we use the
# test's user-owned organization for the test device. The setup itself
# proves the join path subscribed to the latency_probe PubSub topic;
# we confirm by broadcasting on it and watching for a "jobs" push.
assert agent_token.is_cloud_poller
{:ok, site} =
Towerops.Sites.create_site(%{
name: "Cloud Site",
organization_id: organization.id
})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "Probe Device",
ip_address: "10.66.0.1",
site_id: site.id,
organization_id: organization.id
})
Phoenix.PubSub.broadcast(
Towerops.PubSub,
"agent:#{agent_token.id}:latency_probe",
{:latency_probe_jobs, [device]}
)
assert_push("jobs", %{binary: jobs_binary}, 2_000)
{:ok, decoded} = Base.decode64(jobs_binary)
{:ok, job_list} = AgentJobList.decode(decoded)
ping_jobs = Enum.filter(job_list.jobs, &(&1.job_type == :PING))
assert length(ping_jobs) == 5
end
end
end