1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(vmbackups): a schedule shows how its last 14 days of backups went

The Schedules list marks each of the last 14 days by its worst run
(failed, skipped, running, backed up), the day's counts on hover. A
schedule's page opens with a grid of its VMs by day, each mark linking
to its job, and lists the runs that did not back up with their reason;
the settings form follows. A run's outcome now includes how its job
ended: before, every submitted run read "Backup job", a failed one too.
This commit is contained in:
catborise 2026-11-24 13:38:23 +03:00
parent 9809e5b79a
commit ab70ea2a62
11 changed files with 348 additions and 78 deletions

View file

@ -12,6 +12,7 @@ from computes.models import Compute, Tag
from instances.models import Instance
from instances.tests.test_page_controls import PageControls
from logs.models import Logs
from operations.models import Operation
from operations.utils import say
from vmbackups.models import Backup, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool
@ -119,6 +120,49 @@ class PolicyPageTests(TestCase):
self.assertContains(page, "Backup schedule nightly is saved.")
self.assertContains(page, "3 + 4 weekly + 6 monthly")
def add_run(self, policy, vm, days_ago, state=BackupRun.SUBMITTED, job_state=None, note=(), job_error=()):
day = datetime.date.today() - datetime.timedelta(days=days_ago)
job = job_state and Operation.objects.create(kind="backup.create", username="pp_admin", submission_key=f"{vm.pk}-{day}",
resource="r", state=job_state, error=list(job_error))
return BackupRun.objects.create(policy=policy, instance=vm, day=day, state=state, operation=job, note=list(note))
def test_the_runs_of_the_last_14_days(self):
policy = self.schedule()
self.add_run(policy, self.vm1, 0, job_state=Operation.SUCCEEDED)
failed = self.add_run(policy, self.vm1, 1, job_state=Operation.FAILED, job_error=say("The guest agent did not answer"))
self.add_run(policy, self.vm1, 2, BackupRun.SKIPPED, note=say("The host was in maintenance"))
self.add_run(policy, self.vm1, 14, BackupRun.FAILED) # before the 14 days
self.add_run(policy, self.vm1, -1, BackupRun.FAILED) # after them: the time zone moved west
self.add_run(policy, self.vm3, 3, job_state=Operation.SUCCEEDED) # a VM the schedule no longer backs up
page = lxml_html.fromstring(self.client.get(reverse("vmbackups:policy", args=[policy.pk])).content)
rows = page.xpath("//table[contains(@class, 'run-grid')]/tbody/tr")
self.assertEqual([row.xpath("string(th)") for row in rows], ["vm1", "vm3"])
marks = [mark.get("class").split()[1] for mark in rows[0].xpath(".//*[contains(@class, 'run-mark')]")]
self.assertEqual(marks, ["run-none"] * 11 + ["run-skipped", "run-failed", "run-succeeded"])
self.assertEqual(rows[0].xpath(".//a[contains(@class, 'run-failed')]/@href"),
[reverse("operations:job", args=[failed.operation_id])])
problems = [" ".join(item.text_content().split()) for item in page.xpath("//ul[@id='run-problems']/li")]
self.assertEqual(len(problems), 2)
self.assertIn("vm1 Failed: The guest agent did not answer", problems[0])
self.assertIn("vm1 Skipped: The host was in maintenance", problems[1])
def test_the_list_marks_a_day_by_its_worst_run(self):
policy = self.schedule()
policy.instances.add(self.vm3)
self.add_run(policy, self.vm1, 1, job_state=Operation.SUCCEEDED)
self.add_run(policy, self.vm3, 1, job_state=Operation.FAILED)
self.add_run(policy, self.vm1, 0, job_state=Operation.RUNNING)
self.add_run(policy, self.vm3, 0) # its job gone from the job history
page = lxml_html.fromstring(self.client.get(reverse("vmbackups:policies")).content)
marks = page.xpath("//span[contains(@class, 'run-days')]/span")
self.assertEqual(len(marks), 14)
yesterday = (datetime.date.today() - datetime.timedelta(days=1)).isoformat()
self.assertEqual((marks[-2].get("class"), marks[-2].get("title")),
("run-mark run-failed", f"{yesterday}: 1 failed, 1 backed up"))
self.assertEqual(marks[-1].get("class"), "run-mark run-running")
self.assertTrue(marks[-1].get("title").endswith(": 1 running, 1 started"))
self.assertEqual(marks[0].get("class"), "run-mark run-none")
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",

View file

@ -6,6 +6,7 @@ from django.test import TestCase
from computes.models import Compute
from instances.models import Instance
from operations.models import Operation
from vmbackups.models import Backup, BackupPolicy, BackupRun, BackupTarget
@ -24,6 +25,21 @@ class PolicyModelTests(TestCase):
with self.assertRaises(IntegrityError):
BackupRun.objects.create(policy=self.policy, instance=self.vm, day=datetime.date(2026, 11, 17))
def test_a_run_comes_to_what_its_job_did(self):
def outcome(state, job_state=None):
job = job_state and Operation.objects.create(kind="backup.create", username="pm_admin", submission_key=job_state,
resource="r", state=job_state)
return BackupRun(policy=self.policy, instance=self.vm, state=state, operation=job).outcome
self.assertEqual(outcome(BackupRun.SKIPPED), BackupRun.SKIPPED)
self.assertEqual(outcome(BackupRun.FAILED), BackupRun.FAILED)
self.assertEqual(outcome(BackupRun.SUBMITTED, Operation.SUCCEEDED), BackupRun.SUCCEEDED)
self.assertEqual(outcome(BackupRun.SUBMITTED, Operation.QUEUED), BackupRun.RUNNING)
self.assertEqual(outcome(BackupRun.SUBMITTED, Operation.RUNNING), BackupRun.RUNNING)
for job_state in (Operation.FAILED, Operation.CANCELLED, Operation.UNCERTAIN): # no backup came of it
self.assertEqual(outcome(BackupRun.SUBMITTED, job_state), BackupRun.FAILED)
self.assertEqual(outcome(BackupRun.SUBMITTED), BackupRun.STARTED) # its job is gone from the job history
def test_a_deleted_policy_keeps_its_backups(self):
backup = Backup.objects.create(target=self.target, instance=self.vm, vm_uuid=self.vm.uuid, vm_name="vm1",
compute_name="h1", policy=self.policy, state=Backup.READY)