From 51a7e9b7835bdfcc633982ce134e9b5186498813 Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Fri, 4 Nov 2022 16:17:01 -0500 Subject: [PATCH] Update tests, and fix model form --- netbox/dcim/forms/model_forms.py | 3 +- netbox/dcim/tests/test_api.py | 17 +++++- netbox/dcim/tests/test_filtersets.py | 62 ++++++++++++-------- netbox/dcim/tests/test_models.py | 8 ++- netbox/dcim/tests/test_views.py | 84 ++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 26 deletions(-) diff --git a/netbox/dcim/forms/model_forms.py b/netbox/dcim/forms/model_forms.py index 60cd88ffa..352e42001 100644 --- a/netbox/dcim/forms/model_forms.py +++ b/netbox/dcim/forms/model_forms.py @@ -1707,7 +1707,8 @@ class VirtualDeviceContextForm(TenancyForm, NetBoxModelForm): model = VirtualDeviceContext fields = [ 'region', 'site_group', 'site', 'location', 'rack', - 'device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tenant_group', 'tenant', 'comments' + 'device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tenant_group', 'tenant', + 'comments', 'tags' ] help_texts = {} widgets = { diff --git a/netbox/dcim/tests/test_api.py b/netbox/dcim/tests/test_api.py index 82b4889d6..bcad62458 100644 --- a/netbox/dcim/tests/test_api.py +++ b/netbox/dcim/tests/test_api.py @@ -1485,6 +1485,12 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase ) Interface.objects.bulk_create(interfaces) + vdcs = ( + VirtualDeviceContext(name='VDC 1', identifier=1, device=device), + VirtualDeviceContext(name='VDC 2', identifier=2, device=device) + ) + VirtualDeviceContext.objects.bulk_create(vdcs) + vlans = ( VLAN(name='VLAN 1', vid=1), VLAN(name='VLAN 2', vid=2), @@ -1533,6 +1539,7 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase }, { 'device': device.pk, + 'vdcs': vdcs[0].pk, 'name': 'Interface 6', 'type': 'virtual', 'mode': InterfaceModeChoices.MODE_TAGGED, @@ -1543,6 +1550,7 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase }, { 'device': device.pk, + 'vdcs': vdcs[1].pk, 'name': 'Interface 7', 'type': InterfaceTypeChoices.TYPE_80211A, 'tx_power': 10, @@ -1551,6 +1559,7 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase }, { 'device': device.pk, + 'vdcs': vdcs[1].pk, 'name': 'Interface 8', 'type': InterfaceTypeChoices.TYPE_80211A, 'tx_power': 10, @@ -2164,4 +2173,10 @@ class PowerFeedTest(APIViewTestCases.APIViewTestCase): }, ] -# TODO: VDC Test Cases + +class VirtualDeviceContextTest(APIViewTestCases.APIViewTestCase): + model = VirtualDeviceContext + brief_fields = ['display', 'id', 'name', 'url', 'identifier', 'device'] + bulk_update_data = { + 'status': 'planned', + } diff --git a/netbox/dcim/tests/test_filtersets.py b/netbox/dcim/tests/test_filtersets.py index 1537fe538..9c00239a5 100644 --- a/netbox/dcim/tests/test_filtersets.py +++ b/netbox/dcim/tests/test_filtersets.py @@ -4253,9 +4253,8 @@ class PowerFeedTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'connected': False} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) -class VirtualDeviceContextBaseTestCase(TestCase, ChangeLoggedFilterSetTests): - queryset = VirtualDeviceContext.objects.all() - filterset = InterfaceFilterSet + +class VirtualDeviceContextBaseTestCase: @classmethod def setUpTestData(cls): @@ -4311,46 +4310,65 @@ class VirtualDeviceContextBaseTestCase(TestCase, ChangeLoggedFilterSetTests): VirtualDeviceContext.objects.bulk_create(vdcs) interfaces = ( - Interface(device=devices[0], vdc=vdcs[0], name='Interface 1', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[0], vdc=vdcs[1], name='Interface 2', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[1], vdc=vdcs[2], name='Interface 3', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[1], vdc=vdcs[3], name='Interface 4', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[2], vdc=vdcs[4], name='Interface 5', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[2], vdc=vdcs[5], name='Interface 6', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[0], vdc=vdcs[0], name='Interface 7', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[0], vdc=vdcs[1], name='Interface 8', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[1], vdc=vdcs[2], name='Interface 9', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[1], vdc=vdcs[3], name='Interface 10', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[2], vdc=vdcs[4], name='Interface 11', type=InterfaceTypeChoices.TYPE_1GE_FIXED), - Interface(device=devices[2], vdc=vdcs[5], name='Interface 12', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], name='Interface 1', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], name='Interface 2', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], name='Interface 3', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], name='Interface 4', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], name='Interface 5', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], name='Interface 6', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], name='Interface 7', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], name='Interface 8', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], name='Interface 9', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], name='Interface 10', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], name='Interface 11', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], name='Interface 12', type=InterfaceTypeChoices.TYPE_1GE_FIXED), ) Interface.objects.bulk_create(interfaces) + interfaces[0].vdcs.set([vdcs[0], vdcs[1]]) + interfaces[1].vdcs.set([vdcs[0], vdcs[1]]) + interfaces[2].vdcs.set([vdcs[2], vdcs[3]]) + interfaces[3].vdcs.set([vdcs[2], vdcs[3]]) + interfaces[4].vdcs.set([vdcs[4], vdcs[5]]) + interfaces[5].vdcs.set([vdcs[4], vdcs[5]]) + interfaces[6].vdcs.set([vdcs[0]]) + interfaces[7].vdcs.set([vdcs[1]]) + interfaces[8].vdcs.set([vdcs[2]]) + interfaces[9].vdcs.set([vdcs[3]]) + interfaces[10].vdcs.set([vdcs[4]]) + interfaces[11].vdcs.set([vdcs[5]]) -class VirtualDeviceContextTestCase(VirtualDeviceContextBaseTestCase): + +class VirtualDeviceContextTestCase(VirtualDeviceContextBaseTestCase, TestCase, ChangeLoggedFilterSetTests): + queryset = VirtualDeviceContext.objects.all() + filterset = VirtualDeviceContextFilterSet def test_device(self): params = {'device': ['Device 1', 'Device 2']} - self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6) def test_status(self): params = {'status': ['active']} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3) -class VirtualDeviceContextInterfaceTestCase(VirtualDeviceContextBaseTestCase): +class VirtualDeviceContextInterfaceTestCase(VirtualDeviceContextBaseTestCase, TestCase, ChangeLoggedFilterSetTests): + queryset = Interface.objects.all() + filterset = InterfaceFilterSet + def test_vdc(self): + params = {'vdc': ['VDC 1']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 9) + devices = Device.objects.first() vdc = VirtualDeviceContext.objects.filter(device=devices) - params = {'vdc': ['VDC 1']} - self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6) - params = {'vdc_id': vdc.values_list('pk')} + params = {'vdc_id': vdc.values_list('pk', flat=True)} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) def test_vdc_identifier(self): devices = Device.objects.first() vdc = VirtualDeviceContext.objects.filter(device=devices) - params = {'vdc_identifier': vdc.values_list('identifier')} + params = {'vdc_identifier': vdc.values_list('identifier', flat=True)} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) # TODO: Connection filters diff --git a/netbox/dcim/tests/test_models.py b/netbox/dcim/tests/test_models.py index 646738977..3a18b6cc8 100644 --- a/netbox/dcim/tests/test_models.py +++ b/netbox/dcim/tests/test_models.py @@ -589,6 +589,7 @@ class CableTestCase(TestCase): with self.assertRaises(ValidationError): cable.clean() + class CableTestCase(TestCase): def setUp(self): @@ -609,5 +610,8 @@ class CableTestCase(TestCase): self.vdc2 = VirtualDeviceContext.objects.create(device=self.device, name="VDC 2", identifier=1) self.interface1 = Interface.objects.create(device=self.device1, name='Eth1/1', type='10gbase-t') - self.interface2 = Interface.objects.create(device=self.device2, name='Eth1/2', vdc=self.vdc1, type='10gbase-t') - self.interface3 = Interface.objects.create(device=self.device2, name='Eth1/3', vdc=self.vdc2, type='10gbase-t') + self.interface2 = Interface.objects.create(device=self.device2, name='Eth1/2', type='10gbase-t') + self.interface3 = Interface.objects.create(device=self.device2, name='Eth1/3', type='10gbase-t') + + self.interface2.vdcs.set([self.vdc1]) + self.interface3.vdcs.set([self.vdc2]) diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index 18f1c16e1..90f4b2582 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -3078,3 +3078,87 @@ class PowerFeedTestCase(ViewTestCases.PrimaryObjectViewTestCase): self.assertHttpStatus(response, 200) # TODO: VDC Test Cases + + +class VirtualDeviceContextTestCase(ViewTestCases.PrimaryObjectViewTestCase): + model = VirtualDeviceContext + + @classmethod + def setUpTestData(cls): + + sites = ( + Site(name='Site 1', slug='site-1'), + Site(name='Site 2', slug='site-2'), + ) + Site.objects.bulk_create(sites) + + location = Location(site=sites[0], name='Location 1', slug='location-1') + location.save() + + racks = ( + Rack(name='Rack 1', site=sites[0], location=location), + Rack(name='Rack 2', site=sites[1]), + ) + Rack.objects.bulk_create(racks) + + manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1') + + devicetypes = ( + DeviceType(model='Device Type 1', slug='device-type-1', manufacturer=manufacturer), + DeviceType(model='Device Type 2', slug='device-type-2', manufacturer=manufacturer), + ) + DeviceType.objects.bulk_create(devicetypes) + + deviceroles = ( + DeviceRole(name='Device Role 1', slug='device-role-1'), + DeviceRole(name='Device Role 2', slug='device-role-2'), + ) + DeviceRole.objects.bulk_create(deviceroles) + + platforms = ( + Platform(name='Platform 1', slug='platform-1'), + Platform(name='Platform 2', slug='platform-2'), + ) + Platform.objects.bulk_create(platforms) + + devices = ( + Device(name='Device 1', site=sites[0], rack=racks[0], device_type=devicetypes[0], device_role=deviceroles[0], platform=platforms[0]), + ) + Device.objects.bulk_create(devices) + + vdcs = ( + VirtualDeviceContext(name='VDC 1', identifier=1, device=devices[0], status='active'), + VirtualDeviceContext(name='VDC 2', identifier=2, device=devices[0], status='active'), + VirtualDeviceContext(name='VDC 3', identifier=3, device=devices[0], status='active'), + ) + VirtualDeviceContext.objects.bulk_create(vdcs) + + tags = create_tags('Alpha', 'Bravo', 'Charlie') + + cls.form_data = { + 'device': devices[0].pk, + 'status': 'active', + 'name': 'VDC 4', + 'identifier': 4, + 'primary_ip4': None, + 'primary_ip6': None, + 'tags': [t.pk for t in tags], + } + + cls.csv_data = ( + "device,status,name,identifier", + "Device 1,active,VDC 5,5", + "Device 1,active,VDC 6,6", + "Device 1,active,VDC 7,7", + ) + + cls.csv_update_data = ( + "id,status", + f"{vdcs[0].pk},{VirtualDeviceContextStatusChoices.STATUS_PLANNED}", + f"{vdcs[1].pk},{VirtualDeviceContextStatusChoices.STATUS_PLANNED}", + f"{vdcs[2].pk},{VirtualDeviceContextStatusChoices.STATUS_PLANNED}", + ) + + cls.bulk_edit_data = { + 'status': VirtualDeviceContextStatusChoices.STATUS_OFFLINE, + }