mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(computes): a libvirt host is one compute, and a compute's VMs stay on their host
One host could be added twice, under another name, as TCP and SSH, or by another address: its VMs were listed once per record, each with its own owners, and counted twice in the capacity totals. A compute with VMs could also be pointed at another host: the owners of a VM then reached the VM with the same UUID there, a restored or copied one. A compute now keeps its libvirt host UUID. Adding a compute or changing its connection, on the web or the API, connects and reads it: a host that is already a compute is refused, and a compute with VMs moves only to an address of the same host, which must answer. Computes added before are read when another one is checked.
This commit is contained in:
parent
2db25314ea
commit
29ec20682b
17 changed files with 567 additions and 259 deletions
|
|
@ -1,5 +1,18 @@
|
|||
"""Test doubles for the computes tests."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from libvirt import libvirtError
|
||||
|
||||
|
||||
def no_host_answers(test):
|
||||
"""For test: the hosts a compute form or API call tries (Compute.clean)
|
||||
cannot be reached, so none is connected to, the machine's own libvirt
|
||||
least of all."""
|
||||
patcher = patch("computes.models.wvmHostDetails", side_effect=libvirtError("unreachable"))
|
||||
patcher.start()
|
||||
test.addCleanup(patcher.stop)
|
||||
|
||||
|
||||
class Domain:
|
||||
"""A libvirt domain as the VM list reads it: its name and UUID."""
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ from operations.models import Claim
|
|||
from operations.utils import busy_message
|
||||
|
||||
from computes.models import Compute
|
||||
from computes.tests.fakes import no_host_answers
|
||||
|
||||
|
||||
class ComputeAuditRowTests(TestCase):
|
||||
|
|
@ -16,6 +17,7 @@ class ComputeAuditRowTests(TestCase):
|
|||
self.admin = get_user_model().objects.create_superuser("audit-admin", "audit@example.com", "pw")
|
||||
self.client.force_login(self.admin)
|
||||
Logs.objects.all().delete() # the login's row
|
||||
no_host_answers(self)
|
||||
|
||||
def messages(self):
|
||||
return list(Logs.objects.order_by("id").values_list("message", flat=True))
|
||||
|
|
|
|||
|
|
@ -6,11 +6,13 @@ from django.test import TestCase
|
|||
from django.urls import reverse
|
||||
|
||||
from computes.models import Compute
|
||||
from computes.tests.fakes import no_host_answers
|
||||
|
||||
|
||||
class ComputePasswordTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client.force_login(get_user_model().objects.create_superuser("cp-admin", "cp@example.com", "pw"))
|
||||
no_host_answers(self)
|
||||
|
||||
def edit(self, compute, password):
|
||||
data = {"name": compute.name, "hostname": compute.hostname, "login": "admin", "password": password,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from django.test import TestCase
|
|||
from django.urls import reverse
|
||||
|
||||
from computes.models import Compute
|
||||
from computes.tests.fakes import no_host_answers
|
||||
|
||||
KEY, OTHER = "5e" * 32, "a1" * 32
|
||||
|
||||
|
|
@ -13,6 +14,7 @@ KEY, OTHER = "5e" * 32, "a1" * 32
|
|||
class GstfsdKeyTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client.force_login(get_user_model().objects.create_superuser("gk-admin", "gk@example.com", "pw"))
|
||||
no_host_answers(self)
|
||||
|
||||
def edit(self, compute, key):
|
||||
data = {"name": compute.name, "hostname": compute.hostname, "login": "admin", "password": "",
|
||||
|
|
|
|||
108
computes/tests/test_host_identity.py
Normal file
108
computes/tests/test_host_identity.py
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
"""One libvirt host is one compute, and a compute's VMs stay on their host:
|
||||
the host UUID of a new or changed connection is read and checked."""
|
||||
|
||||
from unittest.mock import MagicMock, PropertyMock, patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from instances.models import Instance
|
||||
from libvirt import libvirtError
|
||||
from operations import claims
|
||||
from operations.models import Claim
|
||||
|
||||
from computes.models import Compute
|
||||
|
||||
# the host each address reaches, by its UUID; an address not here does not answer
|
||||
HOSTS = {"192.0.2.1": "11111111-1111-1111-1111-111111111111", "kvm1.example.org": "11111111-1111-1111-1111-111111111111",
|
||||
"192.0.2.2": "22222222-2222-2222-2222-222222222222"}
|
||||
|
||||
|
||||
def host(hostname, login, password, conn_type):
|
||||
if hostname not in HOSTS:
|
||||
raise libvirtError(f"{hostname} did not answer within 5 s")
|
||||
return MagicMock(**{"host_uuid.return_value": HOSTS[hostname]})
|
||||
|
||||
|
||||
class HostIdentityTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client.force_login(get_user_model().objects.create_superuser("hi-admin", "hi@example.com", "pw"))
|
||||
patcher = patch("computes.models.wvmHostDetails", side_effect=host)
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def add(self, name, hostname):
|
||||
return self.client.post(reverse("add_tcp_host"), {"name": name, "hostname": hostname, "login": "wvc",
|
||||
"password": "pw", "details": "", "type": 1})
|
||||
|
||||
def edit(self, compute, hostname):
|
||||
return self.client.post(reverse("compute_update", args=[compute.id]),
|
||||
{"name": compute.name, "hostname": hostname, "login": "wvc", "password": "",
|
||||
"details": "", "type": 1})
|
||||
|
||||
def test_a_host_is_added_once_whatever_it_is_reached_by(self):
|
||||
self.add("kvm1", "192.0.2.1")
|
||||
self.assertEqual(Compute.objects.get(name="kvm1").host_uuid, HOSTS["192.0.2.1"])
|
||||
response = self.add("kvm1-again", "kvm1.example.org")
|
||||
self.assertContains(response, "This host is already added as kvm1")
|
||||
self.assertFalse(Compute.objects.filter(name="kvm1-again").exists())
|
||||
|
||||
def test_a_compute_added_before_is_recorded_on_the_computes_page(self):
|
||||
Compute.objects.create(name="old", hostname="192.0.2.1", login="wvc", password="pw", type=1)
|
||||
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True), \
|
||||
patch("computes.views.shown_capacity", return_value=None):
|
||||
self.client.get(reverse("computes"))
|
||||
self.assertEqual(Compute.objects.get(name="old").host_uuid, HOSTS["192.0.2.1"])
|
||||
self.assertContains(self.add("new", "kvm1.example.org"), "This host is already added as old")
|
||||
|
||||
def test_a_compute_with_vms_whose_host_is_not_known_keeps_its_connection(self):
|
||||
compute = Compute.objects.create(name="old", hostname="192.0.2.9", login="wvc", password="pw", type=1)
|
||||
Instance.objects.create(compute=compute, name="web-1", uuid="33333333-3333-3333-3333-333333333333")
|
||||
self.assertContains(self.edit(compute, "192.0.2.2"), "The host its VMs are on is not known yet")
|
||||
compute.refresh_from_db()
|
||||
self.assertEqual(compute.hostname, "192.0.2.9")
|
||||
|
||||
def test_a_compute_with_vms_moves_only_to_an_address_of_their_host(self):
|
||||
self.add("kvm1", "192.0.2.1")
|
||||
compute = Compute.objects.get(name="kvm1")
|
||||
Instance.objects.create(compute=compute, name="web-1", uuid="33333333-3333-3333-3333-333333333333")
|
||||
self.assertContains(self.edit(compute, "192.0.2.2"), "This is another host than the one its VMs")
|
||||
self.assertContains(self.edit(compute, "192.0.2.9"), "The panel cannot reach the host to tell that its VMs")
|
||||
self.edit(compute, "kvm1.example.org")
|
||||
compute.refresh_from_db()
|
||||
self.assertEqual((compute.hostname, compute.host_uuid), ("kvm1.example.org", HOSTS["192.0.2.1"]))
|
||||
|
||||
def test_a_compute_without_vms_may_be_another_host_or_none_yet(self):
|
||||
self.add("kvm", "192.0.2.9") # added while it does not answer
|
||||
compute = Compute.objects.get(name="kvm")
|
||||
self.assertEqual(compute.host_uuid, "")
|
||||
self.edit(compute, "192.0.2.2")
|
||||
compute.refresh_from_db()
|
||||
self.assertEqual(compute.host_uuid, HOSTS["192.0.2.2"])
|
||||
|
||||
def test_a_change_is_checked_while_the_compute_is_held(self):
|
||||
# no VM list can add the old host's VMs between the check and the save
|
||||
compute = Compute.objects.create(name="kvm", hostname="192.0.2.9", login="wvc", password="pw", type=1)
|
||||
held = []
|
||||
|
||||
def checked(*args):
|
||||
held.append(set(Claim.objects.values_list("resource", "mode")))
|
||||
return host(*args)
|
||||
|
||||
with patch("computes.models.wvmHostDetails", side_effect=checked):
|
||||
self.edit(compute, "192.0.2.2")
|
||||
self.client.patch(f"/api/v1/computes/{compute.id}/", {"hostname": "192.0.2.1"}, content_type="application/json")
|
||||
for claimed in held:
|
||||
self.assertLessEqual({(claims.compute_key(compute.id), Claim.EXCLUSIVE), (claims.hosts_key(), Claim.EXCLUSIVE)},
|
||||
claimed)
|
||||
self.assertEqual(len(held), 2)
|
||||
|
||||
def test_the_api_checks_the_host_too(self):
|
||||
self.add("kvm1", "192.0.2.1")
|
||||
data = {"name": "kvm1-api", "hostname": "kvm1.example.org", "login": "wvc", "password": "pw", "type": 1}
|
||||
response = self.client.post("/api/v1/computes/", data, content_type="application/json")
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertIn("This host is already added as kvm1", str(response.json()))
|
||||
data["hostname"] = "192.0.2.2"
|
||||
self.assertEqual(self.client.post("/api/v1/computes/", data, content_type="application/json").status_code, 201)
|
||||
self.assertEqual(Compute.objects.get(name="kvm1-api").host_uuid, HOSTS["192.0.2.2"])
|
||||
|
|
@ -9,6 +9,7 @@ from libvirt import libvirtError
|
|||
|
||||
from vrtManager.connection import CONN_SOCKET, connection_manager
|
||||
from computes.models import Compute
|
||||
from computes.tests.fakes import no_host_answers
|
||||
from vrtManager.tests import livetest
|
||||
|
||||
TEST_COMPUTE_HOST = os.environ.get("TEST_LIBVIRT_HOST", "localhost")
|
||||
|
|
@ -58,6 +59,7 @@ class ComputesTestCase(TestCase):
|
|||
|
||||
@patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError("unreachable"))
|
||||
def test_create_update_delete(self, connection):
|
||||
no_host_answers(self)
|
||||
response = self.client.get(reverse("add_socket_host"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue