towerops/test/support/fixtures/snmp_fixtures.ex
Graham McIntire 53f7a31c2d
test: add comprehensive VISP sync tests and Gaiia/SNMP fixtures
- Created comprehensive VISP sync test suite (10 tests, all passing)
  - Tests Decimal-based MRR calculations (float, integer, nil handling)
  - Tests API error handling (401, 403, 500, connection errors)
  - Tests sync status and message updates
  - Validates financial correctness after float→Decimal refactor

- Added GaiiaFixtures module for test support
  - gaiia_integration_fixture/2
  - gaiia_account_fixture/3
  - gaiia_inventory_item_fixture/2
  - gaiia_network_site_fixture/3

- Extended SnmpFixtures with missing helpers
  - arp_entry_fixture/2 for ARP table tests
  - wireless_client_fixture/3 wrapper for positional args

- Extended DevicesFixtures and OrganizationsFixtures
  - device_fixture/3 wrapper (org_id, site_id, attrs)
  - site_fixture/2 for creating test sites

Note: subscriber_matching_test.exs created but has 10 failing tests
(testing private functions, missing Repo.query_count/1). Will address
separately if needed.
2026-03-10 12:36:07 -05:00

182 lines
5.6 KiB
Elixir

defmodule Towerops.SnmpFixtures do
@moduledoc """
This module defines test helpers for creating SNMP entities.
"""
alias Towerops.DevicesFixtures
alias Towerops.Organizations
alias Towerops.Snmp
@doc """
Generate an SNMP device with all required fields.
## Options
- `:device` - Existing device to attach SNMP device to
- `:device_id` - ID of device to attach SNMP device to
- All other SNMP Device fields (sys_name, sys_descr, etc.)
## Examples
iex> snmp_device_fixture()
%Snmp.Device{sys_name: "device", ...}
iex> snmp_device_fixture(%{sys_name: "router1"})
%Snmp.Device{sys_name: "router1", ...}
"""
def snmp_device_fixture(attrs \\ %{}) do
# Handle device creation or lookup
device =
cond do
attrs[:device] -> attrs[:device]
attrs[:device_id] -> Towerops.Devices.get_device!(attrs[:device_id])
true -> DevicesFixtures.device_fixture()
end
# Default SNMP device attributes
default_attrs = %{
device_id: device.id,
sys_name: "device-#{System.unique_integer([:positive])}",
sys_descr: "Test Device",
sys_object_id: "1.3.6.1.4.1.14988.1",
manufacturer: "Test Manufacturer",
model: "Test Model"
}
# Merge with provided attrs
merged_attrs =
Map.merge(
default_attrs,
attrs
|> Map.drop([:device, "device"])
|> Map.new(fn {k, v} -> {to_atom_key(k), v} end)
)
{:ok, snmp_device} =
%Snmp.Device{}
|> Snmp.Device.changeset(merged_attrs)
|> Towerops.Repo.insert()
snmp_device
end
@doc """
Generate a wireless client with all required fields.
## Options
- `:device` - Existing device to attach client to
- `:device_id` - ID of device to attach client to
- `:organization` - Organization for the client
- `:organization_id` - ID of organization for the client
- All other WirelessClient fields (mac_address, signal_strength, etc.)
## Examples
iex> wireless_client_fixture()
%WirelessClient{mac_address: "AA:BB:CC:DD:EE:FF", ...}
iex> wireless_client_fixture(%{signal_strength: -85})
%WirelessClient{signal_strength: -85, ...}
"""
def wireless_client_fixture(attrs \\ %{}) do
# Handle device creation or lookup
device = attrs[:device] || create_device_with_organization(attrs)
organization = device.organization || Organizations.get_organization!(device.organization_id)
# Generate unique MAC address
mac_suffix = 16_777_215 |> :rand.uniform() |> Integer.to_string(16) |> String.pad_leading(6, "0")
mac_parts = mac_suffix |> String.graphemes() |> Enum.chunk_every(2) |> Enum.map(&Enum.join/1)
default_mac = "AA:BB:CC:#{Enum.join(mac_parts, ":")}"
# Default wireless client attributes
default_attrs = %{
device_id: device.id,
organization_id: organization.id,
mac_address: default_mac,
ip_address: "10.0.#{:rand.uniform(254)}.#{:rand.uniform(254)}",
hostname: "client-#{System.unique_integer([:positive])}",
signal_strength: -65,
snr: 25,
distance: 1000,
tx_rate: 100_000,
rx_rate: 50_000,
uptime_seconds: 3600,
last_seen_at: DateTime.truncate(DateTime.utc_now(), :second),
metadata: %{}
}
# Merge with provided attrs, converting string keys if needed
merged_attrs =
Map.merge(
default_attrs,
attrs
|> Map.drop([:device, :organization, "device", "organization"])
|> Map.new(fn {k, v} -> {to_atom_key(k), v} end)
)
{:ok, wireless_client} =
%Snmp.WirelessClient{}
|> Snmp.WirelessClient.changeset(merged_attrs)
|> Towerops.Repo.insert()
wireless_client
end
defp to_atom_key(key) when is_atom(key), do: key
defp to_atom_key(key) when is_binary(key), do: String.to_existing_atom(key)
@doc """
Generate an ARP entry with all required fields.
## Options
- `:device_id` - ID of device for ARP entry
- All other ARPEntry fields (mac_address, ip_address, etc.)
"""
def arp_entry_fixture(device_id, attrs \\ %{}) when is_binary(device_id) do
# Generate unique MAC and IP if not provided
mac_suffix = 16_777_215 |> :rand.uniform() |> Integer.to_string(16) |> String.pad_leading(6, "0")
mac_parts = mac_suffix |> String.graphemes() |> Enum.chunk_every(2) |> Enum.map(&Enum.join/1)
default_mac = "AA:BB:CC:#{Enum.join(mac_parts, ":")}"
default_attrs = %{
device_id: device_id,
mac_address: default_mac,
ip_address: "10.0.#{:rand.uniform(254)}.#{:rand.uniform(254)}",
interface: "ether1",
last_seen_at: DateTime.truncate(DateTime.utc_now(), :second)
}
merged_attrs =
Map.merge(
default_attrs,
Map.new(attrs, fn {k, v} -> {to_atom_key(k), v} end)
)
{:ok, arp_entry} =
%Snmp.ArpEntry{}
|> Snmp.ArpEntry.changeset(merged_attrs)
|> Towerops.Repo.insert()
arp_entry
end
@doc """
Wrapper for wireless_client_fixture with positional arguments for device_id and org_id.
"""
def wireless_client_fixture(device_id, organization_id, attrs)
when is_binary(device_id) and is_binary(organization_id) do
wireless_client_fixture(Map.merge(attrs, %{device_id: device_id, organization_id: organization_id}))
end
defp create_device_with_organization(attrs) do
case {attrs[:device_id], attrs[:organization_id]} do
{device_id, _} when not is_nil(device_id) ->
Towerops.Devices.get_device!(device_id)
{_, organization_id} when not is_nil(organization_id) ->
DevicesFixtures.device_fixture(%{organization_id: organization_id})
_ ->
DevicesFixtures.device_fixture()
end
end
end