Fix linter failures

This commit is contained in:
Lucian Petrut
2026-09-09 08:43:42 +00:00
parent 2c8580d750
commit f64c753d9c
12 changed files with 165 additions and 136 deletions
+4 -5
View File
@@ -5,12 +5,13 @@
from __future__ import annotations
import contextlib
import ctypes
import os
import time
import uuid
from dataclasses import dataclass
from typing import Any, Optional
from typing import Any
import pytest
import yaml
@@ -76,7 +77,7 @@ class LabEnv:
)
def vixdisklib_connect_kwargs(
self, extra: Optional[dict[str, Any]] = None
self, extra: dict[str, Any] | None = None
) -> dict[str, Any]:
"""Return common ``VixDiskLib_ConnectEx`` arguments for the temp VM."""
kwargs: dict[str, Any] = {
@@ -283,10 +284,8 @@ def create_lab_vm() -> LabEnv:
)
except Exception:
if vm is not None:
try:
with contextlib.suppress(Exception):
_wait_for_task(vm.Destroy_Task())
except Exception:
pass
raise
finally:
Disconnect(si)
+17 -13
View File
@@ -3,7 +3,7 @@
"""Compare writes and reads from VDDK with openvixdisklib."""
from typing import Any, Optional
from typing import Any
import pytest
@@ -12,7 +12,7 @@ from tests.integration import vixdisklib
from tests.integration.base import SECTOR_AT_1GB, SECTOR_SIZE, LabEnv, pattern_bytes
def _connect_extra(lab: LabEnv, module: Any) -> Optional[dict[str, Any]]:
def _connect_extra(lab: LabEnv, module: Any) -> dict[str, Any] | None:
"""Return extra ``connect`` kwargs needed by ``module``."""
if module is open_vix:
return {"allow_untrusted": lab.allow_untrusted}
@@ -28,11 +28,13 @@ def _write_sectors(
)
buf = module.get_buffer(SECTOR_SIZE)
kwargs = lab.vixdisklib_connect_kwargs(_connect_extra(lab, module))
with handle.connect(**kwargs) as conn:
with handle.open(conn, lab.disk_path, flags=flags) as disk:
for start, data in payloads.items():
buf[:SECTOR_SIZE] = data
handle.write(disk, start, 1, buf)
with (
handle.connect(**kwargs) as conn,
handle.open(conn, lab.disk_path, flags=flags) as disk,
):
for start, data in payloads.items():
buf[:SECTOR_SIZE] = data
handle.write(disk, start, 1, buf)
def _read_sectors(
@@ -45,12 +47,14 @@ def _read_sectors(
buf = module.get_buffer(SECTOR_SIZE)
result: dict[int, bytes] = {}
kwargs = lab.vixdisklib_connect_kwargs(_connect_extra(lab, module))
with handle.connect(**kwargs) as conn:
with handle.open(conn, lab.disk_path, flags=flags) as disk:
for start in sectors:
buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, start, 1, buf)
result[start] = buf.raw[:SECTOR_SIZE]
with (
handle.connect(**kwargs) as conn,
handle.open(conn, lab.disk_path, flags=flags) as disk,
):
for start in sectors:
buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, start, 1, buf)
result[start] = buf.raw[:SECTOR_SIZE]
return result
+12 -10
View File
@@ -21,14 +21,16 @@ class TestNfcOpen:
) -> None:
"""Open the temp VMDK, write sector 0, and read it back."""
expected = pattern_bytes(SECTOR_SIZE, b"NFC-OPEN-S0")
with lab.authenticate(read_only=False, nfc_ssl=nfc_ssl) as session:
with nfc_open.open_disk(
with (
lab.authenticate(read_only=False, nfc_ssl=nfc_ssl) as session,
nfc_open.open_disk(
session, lab.disk_path, read_only=False, compression=compression
) as disk:
assert disk.path == lab.disk_path
assert disk.handle > 0
assert disk.sector_size == SECTOR_SIZE
disk.write(0, 1, expected)
got = disk.read(0, 1)
assert got is not expected
assert got == expected
) as disk,
):
assert disk.path == lab.disk_path
assert disk.handle > 0
assert disk.sector_size == SECTOR_SIZE
disk.write(0, 1, expected)
got = disk.read(0, 1)
assert got is not expected
assert got == expected
+41 -37
View File
@@ -30,38 +30,40 @@ class TestNfcReadWrite:
(0, 129),
(256, 64),
]
with lab.authenticate(read_only=False) as session:
with nfc_open.open_disk(
with (
lab.authenticate(read_only=False) as session,
nfc_open.open_disk(
session, lab.disk_path, read_only=False, compression=compression
) as disk:
for start, n_sectors in ranges:
length = n_sectors * SECTOR_SIZE
seed = f"NFC-R{start}:{n_sectors}-".encode()
to_write = pattern_bytes(length, seed)
disk.write(start, n_sectors, to_write)
got = disk.read(start, n_sectors)
assert got is not to_write
assert len(got) == length
assert got == to_write
) as disk,
):
for start, n_sectors in ranges:
length = n_sectors * SECTOR_SIZE
seed = f"NFC-R{start}:{n_sectors}-".encode()
to_write = pattern_bytes(length, seed)
disk.write(start, n_sectors, to_write)
got = disk.read(start, n_sectors)
assert got is not to_write
assert len(got) == length
assert got == to_write
two_seed = b"NFC-TWO-SECTOR"
two_to_write = pattern_bytes(2 * SECTOR_SIZE, two_seed)
disk.write(0, 2, two_to_write)
two_got = disk.read(0, 2)
assert two_got is not two_to_write
assert two_got == two_to_write
assert disk.read(1, 1) == two_to_write[SECTOR_SIZE:]
two_seed = b"NFC-TWO-SECTOR"
two_to_write = pattern_bytes(2 * SECTOR_SIZE, two_seed)
disk.write(0, 2, two_to_write)
two_got = disk.read(0, 2)
assert two_got is not two_to_write
assert two_got == two_to_write
assert disk.read(1, 1) == two_to_write[SECTOR_SIZE:]
big_seed = b"NFC-129-SECTOR-WRITE"
big_to_write = pattern_bytes(129 * SECTOR_SIZE, big_seed)
disk.write(0, 129, big_to_write)
big_got = disk.read(0, 129)
assert big_got is not big_to_write
assert big_got == big_to_write
assert (
big_got[SECTOR_SIZE : 2 * SECTOR_SIZE]
== big_to_write[SECTOR_SIZE : 2 * SECTOR_SIZE]
)
big_seed = b"NFC-129-SECTOR-WRITE"
big_to_write = pattern_bytes(129 * SECTOR_SIZE, big_seed)
disk.write(0, 129, big_to_write)
big_got = disk.read(0, 129)
assert big_got is not big_to_write
assert big_got == big_to_write
assert (
big_got[SECTOR_SIZE : 2 * SECTOR_SIZE]
== big_to_write[SECTOR_SIZE : 2 * SECTOR_SIZE]
)
@pytest.mark.parametrize(
"compression",
@@ -72,12 +74,14 @@ class TestNfcReadWrite:
"""Write 32 MiB (512 AIO chunks) and read it back in one request."""
n_sectors = _32MIB // SECTOR_SIZE
to_write = os.urandom(_32MIB)
with lab.authenticate(read_only=False) as session:
with nfc_open.open_disk(
with (
lab.authenticate(read_only=False) as session,
nfc_open.open_disk(
session, lab.disk_path, read_only=False, compression=compression
) as disk:
disk.write(0, n_sectors, to_write)
got = disk.read(0, n_sectors)
assert got is not to_write
assert len(got) == _32MIB
assert got == to_write
) as disk,
):
disk.write(0, n_sectors, to_write)
got = disk.read(0, n_sectors)
assert got is not to_write
assert len(got) == _32MIB
assert got == to_write
+22 -16
View File
@@ -55,15 +55,17 @@ class TestOpenvixdisklib:
SECTOR_AT_1GB: pattern_bytes(SECTOR_SIZE, b"OVDL-1GB"),
}
assert handle.get_transport_modes() == ["nbdssl", "nbd"]
with handle.connect(**connect_kwargs) as conn:
with handle.open(conn, lab.disk_path, flags=open_flags) as disk:
assert handle.get_transport_mode(disk) == transport_mode
for start, expected in patterns.items():
write_buf[:SECTOR_SIZE] = expected
handle.write(disk, start, 1, write_buf)
read_buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, start, 1, read_buf)
assert read_buf.raw[:SECTOR_SIZE] == expected
with (
handle.connect(**connect_kwargs) as conn,
handle.open(conn, lab.disk_path, flags=open_flags) as disk,
):
assert handle.get_transport_mode(disk) == transport_mode
for start, expected in patterns.items():
write_buf[:SECTOR_SIZE] = expected
handle.write(disk, start, 1, write_buf)
read_buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, start, 1, read_buf)
assert read_buf.raw[:SECTOR_SIZE] == expected
def test_read_only_open_snapshot_parent(self, lab: LabEnv) -> None:
"""Read-only Open uses NfcGetVmFiles, including a snapshot parent path.
@@ -92,15 +94,19 @@ class TestOpenvixdisklib:
read_flags = vixdisklib.VIXDISKLIB_FLAG_OPEN_READ_ONLY
def read_sector(path: str) -> bytes:
with handle.connect(**read_kwargs) as conn:
with handle.open(conn, path, flags=read_flags) as disk:
read_buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, 0, 1, read_buf)
with (
handle.connect(**read_kwargs) as conn,
handle.open(conn, path, flags=read_flags) as disk,
):
read_buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, 0, 1, read_buf)
return read_buf.raw[:SECTOR_SIZE]
with handle.connect(**write_kwargs) as conn:
with handle.open(conn, lab.disk_path, flags=0) as disk:
handle.write(disk, 0, 1, write_buf)
with (
handle.connect(**write_kwargs) as conn,
handle.open(conn, lab.disk_path, flags=0) as disk,
):
handle.write(disk, 0, 1, write_buf)
assert read_sector(lab.disk_path) == expected
+8 -6
View File
@@ -17,9 +17,11 @@ class TestVddk:
read_buf = vixdisklib.get_buffer(SECTOR_SIZE)
expected = pattern_bytes(SECTOR_SIZE, b"VDDK-S0")
write_buf[:SECTOR_SIZE] = expected
with handle.connect(**lab.vixdisklib_connect_kwargs()) as conn:
with handle.open(conn, lab.disk_path, flags=0) as disk:
handle.write(disk, 0, 1, write_buf)
read_buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, 0, 1, read_buf)
assert read_buf.raw[:SECTOR_SIZE] == expected
with (
handle.connect(**lab.vixdisklib_connect_kwargs()) as conn,
handle.open(conn, lab.disk_path, flags=0) as disk,
):
handle.write(disk, 0, 1, write_buf)
read_buf[:SECTOR_SIZE] = b"\xa5" * SECTOR_SIZE
handle.read(disk, 0, 1, read_buf)
assert read_buf.raw[:SECTOR_SIZE] == expected
Executable → Regular
+6 -4
View File
@@ -82,7 +82,7 @@ def get_buffer(size):
return ctypes.create_string_buffer(size)
class VixDiskLibHandle(object):
class VixDiskLibHandle:
"""Class which acts as a proxy for vixDiskLib-related operations:"""
def __init__(self, config_path=None, vixdisklib_compatibility_version=None):
@@ -130,7 +130,7 @@ class VixDiskLibHandle(object):
)
if not version_used:
raise Exception(
raise RuntimeError(
"Could not initialize vixDiskLib with any of the following "
"versions: %s" % target_versions
)
@@ -234,7 +234,9 @@ class VixDiskLibHandle(object):
self._vix_disklib.VixDiskLib_Exit.argtypes = []
self._vix_disklib.VixDiskLib_Exit.restype = None
def _check_err(self, err, allowed_values=[vix_disklib_errors.VIX_OK]):
def _check_err(self, err, allowed_values=None):
if allowed_values is None:
allowed_values = [vix_disklib_errors.VIX_OK]
if err not in allowed_values:
err_msg = self._vix_disklib.VixDiskLib_GetErrorText(err, None)
err_msg_copy = str(ctypes.cast(err_msg, ctypes.c_char_p).value.decode())
@@ -279,7 +281,7 @@ class VixDiskLibHandle(object):
LOG.debug("Original vixDiskLib error message: %s", err_msg_copy)
err_msg = msg
raise Exception(err_msg)
raise RuntimeError(err_msg)
def get_transport_modes(self):
transport_modes = self._vix_disklib.VixDiskLib_ListTransportModes()