"""Rescue ISO catalog and bootable-file boundary tests.""" from __future__ import annotations import pytest from virsh_panel.cloud_templates import TemplateError from virsh_panel.rescue_templates import ( RescueTemplateStore, rescue_scope, rescue_volume_name, verify_bootable_iso, ) def _profile(rescue_iso_pool="rescue"): return { "host": "vmhost.example", "port": 2222, "fingerprint": "SHA256:vmhost", "iso_pool": "iso", "rescue_iso_pool": rescue_iso_pool, } def _iso_bytes(bootable=True): image = bytearray(1024 * 1024) for sector, kind in ((16, 1), (17, 0 if bootable else 2), (18, 255)): offset = sector * 2048 image[offset] = kind image[offset + 1:offset + 6] = b"CD001" image[offset + 6] = 1 if bootable: image[17 * 2048 + 7:17 * 2048 + 30] = b"EL TORITO SPECIFICATION" image[17 * 2048 + 71:17 * 2048 + 75] = (20).to_bytes(4, "little") return bytes(image) def test_rescue_catalog_is_scoped_to_rescue_pool_and_rejects_duplicate_names(): catalog = RescueTemplateStore(None) first_scope = rescue_scope(_profile()) second_scope = rescue_scope(_profile("other-rescue")) assert first_scope != second_scope template_id = "a" * 32 record = { "id": template_id, "name": "System Rescue", "volume_name": rescue_volume_name(template_id), "size_bytes": 1024 * 1024, "sha256": "b" * 64, } catalog.add(first_scope, record) assert catalog.list(second_scope) == [] assert catalog.get(first_scope, template_id)["name"] == "System Rescue" with pytest.raises(TemplateError): catalog.add(first_scope, {**record, "id": "c" * 32, "volume_name": rescue_volume_name("c" * 32)}) catalog.delete(first_scope, template_id) assert catalog.list(first_scope) == [] def test_rescue_catalog_requires_separate_configured_pool(): with pytest.raises(TemplateError, match="救援 ISO 目录"): rescue_scope(_profile("")) assert rescue_scope(_profile()) == rescue_scope({ **_profile(), "iso_pool": "other-install-iso", }) def test_rescue_iso_must_include_el_torito_boot_record(tmp_path): path = tmp_path / "rescue.iso" path.write_bytes(_iso_bytes()) verify_bootable_iso(path) path.write_bytes(_iso_bytes(bootable=False)) with pytest.raises(TemplateError, match="El Torito"): verify_bootable_iso(path) path.write_bytes(b"not an ISO") with pytest.raises(TemplateError): verify_bootable_iso(path)