1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00
webvirtcloud/vmbackups/tests/test_policies.py
catborise a145c07601 feat(vmbackups): the Schedules pages
Backups -> Schedules lists the backup schedules and adds one: a name, a
target, every VM of a compute or chosen VMs (a searchable list), a
daily time in the panel's time zone (the form names it), how many
backups of each VM to keep, Require frozen file systems, Enabled. The
VMs' hosts must have a pool of the target. The superuser who saves a
schedule is its owner, whom its jobs run as. A schedule's page edits or
deletes it (its backups are kept) and lists its runs: the backup job,
or why the day's backup was skipped or failed.

The Backups page has a Schedule column (or By hand) and a Missing state;
a missing backup offers no Restore. A VM's Backups tab names the
schedules that cover it. README: a paragraph on schedules.

A target with schedules is not deleted: its page says to delete them
first.
2026-11-17 11:38:35 +03:00

135 lines
7.4 KiB
Python

"""The Schedules pages: add, edit, delete a backup schedule; its runs."""
import datetime
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from lxml import html as lxml_html
from computes.models import Compute
from instances.models import Instance
from instances.tests.test_page_controls import PageControls
from logs.models import Logs
from vmbackups.models import Backup, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool
class PolicyPageTests(TestCase):
def setUp(self):
self.admin = get_user_model().objects.create_superuser("pp_admin", "a@example.com", "pw")
self.client.force_login(self.admin)
self.c1 = Compute.objects.create(name="h1", hostname="192.0.2.81", login="u", password="p", type=1)
self.c2 = Compute.objects.create(name="h2", hostname="192.0.2.82", login="u", password="p", type=1)
self.vm1 = Instance.objects.create(compute=self.c1, name="vm1", uuid="81818181-0000-4000-8000-000000000001")
self.vm3 = Instance.objects.create(compute=self.c2, name="vm3", uuid="81818181-0000-4000-8000-000000000003")
self.target = BackupTarget.objects.create(name="nfs1")
BackupTargetPool.objects.create(target=self.target, compute=self.c1, pool="b1", pool_uuid="u")
def post(self, **fields):
data = {"name": "nightly", "target": self.target.pk, "time": "02:00", "keep": "7", "enabled": "on", **fields}
return self.client.post(reverse("vmbackups:policies"), data)
def test_a_schedule_of_chosen_vms(self):
response = self.post(instances=[self.vm1.pk])
policy = BackupPolicy.objects.get()
self.assertRedirects(response, reverse("vmbackups:policy", args=[policy.pk]), fetch_redirect_response=False)
self.assertEqual((list(policy.instances.all()), policy.compute, policy.time, policy.keep, policy.owner),
([self.vm1], None, datetime.time(2), 7, self.admin))
self.assertTrue(Logs.objects.filter(message__contains="Backup schedule nightly").exists())
def test_a_schedule_of_a_compute(self):
self.post(compute=self.c1.pk)
self.assertEqual(BackupPolicy.objects.get().compute, self.c1)
def test_compute_or_vms_not_both_nor_neither(self):
for fields in ({}, {"compute": self.c1.pk, "instances": [self.vm1.pk]}):
with self.subTest(fields):
response = self.post(**fields)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Choose a compute or VMs")
self.assertFalse(BackupPolicy.objects.exists())
def test_vms_on_a_host_without_the_target_are_refused(self):
response = self.post(instances=[self.vm3.pk])
self.assertContains(response, "has no storage pool on h2")
response = self.post(compute=self.c2.pk)
self.assertContains(response, "has no storage pool on h2")
self.assertFalse(BackupPolicy.objects.exists())
def test_keep_at_least_one(self):
self.assertEqual(self.post(instances=[self.vm1.pk], keep="0").status_code, 200)
self.assertFalse(BackupPolicy.objects.exists())
def schedule(self):
policy = BackupPolicy.objects.create(name="nightly", target=self.target, time=datetime.time(2), keep=7,
owner=self.admin)
policy.instances.add(self.vm1)
return policy
def test_edit_and_the_runs(self):
policy = self.schedule()
BackupRun.objects.create(policy=policy, instance=self.vm1, day=datetime.date.today(), state=BackupRun.SKIPPED,
note="The host was in maintenance")
url = reverse("vmbackups:policy", args=[policy.pk])
page = self.client.get(url)
self.assertContains(page, "The host was in maintenance")
self.client.post(url, {"name": "nightly", "target": self.target.pk, "time": "03:30", "keep": "3",
"instances": [self.vm1.pk]})
policy.refresh_from_db()
self.assertEqual((policy.time, policy.keep, policy.enabled), (datetime.time(3, 30), 3, False))
def test_delete_keeps_the_backups(self):
policy = self.schedule()
backup = Backup.objects.create(target=self.target, instance=self.vm1, vm_uuid=self.vm1.uuid, vm_name="vm1",
compute_name="h1", state=Backup.READY, policy=policy)
response = self.client.post(reverse("vmbackups:policy", args=[policy.pk]), {"delete": "1"})
self.assertRedirects(response, reverse("vmbackups:policies"), fetch_redirect_response=False)
self.assertFalse(BackupPolicy.objects.exists())
self.assertTrue(Backup.objects.filter(pk=backup.pk, policy=None).exists())
def test_the_list_and_the_backups_page(self):
policy = self.schedule()
Backup.objects.create(target=self.target, instance=self.vm1, vm_uuid=self.vm1.uuid, vm_name="vm1",
compute_name="h1", state=Backup.MISSING, policy=policy)
page = lxml_html.fromstring(self.client.get(reverse("vmbackups:policies")).content)
self.assertIn("nightly", page.xpath("string(//table)"))
index = lxml_html.fromstring(self.client.get(reverse("vmbackups:index")).content)
row = index.xpath("string(//table[@id='backups']/tbody/tr)")
self.assertIn("nightly", row)
self.assertIn("Missing", row)
self.assertFalse(index.xpath("//a[contains(@href, '/restore/')]")) # a missing backup is not restored
def test_a_target_with_a_schedule_is_not_deleted(self):
self.schedule()
url = reverse("vmbackups:target", args=[self.target.pk])
from django.contrib.messages import get_messages
response = self.client.post(url, {"delete": "1"})
self.assertRedirects(response, url, fetch_redirect_response=False)
self.assertIn("Backup target nfs1 has schedules; delete them first",
[str(m) for m in get_messages(response.wsgi_request)])
self.assertTrue(BackupTarget.objects.filter(pk=self.target.pk).exists())
def test_only_superusers(self):
policy = self.schedule()
get_user_model().objects.create_user("pp_user", password="pw")
self.client.force_login(get_user_model().objects.get(username="pp_user"))
self.assertEqual(self.client.get(reverse("vmbackups:policies")).status_code, 403)
self.assertEqual(self.client.get(reverse("vmbackups:policy", args=[policy.pk])).status_code, 403)
class PolicyTabTests(PageControls, TestCase):
def test_the_vm_tab_names_its_schedules(self):
admin = get_user_model().objects.create_superuser("pt-admin", "a@example.com", "x")
target = BackupTarget.objects.create(name="nfs1")
BackupTargetPool.objects.create(target=target, compute=self.compute, pool="b1", pool_uuid="u")
chosen = BackupPolicy.objects.create(name="chosen", target=target, time=datetime.time(2), keep=2, owner=admin)
chosen.instances.add(self.instance)
BackupPolicy.objects.create(name="whole-host", target=target, compute=self.compute, time=datetime.time(3), keep=2,
owner=admin)
with patch("vmbackups.utils.wvmBackup"):
self.controls(admin)
tab = lxml_html.fromstring(self.last_page).xpath("string(//div[@id='backups'])")
self.assertIn("chosen", tab)
self.assertIn("whole-host", tab)