From 496d3a48ad28e8bfc6182da5b96b44e4c8dd505e Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 8 Nov 2026 12:15:33 +0300 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- computes/tests/test_reconciliation.py | 6 ------ instances/tests/test_create_dialog_cpu.py | 4 ++-- instances/tests/test_index.py | 2 +- instances/tests/test_index_host_alerts.py | 4 ++-- instances/tests/test_list_rpc.py | 2 +- instances/tests/test_list_unreachable.py | 2 +- instances/tests/test_live_dataloss.py | 6 +++--- instances/tests/test_live_guest.py | 4 ++-- instances/tests/test_live_iscsi.py | 4 ++-- instances/tests/test_live_lvm.py | 4 ++-- instances/tests/test_live_views.py | 4 ++-- instances/tests/test_states.py | 2 +- instances/tests/test_vm_states.py | 2 +- instances/utils.py | 6 ------ instances/views.py | 12 +++++++++--- 15 files changed, 29 insertions(+), 35 deletions(-) diff --git a/computes/tests/test_reconciliation.py b/computes/tests/test_reconciliation.py index 8ebfdfe6..0e30fe6a 100644 --- a/computes/tests/test_reconciliation.py +++ b/computes/tests/test_reconciliation.py @@ -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 diff --git a/instances/tests/test_create_dialog_cpu.py b/instances/tests/test_create_dialog_cpu.py index 2c21fe37..5bd34afa 100644 --- a/instances/tests/test_create_dialog_cpu.py +++ b/instances/tests/test_create_dialog_cpu.py @@ -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) diff --git a/instances/tests/test_index.py b/instances/tests/test_index.py index 2acebeeb..77f46049 100644 --- a/instances/tests/test_index.py +++ b/instances/tests/test_index.py @@ -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 diff --git a/instances/tests/test_index_host_alerts.py b/instances/tests/test_index_host_alerts.py index 898176e6..9bdea501 100644 --- a/instances/tests/test_index_host_alerts.py +++ b/instances/tests/test_index_host_alerts.py @@ -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"]) diff --git a/instances/tests/test_list_rpc.py b/instances/tests/test_list_rpc.py index bf1d4c8b..372b48e4 100644 --- a/instances/tests/test_list_rpc.py +++ b/instances/tests/test_list_rpc.py @@ -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")] diff --git a/instances/tests/test_list_unreachable.py b/instances/tests/test_list_unreachable.py index 669d6c03..19fd0ce7 100644 --- a/instances/tests/test_list_unreachable.py +++ b/instances/tests/test_list_unreachable.py @@ -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)), diff --git a/instances/tests/test_live_dataloss.py b/instances/tests/test_live_dataloss.py index 56a9cb25..cbc21cc9 100644 --- a/instances/tests/test_live_dataloss.py +++ b/instances/tests/test_live_dataloss.py @@ -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): "hvm" "" ) - 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() diff --git a/instances/tests/test_live_guest.py b/instances/tests/test_live_guest.py index 7b992338..bb9329d9 100644 --- a/instances/tests/test_live_guest.py +++ b/instances/tests/test_live_guest.py @@ -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): """) - refr(self.compute) + refresh_instance_database(self.compute) return dom, Instance.objects.get(compute=self.compute, uuid=dom.UUIDString()) def start(self, dom): diff --git a/instances/tests/test_live_iscsi.py b/instances/tests/test_live_iscsi.py index 472670bb..d7ab77b3 100644 --- a/instances/tests/test_live_iscsi.py +++ b/instances/tests/test_live_iscsi.py @@ -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): "1hvm" f"{PCI}" ) - refr(self.compute) + refresh_instance_database(self.compute) inst = Instance.objects.get(compute=self.compute, uuid=dom.UUIDString()) if running: dom.create() diff --git a/instances/tests/test_live_lvm.py b/instances/tests/test_live_lvm.py index b0bae022..a6582645 100644 --- a/instances/tests/test_live_lvm.py +++ b/instances/tests/test_live_lvm.py @@ -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): "hvm" f"{disks}{PCI}" ) - 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): diff --git a/instances/tests/test_live_views.py b/instances/tests/test_live_views.py index f4bca803..7ccc9e8b 100644 --- a/instances/tests/test_live_views.py +++ b/instances/tests/test_live_views.py @@ -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: diff --git a/instances/tests/test_states.py b/instances/tests/test_states.py index 7a90efdd..59c577e4 100644 --- a/instances/tests/test_states.py +++ b/instances/tests/test_states.py @@ -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: diff --git a/instances/tests/test_vm_states.py b/instances/tests/test_vm_states.py index c1b365b7..a6820618 100644 --- a/instances/tests/test_vm_states.py +++ b/instances/tests/test_vm_states.py @@ -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}")): diff --git a/instances/utils.py b/instances/utils.py index f904a7bd..ae38fab4 100644 --- a/instances/utils.py +++ b/instances/utils.py @@ -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.""" diff --git a/instances/views.py b/instances/views.py index cbf90e6f..49ff4c1e 100755 --- a/instances/views.py +++ b/instances/views.py @@ -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]))