mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 21:18:16 -06:00
Cleanup & rewrite test
This commit is contained in:
parent
2f19c79b4c
commit
8088db987e
@ -126,25 +126,23 @@ class ConfigContextModelQuerySet(RestrictedQuerySet):
|
||||
|
||||
base_query.add((Q(roles=OuterRef('role')) | Q(roles=None)), Q.AND)
|
||||
base_query.add((Q(sites=OuterRef('site')) | Q(sites=None)), Q.AND)
|
||||
region_field = 'site__region'
|
||||
sitegroup_field = 'site__group'
|
||||
|
||||
base_query.add(
|
||||
(Q(
|
||||
regions__tree_id=OuterRef(f'{region_field}__tree_id'),
|
||||
regions__level__lte=OuterRef(f'{region_field}__level'),
|
||||
regions__lft__lte=OuterRef(f'{region_field}__lft'),
|
||||
regions__rght__gte=OuterRef(f'{region_field}__rght'),
|
||||
regions__tree_id=OuterRef('site__region__tree_id'),
|
||||
regions__level__lte=OuterRef('site__region__level'),
|
||||
regions__lft__lte=OuterRef('site__region__lft'),
|
||||
regions__rght__gte=OuterRef('site__region__rght'),
|
||||
) | Q(regions=None)),
|
||||
Q.AND
|
||||
)
|
||||
|
||||
base_query.add(
|
||||
(Q(
|
||||
site_groups__tree_id=OuterRef(f'{sitegroup_field}__tree_id'),
|
||||
site_groups__level__lte=OuterRef(f'{sitegroup_field}__level'),
|
||||
site_groups__lft__lte=OuterRef(f'{sitegroup_field}__lft'),
|
||||
site_groups__rght__gte=OuterRef(f'{sitegroup_field}__rght'),
|
||||
site_groups__tree_id=OuterRef('site__group__tree_id'),
|
||||
site_groups__level__lte=OuterRef('site__group__level'),
|
||||
site_groups__lft__lte=OuterRef('site__group__lft'),
|
||||
site_groups__rght__gte=OuterRef('site__group__rght'),
|
||||
) | Q(site_groups=None)),
|
||||
Q.AND
|
||||
)
|
||||
|
@ -359,105 +359,40 @@ class ConfigContextTest(TestCase):
|
||||
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
||||
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
||||
|
||||
def test_annotation_same_as_get_for_object_virtualmachine_relations_direct_site(self):
|
||||
region = Region.objects.first()
|
||||
sitegroup = SiteGroup.objects.first()
|
||||
def test_virtualmachine_site_context(self):
|
||||
"""
|
||||
Check that config context associated with a site applies to a VM whether the VM is assigned
|
||||
directly to that site or via its cluster.
|
||||
"""
|
||||
site = Site.objects.first()
|
||||
platform = Platform.objects.first()
|
||||
tenantgroup = TenantGroup.objects.first()
|
||||
tenant = Tenant.objects.first()
|
||||
tag = Tag.objects.first()
|
||||
cluster_type = ClusterType.objects.create(name="Cluster Type")
|
||||
cluster_group = ClusterGroup.objects.create(name="Cluster Group")
|
||||
cluster = Cluster.objects.create(
|
||||
name="Cluster",
|
||||
group=cluster_group,
|
||||
type=cluster_type,
|
||||
site=site,
|
||||
)
|
||||
cluster = Cluster.objects.create(name="Cluster", type=cluster_type, site=site)
|
||||
vm_role = DeviceRole.objects.first()
|
||||
|
||||
region_context = ConfigContext.objects.create(
|
||||
name="region",
|
||||
# Create a ConfigContext associated with the site
|
||||
context = ConfigContext.objects.create(
|
||||
name="context1",
|
||||
weight=100,
|
||||
data={"region": 1}
|
||||
data={"foo": True}
|
||||
)
|
||||
region_context.regions.add(region)
|
||||
context.sites.add(site)
|
||||
|
||||
sitegroup_context = ConfigContext.objects.create(
|
||||
name="sitegroup",
|
||||
weight=100,
|
||||
data={"sitegroup": 1}
|
||||
# Create one VM assigned directly to the site, and one assigned via the cluster
|
||||
vm1 = VirtualMachine.objects.create(name="VM 1", site=site, role=vm_role)
|
||||
vm2 = VirtualMachine.objects.create(name="VM 2", cluster=cluster, role=vm_role)
|
||||
|
||||
# Check that their individually-rendered config contexts are identical
|
||||
self.assertEqual(
|
||||
vm1.get_config_context(),
|
||||
vm2.get_config_context()
|
||||
)
|
||||
sitegroup_context.site_groups.add(sitegroup)
|
||||
|
||||
site_context = ConfigContext.objects.create(
|
||||
name="site",
|
||||
weight=100,
|
||||
data={"site": 1}
|
||||
# Check that their annotated config contexts are identical
|
||||
vms = VirtualMachine.objects.filter(pk__in=(vm1.pk, vm2.pk)).annotate_config_context_data()
|
||||
self.assertEqual(
|
||||
vms[0].get_config_context(),
|
||||
vms[1].get_config_context()
|
||||
)
|
||||
site_context.sites.add(site)
|
||||
|
||||
platform_context = ConfigContext.objects.create(
|
||||
name="platform",
|
||||
weight=100,
|
||||
data={"platform": 1}
|
||||
)
|
||||
platform_context.platforms.add(platform)
|
||||
|
||||
tenant_group_context = ConfigContext.objects.create(
|
||||
name="tenant group",
|
||||
weight=100,
|
||||
data={"tenant_group": 1}
|
||||
)
|
||||
tenant_group_context.tenant_groups.add(tenantgroup)
|
||||
|
||||
tenant_context = ConfigContext.objects.create(
|
||||
name="tenant",
|
||||
weight=100,
|
||||
data={"tenant": 1}
|
||||
)
|
||||
tenant_context.tenants.add(tenant)
|
||||
|
||||
tag_context = ConfigContext.objects.create(
|
||||
name="tag",
|
||||
weight=100,
|
||||
data={"tag": 1}
|
||||
)
|
||||
tag_context.tags.add(tag)
|
||||
|
||||
cluster_type_context = ConfigContext.objects.create(
|
||||
name="cluster type",
|
||||
weight=100,
|
||||
data={"cluster_type": 1}
|
||||
)
|
||||
cluster_type_context.cluster_types.add(cluster_type)
|
||||
|
||||
cluster_group_context = ConfigContext.objects.create(
|
||||
name="cluster group",
|
||||
weight=100,
|
||||
data={"cluster_group": 1}
|
||||
)
|
||||
cluster_group_context.cluster_groups.add(cluster_group)
|
||||
|
||||
cluster_context = ConfigContext.objects.create(
|
||||
name="cluster",
|
||||
weight=100,
|
||||
data={"cluster": 1}
|
||||
)
|
||||
cluster_context.clusters.add(cluster)
|
||||
|
||||
virtual_machine = VirtualMachine.objects.create(
|
||||
name="VM 2",
|
||||
site=site,
|
||||
tenant=tenant,
|
||||
platform=platform,
|
||||
role=DeviceRole.objects.first()
|
||||
)
|
||||
virtual_machine.tags.add(tag)
|
||||
|
||||
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
||||
|
||||
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
||||
|
||||
def test_multiple_tags_return_distinct_objects(self):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user