from __future__ import annotations

import gzip
import io

from dissect.target import container
from dissect.target.containers.vmdk import VmdkContainer
from dissect.target.filesystem import VirtualFilesystem
from tests._utils import absolute_path


def test_vmdk_container() -> None:
    """Test that VMDK containers are properly opened.

    Generated with::

        VBoxManage createmedium disk --filename "./small.vmdk" --size 2 --format=VMDK
    """
    path = absolute_path("_data/containers/vmdk/small.vmdk.gz")
    gz_file = gzip.GzipFile(path)
    fh = container.open(gz_file)
    assert isinstance(fh, VmdkContainer)
    a = fh.read(20)
    assert a == b"\x00" * 20
    assert fh.tell() == 20
    fh.seek(0, whence=io.SEEK_END)
    assert fh.tell() == 2097152
    fh.close()
    gz_file.close()


def test_vmdk_detect_path() -> None:
    """Test that VMDK containers are properly opened, when using the extension based matching."""
    vfs = VirtualFilesystem()
    vfs.map_file("small.vmdk", absolute_path("_data/containers/vmdk/small.vmdk.gz"), compression="gzip")
    fh = container.open(vfs.path("small.vmdk"))
    assert isinstance(fh, VmdkContainer)
    a = fh.read(20)
    assert a == b"\x00" * 20
    assert fh.tell() == 20
    fh.seek(0, whence=io.SEEK_END)
    assert fh.tell() == 2097152
