test_models.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from django.test import TestCase
  2. from virtualization.models import *
  3. from tenancy.models import Tenant
  4. class VirtualMachineTestCase(TestCase):
  5. def setUp(self):
  6. cluster_type = ClusterType.objects.create(name='Test Cluster Type 1', slug='Test Cluster Type 1')
  7. self.cluster = Cluster.objects.create(name='Test Cluster 1', type=cluster_type)
  8. def test_vm_duplicate_name_per_cluster(self):
  9. vm1 = VirtualMachine(
  10. cluster=self.cluster,
  11. name='Test VM 1'
  12. )
  13. vm1.save()
  14. vm2 = VirtualMachine(
  15. cluster=vm1.cluster,
  16. name=vm1.name
  17. )
  18. # Two VMs assigned to the same Cluster and no Tenant should fail validation
  19. with self.assertRaises(ValidationError):
  20. vm2.full_clean()
  21. tenant = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
  22. vm1.tenant = tenant
  23. vm1.save()
  24. vm2.tenant = tenant
  25. # Two VMs assigned to the same Cluster and the same Tenant should fail validation
  26. with self.assertRaises(ValidationError):
  27. vm2.full_clean()
  28. vm2.tenant = None
  29. # Two VMs assigned to the same Cluster and different Tenants should pass validation
  30. vm2.full_clean()
  31. vm2.save()