1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 08:26:03 +00:00

test: each test is in the tests package of the code it tests

Tests that build vrtManager objects and touch no view, form or model of
an app lived in that app; vrtManager/tests already holds such tests, so
the convention was split. They move to vrtManager/tests: the firmware,
CPU mode and PCIe port definitions of a new VM (test_create), USB and PCI
host devices, the console listener, host CPU reads, interface reads,
deletes and XML, network filters, PCI passthrough rules, node devices,
network changes, addresses and leases, pool XML, secrets, and the live
tests that use vrtManager only (disk resize, ISO, snapshots, XML, live
clone). livetest.py, the live-test helper six apps import, and the
network test driver move with them.

Others go to their app: the quota with a host down to instances, the
401/403 Logs rows (webvirtcloud's middleware) to webvirtcloud, the VM
page's tab script test to instances, novncd's own tests out of the
console views tests, the compute lock and input validation tests out of
the computes views tests, the interface views out of the forms tests.
instances/tests/test_views.py, entirely live, is test_live_views.py.

Test modules imported fixtures from each other; shared ones are now in
vrtManager/tests/fakes.py (migration fakes, node devices, IOMMU groups,
leases, a NAT network, a Ceph secret), interfaces/tests/fakes.py,
console/tests/daemons.py (novncd loaded once), livetest.wait_for and
instances/tests/create_page.BODY. Every live test checks
livetest.enabled(). The suite runs the same 2097 tests.
This commit is contained in:
catborise 2026-11-06 11:33:42 +03:00
parent 4e0952aa50
commit bdc8a6755c
73 changed files with 2231 additions and 2132 deletions

View file

@ -1,77 +0,0 @@
// node instance_tabs.test.js path/to/static/js/instance_tabs.js
const assert = require("assert");
const {restoreInstanceTab} = require(process.argv[2]);
// A minimal DOM: elements with an id, classes, a parent, children, and the
// few selectors restoreInstanceTab uses.
function el(id, classes, children = [], attrs = {}) {
const node = {id, classList: new Set(classes), children, attrs, parentElement: null};
node.classList.contains = (c) => node.classList.has(c);
node.getAttribute = (name) => attrs[name];
for (const child of children) child.parentElement = node;
node.querySelector = (selector) => find(node, selector);
return node;
}
function* walk(node) {
for (const child of node.children) {
yield child;
yield* walk(child);
}
}
function matches(node, selector) {
if (selector === "[role=tablist]") return node.attrs.role === "tablist";
if (selector === ".nav-link.active") return node.classList.has("nav-link") && node.classList.has("active");
if (selector === ".nav-link:not(#osinfo-tab)") return node.classList.has("nav-link") && node.id !== "osinfo-tab";
if (selector === "#osinfo-tab") return node.id === "osinfo-tab";
throw new Error("unsupported selector " + selector);
}
function find(root, selector) {
for (const node of walk(root)) if (matches(node, selector)) return node;
return null;
}
function link(target, extra = []) {
return el(target === "#osinfo" ? "osinfo-tab" : "", ["nav-link", ...extra], [], {"data-bs-target": target});
}
// The VM page: top-level panes, Settings with its own tabs and panes.
function page(settingsLinks) {
const settings = el("settings", ["tab-pane"], [
el("", ["nav", "nav-underline"], settingsLinks, {role: "tablist"}),
el("", ["tab-content"], [el("osinfo", ["tab-pane"]), el("options", ["tab-pane"]), el("vncsettings", ["tab-pane"])]),
]);
const root = el("", [], [el("", ["tab-content"], [el("power", ["tab-pane", "active"]), el("access", ["tab-pane"]), settings])]);
root.getElementById = (id) => {
for (const node of walk(root)) if (node.id === id) return node;
return null;
};
return root;
}
function run(doc, hash) {
const shown = [];
let loaded = 0;
restoreInstanceTab(doc, hash, (target) => shown.push(target), () => loaded++);
return {shown, loaded};
}
const settled = () => page([link("#options", ["active"])]);
// a top-level pane opens directly
assert.deepStrictEqual(run(settled(), "#access").shown, ["#access"]);
// a pane inside Settings opens Settings first
assert.deepStrictEqual(run(settled(), "#options").shown, ["#settings", "#options"]);
// no hash, an unknown one, or one with odd characters opens nothing
for (const hash of ["", "#", "#nosuchpane", '#a"]b']) {
assert.deepStrictEqual(run(settled(), hash).shown, [], hash);
}
// Settings without an active section opens its first one, not OS info
assert.deepStrictEqual(run(page([link("#osinfo"), link("#vncsettings")]), "").shown, ["#vncsettings"]);
// OS info as the only section is selected and loaded
assert.deepStrictEqual(run(page([link("#osinfo")]), ""), {shown: ["#osinfo"], loaded: 1});
// a redirect to OS info loads it
assert.deepStrictEqual(run(settled(), "#osinfo"), {shown: ["#settings", "#osinfo"], loaded: 1});
console.log("ok");

View file

@ -0,0 +1,26 @@
"""A 401 or 403 answered to a signed-in user or to a request with an API
token leaves a Logs row; an anonymous one does not."""
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from logs.models import Logs
class AccessDeniedTests(TestCase):
def test_a_page_refused_to_a_signed_in_user_is_logged(self):
self.client.force_login(get_user_model().objects.create_user("plain"))
response = self.client.get(reverse("admin:user_list"), HTTP_X_REAL_IP="192.0.2.7")
self.assertEqual(response.status_code, 403)
row = Logs.objects.get(message__startswith="Access denied")
self.assertEqual((row.user, row.success, row.ip), ("plain", False, "192.0.2.7"))
self.assertEqual(row.message, "Access denied (403): GET /admin/users/")
def test_a_refused_api_token_is_logged(self):
response = self.client.get(reverse("instance-list"), HTTP_AUTHORIZATION="Token wvc_" + "0" * 64)
self.assertEqual(response.status_code, 401)
self.assertEqual(Logs.objects.get(message__startswith="Access denied").message, "Access denied (401): GET /api/v1/instances/")
def test_an_anonymous_refusal_is_not_logged(self):
self.assertEqual(self.client.get(reverse("instance-list")).status_code, 401)
self.assertFalse(Logs.objects.filter(message__startswith="Access denied").exists())

View file

@ -1,23 +0,0 @@
"""The VM page opens the tab a redirect points to (static/js/instance_tabs.js).
Its behaviour is checked with Node."""
import shutil
import subprocess
import unittest
from pathlib import Path
from django.conf import settings
BASE = Path(settings.BASE_DIR)
class InstanceTabsTestCase(unittest.TestCase):
@unittest.skipUnless(shutil.which("node"), "node is not installed")
def test_instance_tabs_js(self):
result = subprocess.run(
["node", str(BASE / "webvirtcloud/tests/js/instance_tabs.test.js"), str(BASE / "static/js/instance_tabs.js")],
capture_output=True,
text=True,
timeout=60,
)
self.assertEqual(result.returncode, 0, result.stderr)

View file

@ -8,7 +8,7 @@ import libvirt
from computes.models import Compute
from django.contrib.auth import get_user_model
from django.test import TestCase
from networks.tests.driver import without_inactive_flag
from vrtManager.tests.driver import without_inactive_flag
from django.urls import NoReverseMatch, URLPattern, URLResolver, get_resolver, reverse
from instances.models import Flavor, Instance
from lxml import html