|
|
@@ -23,11 +23,15 @@ from core.models import DataFile, DataSource, ObjectType
|
|
|
from dcim.api.serializers import DeviceSerializer, InterfaceSerializer, RackSerializer, SiteSerializer
|
|
|
from dcim.api.views import DeviceTypeViewSet, SiteViewSet
|
|
|
from dcim.models import (
|
|
|
+ Cable,
|
|
|
+ ConsolePort,
|
|
|
+ ConsoleServerPort,
|
|
|
Device,
|
|
|
DeviceRole,
|
|
|
DeviceType,
|
|
|
Interface,
|
|
|
InterfaceTemplate,
|
|
|
+ Location,
|
|
|
Manufacturer,
|
|
|
Rack,
|
|
|
Region,
|
|
|
@@ -45,8 +49,11 @@ from netbox.api.viewsets.mixins import BulkUpdateModelMixin
|
|
|
from users.constants import TOKEN_PREFIX
|
|
|
from users.models import ObjectPermission, Token, User
|
|
|
from utilities.api import get_serializer_for_model
|
|
|
-from utilities.testing import APITestCase, create_test_device
|
|
|
+from utilities.testing import APITestCase, create_test_device, create_test_virtualmachine
|
|
|
+from virtualization.models import VMInterface
|
|
|
from vpn.api.serializers import L2VPNSerializer
|
|
|
+from vpn.choices import L2VPNTypeChoices
|
|
|
+from vpn.models import L2VPN, L2VPNTermination
|
|
|
|
|
|
|
|
|
class AppTestCase(APITestCase):
|
|
|
@@ -963,6 +970,72 @@ class IncludeFieldsTestCase(APITestCase):
|
|
|
count_queries(sites[1])
|
|
|
self.assertEqual(count_queries(sites[0]), count_queries(sites[1]))
|
|
|
|
|
|
+ def test_include_query_count_with_viewset_lookups(self):
|
|
|
+ """
|
|
|
+ Included objects load the lookups of their own viewset's queryset, so they add no more queries per object than
|
|
|
+ on their own endpoint.
|
|
|
+ """
|
|
|
+ self.add_permissions(
|
|
|
+ 'dcim.view_device', 'dcim.view_consoleport', 'virtualization.view_virtualmachine',
|
|
|
+ 'virtualization.view_vminterface',
|
|
|
+ )
|
|
|
+ peer_device = create_test_device('Peer Device', site=self.devices[0].site)
|
|
|
+ l2vpn = L2VPN.objects.create(name='L2VPN 1', slug='l2vpn-1', type=L2VPNTypeChoices.TYPE_VXLAN)
|
|
|
+ console_ports, virtual_machines, vm_interfaces = [], [], []
|
|
|
+ for i in range(len(self.devices)):
|
|
|
+ console_ports.append(ConsolePort.objects.create(device=self.devices[i], name='Console'))
|
|
|
+ peer = ConsoleServerPort.objects.create(device=peer_device, name=f'Console Server {i}')
|
|
|
+ Cable(a_terminations=[console_ports[i]], b_terminations=[peer]).save()
|
|
|
+ virtual_machines.append(create_test_virtualmachine(f'Virtual Machine {i}'))
|
|
|
+ vm_interfaces.append(VMInterface.objects.create(virtual_machine=virtual_machines[i], name='eth0'))
|
|
|
+ L2VPNTermination.objects.create(l2vpn=l2vpn, assigned_object=vm_interfaces[i])
|
|
|
+
|
|
|
+ def get_added_queries(url, objects):
|
|
|
+ """Return the queries listing all the objects adds to listing the first."""
|
|
|
+ counts = []
|
|
|
+ for objs in (objects, objects[:1], objects):
|
|
|
+ with CaptureQueriesContext(connection) as queries:
|
|
|
+ data = self.get(url + ''.join(f'&id={obj.pk}' for obj in objs))
|
|
|
+ self.assertEqual(len(data['results']), len(objs))
|
|
|
+ # Token authentication refreshes last_used once a minute
|
|
|
+ counts.append(sum('"users_token"' not in query['sql'] for query in queries.captured_queries))
|
|
|
+ # The first request warms per-process caches (e.g. ContentType)
|
|
|
+ return counts[2] - counts[1]
|
|
|
+
|
|
|
+ devices_url = f'{reverse("dcim-api:device-list")}?include='
|
|
|
+ console_ports_url = f'{reverse("dcim-api:consoleport-list")}?fields=id,'
|
|
|
+ vms_url = f'{reverse("virtualization-api:virtualmachine-list")}?include='
|
|
|
+ vm_interfaces_url = f'{reverse("virtualization-api:vminterface-list")}?fields=id,'
|
|
|
+ for url, objects, own_url, own_objects in (
|
|
|
+ (f'{devices_url}console_ports.link_peers', self.devices, f'{console_ports_url}link_peers', console_ports),
|
|
|
+ (
|
|
|
+ f'{devices_url}console_ports.connected_endpoints', self.devices,
|
|
|
+ f'{console_ports_url}connected_endpoints', console_ports,
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ f'{vms_url}interfaces.l2vpn_termination', virtual_machines,
|
|
|
+ f'{vm_interfaces_url}l2vpn_termination', vm_interfaces,
|
|
|
+ ),
|
|
|
+ ):
|
|
|
+ with self.subTest(url=url):
|
|
|
+ self.assertEqual(get_added_queries(url, objects), get_added_queries(own_url, own_objects))
|
|
|
+ self.assertEqual(get_added_queries(f'{devices_url}console_ports.link_peers', self.devices), 0)
|
|
|
+
|
|
|
+ def test_included_annotations(self):
|
|
|
+ """
|
|
|
+ Included objects carry the annotations of their own viewset's queryset.
|
|
|
+ """
|
|
|
+ self.add_permissions('dcim.view_site', 'dcim.view_location')
|
|
|
+ site = self.devices[0].site
|
|
|
+ location = Location.objects.create(site=site, name='Location 1', slug='location-1')
|
|
|
+ Rack.objects.create(site=site, location=location, name='Rack 1')
|
|
|
+ Device.objects.filter(pk=self.devices[0].pk).update(location=location)
|
|
|
+
|
|
|
+ url = reverse('dcim-api:site-detail', kwargs={'pk': site.pk})
|
|
|
+ data = self.get(f'{url}?include=locations.device_count')
|
|
|
+ self.assertEqual(data['locations'][0]['rack_count'], 1)
|
|
|
+ self.assertEqual(data['locations'][0]['device_count'], 1)
|
|
|
+
|
|
|
def test_invalid_paths_without_permission(self):
|
|
|
# Permission is checked before the paths are validated
|
|
|
self.assertHttpStatus(self.client.get(f'{self.url}?include=foo', **self.header), status.HTTP_403_FORBIDDEN)
|
|
|
@@ -1013,11 +1086,11 @@ class IncludeFieldsTestCase(APITestCase):
|
|
|
# Unrecognized top-level names to omit are ignored, as before
|
|
|
self.assertIn('name', self.get(f'{self.url}?omit=foo'))
|
|
|
|
|
|
- # Values which the viewset annotates aren't available on nested objects
|
|
|
+ # Values which the viewset annotates only on its own responses aren't available on nested objects
|
|
|
self.add_permissions('dcim.view_site')
|
|
|
url = reverse('dcim-api:site-list')
|
|
|
- data = self.get(f'{url}?include=locations.device_count', status_code=status.HTTP_400_BAD_REQUEST)
|
|
|
- self.assertTrue(data['include'][0].startswith('locations.device_count: '), data)
|
|
|
+ data = self.get(f'{url}?include=racks.device_count', status_code=status.HTTP_400_BAD_REQUEST)
|
|
|
+ self.assertTrue(data['include'][0].startswith('racks.device_count: '), data)
|
|
|
|
|
|
# A viewset which overrides initial() validates the paths too
|
|
|
url = reverse('core-api:objecttype-list')
|