From 819d9194620899a043269e83e30235ea66c1c598 Mon Sep 17 00:00:00 2001 From: scetron Date: Fri, 21 Mar 2025 12:07:07 -0400 Subject: [PATCH 1/2] update cable term_type + test --- .../adapters/sync_network_data_adapters.py | 7 +++- .../tests/test_sync_network_data_adapters.py | 33 ++++++++++++------- nautobot_device_onboarding/tests/utils.py | 2 +- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py b/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py index da438ab2..2670d830 100644 --- a/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py +++ b/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py @@ -5,6 +5,7 @@ import diffsync from diffsync.enum import DiffSyncModelFlags from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from nautobot.dcim.models import Interface from nautobot.ipam.models import VLAN, VRF, IPAddress @@ -249,9 +250,13 @@ def load_cables(self): Only cables returned by the CommandGetter job should be synced. """ + dcim_interface_content_type = ContentType.objects.get_for_model(Interface) for device in self.job.devices_to_load: for cable in device.get_cables(): - if cable.termination_a.type != "dcim.interface" or cable.termination_b.type != "dcim.interface": + if ( + cable.termination_a_type != dcim_interface_content_type + or cable.termination_b_type != dcim_interface_content_type + ): self.job.logger.warning( f"Skipping Cable: {cable}. Only cables with interface terminations are supported." ) diff --git a/nautobot_device_onboarding/tests/test_sync_network_data_adapters.py b/nautobot_device_onboarding/tests/test_sync_network_data_adapters.py index fe7aed11..4b6db870 100644 --- a/nautobot_device_onboarding/tests/test_sync_network_data_adapters.py +++ b/nautobot_device_onboarding/tests/test_sync_network_data_adapters.py @@ -2,6 +2,7 @@ from unittest.mock import MagicMock, patch +from django.contrib.contenttypes.models import ContentType from nautobot.core.testing import TransactionTestCase from nautobot.dcim.models import Cable, Device, Interface from nautobot.extras.models import JobResult @@ -351,17 +352,27 @@ def test_load_vrfs(self): def test_load_cables(self): """Test loading Nautobot cable data into the diffsync store.""" - self.sync_network_data_adapter.load_cables() - for cable in Cable.objects.all(): - if not cable.termination_a.type == "dcim.interface" or cable.termination_b.type == "dcim.interface": - # skip loading cables connected to circuits (they should be skipped by the adapter) - continue - unique_id = f"dcim__interface__{cable.termination_a.device.name}__{cable.termination_a.name}__dcim__interface__{cable.termination_b.device.name}__{cable.termination_b.name}" - diffsync_obj = self.sync_network_data_adapter.get("cable", unique_id) - self.assertEqual(cable.termination_a.device.name, diffsync_obj.termination_a__device__name) - self.assertEqual(cable.termination_a.name, diffsync_obj.termination_a__name) - self.assertEqual(cable.termination_b.device.name, diffsync_obj.termination_b__device__name) - self.assertEqual(cable.termination_b.name, diffsync_obj.termination_b__name) + dcim_interface_content_type = ContentType.objects.get_for_model(Interface) + + with self.assertLogs(self.job.logger, level="WARNING") as logs: + self.sync_network_data_adapter.load_cables() + for cable in Cable.objects.all(): + if ( + cable.termination_a_type != dcim_interface_content_type + or cable.termination_b_type != dcim_interface_content_type + ): + self.assertIn( + f"WARNING:nautobot_device_onboarding.jobs:Skipping Cable: {cable}. Only cables with interface terminations are supported.", + logs.output[0], + ) + continue + unique_id = f"dcim__interface__{cable.termination_a.device.name}__{cable.termination_a.name}__dcim__interface__{cable.termination_b.device.name}__{cable.termination_b.name}" + diffsync_obj = self.sync_network_data_adapter.get("cable", unique_id) + self.assertEqual(cable.termination_a.device.name, diffsync_obj.termination_a__device__name) + self.assertEqual(cable.termination_a.name, diffsync_obj.termination_a__name) + self.assertEqual(cable.termination_b.device.name, diffsync_obj.termination_b__device__name) + self.assertEqual(cable.termination_b.name, diffsync_obj.termination_b__name) + # self.assertIn(f"WARNING - Skipping Cable: #{cable}. Only cables with interface terminations are supported.", logs.output[0]) def test_load_vrf_to_interface(self): """Test loading Nautobot vrf interface assignments into the Diffsync store.""" diff --git a/nautobot_device_onboarding/tests/utils.py b/nautobot_device_onboarding/tests/utils.py index 97756618..ad94acfa 100644 --- a/nautobot_device_onboarding/tests/utils.py +++ b/nautobot_device_onboarding/tests/utils.py @@ -160,7 +160,7 @@ def sync_network_data_ensure_required_nautobot_objects(): location=location, ) - cable_to_circuit_1 = Cable.objects.get_or_create( + cable_to_circuit_1, _ = Cable.objects.get_or_create( termination_a_type=ContentType.objects.get_for_model(Interface), termination_a_id=interface_4.id, termination_b_type=ContentType.objects.get_for_model(CircuitTermination), From 5dfe2346f99456613244fa13bf602bc12727372d Mon Sep 17 00:00:00 2001 From: scetron Date: Fri, 21 Mar 2025 15:24:02 -0400 Subject: [PATCH 2/2] add fragments --- changes/326.added | 1 + changes/326.fixed | 1 + 2 files changed, 2 insertions(+) create mode 100644 changes/326.added create mode 100644 changes/326.fixed diff --git a/changes/326.added b/changes/326.added new file mode 100644 index 00000000..3bdb1b3c --- /dev/null +++ b/changes/326.added @@ -0,0 +1 @@ +Added testing for cable termination type when adding cables to diffsync store. diff --git a/changes/326.fixed b/changes/326.fixed new file mode 100644 index 00000000..df37cbd5 --- /dev/null +++ b/changes/326.fixed @@ -0,0 +1 @@ +Fixed incorrect call to cable termination type.