1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/instances/tests/test_list_hung_host.py
catborise cf1f7ad055 fix(instances): a host that does not answer no longer holds the VM lists
The VM list and its poll read each host in turn. A host that is up but
stuck (a daemon or an NFS mount that does not answer) kept the call
waiting: the list did not open in 200 s (seen live with compute1's
virtqemud stopped), and the per-host limit freed it only when gunicorn
ended the request after 600 s. The list and the poll now ask all hosts
at once, each in a thread counted on its host like a request, and wait
10 s at most: a host that has not answered is shown as not answering,
and the VMs of the others are listed. Live: the list opens in 10 s,
then in 5 s while the host stays stuck.
2026-11-13 16:07:01 +03:00

55 lines
2.4 KiB
Python

"""A VM list asks all hosts at once and waits LIST_HOST_WAIT at most: a host
that does not answer (a stuck daemon) is shown not answering, and the other
hosts' VMs are listed."""
import threading
import time
from types import SimpleNamespace
from unittest.mock import PropertyMock, patch
from computes.models import Compute
from django.test import TestCase
from instances import utils
class HungHostTests(TestCase):
def test_a_host_that_does_not_answer_is_left_out_in_time(self):
hung, ok = (Compute.objects.create(name=name, hostname=f"192.0.2.{n}", login="", password="", type=1)
for n, name in ((1, "hung"), (2, "ok")))
release = threading.Event()
self.addCleanup(release.set)
def states(proxy_of):
def get_domain_states():
if proxy_of == "hung":
release.wait(5)
return {"u-1": ["vm", 1]}
return SimpleNamespace(get_domain_states=get_domain_states)
# each copy of a compute (one per thread) gets the proxy of its host
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True), \
patch.object(Compute, "proxy", property(lambda compute: states(compute.name))), \
patch.object(utils, "LIST_HOST_WAIT", 0.5):
start = time.monotonic()
result = utils.host_domain_states([hung, ok])
waited = time.monotonic() - start
self.assertLess(waited, 2)
self.assertEqual(result, {hung.pk: None, ok.pk: {"u-1": ["vm", 1]}})
self.assertIs(hung.status, False)
self.assertIn("did not answer within", str(hung.connection_error))
def test_a_host_whose_read_fails_is_marked(self):
# connected, but its answer an error: the page says so and does not ask it again
from libvirt import libvirtError
host = Compute.objects.create(name="failing", hostname="192.0.2.3", login="", password="", type=1)
def fail():
raise libvirtError("internal error: no domains for you")
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True), \
patch.object(Compute, "proxy", property(lambda compute: SimpleNamespace(get_domain_states=fail))):
self.assertEqual(utils.host_domain_states([host]), {host.pk: None})
self.assertIs(host.status, False)
self.assertIn("no domains for you", str(host.connection_error))