Update tests, and fix model form

This commit is contained in:
Daniel Sheppard 2022-11-04 16:17:01 -05:00
parent c9afb2895e
commit 51a7e9b783
5 changed files with 148 additions and 26 deletions

View File

@ -1707,7 +1707,8 @@ class VirtualDeviceContextForm(TenancyForm, NetBoxModelForm):
model = VirtualDeviceContext model = VirtualDeviceContext
fields = [ fields = [
'region', 'site_group', 'site', 'location', 'rack', '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 = {} help_texts = {}
widgets = { widgets = {

View File

@ -1485,6 +1485,12 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase
) )
Interface.objects.bulk_create(interfaces) 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 = ( vlans = (
VLAN(name='VLAN 1', vid=1), VLAN(name='VLAN 1', vid=1),
VLAN(name='VLAN 2', vid=2), VLAN(name='VLAN 2', vid=2),
@ -1533,6 +1539,7 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase
}, },
{ {
'device': device.pk, 'device': device.pk,
'vdcs': vdcs[0].pk,
'name': 'Interface 6', 'name': 'Interface 6',
'type': 'virtual', 'type': 'virtual',
'mode': InterfaceModeChoices.MODE_TAGGED, 'mode': InterfaceModeChoices.MODE_TAGGED,
@ -1543,6 +1550,7 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase
}, },
{ {
'device': device.pk, 'device': device.pk,
'vdcs': vdcs[1].pk,
'name': 'Interface 7', 'name': 'Interface 7',
'type': InterfaceTypeChoices.TYPE_80211A, 'type': InterfaceTypeChoices.TYPE_80211A,
'tx_power': 10, 'tx_power': 10,
@ -1551,6 +1559,7 @@ class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase
}, },
{ {
'device': device.pk, 'device': device.pk,
'vdcs': vdcs[1].pk,
'name': 'Interface 8', 'name': 'Interface 8',
'type': InterfaceTypeChoices.TYPE_80211A, 'type': InterfaceTypeChoices.TYPE_80211A,
'tx_power': 10, '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',
}

View File

@ -4253,9 +4253,8 @@ class PowerFeedTestCase(TestCase, ChangeLoggedFilterSetTests):
params = {'connected': False} params = {'connected': False}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
class VirtualDeviceContextBaseTestCase(TestCase, ChangeLoggedFilterSetTests):
queryset = VirtualDeviceContext.objects.all() class VirtualDeviceContextBaseTestCase:
filterset = InterfaceFilterSet
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
@ -4311,46 +4310,65 @@ class VirtualDeviceContextBaseTestCase(TestCase, ChangeLoggedFilterSetTests):
VirtualDeviceContext.objects.bulk_create(vdcs) VirtualDeviceContext.objects.bulk_create(vdcs)
interfaces = ( interfaces = (
Interface(device=devices[0], vdc=vdcs[0], name='Interface 1', type=InterfaceTypeChoices.TYPE_1GE_FIXED), Interface(device=devices[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[0], 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], 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[1], 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], 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[2], 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], 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[0], 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], 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[1], 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], 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[2], name='Interface 12', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
) )
Interface.objects.bulk_create(interfaces) 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): def test_device(self):
params = {'device': ['Device 1', 'Device 2']} 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): def test_status(self):
params = {'status': ['active']} params = {'status': ['active']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3) 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): def test_vdc(self):
params = {'vdc': ['VDC 1']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 9)
devices = Device.objects.first() devices = Device.objects.first()
vdc = VirtualDeviceContext.objects.filter(device=devices) vdc = VirtualDeviceContext.objects.filter(device=devices)
params = {'vdc': ['VDC 1']} params = {'vdc_id': vdc.values_list('pk', flat=True)}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6)
params = {'vdc_id': vdc.values_list('pk')}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
def test_vdc_identifier(self): def test_vdc_identifier(self):
devices = Device.objects.first() devices = Device.objects.first()
vdc = VirtualDeviceContext.objects.filter(device=devices) 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) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
# TODO: Connection filters # TODO: Connection filters

View File

@ -589,6 +589,7 @@ class CableTestCase(TestCase):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
cable.clean() cable.clean()
class CableTestCase(TestCase): class CableTestCase(TestCase):
def setUp(self): def setUp(self):
@ -609,5 +610,8 @@ class CableTestCase(TestCase):
self.vdc2 = VirtualDeviceContext.objects.create(device=self.device, name="VDC 2", identifier=1) 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.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.interface2 = Interface.objects.create(device=self.device2, name='Eth1/2', type='10gbase-t')
self.interface3 = Interface.objects.create(device=self.device2, name='Eth1/3', vdc=self.vdc2, 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])

View File

@ -3078,3 +3078,87 @@ class PowerFeedTestCase(ViewTestCases.PrimaryObjectViewTestCase):
self.assertHttpStatus(response, 200) self.assertHttpStatus(response, 200)
# TODO: VDC Test Cases # 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,
}