From e79841ab1aa165747567ecaa36a72d992bcf1e9e Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 3 Nov 2026 14:45:45 +0300 Subject: [PATCH] fix(storages): a pool the host does not have goes back to the pools list Opening /computes//storage// for a pool the host does not have (or a host that does not answer) raised libvirt's error out of the view: a 500. Now the pools list shows the reason, as the network page does. --- storages/tests/test_views.py | 16 ++++++++++++++++ storages/views.py | 8 +++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/storages/tests/test_views.py b/storages/tests/test_views.py index 8ef08e5c..888c0c7e 100644 --- a/storages/tests/test_views.py +++ b/storages/tests/test_views.py @@ -40,6 +40,22 @@ class GetVolumesViewTests(TestCase): self.assertEqual(response.json()["vols"], ["a.qcow2", "b.qcow2"]) +class PoolPageTests(TestCase): + def setUp(self): + get_user_model().objects.create_superuser(username="pool_admin", password="password", email="pool_admin@example.com") + self.client.login(request=RequestFactory().get("/"), username="pool_admin", password="password") + self.compute = Compute.objects.create(name="PoolCompute", hostname="localhost", type=1, login="u", password="p") + + @patch("storages.views.wvmStorage") + def test_a_pool_the_host_does_not_have_goes_back_to_the_pools_with_the_reason(self, mock_wvm): + from libvirt import libvirtError + + mock_wvm.side_effect = libvirtError("Storage pool not found: no storage pool with matching name 'default'") + response = self.client.get(reverse("storage", args=[self.compute.id, "default"])) + self.assertRedirects(response, reverse("storages", args=[self.compute.id]), fetch_redirect_response=False) + self.assertIn("no storage pool with matching name", str(list(get_messages(response.wsgi_request))[0])) + + class VolumeDeleteTests(TestCase): """A volume a VM still needs is refused with its reason, on the page and in the API.""" diff --git a/storages/views.py b/storages/views.py index 0a7aed90..b4e5f9f1 100644 --- a/storages/views.py +++ b/storages/views.py @@ -119,9 +119,11 @@ def storage(request, compute_id, pool): compute = get_object_or_404(Compute, pk=compute_id) form = CreateVolumeForm() - conn = wvmStorage( - compute.hostname, compute.login, compute.password, compute.type, pool - ) + try: + conn = wvmStorage(compute.hostname, compute.login, compute.password, compute.type, pool) + except libvirtError as lib_err: # no such pool on the host, or the host does not answer + messages.error(request, lib_err) + return HttpResponseRedirect(reverse("storages", args=[compute_id])) if request.method == "POST" and "iso_upload" in request.POST: try: