mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
from django.core.exceptions import ValidationError
|
|
from django.test import TestCase
|
|
|
|
from dcim.models import Site
|
|
from virtualization.models import *
|
|
from tenancy.models import Tenant
|
|
|
|
|
|
class VirtualMachineTestCase(TestCase):
|
|
|
|
def test_vm_duplicate_name_per_cluster(self):
|
|
cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
|
|
cluster = Cluster.objects.create(name='Cluster 1', type=cluster_type)
|
|
|
|
vm1 = VirtualMachine(
|
|
cluster=cluster,
|
|
name='Test VM 1'
|
|
)
|
|
vm1.save()
|
|
|
|
vm2 = VirtualMachine(
|
|
cluster=vm1.cluster,
|
|
name=vm1.name
|
|
)
|
|
|
|
# Two VMs assigned to the same Cluster and no Tenant should fail validation
|
|
with self.assertRaises(ValidationError):
|
|
vm2.full_clean()
|
|
|
|
tenant = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
|
|
vm1.tenant = tenant
|
|
vm1.save()
|
|
vm2.tenant = tenant
|
|
|
|
# Two VMs assigned to the same Cluster and the same Tenant should fail validation
|
|
with self.assertRaises(ValidationError):
|
|
vm2.full_clean()
|
|
|
|
vm2.tenant = None
|
|
|
|
# Two VMs assigned to the same Cluster and different Tenants should pass validation
|
|
vm2.full_clean()
|
|
vm2.save()
|
|
|
|
def test_vm_mismatched_site_cluster(self):
|
|
cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
|
|
|
|
sites = (
|
|
Site(name='Site 1', slug='site-1'),
|
|
Site(name='Site 2', slug='site-2'),
|
|
)
|
|
Site.objects.bulk_create(sites)
|
|
|
|
clusters = (
|
|
Cluster(name='Cluster 1', type=cluster_type, site=sites[0]),
|
|
Cluster(name='Cluster 2', type=cluster_type, site=sites[1]),
|
|
Cluster(name='Cluster 3', type=cluster_type, site=None),
|
|
)
|
|
Cluster.objects.bulk_create(clusters)
|
|
|
|
# VM with site only should pass
|
|
VirtualMachine(name='vm1', site=sites[0]).full_clean()
|
|
|
|
# VM with non-site cluster only should pass
|
|
VirtualMachine(name='vm1', cluster=clusters[2]).full_clean()
|
|
|
|
# VM with mismatched site & cluster should fail
|
|
with self.assertRaises(ValidationError):
|
|
VirtualMachine(name='vm1', site=sites[0], cluster=clusters[1]).full_clean()
|
|
|
|
# VM with cluster site but no direct site should fail
|
|
with self.assertRaises(ValidationError):
|
|
VirtualMachine(name='vm1', site=None, cluster=clusters[0]).full_clean()
|