|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import gzip |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import MagicMock |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from dissect.hypervisor.disk.c_vmdk import c_vmdk |
8 | | -from dissect.hypervisor.disk.vmdk import VMDK, DiskDescriptor, ExtentDescriptor |
| 10 | +from dissect.hypervisor.disk.vmdk import VMDK, DiskDescriptor, ExtentDescriptor, open_parent |
9 | 11 | from tests._util import absolute_path |
10 | 12 |
|
11 | 13 |
|
@@ -203,3 +205,40 @@ def test_vmdk_extent_description(extent_description: str, expected_extents: list |
203 | 205 |
|
204 | 206 | descriptor = DiskDescriptor.parse(extent_description) |
205 | 207 | assert descriptor.extents == expected_extents |
| 208 | + |
| 209 | + |
| 210 | +def test_open_parent_all_cases(monkeypatch: pytest.MonkeyPatch) -> None: |
| 211 | + """Test open_parent handles absolute and relative filename_hint paths.""" |
| 212 | + |
| 213 | + # Mock Path.exists to simulate file existence |
| 214 | + def mock_exists(path: Path) -> bool: |
| 215 | + return str(path) in { |
| 216 | + "/a/b/c/d.vmdk", # Case: absolute path |
| 217 | + "base/relative/hint.vmdk", # Case: full relative path |
| 218 | + "base/hint.vmdk", # Case: basename in same dir |
| 219 | + "../sibling/hint.vmdk", # Case: fallback to sibling |
| 220 | + } |
| 221 | + |
| 222 | + with monkeypatch.context() as m: |
| 223 | + m.setattr("pathlib.Path.exists", mock_exists) |
| 224 | + |
| 225 | + # Mock VMDK to avoid real file I/O |
| 226 | + mock_vmdk = MagicMock() |
| 227 | + m.setattr("dissect.hypervisor.disk.vmdk.VMDK", lambda path: mock_vmdk) |
| 228 | + mock_vmdk.path = "mocked-path" |
| 229 | + |
| 230 | + # Case: Absolute path — should use /a/b/c/d.vmdk directly |
| 231 | + vmdk = open_parent(Path("base"), "/a/b/c/d.vmdk") |
| 232 | + assert str(vmdk.path) == "mocked-path" |
| 233 | + |
| 234 | + # Case: Full relative path — try base/relative/hint.vmdk |
| 235 | + vmdk = open_parent(Path("base"), "relative/hint.vmdk") |
| 236 | + assert str(vmdk.path) == "mocked-path" |
| 237 | + |
| 238 | + # Case: Basename only — fall back to base/hint.vmdk |
| 239 | + vmdk = open_parent(Path("base"), "hint.vmdk") |
| 240 | + assert str(vmdk.path) == "mocked-path" |
| 241 | + |
| 242 | + # Case: Fallback to sibling — try ../sibling/hint.vmdk |
| 243 | + vmdk = open_parent(Path("base"), "sibling/hint.vmdk") |
| 244 | + assert str(vmdk.path) == "mocked-path" |
0 commit comments