|
| 1 | +# ovirt-imageio |
| 2 | +# Copyright (C) 2022 Red Hat, Inc. |
| 3 | +# |
| 4 | +# This program is free software; you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation; either version 2 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | + |
| 9 | +import contextlib |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import uuid |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | + |
| 16 | +import pytest |
| 17 | +import ovirtsdk4 as sdk |
| 18 | + |
| 19 | +from ovirt_imageio._internal import qemu_img |
| 20 | + |
| 21 | + |
| 22 | +log = logging.getLogger("test") |
| 23 | + |
| 24 | + |
| 25 | +class ClientError(Exception): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +def run_upload_disk(storage_domain, image, disk_id=None, log_level=None): |
| 30 | + # Make sure it runs with the same tox environment executable |
| 31 | + cmd = [sys.executable, './ovirt-img', 'upload-disk', '-c', 'test'] |
| 32 | + cmd.extend(['-s', storage_domain]) |
| 33 | + if log_level: |
| 34 | + cmd.extend(['--log-level', log_level]) |
| 35 | + if disk_id: |
| 36 | + cmd.extend(['--disk-id', disk_id]) |
| 37 | + cmd.append(image) |
| 38 | + try: |
| 39 | + subprocess.run(cmd, check=True) |
| 40 | + except subprocess.CalledProcessError as exc: |
| 41 | + raise ClientError(f'Client Error: {exc}') from exc |
| 42 | + |
| 43 | + |
| 44 | +def run_download_disk(disk_id, image, log_level=None): |
| 45 | + # Make sure it runs with the same tox environment executable |
| 46 | + cmd = [sys.executable, './ovirt-img', 'download-disk', '-c', 'test'] |
| 47 | + if log_level: |
| 48 | + cmd.extend(['--log-level', log_level]) |
| 49 | + cmd.extend([disk_id, image]) |
| 50 | + try: |
| 51 | + subprocess.run(cmd, check=True) |
| 52 | + except subprocess.CalledProcessError as exc: |
| 53 | + raise ClientError(f'Client Error: {exc}') from exc |
| 54 | + |
| 55 | + |
| 56 | +def remove_disk(conf, sd_name, disk_id): |
| 57 | + connection = sdk.Connection( |
| 58 | + url=f'{conf["engine_url"]}/ovirt-engine/api', |
| 59 | + username=conf["username"], |
| 60 | + password=conf["password"], |
| 61 | + ca_file=conf["cafile"] |
| 62 | + ) |
| 63 | + with contextlib.closing(connection): |
| 64 | + sd_service = connection.system_service().storage_domains_service() |
| 65 | + found_sd = sd_service.list(search=f'name={sd_name}') |
| 66 | + if not found_sd: |
| 67 | + raise RuntimeError(f"Couldn't find storage domain {sd_name}") |
| 68 | + |
| 69 | + sd = found_sd[0] |
| 70 | + sd_service = sd_service.storage_domain_service(sd.id) |
| 71 | + sd_service.disks_service().disk_service(disk_id).remove() |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize("fmt", ["raw", "qcow2"]) |
| 75 | +def test_upload_download(config, tmpdir, fmt): |
| 76 | + image = os.path.join(tmpdir, f'image.{fmt}') |
| 77 | + qemu_img.create(image, fmt, size='10g') |
| 78 | + test_config = config["tests"]["upload-download"] |
| 79 | + for sd_name in test_config.get("storage-domains", []): |
| 80 | + disk_id = str(uuid.uuid4()) |
| 81 | + try: |
| 82 | + log.info("Upload %s image to SD %s", fmt, sd_name) |
| 83 | + run_upload_disk(sd_name, image, disk_id) |
| 84 | + down_img = os.path.join(tmpdir, f'downloaded.{fmt}') |
| 85 | + log.info("Download image %s", disk_id) |
| 86 | + run_download_disk(disk_id, down_img) |
| 87 | + log.info("Comparing images") |
| 88 | + qemu_img.compare(image, down_img) |
| 89 | + except ClientError as exc: |
| 90 | + log.error("%s", exc) |
| 91 | + # Skip disk cleanup if client failed |
| 92 | + return |
| 93 | + finally: |
| 94 | + remove_disk(config["common"], sd_name, disk_id) |
0 commit comments