mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
test: no test connects to a real host
Tests of eight files patched a host's status but not what the page also read, its connection, connection_error or proxy, and so connected for real: over ssh to 10.0.0.9 or over TCP to unroutable addresses (5 to 10 s each until the connect timeout), and to the machine's own libvirt (qemu:///system). A failed host is not tried again for a while, so only the first such test of a process connected, and which one depended on the order. They now patch the connection itself, or what the page reads, and use RFC 5737 addresses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
0d225edda4
commit
9f323d709e
8 changed files with 41 additions and 22 deletions
|
|
@ -1,3 +1,5 @@
|
|||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
from appsettings.models import AppSettings
|
||||
from appsettings.settings import app_settings
|
||||
from computes.models import Compute
|
||||
|
|
@ -7,6 +9,7 @@ from django.contrib.auth.models import Permission
|
|||
from django.shortcuts import reverse
|
||||
from django.test import Client, RequestFactory, TestCase, override_settings
|
||||
from instances.models import Instance
|
||||
from libvirt import libvirtError
|
||||
|
||||
|
||||
from accounts.forms import UserInstanceForm, UserSSHKeyForm
|
||||
|
|
@ -36,6 +39,10 @@ class AccountsTestCase(TestCase):
|
|||
)
|
||||
|
||||
def setUp(self):
|
||||
# the pages read no host: its compute is not there
|
||||
down = patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError("unreachable"))
|
||||
down.start()
|
||||
self.addCleanup(down.stop)
|
||||
self.client.force_login(self.admin_user)
|
||||
permission = Permission.objects.get(codename="change_password")
|
||||
self.test_user.user_permissions.add(permission)
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ class OverviewTestCase(TestCase):
|
|||
self.assertFalse(page.xpath("//div[@class='card-header'][normalize-space()='Hardware']"))
|
||||
|
||||
def test_the_list_links_the_overview_of_a_host_it_cannot_reach(self):
|
||||
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=False):
|
||||
with patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError("unreachable")):
|
||||
page = html.fromstring(self.client.get(reverse("computes")).content)
|
||||
# its name and its menu's Overview
|
||||
self.assertEqual(len(page.xpath(f"//tbody//a[@href='{self.url}']")), 2)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import json
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import PropertyMock, patch
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.shortcuts import reverse
|
||||
|
|
@ -51,11 +51,13 @@ class ComputesTestCase(TestCase):
|
|||
except Exception:
|
||||
self.skipTest("Live libvirt compute host not available")
|
||||
|
||||
def test_index(self):
|
||||
@patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError("unreachable"))
|
||||
def test_index(self, connection):
|
||||
response = self.client.get(reverse("computes"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_create_update_delete(self):
|
||||
@patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError("unreachable"))
|
||||
def test_create_update_delete(self, connection):
|
||||
response = self.client.get(reverse("add_socket_host"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
|
|
|||
|
|
@ -135,7 +135,8 @@ class CreateDialogHostsTestCase(TestCase):
|
|||
self.addCleanup(p.stop)
|
||||
|
||||
def dialog(self, reachable):
|
||||
with patch.object(Compute, "status", property(lambda compute: compute.name in reachable)):
|
||||
with patch.object(Compute, "status", property(lambda compute: compute.name in reachable)), \
|
||||
patch.object(Compute, "connection_error", property(lambda c: None if c.name in reachable else "unreachable")):
|
||||
response = self.client.get(reverse("instances:index"))
|
||||
return html.fromstring(response.content).get_element_by_id("AddInstance")
|
||||
|
||||
|
|
|
|||
|
|
@ -10,22 +10,22 @@ from django.contrib.messages import get_messages
|
|||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from instances.models import Instance
|
||||
from libvirt import libvirtError
|
||||
|
||||
ERROR = "Connection Failed: ssh: connect to host 10.0.0.9 port 22: No route to host"
|
||||
ERROR = "Connection Failed: ssh: connect to host 192.0.2.9 port 22: No route to host"
|
||||
|
||||
|
||||
class DetailOfUnreachableHostTests(TestCase):
|
||||
def setUp(self):
|
||||
self.compute = Compute.objects.create(name="far-host", hostname="10.0.0.9", login="root", password="", type=2)
|
||||
self.compute = Compute.objects.create(name="far-host", hostname="192.0.2.9", login="root", password="", type=2)
|
||||
self.vm = Instance.objects.create(compute=self.compute, name="far-vm", uuid="56565656-0000-0000-0000-000000000000")
|
||||
self.owner = get_user_model().objects.create_user("far-owner", password="pw")
|
||||
UserInstance.objects.create(user=self.owner, instance=self.vm)
|
||||
|
||||
def get(self, user):
|
||||
self.client.force_login(user)
|
||||
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=False), patch.object(
|
||||
Compute, "connection_error", new_callable=PropertyMock, return_value=ERROR
|
||||
), patch("instances.models.wvmInstance", side_effect=AssertionError("VM read on a down host")):
|
||||
with patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError(ERROR)), \
|
||||
patch("instances.models.wvmInstance", side_effect=AssertionError("VM read on a down host")):
|
||||
response = self.client.get(reverse("instances:instance", args=[self.vm.id]))
|
||||
self.assertRedirects(response, reverse("instances:index"), fetch_redirect_response=False)
|
||||
return [str(m) for m in get_messages(response.wsgi_request)]
|
||||
|
|
@ -35,7 +35,7 @@ class DetailOfUnreachableHostTests(TestCase):
|
|||
self.assertEqual(len(messages), 1)
|
||||
self.assertIn("far-vm", messages[0])
|
||||
self.assertNotIn("far-host", messages[0])
|
||||
self.assertNotIn("10.0.0.9", messages[0])
|
||||
self.assertNotIn("192.0.2.9", messages[0])
|
||||
self.assertIn("Jobs", messages[0])
|
||||
|
||||
def test_superuser_sees_the_host_and_its_error(self):
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"""Host connection errors on the instance list: administrators see which host
|
||||
and why; other users only learn that a host of their own VMs is unreachable."""
|
||||
|
||||
from unittest.mock import PropertyMock, patch
|
||||
from unittest.mock import MagicMock, PropertyMock, patch
|
||||
|
||||
from accounts.models import UserInstance
|
||||
from computes.models import Compute
|
||||
|
|
@ -18,8 +18,8 @@ class IndexHostAlertTests(TestCase):
|
|||
def setUp(self):
|
||||
User = get_user_model()
|
||||
self.user = User.objects.create_user(username="tenant", password="pwd")
|
||||
self.mine = Compute.objects.create(name="mine-host", hostname="10.0.0.1", type=1, login="u", password="p")
|
||||
self.other = Compute.objects.create(name="other-host", hostname="10.0.0.2", type=1, login="u", password="p")
|
||||
self.mine = Compute.objects.create(name="mine-host", hostname="198.51.100.1", type=1, login="u", password="p")
|
||||
self.other = Compute.objects.create(name="other-host", hostname="198.51.100.2", type=1, login="u", password="p")
|
||||
vm = Instance.objects.create(compute=self.mine, name="tenant-vm", uuid="11111111-1111-1111-1111-111111111111")
|
||||
UserInstance.objects.create(instance=vm, user=self.user)
|
||||
|
||||
|
|
@ -33,7 +33,9 @@ class IndexHostAlertTests(TestCase):
|
|||
self.client.force_login(user)
|
||||
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):
|
||||
), patch.object(Instance, "info", new_callable=PropertyMock, return_value=None), patch.object(
|
||||
Compute, "proxy", new_callable=PropertyMock, return_value=MagicMock(**{"get_domain_rows.return_value": {}})
|
||||
):
|
||||
response = self.client.get(reverse("instances:index"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
return response
|
||||
|
|
@ -41,7 +43,7 @@ class IndexHostAlertTests(TestCase):
|
|||
def test_user_sees_no_infrastructure_details(self):
|
||||
response = self.get(self.user, down={"mine-host", "other-host"})
|
||||
self.assertContains(response, USER_MESSAGE, count=1)
|
||||
for text in ("mine-host", "other-host", "10.0.0.1", "10.0.0.2", "Connection Failed"):
|
||||
for text in ("mine-host", "other-host", "198.51.100.1", "198.51.100.2", "Connection Failed"):
|
||||
self.assertNotContains(response, text)
|
||||
|
||||
def test_user_is_not_told_about_hosts_without_their_vms(self):
|
||||
|
|
@ -52,15 +54,15 @@ class IndexHostAlertTests(TestCase):
|
|||
def test_superuser_sees_each_host_and_its_error(self):
|
||||
admin = get_user_model().objects.create_superuser(username="root", password="pwd", email="r@example.com")
|
||||
response = self.get(admin, down={"mine-host", "other-host"})
|
||||
self.assertContains(response, "Connection Failed: no route to 10.0.0.1")
|
||||
self.assertContains(response, "Connection Failed: no route to 10.0.0.2")
|
||||
self.assertContains(response, "Connection Failed: no route to 198.51.100.1")
|
||||
self.assertContains(response, "Connection Failed: no route to 198.51.100.2")
|
||||
self.assertNotContains(response, USER_MESSAGE)
|
||||
|
||||
def test_global_viewer_sees_each_host_and_its_error(self):
|
||||
viewer = get_user_model().objects.create_user(username="viewer", password="pwd")
|
||||
viewer.user_permissions.add(Permission.objects.get(codename="view_instances"))
|
||||
response = self.get(viewer, down={"other-host"})
|
||||
self.assertContains(response, "Connection Failed: no route to 10.0.0.2")
|
||||
self.assertContains(response, "Connection Failed: no route to 198.51.100.2")
|
||||
|
||||
def test_global_viewer_sees_each_vms_host_without_the_host_page(self):
|
||||
viewer = get_user_model().objects.create_user(username="viewer2", password="pwd")
|
||||
|
|
@ -78,7 +80,9 @@ class IndexHostAlertTests(TestCase):
|
|||
|
||||
self.client.force_login(self.user)
|
||||
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):
|
||||
patch.object(Instance, "info", new_callable=PropertyMock, return_value=None), \
|
||||
patch.object(Compute, "proxy", new_callable=PropertyMock,
|
||||
return_value=MagicMock(**{"get_domain_rows.return_value": {}})):
|
||||
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"])
|
||||
self.assertNotIn("other-host", read)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
from types import SimpleNamespace
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
from libvirt import libvirtError
|
||||
from vrtManager.util import OperationError
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TransactionTestCase
|
||||
|
|
@ -72,8 +73,10 @@ class SnapshotJobsTestCase(TransactionTestCase):
|
|||
|
||||
def test_a_name_that_is_empty_or_not_a_snapshot_name_is_refused_on_the_page(self):
|
||||
for name in ("", ".hidden", "a b"):
|
||||
response = self.client.post(reverse("instances:snapshot", args=[self.instance.pk]),
|
||||
{"name": name, "submission_key": "key-snap0001"}, follow=True)
|
||||
# the VM page it goes back to, of a host that is not there
|
||||
with patch.object(Compute, "connection", new_callable=PropertyMock, return_value=libvirtError("unreachable")):
|
||||
response = self.client.post(reverse("instances:snapshot", args=[self.instance.pk]),
|
||||
{"name": name, "submission_key": "key-snap0001"}, follow=True)
|
||||
self.assertContains(response, "Snapshot names are 1-64 letters")
|
||||
self.assertFalse(Operation.objects.exists())
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ class VmStatesTestCase(TestCase):
|
|||
proxy.get_domain_states.side_effect = read
|
||||
# self.up answers, self.down cannot be reached
|
||||
with patch.object(Compute, "status", property(lambda c: c.pk == self.up.pk)), \
|
||||
patch.object(Compute, "connection_error", property(lambda c: None if c.pk == self.up.pk else "unreachable")), \
|
||||
patch.object(Compute, "proxy", new_callable=PropertyMock, return_value=proxy):
|
||||
return self.client.get(reverse("instances:states") + query)
|
||||
|
||||
|
|
@ -82,6 +83,7 @@ class VmStatesTestCase(TestCase):
|
|||
def test_the_lists_poll_it(self):
|
||||
self.client.force_login(self.admin)
|
||||
with patch.object(Compute, "status", property(lambda c: c.pk == self.up.pk)), \
|
||||
patch.object(Compute, "connection_error", property(lambda c: None if c.pk == self.up.pk else "unreachable")), \
|
||||
patch.object(Compute, "proxy", new_callable=PropertyMock,
|
||||
return_value=MagicMock(**{"get_domain_rows.return_value": {}})), \
|
||||
patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue