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

refactor(instances): the VM list calls refresh_instance_database itself

instances.utils.refr() only imported and called computes.utils.refresh_instance_database(); the views import it directly, as they import its siblings from computes.utils.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-08 12:15:33 +03:00
parent e554390ddf
commit 496d3a48ad
15 changed files with 29 additions and 35 deletions

View file

@ -8,7 +8,6 @@ from accounts.models import UserInstance
from computes.models import Compute
from computes.utils import refresh_instance_database
from instances.models import Instance
from instances.utils import refr
from computes.tests.fakes import Domain
@ -125,11 +124,6 @@ class InstanceReconciliationTestCase(TestCase):
self.assertTrue(Instance.objects.filter(id=inst.id).exists())
def test_refr_invokes_refresh_instance_database(self):
with patch("computes.utils.refresh_instance_database") as mock_refresh:
refr(self.compute)
mock_refresh.assert_called_once_with(self.compute)
class ReconciliationTransactionTestCase(TransactionTestCase):
"""libvirt is asked outside the DB transaction: on SQLite an open transaction

View file

@ -77,7 +77,7 @@ class CreateDialogCpuTestCase(TestCase):
patch.object(Compute, name, new_callable=PropertyMock, return_value=value)
for name, value in (("status", True), ("cpu_count", 1), ("ram_size", 1024), ("ram_usage", 0))
]
patches += [patch("instances.views.utils.refr"), patch("computes.models.wvmHostDetails")]
patches += [patch("instances.views.refresh_instance_database"), patch("computes.models.wvmHostDetails")]
for p in patches:
self.details = p.start() # the last one: the host connection
self.addCleanup(p.stop)
@ -130,7 +130,7 @@ class CreateDialogHostsTestCase(TestCase):
p = patch.object(Compute, name, new_callable=PropertyMock, return_value=value)
p.start()
self.addCleanup(p.stop)
p = patch("instances.views.utils.refr")
p = patch("instances.views.refresh_instance_database")
p.start()
self.addCleanup(p.stop)

View file

@ -28,7 +28,7 @@ class InstancesIndexTests(TestCase):
Instance.objects.filter(pk=instance.pk).delete()
self.assertFalse(Instance.objects.filter(pk=instance.pk).exists())
with patch("instances.views.utils.refr", side_effect=synchronize), patch.object(
with patch("instances.views.refresh_instance_database", side_effect=synchronize), patch.object(
Compute, "status", new_callable=PropertyMock, return_value=True
), patch.object(
Compute, "cpu_count", new_callable=PropertyMock, return_value=1

View file

@ -31,7 +31,7 @@ class IndexHostAlertTests(TestCase):
return None if status(compute) else f"Connection Failed: no route to {compute.hostname}"
self.client.force_login(user)
with patch("instances.views.utils.refr"), patch.object(Compute, "status", property(status)), patch.object(
with patch("instances.views.refresh_instance_database"), patch.object(Compute, "status", property(status)), patch.object(
Compute, "connection_error", property(error)
), patch.object(Instance, "info", new_callable=PropertyMock, return_value=None):
response = self.client.get(reverse("instances:index"))
@ -77,7 +77,7 @@ class IndexHostAlertTests(TestCase):
return True
self.client.force_login(self.user)
with patch("instances.views.utils.refr") as refr, patch.object(Compute, "status", property(status)), \
with patch("instances.views.refresh_instance_database") as refr, patch.object(Compute, "status", property(status)), \
patch.object(Instance, "info", new_callable=PropertyMock, return_value=None):
self.assertEqual(self.client.get(reverse("instances:index")).status_code, 200)
self.assertEqual([call.args[0].name for call in refr.call_args_list], ["mine-host"])

View file

@ -46,7 +46,7 @@ class ListReadTests(TestCase):
return patch.object(cls, name, call)
patches = [patch("instances.views.utils.refr"), patch("computes.views.utils.refresh_instance_database"),
patches = [patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database"),
patch("vrtManager.connection.connection_manager.get_connection", return_value=self.conn),
counted(libvirt.virDomain, "info"), counted(libvirt.virDomain, "XMLDesc"),
counted(libvirt.virConnect, "lookupByUUIDString")]

View file

@ -46,7 +46,7 @@ class UnreachableListTests(TestCase):
else:
patches.append(patch.object(Compute, name, property(no_live_host_data)))
patches += [
patch("instances.views.utils.refr"),
patch("instances.views.refresh_instance_database"),
patch("computes.views.utils.refresh_instance_database"),
patch.object(Compute, "proxy", new_callable=PropertyMock,
return_value=SimpleNamespace(get_domain_rows=domain_rows)),

View file

@ -24,7 +24,7 @@ from instances.jobs import DeleteInstance
from instances.models import Instance
from operations.jobs import Context
from operations.models import Operation, acquire
from instances.utils import refr
from computes.utils import refresh_instance_database
P = livetest.PREFIX
@ -70,7 +70,7 @@ class LiveDataLossTestCase(TestCase):
def vm(self, name, disks, uefi=False, extra_devices=""):
dom = livetest.define_vm(self.conn, P + name, disks, uefi=uefi, extra_devices=extra_devices)
refr(self.compute)
refresh_instance_database(self.compute)
return dom, Instance.objects.get(compute=self.compute, uuid=dom.UUIDString())
def post(self, view, instance, data=None):
@ -243,7 +243,7 @@ class LiveDataLossTestCase(TestCase):
"<os firmware='efi'><type arch='x86_64' machine='q35'>hvm</type></os>"
"<features><acpi/></features><devices/></domain>"
)
refr(self.compute)
refresh_instance_database(self.compute)
inst = Instance.objects.get(compute=self.compute, uuid=dom.UUIDString())
dom.createWithFlags(libvirt.VIR_DOMAIN_START_PAUSED) # creates the NVRAM file
dom.destroy()

View file

@ -29,7 +29,7 @@ from operations.models import Operation
from vrtManager.tests import livetest
from .test_create_instance import run
from instances.models import Instance
from instances.utils import refr
from computes.utils import refresh_instance_database
P = livetest.PREFIX
BOOT_TIMEOUT = 300
@ -132,7 +132,7 @@ class LiveGuestTestCase(TestCase):
<serial type='pty'/>
</devices>
</domain>""")
refr(self.compute)
refresh_instance_database(self.compute)
return dom, Instance.objects.get(compute=self.compute, uuid=dom.UUIDString())
def start(self, dom):

View file

@ -21,7 +21,7 @@ from vrtManager.connection import connection_manager
from vrtManager.tests import livetest
from instances.models import Instance
from instances.utils import refr
from computes.utils import refresh_instance_database
P = livetest.PREFIX
POOL = P + "iscsi"
@ -85,7 +85,7 @@ class LiveIscsiTestCase(TestCase):
"<vcpu>1</vcpu><os><type arch='x86_64' machine='q35'>hvm</type></os><features><acpi/></features>"
f"<devices>{PCI}</devices></domain>"
)
refr(self.compute)
refresh_instance_database(self.compute)
inst = Instance.objects.get(compute=self.compute, uuid=dom.UUIDString())
if running:
dom.create()

View file

@ -21,7 +21,7 @@ from vrtManager.connection import connection_manager
from vrtManager.tests import livetest
from instances.models import Instance
from instances.utils import refr
from computes.utils import refresh_instance_database
P = livetest.PREFIX
POOL = P + "lvm"
@ -106,7 +106,7 @@ class LiveLvmTestCase(TestCase):
"<os><type arch='x86_64' machine='q35'>hvm</type></os><features><acpi/></features>"
f"<devices>{disks}{PCI}</devices></domain>"
)
refr(self.compute)
refresh_instance_database(self.compute)
return dom, Instance.objects.get(compute=self.compute, uuid=dom.UUIDString())
def lv(self, name, gib=1):

View file

@ -18,7 +18,7 @@ from operations.models import Operation
from vrtManager.tests import livetest
from .test_create_instance import run
from instances.models import Flavor, Instance
from instances.utils import refr
from computes.utils import refresh_instance_database
TEST_COMPUTE_HOST = os.environ.get("TEST_LIBVIRT_HOST", "localhost")
TEST_COMPUTE_LOGIN = os.environ.get("TEST_LIBVIRT_LOGIN", "")
@ -87,7 +87,7 @@ class InstancesTestCase(TestCase):
# Create testing vm from XML
cls.connection._defineXML(cls.xml)
refr(cls.compute)
refresh_instance_database(cls.compute)
# The compute may hold other VMs; never pick one of them.
cls.instance: Instance = Instance.objects.get(compute=cls.compute, name="wvc-test-vm")
except Exception as e:

View file

@ -60,7 +60,7 @@ class StateTests(TestCase):
props = {"status": True, "connection_error": None, "cpu_count": 1, "ram_size": 1, "ram_usage": 0,
"proxy": SimpleNamespace(get_domain_rows=lambda uuids=None: rows)}
patches = [patch.object(Compute, k, new_callable=PropertyMock, return_value=v) for k, v in props.items()]
patches += [patch("instances.views.utils.refr"), patch("computes.views.utils.refresh_instance_database")]
patches += [patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database")]
for p in patches:
p.start()
try:

View file

@ -84,7 +84,7 @@ class VmStatesTestCase(TestCase):
with patch.object(Compute, "status", property(lambda c: c.pk == self.up.pk)), \
patch.object(Compute, "proxy", new_callable=PropertyMock,
return_value=MagicMock(**{"get_domain_rows.return_value": {}})), \
patch("instances.views.utils.refr"), patch("computes.views.utils.refresh_instance_database"):
patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database"):
for url, states_url in ((reverse("instances:index"), reverse("instances:states")),
(reverse("instances", args=[self.up.pk]),
reverse("instances:states") + f"?compute={self.up.pk}")):

View file

@ -436,12 +436,6 @@ def migration_target(compute):
return compute.hostname
def refr(compute):
from computes.utils import refresh_instance_database
refresh_instance_database(compute)
def dhcpd_conf():
"""Optional dhcpd.conf in the application directory; clone names and MAC
addresses are taken from its host entries when it exists."""

View file

@ -16,7 +16,13 @@ from admin.decorators import superuser_only
from appsettings.settings import app_settings
from computes.models import Compute
from computes.validators import validate_hostname, validate_login
from computes.utils import libvirt_compute_lock, libvirt_instance_lock, lock_ownership, user_quota_lock
from computes.utils import (
libvirt_compute_lock,
libvirt_instance_lock,
lock_ownership,
refresh_instance_database,
user_quota_lock,
)
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
@ -75,7 +81,7 @@ def index(request):
# a user who sees no hosts makes the panel read only those of their own VMs
computes = _listed_computes(request.user, list(Compute.objects.all().order_by("name")))
for compute in computes:
utils.refr(compute)
refresh_instance_database(compute)
show_hosts = request.user.is_superuser or request.user.has_perm("instances.view_instances")
own_hosts_down = False
@ -2459,7 +2465,7 @@ def create_instance_select_type(request, compute_id):
with libvirt_compute_lock(compute):
with audit(request, compute, f"VM {name}", "define from XML", instance=name) as entry:
conn._defineXML(xml)
utils.refr(compute)
refresh_instance_database(compute)
instance = compute.instance_set.get(name=name)
entry.uuid = instance.uuid
return redirect(reverse("instances:instance", args=[instance.id]))