1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00
webvirtcloud/logs/tests/test_logs.py
catborise c1d36053d6 feat(logs): every VM operation writes an audit row the VM's Logs tab finds by UUID
Operations on a VM now write their rows the way the admin audit does: one
row per operation, done or failed with a safe reason, with the user, IP and
host. Each row names the VM ("VM web-1: delete snapshot snap1") and carries
its UUID in the new Logs.instance_uuid column.

- audit_vm and failed_vm wrap audit() and failed() for a VM. A failure the
  view turns into a message is caught outside the block, so its failed row
  is written; busy claims, permission and quota refusals and invalid input
  write nothing. No password, key or XML goes into a row.
- All VM page operations move from addlogmsg to audit_vm, and addlogmsg
  goes. QoS changes and defining a VM from XML are logged now; a gstfsd
  error reply (root password, SSH key) is a failed row, not a done one.
- Partial results are rows of their own: the VM delete, then each disk
  delete; the disk detach, then the volume delete; the migration, then a
  failed autostart restore, NVRAM removal or record on the target.
- Migration rows use the compute name as the host column, as every other
  row does.
- VM job rows (create, clone, snapshots) and their resolutions carry the
  VM's UUID; snapshot job intents now record it. The owner rows of the VM
  page and the accounts pages, and the ownership system rows, carry it too.
- The Logs tab loads vm_logs/<pk>/ and shows the rows with the VM's UUID,
  so a rename keeps the history, two VMs with one name are told apart and
  a VM on two computes is one VM. It gains a Result column. Rows from
  before this change have no UUID and stay on the Logs page only.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-24 14:14:53 +03:00

60 lines
3.3 KiB
Python

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from computes.models import Compute
from instances.models import Instance
from logs.audit import write
UUID = "12345678-1234-1234-1234-123456789012"
class VMLogsTestCase(TestCase):
"""The VM page's Logs tab: the rows of one VM, found by its UUID."""
def setUp(self):
User = get_user_model()
self.superuser = User.objects.create_superuser("admin_logs", "admin@example.com", "pw")
self.normal_user = User.objects.create_user("normal_logs", "user@example.com", "pw")
self.compute = Compute.objects.create(name="c1", hostname="192.0.2.1", type=1)
self.instance = Instance.objects.create(compute=self.compute, name="test-vm", uuid=UUID)
self.client.force_login(self.superuser)
def shown(self, instance=None):
response = self.client.get(reverse("vm_logs", args=[(instance or self.instance).pk]))
self.assertEqual(response.status_code, 200)
return [(row["message"], row["result"]) for row in response.json()]
def test_rows_of_the_vm_with_their_result(self):
write("admin", "c1", "test-vm", "VM test-vm: power on", True, uuid=UUID)
write("admin", "c1", "test-vm", "VM test-vm: power off: not running", False, uuid=UUID)
row = self.client.get(reverse("vm_logs", args=[self.instance.pk])).json()[0]
self.assertEqual((row["user"], row["host"], row["instance"]), ("admin", "c1", "test-vm"))
self.assertEqual(self.shown(), [("VM test-vm: power off: not running", "Failed"),
("VM test-vm: power on", "Done")])
def test_a_vm_is_found_by_uuid_not_by_name(self):
other = Compute.objects.create(name="c2", hostname="192.0.2.2", type=1)
namesake = Instance.objects.create(compute=other, name="test-vm", uuid="87654321-4321-4321-4321-210987654321")
write("admin", "c1", "old-name", "VM old-name: power on", True, uuid=UUID) # before a rename
write("admin", "c2", "test-vm", "VM test-vm: power on", True, uuid=namesake.uuid)
write("admin", "c1", "test-vm", "Instance is created") # a row from before the UUID column
self.assertEqual(self.shown(), [("VM old-name: power on", "Done")])
self.assertEqual(self.shown(namesake), [("VM test-vm: power on", "Done")])
def test_one_vm_on_two_computes_shows_the_rows_of_both(self):
other = Compute.objects.create(name="c2", hostname="192.0.2.2", type=1)
moved = Instance.objects.create(compute=other, name="test-vm", uuid=UUID)
write("admin", "c1", "test-vm", "VM test-vm: migrate (live) to 192.0.2.2", True, uuid=UUID)
write("admin", "c2", "test-vm", "VM test-vm: power off", None, uuid=UUID)
self.assertEqual(self.shown(moved), [("VM test-vm: power off", "Unknown"),
("VM test-vm: migrate (live) to 192.0.2.2", "Done")])
self.assertEqual(self.shown(moved), self.shown())
def test_unknown_vm(self):
self.assertEqual(self.client.get(reverse("vm_logs", args=[999999])).status_code, 404)
def test_non_superuser_forbidden(self):
self.client.force_login(self.normal_user)
response = self.client.get(reverse("vm_logs", args=[self.instance.pk]))
self.assertNotEqual(response.status_code, 200)