|
| 1 | +import pytest |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | +import urllib.request |
| 7 | +from datetime import datetime |
| 8 | +from pathlib import Path |
| 9 | +from urllib.parse import urlparse |
| 10 | +from uuid import uuid4 |
| 11 | + |
| 12 | +from lib.common import GiB, PackageManagerEnum |
| 13 | +from lib.host import Host |
| 14 | +from lib.sr import SR |
| 15 | +from lib.vbd import VBD |
| 16 | +from lib.vdi import VDI, ImageFormat |
| 17 | +from lib.vm import VM |
| 18 | + |
| 19 | +from .helpers import FioBenchmarkCSV, load_results_from_csv |
| 20 | + |
| 21 | +from typing import Generator, assert_never |
| 22 | + |
| 23 | +MAX_LENGTH = 64 * GiB |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def running_unix_vm_with_fio(running_unix_vm: VM) -> Generator[VM, None, None]: |
| 28 | + vm = running_unix_vm |
| 29 | + snapshot = vm.snapshot() |
| 30 | + |
| 31 | + package_manager = vm.detect_package_manager() |
| 32 | + match package_manager: |
| 33 | + case PackageManagerEnum.APT_GET: |
| 34 | + vm.ssh("apt-get update && apt install -y fio") |
| 35 | + case PackageManagerEnum.YUM: |
| 36 | + vm.ssh("yum install -y fio") |
| 37 | + case PackageManagerEnum.DNF: |
| 38 | + vm.ssh("dnf install -y fio") |
| 39 | + case PackageManagerEnum.ZYPPER: |
| 40 | + vm.ssh("zypper install -y fio") |
| 41 | + case PackageManagerEnum.APK: |
| 42 | + vm.ssh("apk add fio") |
| 43 | + case PackageManagerEnum.UNKNOWN: |
| 44 | + raise RuntimeError("Unsupported package manager: could not install fio") |
| 45 | + case _: |
| 46 | + assert_never(package_manager) |
| 47 | + |
| 48 | + yield vm |
| 49 | + |
| 50 | + # teardown |
| 51 | + try: |
| 52 | + snapshot.revert() |
| 53 | + finally: |
| 54 | + snapshot.destroy() |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture(scope="function") |
| 58 | +def vdi_on_local_sr(host: Host, local_sr_on_hostA1: SR, image_format: ImageFormat) -> Generator[VDI, None, None]: |
| 59 | + sr = local_sr_on_hostA1 |
| 60 | + vdi = sr.create_vdi("testVDI", MAX_LENGTH, image_format=image_format) |
| 61 | + logging.info(f">> Created VDI {vdi.uuid} of type {image_format}") |
| 62 | + |
| 63 | + yield vdi |
| 64 | + |
| 65 | + # teardown |
| 66 | + logging.info(f"<< Destroying VDI {vdi.uuid}") |
| 67 | + vdi.destroy() |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture(scope="function") |
| 71 | +def plugged_vbd(vdi_on_local_sr: VDI, running_unix_vm_with_fio: VM) -> Generator[VBD, None, None]: |
| 72 | + vm = running_unix_vm_with_fio |
| 73 | + vdi = vdi_on_local_sr |
| 74 | + vbd = vm.create_vbd("autodetect", vdi.uuid) |
| 75 | + |
| 76 | + logging.info(f">> Plugging VDI {vdi.uuid} on VM {vm.uuid}") |
| 77 | + vbd.plug() |
| 78 | + |
| 79 | + yield vbd |
| 80 | + |
| 81 | + # teardown |
| 82 | + logging.info(f"<< Unplugging VDI {vdi.uuid} from VM {vm.uuid}") |
| 83 | + vbd.unplug() |
| 84 | + vbd.destroy() |
| 85 | + |
| 86 | + |
| 87 | +@pytest.fixture(scope="module") |
| 88 | +def local_temp_dir() -> Generator[Path, None, None]: |
| 89 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 90 | + yield Path(tmpdir) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture(scope="module") |
| 94 | +def result_csv_file(local_temp_dir: Path) -> Path: |
| 95 | + return local_temp_dir / f"results_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.csv" |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture(scope="module") |
| 99 | +def temp_dir(running_unix_vm_with_fio: VM) -> Generator[Path, None, None]: |
| 100 | + vm = running_unix_vm_with_fio |
| 101 | + tmpdir = vm.ssh("mktemp -d") |
| 102 | + |
| 103 | + yield Path(tmpdir) |
| 104 | + |
| 105 | + # teardown |
| 106 | + vm.ssh(f"rm -r {tmpdir}") |
| 107 | + |
| 108 | + |
| 109 | +def pytest_addoption(parser: pytest.Parser) -> None: |
| 110 | + parser.addoption( |
| 111 | + "--benchmark-previous-results", |
| 112 | + action="store", |
| 113 | + default=None, |
| 114 | + help="Path/URI to previous CSV results file for comparison", |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.fixture(scope="session") |
| 119 | +def prev_results(pytestconfig: pytest.Config) -> dict[str, list[FioBenchmarkCSV]]: |
| 120 | + csv_uri = pytestconfig.getoption("--benchmark-previous-results") |
| 121 | + if not csv_uri: |
| 122 | + return {} |
| 123 | + csv_path = csv_uri |
| 124 | + if urlparse(csv_uri).scheme != "": |
| 125 | + logging.info("Detected CSV path as an url") |
| 126 | + csv_path = f"/tmp/{uuid4()}.csv" |
| 127 | + urllib.request.urlretrieve(csv_uri, csv_path) |
| 128 | + logging.info(f"Fetching CSV file from {csv_uri} to {csv_path}") |
| 129 | + if not os.path.exists(csv_path): |
| 130 | + raise FileNotFoundError(csv_path) |
| 131 | + return load_results_from_csv(csv_path) |
0 commit comments