Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Dec 24, 2024
1 parent 485b8e6 commit 568c70e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
31 changes: 25 additions & 6 deletions tests/fixtures/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import requests


def mock_api_requests(nodes, vms, backups=[]):
routes = generate_routes(nodes, vms, backups)
def mock_api_requests(nodes, vms, backup_jobs=[], storage_content=[]):
routes = generate_routes(nodes, vms, backup_jobs, storage_content)

def side_effect(method, url, *args, **kwargs):
print(f"{method} {url}")
Expand All @@ -21,14 +21,15 @@ def side_effect(method, url, *args, **kwargs):
return side_effect


def generate_routes(nodes, vms, backups):
def generate_routes(nodes, vms, backup_jobs, storage_content):
routes = {
"/api2/json/cluster/status": [
{"type": "cluster", "version": 2, "quorate": 1, "nodes": len(nodes), "id": "cluster", "name": "devel"},
*[n["status"] for n in nodes],
],
"/api2/json/cluster/resources": [
*[n["resource"] for n in nodes],
*[storage_resource("s3", n["status"]["name"]) for n in nodes],
*vms,
],
"/api2/json/nodes": [
Expand All @@ -38,7 +39,9 @@ def generate_routes(nodes, vms, backups):
"/api2/json/cluster/ha/groups": [],
"/api2/json/cluster/ha/status/manager_status": [],
"/api2/json/cluster/ha/resources": [],
"/api2/json/cluster/backup": backups,
"/api2/json/cluster/backup": backup_jobs,
"/api2/json/nodes/pve-devel-1/storage/s3/content": storage_content,
"/api2/json/nodes/pve-devel-2/storage/s3/content": storage_content,
**generate_vm_routes(nodes, vms),
}

Expand Down Expand Up @@ -168,7 +171,7 @@ def vm(id, node, status="running"):
}


def backup_job(job_id, vmids):
def backup_job(job_id, vmid):
return {
"id": f"backup-d71917f0-{job_id:04x}",
"prune-backups": {"keep-last": "3"},
Expand All @@ -180,11 +183,26 @@ def backup_job(job_id, vmids):
"type": "vzdump",
"next-run": 1735430400,
"mode": "snapshot",
"vmid": ",".join([str(v) for v in vmids]),
"vmid": vmid,
"compress": "zstd",
}


def storage_resource(name, node_name):
return {
"content": "snippets,images,iso,backup,rootdir,vztmpl",
"id": f"storage/{node_name}/{name}",
"disk": 0,
"storage": name,
"shared": 1,
"status": "available",
"maxdisk": 33601372160,
"type": "storage",
"node": node_name,
"plugintype": "s3",
}


def storage_content(volid, content, ctime, format, options):
return {
"volid": volid,
Expand All @@ -200,6 +218,7 @@ def backup(storage, vmid, created_at):
created_at_str = created_at.strftime("%Y_%m_%d-%H_%M_%S")
volid = f"{storage}:backup/vz-dump-qemu-{vmid}-{created_at_str}.vma.zst"
options = {
"vmid": vmid,
"notes": f"VM {vmid}",
"subtype": "qemu",
}
Expand Down
33 changes: 26 additions & 7 deletions tests/sanitycheck/test_vm_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pvecontrol.cluster import PVECluster
from pvecontrol.sanitycheck.tests.vm_backups import VmBackups
from pvecontrol.sanitycheck import SanityCheck
from pvecontrol.sanitycheck.checks import CheckCode
from tests.fixtures.api import mock_api_requests, node, vm, backup_job, backup
from datetime import datetime, timedelta

Expand All @@ -15,17 +16,21 @@ def test_sanitycheck_vm_backups(request, _proxmox_http_auth):
]
vms = [
vm(100, nodes[0]),
vm(101, nodes[1]),
vm(101, nodes[0]),
vm(102, nodes[1]),
vm(103, nodes[1]),
]
backup_jobs = [
backup_job(1, [100]),
backup_job(2, [101]),
backup_job(1, "100"),
backup_job(2, "101"),
backup_job(3, "102"),
]
storage_content = [
backup("local", 100, datetime.now() - timedelta(days=4)),
backup("s3", 100, datetime.now() - timedelta(minutes=110)),
backup("s3", 101, datetime.now() - timedelta(minutes=90)),
]

request.side_effect = mock_api_requests(nodes, vms, backup_jobs)
request.side_effect = mock_api_requests(nodes, vms, backup_jobs, storage_content)

proxmox = PVECluster(
"name",
Expand Down Expand Up @@ -54,5 +59,19 @@ def test_sanitycheck_vm_backups(request, _proxmox_http_auth):
exitcode = sc.get_exit_code()
sc.display()

assert exitcode == 0
assert len(vm_backups_check.messages) == 3
assert exitcode == 1
assert len(vm_backups_check.messages) == 7
assert vm_backups_check.messages[0].code == CheckCode.OK
assert "vm-100" in vm_backups_check.messages[0].message
assert vm_backups_check.messages[1].code == CheckCode.OK
assert "vm-101" in vm_backups_check.messages[1].message
assert vm_backups_check.messages[2].code == CheckCode.OK
assert "vm-102" in vm_backups_check.messages[2].message
assert vm_backups_check.messages[3].code == CheckCode.CRIT
assert "vm-103" in vm_backups_check.messages[3].message
assert vm_backups_check.messages[4].code == CheckCode.WARN
assert "vm-100" in vm_backups_check.messages[4].message
assert vm_backups_check.messages[5].code == CheckCode.OK
assert "vm-101" in vm_backups_check.messages[5].message
assert vm_backups_check.messages[6].code == CheckCode.WARN
assert "vm-102" in vm_backups_check.messages[6].message

0 comments on commit 568c70e

Please sign in to comment.