1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

test: the computes tests share one fake libvirt domain

test_ownership, test_ownership_race and test_reconciliation each defined
the same fake domain (a name and a UUID); it is computes/tests/fakes.py.
This commit is contained in:
catborise 2026-11-06 11:18:11 +03:00
parent 00fb8c1d21
commit 4e0952aa50
4 changed files with 23 additions and 40 deletions

14
computes/tests/fakes.py Normal file
View file

@ -0,0 +1,14 @@
"""Test doubles for the computes tests."""
class Domain:
"""A libvirt domain as the VM list reads it: its name and UUID."""
def __init__(self, name, uuid):
self._name, self._uuid = name, uuid
def name(self):
return self._name
def UUIDString(self):
return self._uuid

View file

@ -15,21 +15,11 @@ from logs.models import Logs
from operations import claims from operations import claims
from operations.models import Claim from operations.models import Claim
from rest_framework.test import APIClient from rest_framework.test import APIClient
from computes.tests.fakes import Domain
UUID = "44444444-4444-4444-4444-444444444444" UUID = "44444444-4444-4444-4444-444444444444"
class Domain:
def __init__(self, name, uuid):
self._name, self._uuid = name, uuid
def name(self):
return self._name
def UUIDString(self):
return self._uuid
class OwnershipTestCase(TestCase): class OwnershipTestCase(TestCase):
def setUp(self): def setUp(self):
self.owner = get_user_model().objects.create_user("owner", password="pw") self.owner = get_user_model().objects.create_user("owner", password="pw")

View file

@ -13,21 +13,11 @@ from django.contrib.auth import get_user_model
from django.db import connection from django.db import connection
from django.test import TransactionTestCase from django.test import TransactionTestCase
from instances.models import Instance, InstanceTombstone from instances.models import Instance, InstanceTombstone
from computes.tests.fakes import Domain
UUID = "44444444-4444-4444-4444-444444444444" UUID = "44444444-4444-4444-4444-444444444444"
class Domain:
def __init__(self, name, uuid):
self._name, self._uuid = name, uuid
def name(self):
return self._name
def UUIDString(self):
return self._uuid
class OwnershipRaceTests(TransactionTestCase): class OwnershipRaceTests(TransactionTestCase):
def compute(self, name, domains): def compute(self, name, domains):
c = Compute.objects.create(name=name, hostname=f"{name}.example", login="", password="", type=1) c = Compute.objects.create(name=name, hostname=f"{name}.example", login="", password="", type=1)

View file

@ -9,18 +9,7 @@ from computes.models import Compute
from computes.utils import refresh_instance_database from computes.utils import refresh_instance_database
from instances.models import Instance from instances.models import Instance
from instances.utils import refr from instances.utils import refr
from computes.tests.fakes import Domain
class MockDomain:
def __init__(self, name, uuid_str):
self._name = name
self._uuid_str = uuid_str
def name(self):
return self._name
def UUIDString(self):
return self._uuid_str
class InstanceReconciliationTestCase(TestCase): class InstanceReconciliationTestCase(TestCase):
@ -36,8 +25,8 @@ class InstanceReconciliationTestCase(TestCase):
) )
def test_new_domain_created_in_libvirt_is_added_to_db(self): def test_new_domain_created_in_libvirt_is_added_to_db(self):
dom1 = MockDomain(name="vm-alpha", uuid_str="11111111-1111-1111-1111-111111111111") dom1 = Domain(name="vm-alpha", uuid="11111111-1111-1111-1111-111111111111")
dom2 = MockDomain(name="vm-beta", uuid_str="22222222-2222-2222-2222-222222222222") dom2 = Domain(name="vm-beta", uuid="22222222-2222-2222-2222-222222222222")
mock_proxy = MagicMock() mock_proxy = MagicMock()
mock_proxy.wvm.listAllDomains.return_value = [dom1, dom2] mock_proxy.wvm.listAllDomains.return_value = [dom1, dom2]
@ -81,7 +70,7 @@ class InstanceReconciliationTestCase(TestCase):
original_inst_id = inst.id original_inst_id = inst.id
user_inst = UserInstance.objects.create(instance=inst, user=self.user, is_change=True) user_inst = UserInstance.objects.create(instance=inst, user=self.user, is_change=True)
dom_renamed = MockDomain(name="vm-new-name", uuid_str=uuid) dom_renamed = Domain(name="vm-new-name", uuid=uuid)
mock_proxy = MagicMock() mock_proxy = MagicMock()
mock_proxy.wvm.listAllDomains.return_value = [dom_renamed] mock_proxy.wvm.listAllDomains.return_value = [dom_renamed]
@ -163,7 +152,7 @@ class ReconciliationTransactionTestCase(TransactionTestCase):
def list_domains(): def list_domains():
seen["list"] = connection.in_atomic_block seen["list"] = connection.in_atomic_block
return [MockDomain(name="vm-tx", uuid_str="77777777-7777-7777-7777-777777777777")] return [Domain(name="vm-tx", uuid="77777777-7777-7777-7777-777777777777")]
self.proxy(list_domains) self.proxy(list_domains)
with patch.object(Compute, "status", new_callable=PropertyMock, side_effect=status): with patch.object(Compute, "status", new_callable=PropertyMock, side_effect=status):
@ -174,7 +163,7 @@ class ReconciliationTransactionTestCase(TransactionTestCase):
def test_a_compute_that_matches_the_database_writes_nothing(self): def test_a_compute_that_matches_the_database_writes_nothing(self):
Instance.objects.create(compute=self.compute, name="vm-same", uuid="99999999-9999-9999-9999-999999999999") Instance.objects.create(compute=self.compute, name="vm-same", uuid="99999999-9999-9999-9999-999999999999")
self.proxy(lambda: [MockDomain(name="vm-same", uuid_str="99999999-9999-9999-9999-999999999999")]) self.proxy(lambda: [Domain(name="vm-same", uuid="99999999-9999-9999-9999-999999999999")])
self.compute.__dict__["status"] = True self.compute.__dict__["status"] = True
with CaptureQueriesContext(connection) as queries: with CaptureQueriesContext(connection) as queries:
refresh_instance_database(self.compute) refresh_instance_database(self.compute)
@ -184,7 +173,7 @@ class ReconciliationTransactionTestCase(TransactionTestCase):
def test_compute_deleted_while_listing_gets_no_instances(self): def test_compute_deleted_while_listing_gets_no_instances(self):
def list_domains(): def list_domains():
Compute.objects.filter(pk=self.compute.pk).delete() Compute.objects.filter(pk=self.compute.pk).delete()
return [MockDomain(name="vm-orphan", uuid_str="88888888-8888-8888-8888-888888888888")] return [Domain(name="vm-orphan", uuid="88888888-8888-8888-8888-888888888888")]
self.proxy(list_domains) self.proxy(list_domains)
self.compute.__dict__["status"] = True self.compute.__dict__["status"] = True