1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00
webvirtcloud/instances/tests/test_list_unreachable.py
catborise bdc5569cc8 feat(instances): a VM a fault stopped or paused says why
libvirt gives each state its reason; the panel showed only the state, so a
VM QEMU paused because its storage filled up read "Suspended" like one an
administrator paused.

- instances.states.REASONS: the fault reasons told (disk error, watchdog,
  guest crash or kernel panic, a crash of the guest or its QEMU process,
  libvirt stopping it after a restart, failed post-copy migration, libvirt
  error, failed start); the ordinary ones (user, migration, snapshot) are not
- the lists read the reason with the state in their one getAllDomainStats
  call (get_domain_rows, get_domain_states: a change of reason reloads a
  list) and show it under the state badge
- the VM page reads state and reason in one virDomainGetState (a reason
  means something only with its own state) and tells it in an alert; a VM
  paused by a disk error names each disk and what it met
  (virDomainGetDiskErrors), and when a storage has no free space, what to
  do: an administrator frees space and resumes it. Disk errors are read
  only then: libvirt keeps the last one after a resume. The page's poll
  reloads on a change of reason too

Checked live on a Vagrant host: a guest filling a disk on a 40 MiB pool was
paused (I/O error, vda: no space); the page said so, and after the pool grew
and the VM was resumed the alert was gone.
2026-11-22 21:19:35 +03:00

124 lines
6.3 KiB
Python

"""A VM that cannot be read shows as unknown in the lists instead of turning
the whole page into an error page (host down, failed sync, VM gone)."""
from types import SimpleNamespace
from unittest.mock import PropertyMock, patch
from appsettings.models import AppSettings
from computes.models import Compute
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from instances.models import Instance
from lxml import html
from libvirt import libvirtError
from vrtManager.util import OperationError
ERROR = "Connection Failed: No route to host"
UP_INFO = [1, 4194304, 2097152, 3, 0]
class UnreachableListTests(TestCase):
def setUp(self):
self.client.force_login(
get_user_model().objects.create_superuser(username="down-admin", password="pwd", email="down@example.com")
)
self.compute = Compute.objects.create(name="down-compute", hostname="192.0.2.1", type=1, login="u", password="p")
self.vm_a = Instance.objects.create(compute=self.compute, name="vm-a", uuid="11111111-1111-1111-1111-111111111111")
self.vm_b = Instance.objects.create(compute=self.compute, name="vm-b", uuid="22222222-2222-2222-2222-222222222222")
def get(self, url, rows=None):
"""The page with the host down, or up with these VM rows (get_domain_rows),
or up with the error its listing raises."""
up = rows is not None
def domain_rows(uuids=None):
if isinstance(rows, Exception):
raise rows
return rows
def no_live_host_data(_self):
raise AssertionError("host data read for a host that is down")
props = {"status": up, "connection_error": None if up else ERROR}
patches = [patch.object(Compute, k, new_callable=PropertyMock, return_value=v) for k, v in props.items()]
for name in ("cpu_count", "ram_size", "ram_usage"):
if up:
patches.append(patch.object(Compute, name, new_callable=PropertyMock, return_value=1))
else:
patches.append(patch.object(Compute, name, property(no_live_host_data)))
patches += [
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, get_domain_states=dict)),
]
for p in patches:
p.start()
try:
response = self.client.get(url)
finally:
for p in reversed(patches):
p.stop()
self.assertEqual(response.status_code, 200)
return response
def assert_unknown(self, response, instances):
for vm in instances:
self.assertContains(response, vm.name)
self.assertNotContains(response, reverse("instances:poweron", args=[vm.id]))
self.assertNotContains(response, reverse("instances:poweroff", args=[vm.id]))
states = html.fromstring(response.content).xpath("//span[@data-state]/@data-state")
self.assertEqual(states.count("Unknown"), len(instances))
def test_nongrouped_index_lists_the_vms_of_a_down_host(self):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="nongrouped")
response = self.get(reverse("instances:index"))
self.assert_unknown(response, [self.vm_a, self.vm_b])
self.assertContains(response, ERROR)
def test_a_host_that_stops_answering_while_the_list_is_made_is_no_error_page(self):
# status read True, then the host's keepalive ran out: its numbers fail
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="grouped")
def gone():
raise libvirtError("internal error: client socket is closed")
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True), \
patch.object(Compute, "connection_error", new_callable=PropertyMock, return_value=None), \
patch("instances.views.refresh_instance_database"), \
patch.object(Compute, "proxy", new_callable=PropertyMock, return_value=type("P", (), {
"get_domain_rows": lambda self, uuids=None: {}, "get_domain_states": lambda self: {}, "get_memory_usage": lambda self: gone(),
"host_info": property(lambda self: gone())})()):
response = self.client.get(reverse("instances:index"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "vm-a")
# not offered for a new VM
self.assertNotContains(response, reverse("instances:create_instance_select_type", args=[self.compute.id]))
def test_grouped_index_lists_the_vms_of_a_down_host(self):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="grouped")
response = self.get(reverse("instances:index"))
self.assert_unknown(response, [self.vm_a, self.vm_b])
self.assertContains(response, "Not connected")
def test_one_unreadable_vm_does_not_hide_the_others(self):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="nongrouped")
# vm-b is gone from its host
response = self.get(reverse("instances:index"), {self.vm_a.uuid: (UP_INFO, "", 0)})
self.assert_unknown(response, [self.vm_b])
self.assertContains(response, reverse("instances:poweroff", args=[self.vm_a.id]))
def test_a_host_whose_listing_fails_shows_its_vms_unknown(self):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="nongrouped")
response = self.get(reverse("instances:index"), OperationError(ERROR))
self.assert_unknown(response, [self.vm_a, self.vm_b])
def test_compute_instances_page_of_a_down_host(self):
response = self.get(reverse("instances", args=[self.compute.id]))
self.assert_unknown(response, [self.vm_a, self.vm_b])
self.assertContains(response, ERROR)
def test_compute_instances_page_does_not_claim_a_down_host_is_empty(self):
Instance.objects.all().delete()
response = self.get(reverse("instances", args=[self.compute.id]))
self.assertNotContains(response, "have any Instances")
self.assertContains(response, ERROR)