Jelajahi Sumber

Add InterfaceTestCase for virtual machines

Jeremy Stretch 6 tahun lalu
induk
melakukan
3e79b9d26a

+ 11 - 4
netbox/utilities/testing/testcases.py

@@ -104,16 +104,23 @@ class StandardTestCases:
             if self.model is None:
                 raise Exception("Test case requires model to be defined")
 
-        def _get_url(self, action, instance=None):
+        def _get_base_url(self):
             """
-            Return the URL name for a specific action. An instance must be specified for
-            get/edit/delete views.
+            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).
             """
-            url_format = '{}:{}_{{}}'.format(
+            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 = self._get_base_url()
+
             if action in ('list', 'add', 'import', 'bulk_edit', 'bulk_delete'):
                 return reverse(url_format.format(action))
 

+ 76 - 1
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)",
+        )