|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from dissect.target.loader import open as loader_open |
| 9 | +from dissect.target.loaders.tar import TarLoader |
| 10 | +from dissect.target.loaders.vmsupport import VmSupportLoader, VmSupportTarSubloader |
| 11 | +from dissect.target.target import Target |
| 12 | +from tests._utils import absolute_path |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from collections.abc import Callable |
| 16 | + from pathlib import Path |
| 17 | + |
| 18 | + from pytest_benchmark.fixture import BenchmarkFixture |
| 19 | + |
| 20 | + from dissect.target.loader import Loader |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def mock_vmsupport_dir(tmp_path: Path) -> Path: |
| 25 | + root = tmp_path / "esx-localhost-2026-01-09--16.04-135806" |
| 26 | + |
| 27 | + (root / "etc" / "vmware").mkdir(parents=True) |
| 28 | + (root / "action.log").write_bytes(b"") |
| 29 | + (root / "error.log").write_bytes(b"") |
| 30 | + (root / "etc" / "vmware" / "esx.conf").write_bytes(b'/resourceGroups/version = "8.0.3"\n') |
| 31 | + return tmp_path |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + ("opener"), |
| 36 | + [ |
| 37 | + pytest.param(Target.open, id="target-open"), |
| 38 | + pytest.param(lambda x: next(Target.open_all([x])), id="target-open-all"), |
| 39 | + ], |
| 40 | +) |
| 41 | +# vmsupport files with commands/vsi_traverse_-s* removed (~300mo) |
| 42 | +@pytest.mark.parametrize( |
| 43 | + ("path", "loader"), |
| 44 | + [ |
| 45 | + ("_data/loaders/vmsupport/esx-localhost6-2026-01-12--13.56-2107676.tar.gz", TarLoader), |
| 46 | + ("_data/loaders/vmsupport/esx-localhost8-2026-01-09--16.04-135806.tgz", TarLoader), |
| 47 | + ("mock_vmsupport_dir", VmSupportLoader), |
| 48 | + ], |
| 49 | +) |
| 50 | +def test_target_open( |
| 51 | + opener: Callable[[str | Path], Target], path: str, loader: type[Loader], mock_vmsupport_dir: Path |
| 52 | +) -> None: |
| 53 | + """Test that we correctly use the ESXi vm-support loaders when opening a ``Target``.""" |
| 54 | + path = mock_vmsupport_dir if path == "mock_vmsupport_dir" else absolute_path(path) |
| 55 | + |
| 56 | + with patch("dissect.target.target.Target.apply"): |
| 57 | + target = opener(path) |
| 58 | + |
| 59 | + assert isinstance(target._loader, loader) |
| 60 | + if isinstance(target._loader, TarLoader): |
| 61 | + assert isinstance(target._loader.subloader, VmSupportTarSubloader) |
| 62 | + assert target.path == path |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "data_path", |
| 67 | + [ |
| 68 | + "_data/loaders/vmsupport/esx-localhost6-2026-01-12--13.56-2107676.tar.gz", |
| 69 | + "_data/loaders/vmsupport/esx-localhost8-2026-01-09--16.04-135806.tgz", |
| 70 | + ], |
| 71 | +) |
| 72 | +def test_compressed_tar(data_path: str) -> None: |
| 73 | + """Test if we map a compressed vm support tar image correctly.""" |
| 74 | + path = absolute_path(data_path) |
| 75 | + |
| 76 | + loader = loader_open(path) |
| 77 | + assert isinstance(loader, TarLoader) |
| 78 | + |
| 79 | + t = Target() |
| 80 | + loader.map(t) |
| 81 | + assert isinstance(loader.subloader, VmSupportTarSubloader) |
| 82 | + assert len(t.filesystems) == 1 |
| 83 | + |
| 84 | + t.apply() |
| 85 | + test_file = t.fs.path("/etc/vmware/esx.conf") |
| 86 | + assert test_file.exists() |
| 87 | + assert test_file.is_file() |
| 88 | + assert b"/resourceGroups/version" in test_file.open().read() |
| 89 | + |
| 90 | + |
| 91 | +def test_dir(mock_vmsupport_dir: Path) -> None: |
| 92 | + """Test if we map an extracted vm support directory correctly.""" |
| 93 | + |
| 94 | + loader = loader_open(mock_vmsupport_dir) |
| 95 | + assert isinstance(loader, VmSupportLoader) |
| 96 | + |
| 97 | + t = Target() |
| 98 | + loader.map(t) |
| 99 | + assert len(t.filesystems) == 1 |
| 100 | + |
| 101 | + t.apply() |
| 102 | + test_file = t.fs.path("etc/vmware/esx.conf") |
| 103 | + assert test_file.exists() |
| 104 | + assert test_file.is_file() |
| 105 | + assert test_file.open().readline() == b'/resourceGroups/version = "8.0.3"\n' |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + ("archive", "loader"), |
| 110 | + [ |
| 111 | + ("_data/loaders/vmsupport/esx-localhost6-2026-01-12--13.56-2107676.tar.gz", TarLoader), |
| 112 | + ("_data/loaders/vmsupport/esx-localhost8-2026-01-09--16.04-135806.tgz", TarLoader), |
| 113 | + ], |
| 114 | +) |
| 115 | +@pytest.mark.benchmark |
| 116 | +def test_benchmark(benchmark: BenchmarkFixture, archive: str, loader: type[Loader]) -> None: |
| 117 | + file = absolute_path(archive) |
| 118 | + |
| 119 | + benchmark(lambda: loader(file).map(Target())) |
0 commit comments