From 3e79b9d26aab22f874507ac7ca623bdc1a9cbb32 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 4 Feb 2020 16:40:18 -0500 Subject: [PATCH] Add InterfaceTestCase for virtual machines --- netbox/utilities/testing/testcases.py | 15 +++-- netbox/virtualization/tests/test_views.py | 77 ++++++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/netbox/utilities/testing/testcases.py b/netbox/utilities/testing/testcases.py index b55d914d9..094629676 100644 --- a/netbox/utilities/testing/testcases.py +++ b/netbox/utilities/testing/testcases.py @@ -104,15 +104,22 @@ class StandardTestCases: if self.model is None: raise Exception("Test case requires model to be defined") + def _get_base_url(self): + """ + Return the base format for a URL for the test's model. Override this to test for a model which belongs + to a different app (e.g. testing Interfaces within the virtualization app). + """ + return '{}:{}_{{}}'.format( + self.model._meta.app_label, + self.model._meta.model_name + ) + def _get_url(self, action, instance=None): """ Return the URL name for a specific action. An instance must be specified for get/edit/delete views. """ - url_format = '{}:{}_{{}}'.format( - self.model._meta.app_label, - self.model._meta.model_name - ) + url_format = self._get_base_url() if action in ('list', 'add', 'import', 'bulk_edit', 'bulk_delete'): return reverse(url_format.format(action)) diff --git a/netbox/virtualization/tests/test_views.py b/netbox/virtualization/tests/test_views.py index 77f87c92a..40a3b6c53 100644 --- a/netbox/virtualization/tests/test_views.py +++ b/netbox/virtualization/tests/test_views.py @@ -1,4 +1,8 @@ -from dcim.models import DeviceRole, Platform, Site +from netaddr import EUI + +from dcim.choices import InterfaceModeChoices +from dcim.models import DeviceRole, Interface, Platform, Site +from ipam.models import VLAN from utilities.testing import StandardTestCases from virtualization.choices import * from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine @@ -187,3 +191,74 @@ class VirtualMachineTestCase(StandardTestCases.Views): 'disk': 8000, 'comments': 'New comments', } + + +class InterfaceTestCase(StandardTestCases.Views): + model = Interface + + # Disable inapplicable tests + test_list_objects = None + test_import_objects = None + + # TODO + test_create_object = None + test_bulk_edit_objects = None + + def _get_base_url(self): + # Interface belongs to the DCIM app, so we have to override the base URL + return 'virtualization:interface_{}' + + @classmethod + def setUpTestData(cls): + + site = Site.objects.create(name='Site 1', slug='site-1') + devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1') + clustertype = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') + cluster = Cluster.objects.create(name='Cluster 1', type=clustertype, site=site) + virtualmachines = ( + VirtualMachine(name='Virtual Machine 1', cluster=cluster, role=devicerole), + VirtualMachine(name='Virtual Machine 2', cluster=cluster, role=devicerole), + ) + VirtualMachine.objects.bulk_create(virtualmachines) + + Interface.objects.bulk_create([ + Interface(virtual_machine=virtualmachines[0], name='Interface 1'), + Interface(virtual_machine=virtualmachines[0], name='Interface 2'), + Interface(virtual_machine=virtualmachines[0], name='Interface 3'), + ]) + + vlans = ( + VLAN(vid=1, name='VLAN1', site=site), + VLAN(vid=101, name='VLAN101', site=site), + VLAN(vid=102, name='VLAN102', site=site), + VLAN(vid=103, name='VLAN103', site=site), + ) + VLAN.objects.bulk_create(vlans) + + cls.form_data = { + 'virtual_machine': virtualmachines[1].pk, + 'name': 'Interface X', + 'type': InterfaceTypeChoices.TYPE_VIRTUAL, + 'enabled': False, + 'mgmt_only': False, + 'mac_address': EUI('01-02-03-04-05-06'), + 'mtu': 2000, + 'description': 'New description', + 'mode': InterfaceModeChoices.MODE_TAGGED, + 'untagged_vlan': vlans[0].pk, + 'tagged_vlans': [v.pk for v in vlans[1:4]], + 'tags': 'Alpha,Bravo,Charlie', + + # Extraneous model fields + 'device': None, + 'lag': None, + 'cable': None, + 'connection_status': None, + } + + cls.csv_data = ( + "device,name,type", + "Device 1,Interface 4,1000BASE-T (1GE)", + "Device 1,Interface 5,1000BASE-T (1GE)", + "Device 1,Interface 6,1000BASE-T (1GE)", + )