1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/admin/tests/test_tombstones.py
catborise 981ff7b294 fix: a backup's files say so on the pool page; Removed VMs names schedules and backups
On a backup target's pool every backup file was "Not used on this host" and
listed with the unused volumes, though its deletion there is refused: it is
deleted with its backup. The column now says "Backup of <VM>" and counts it
as in use. A VM kept on the Removed VMs page for its schedules or backups
alone showed no owner and no reason; its row now names its schedules and
counts its backups.
2026-11-20 09:03:52 +03:00

85 lines
4.4 KiB
Python

"""Superusers list the ownership kept for VMs that vanished and can remove it."""
from datetime import time, timedelta
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
from instances.models import InstanceTombstone
from logs.models import Logs
from lxml import html
from computes.models import Tag
from vmbackups.models import Backup, BackupPolicy, BackupTarget
UUID = "00000000-0000-4000-8000-000000000001"
class TombstoneTests(TestCase):
def setUp(self):
self.client.force_login(User.objects.create_superuser("ts-admin", "ts@example.com", "pw"))
self.owner = User.objects.create_user("ts-owner", password="pw")
self.tombstone = InstanceTombstone.objects.create(
uuid=UUID,
name="ts-vm",
host="ts-host",
tags=["prod"],
owners=[
{"user": self.owner.id, "is_change": True, "is_delete": False, "is_vnc": True},
{"user": 999999, "is_change": False, "is_delete": False, "is_vnc": False},
],
)
@override_settings(INSTANCE_OWNERSHIP_RETENTION_DAYS=7)
def test_the_list_shows_the_vm_its_owners_and_when_it_expires(self):
removed = timezone.now() - timedelta(days=2)
InstanceTombstone.objects.filter(pk=self.tombstone.pk).update(removed=removed)
response = self.client.get(reverse("admin:tombstone_list"))
self.assertEqual(response.status_code, 200)
row = response.context["tombstones"][0]
self.assertEqual(row.owner_names, ["ts-owner", "deleted user"])
self.assertEqual(row.expires, removed + timedelta(days=7))
self.assertContains(response, "ts-vm")
self.assertContains(response, UUID)
self.assertContains(response, "ts-owner")
self.assertContains(response, "prod")
self.assertContains(response, '<span class="fw-semibold">ts-host</span>', html=True)
def test_only_superusers_see_or_remove_them(self):
self.client.force_login(self.owner)
self.assertEqual(self.client.get(reverse("admin:tombstone_list")).status_code, 403)
url = reverse("admin:tombstone_delete", args=[self.tombstone.pk])
self.assertEqual(self.client.post(url).status_code, 403)
self.assertTrue(InstanceTombstone.objects.exists())
def test_the_list_names_its_schedules_and_counts_its_backups(self):
# a VM kept for its backups or schedules alone has no owner to show; only what would come back is told
target = BackupTarget.objects.create(name="t")
chosen, by_tag = (BackupPolicy.objects.create(name=name, target=target, time=time(2), keep=1)
for name in ("nightly", "by-tag"))
BackupPolicy.objects.filter(pk=by_tag.pk).update(tag=Tag.objects.create(name="web"))
kept = [Backup.objects.create(target=target, vm_uuid=UUID, vm_name="ts-vm", compute_name="ts-host").pk
for _ in range(2)]
InstanceTombstone.objects.filter(pk=self.tombstone.pk).update(
owners=[], tags=[], policies=[chosen.pk, by_tag.pk, 999999], backups=[*kept, 999999])
page = html.fromstring(self.client.get(reverse("admin:tombstone_list")).content)
(row,) = page.xpath("//div[@data-search][contains(@data-search, 'ts-vm')]")
text = " ".join(row.text_content().split())
self.assertIn("Schedules: nightly · 2 backups", text)
self.assertNotIn("by-tag", text)
def test_a_get_only_asks_for_confirmation(self):
response = self.client.get(reverse("admin:tombstone_delete", args=[self.tombstone.pk]))
self.assertContains(response, "ts-vm")
self.assertContains(response, "Its owners, tags, protection and schedules no longer come back if its UUID shows up again, nor are its backups linked to it again.")
self.assertTrue(InstanceTombstone.objects.exists())
def test_a_post_removes_it(self):
response = self.client.post(reverse("admin:tombstone_delete", args=[self.tombstone.pk]), follow=True)
self.assertRedirects(response, reverse("admin:tombstone_list"))
self.assertContains(response, "Removed VM ts-vm deleted")
self.assertFalse(InstanceTombstone.objects.exists())
# the VM's history (found by UUID) has it
self.assertEqual(list(Logs.objects.filter(message__startswith="Removed VM").values_list("host", "instance", "instance_uuid")),
[("ts-host", "ts-vm", UUID)])