Skip to content

Commit

Permalink
chore: ✅ fix adapter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bile0026 committed Dec 19, 2024
1 parent 18092c6 commit fe0ed20
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
1 change: 1 addition & 0 deletions nautobot_ssot/tests/librenms/test_librenms_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def load_json(path):
DEVICE_FIXTURE = load_json("./nautobot_ssot/tests/librenms/fixtures/get_librenms_devices.json")["devices"]
LOCATION_FIXTURE = load_json("./nautobot_ssot/tests/librenms/fixtures/get_librenms_locations.json")["locations"]


class TestLibreNMSAdapterTestCase(TransactionTestCase):
"""Test NautobotSsotLibreNMSAdapter class."""

Expand Down
100 changes: 58 additions & 42 deletions nautobot_ssot/tests/librenms/test_nautobot_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@

from django.contrib.contenttypes.models import ContentType
from nautobot.core.testing import TransactionTestCase
from nautobot.dcim.models import DeviceType, LocationType, Manufacturer, Platform
from nautobot.dcim.models import Device as ORMDevice
from nautobot.dcim.models import DeviceType, LocationType, Manufacturer, Platform
from nautobot.dcim.models import Location as ORMLocation
from nautobot.extras.models import Job, JobResult, Role, Status
from nautobot.extras.models import JobResult, Role, Status

from nautobot_ssot.integrations.librenms.constants import (
librenms_status_map,
os_manufacturer_map,
)
from nautobot_ssot.integrations.librenms.diffsync.adapters.librenms import (
LibrenmsAdapter,
)
from nautobot_ssot.integrations.librenms.diffsync.adapters.nautobot import (
NautobotAdapter,
)
from nautobot_ssot.integrations.librenms.diffsync.models.nautobot import (
Device,
Location,
)
from nautobot_ssot.integrations.librenms.jobs import LibrenmsDataSource


Expand All @@ -33,12 +26,8 @@ def load_json(path):
return json.load(file)


DEVICE_FIXTURE = load_json(
"./nautobot_ssot/tests/librenms/fixtures/get_librenms_devices.json"
)["devices"]
LOCATION_FIXTURE = load_json(
"./nautobot_ssot/tests/librenms/fixtures/get_librenms_locations.json"
)["locations"]
DEVICE_FIXTURE = load_json("./nautobot_ssot/tests/librenms/fixtures/get_librenms_devices.json")["devices"]
LOCATION_FIXTURE = load_json("./nautobot_ssot/tests/librenms/fixtures/get_librenms_locations.json")["locations"]


class TestNautobotAdapterTestCase(TransactionTestCase):
Expand All @@ -48,9 +37,7 @@ class TestNautobotAdapterTestCase(TransactionTestCase):

def setUp(self):
"""Initialize test case and populate the database."""
self.active_status, _ = Status.objects.get_or_create(
name="Active", defaults={"slug": "active"}
)
self.active_status, _ = Status.objects.get_or_create(name="Active")

self.site_type, _ = LocationType.objects.get_or_create(name="Site")
self.site_type.content_types.add(ContentType.objects.get_for_model(ORMDevice))
Expand All @@ -66,21 +53,13 @@ def setUp(self):

for device in DEVICE_FIXTURE:
location = ORMLocation.objects.get(name=device["location"])
_manufacturer, _ = Manufacturer.objects.get_or_create(
name=os_manufacturer_map[device["os"]]
)
_manufacturer, _ = Manufacturer.objects.get_or_create(name=os_manufacturer_map[device["os"]])
_role, _role_created = Role.objects.get_or_create(name=device["type"])
if _role_created:
_role.content_types.add(ContentType.objects.get_for_model(ORMDevice))
_status, _ = Status.objects.get_or_create(
name=librenms_status_map[device["status"]]
)
_device_type, _ = DeviceType.objects.get_or_create(
model=device["hardware"], manufacturer=_manufacturer
)
_platform, _ = Platform.objects.get_or_create(
name=device["os"], manufacturer=_manufacturer
)
_status, _ = Status.objects.get_or_create(name=librenms_status_map[device["status"]])
_device_type, _ = DeviceType.objects.get_or_create(model=device["hardware"], manufacturer=_manufacturer)
_platform, _ = Platform.objects.get_or_create(name=device["os"], manufacturer=_manufacturer)
ORMDevice.objects.create(
name=device["sysName"],
device_type=_device_type,
Expand All @@ -104,24 +83,61 @@ def test_load_devices(self):
"""Test that devices are correctly loaded from the Nautobot ORM."""
self.nautobot_adapter.load()

# Get all devices from the adapter
loaded_devices = {
device.get_unique_id() for device in self.nautobot_adapter.get_all("device")
}
loaded_devices = {device.get_unique_id() for device in self.nautobot_adapter.get_all("device")}

# Expected devices from the fixture
expected_devices = {device["sysName"] for device in DEVICE_FIXTURE}

# Verify that all expected devices were loaded
self.assertEqual(
expected_devices, loaded_devices, "Devices were not loaded correctly."
)
self.assertEqual(expected_devices, loaded_devices, "Devices were not loaded correctly.")

# Verify attributes for each device
for device in DEVICE_FIXTURE:
loaded_device = self.nautobot_adapter.get("device", {"name": device["sysName"]})
print(f"Loaded device: {loaded_device}")
print(f"Loaded device type: {type(loaded_device)}")
self.assertIsNotNone(
loaded_device, f"Device {device['sysName']} not found in the adapter."
self.assertIsNotNone(loaded_device, f"Device {device['sysName']} not found in the adapter.")

def test_load_locations(self):
"""Test that locations are correctly loaded from the Nautobot ORM."""
self.nautobot_adapter.load_location()

loaded_locations = {location.get_unique_id() for location in self.nautobot_adapter.get_all("location")}

expected_locations = {location["location"] for location in LOCATION_FIXTURE}

self.assertEqual(expected_locations, loaded_locations, "Locations were not loaded correctly.")

for location in LOCATION_FIXTURE:
loaded_location = self.nautobot_adapter.get("location", {"name": location["location"]})
self.assertIsNotNone(loaded_location, f"Location {location['location']} not found in the adapter.")

# gps coordinates need to be truncated to 6 decimal places
_latitude = None
_longitude = None
if isinstance(location.get("lng"), float):
_longitude = round(location.get("lng"), 6)
else:
_longitude = location.get("lng")
if isinstance(location.get("lat"), float):
_latitude = round(location.get("lat"), 6)
else:
_latitude = location.get("lat")

self.assertEqual(
loaded_location.latitude,
_latitude,
f"Latitude mismatch for {location['location']}.",
)
self.assertEqual(
loaded_location.longitude,
_longitude,
f"Longitude mismatch for {location['location']}.",
)
self.assertEqual(
loaded_location.status,
"Active",
f"Status mismatch for {location['location']}.",
)
self.assertEqual(
loaded_location.location_type,
"Site",
f"Location type mismatch for {location['location']}.",
)

0 comments on commit fe0ed20

Please sign in to comment.