1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vmbackups/tests/test_policy_models.py
catborise 1219a37cc4 feat(vmbackups): backup schedules' records; the worker runs the apps' periodic tasks
A backup schedule (BackupPolicy) names its VMs, chosen or every VM of a
compute at the time it runs (templates left out), a target, a daily time
in the panel's time zone, how many backups of each VM to keep (one at
least), whether frozen file systems are required, and the superuser its
jobs run as. A run (BackupRun) records one VM's backup of one day, keyed
by the VM's row, not its UUID, which two VM rows may share. A backup
keeps the schedule that made it; deleting the schedule keeps its
backups. A backup whose files are gone can be marked missing, and a
target keeps when it was last checked.

The worker runs the tasks apps register with register_periodic about
once a minute, on a thread of their own, one pass at a time, each
guarded: a pass stuck on a host takes no thread recovery or a cancel
needs, and one failing task stops none of the others. Nothing registers
one yet.

backup.create takes the schedule that asked for it; one deleted while
the job was queued leaves the backup without a schedule.
2026-11-17 10:18:04 +03:00

44 lines
2.4 KiB
Python

import datetime
from django.contrib.auth import get_user_model
from django.db import IntegrityError
from django.test import TestCase
from computes.models import Compute
from instances.models import Instance
from vmbackups.models import Backup, BackupPolicy, BackupRun, BackupTarget
class PolicyModelTests(TestCase):
def setUp(self):
self.admin = get_user_model().objects.create_superuser("pm_admin", "a@example.com", "pw")
self.compute = Compute.objects.create(name="h1", hostname="192.0.2.61", login="u", password="p", type=1)
self.vm = Instance.objects.create(compute=self.compute, name="vm1", uuid="61616161-0000-4000-8000-000000000001")
self.target = BackupTarget.objects.create(name="nfs1")
self.policy = BackupPolicy.objects.create(name="nightly", target=self.target, time=datetime.time(2, 0), keep=7,
owner=self.admin)
self.policy.instances.add(self.vm)
def test_one_run_per_policy_vm_and_day(self):
BackupRun.objects.create(policy=self.policy, instance=self.vm, day=datetime.date(2026, 11, 17))
with self.assertRaises(IntegrityError):
BackupRun.objects.create(policy=self.policy, instance=self.vm, day=datetime.date(2026, 11, 17))
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)
self.policy.delete()
backup.refresh_from_db()
self.assertIsNone(backup.policy)
def test_the_vms_of_a_policy(self):
Instance.objects.create(compute=self.compute, name="vm2", uuid="61616161-0000-4000-8000-000000000002")
Instance.objects.create(compute=self.compute, name="tpl", uuid="61616161-0000-4000-8000-000000000003",
is_template=True)
self.assertEqual([vm.name for vm in self.policy.vms()], ["vm1"])
whole = BackupPolicy.objects.create(name="host", target=self.target, compute=self.compute, time=datetime.time(3),
keep=3, owner=self.admin)
self.assertEqual([vm.name for vm in whole.vms()], ["vm1", "vm2"]) # the compute's VMs now, no template
def test_missing_state(self):
self.assertIn(Backup.MISSING, dict(Backup.STATES))