From 568c70edb6cd7067f5c0f6dce10fcf3fe5a2f9e2 Mon Sep 17 00:00:00 2001 From: Paul Laffitte Date: Tue, 24 Dec 2024 18:37:22 +0100 Subject: [PATCH] wip --- tests/fixtures/api.py | 31 +++++++++++++++++++++----- tests/sanitycheck/test_vm_backups.py | 33 ++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/tests/fixtures/api.py b/tests/fixtures/api.py index 7567010..ac1ffb2 100644 --- a/tests/fixtures/api.py +++ b/tests/fixtures/api.py @@ -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}") @@ -21,7 +21,7 @@ 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"}, @@ -29,6 +29,7 @@ def generate_routes(nodes, vms, backups): ], "/api2/json/cluster/resources": [ *[n["resource"] for n in nodes], + *[storage_resource("s3", n["status"]["name"]) for n in nodes], *vms, ], "/api2/json/nodes": [ @@ -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), } @@ -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"}, @@ -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, @@ -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", } diff --git a/tests/sanitycheck/test_vm_backups.py b/tests/sanitycheck/test_vm_backups.py index eb719fb..271cb01 100644 --- a/tests/sanitycheck/test_vm_backups.py +++ b/tests/sanitycheck/test_vm_backups.py @@ -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 @@ -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", @@ -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