|
| 1 | +# Copyright (c) 2024 Broadcom, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the “License”); you may not |
| 4 | +# use this file except in compliance with the License. You may obtain a copy of |
| 5 | +# the License at: |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 10 | +# under the License is distributed on an “AS IS” BASIS, without warranties or |
| 11 | +# conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the |
| 12 | +# specific language governing permissions and limitations under the License. |
| 13 | + |
| 14 | + |
| 15 | +import hashlib |
| 16 | +import os |
| 17 | +import pytest |
| 18 | +import shutil |
| 19 | +import subprocess |
| 20 | +import time |
| 21 | + |
| 22 | + |
| 23 | +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 24 | + |
| 25 | +VMDK_CONVERT=os.path.join(THIS_DIR, "..", "build", "vmdk", "vmdk-convert") |
| 26 | +VMDK_FUSE=os.path.join(THIS_DIR, "..", "build", "vmdk", "vmdk-fuse") |
| 27 | +if not os.path.exists(VMDK_FUSE): |
| 28 | + # skip this if we haven't built vmdk-fuse |
| 29 | + pytestmark = pytest.mark.skip |
| 30 | + |
| 31 | +WORK_DIR=os.path.join(os.getcwd(), "pytest-fuse") |
| 32 | + |
| 33 | +IMAGE_FILE="dummy.img" |
| 34 | +VMDK_FILE="dummy.vmdk" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(scope='module', autouse=True) |
| 38 | +def setup_test(): |
| 39 | + image_file = os.path.join(WORK_DIR, IMAGE_FILE) |
| 40 | + vmdk_file = VMDK_FILE |
| 41 | + |
| 42 | + os.makedirs(WORK_DIR, exist_ok=True) |
| 43 | + |
| 44 | + process = subprocess.run(["dd", "if=/dev/zero", f"of={image_file}", "bs=1024", "count=1024"], cwd=WORK_DIR) |
| 45 | + assert process.returncode == 0 |
| 46 | + |
| 47 | + process = subprocess.run(["mke2fs", image_file], cwd=WORK_DIR) |
| 48 | + assert process.returncode == 0 |
| 49 | + |
| 50 | + process = subprocess.run([VMDK_CONVERT, image_file, vmdk_file], cwd=WORK_DIR) |
| 51 | + assert process.returncode == 0 |
| 52 | + |
| 53 | + yield |
| 54 | + shutil.rmtree(WORK_DIR) |
| 55 | + |
| 56 | + |
| 57 | +def _get_hash(filename, blocksz=1024 * 1024): |
| 58 | + hash_type = "sha256" |
| 59 | + hash = hashlib.new(hash_type) |
| 60 | + with open(filename, "rb") as f: |
| 61 | + while True: |
| 62 | + buf = f.read(blocksz) |
| 63 | + if not buf: |
| 64 | + break |
| 65 | + hash.update(buf) |
| 66 | + return hash.hexdigest() |
| 67 | + |
| 68 | + |
| 69 | +def test_mount(): |
| 70 | + image_file = os.path.join(WORK_DIR, IMAGE_FILE) |
| 71 | + vmdk_file = VMDK_FILE |
| 72 | + |
| 73 | + mounted_image_path = os.path.join(WORK_DIR, "mounted.img") |
| 74 | + |
| 75 | + with open(mounted_image_path, "w") as f: |
| 76 | + pass |
| 77 | + |
| 78 | + hash_orig = _get_hash(image_file) |
| 79 | + |
| 80 | + try: |
| 81 | + process = subprocess.run([VMDK_FUSE, f"--file={vmdk_file}", mounted_image_path], cwd=WORK_DIR) |
| 82 | + assert process.returncode == 0 |
| 83 | + |
| 84 | + hash_mounted = _get_hash(mounted_image_path) |
| 85 | + assert hash_mounted == hash_orig |
| 86 | + |
| 87 | + finally: |
| 88 | + subprocess.run(["fusermount", "-u", mounted_image_path], cwd=WORK_DIR, check=True) |
| 89 | + assert not os.path.ismount(mounted_image_path) |
| 90 | + |
| 91 | + |
| 92 | +def test_mount_ext2(): |
| 93 | + vmdk_file = VMDK_FILE |
| 94 | + |
| 95 | + mounted_image_path = os.path.join(WORK_DIR, "mounted.img") |
| 96 | + mount_dir_path = os.path.join(WORK_DIR, "mntdir") |
| 97 | + |
| 98 | + with open(mounted_image_path, "w") as f: |
| 99 | + pass |
| 100 | + |
| 101 | + os.makedirs(mount_dir_path, exist_ok=True) |
| 102 | + |
| 103 | + try: |
| 104 | + process = subprocess.run([VMDK_FUSE, f"--file={vmdk_file}", mounted_image_path], cwd=WORK_DIR) |
| 105 | + assert process.returncode == 0 |
| 106 | + |
| 107 | + process = subprocess.run(["fuse2fs", mounted_image_path, mount_dir_path]) |
| 108 | + assert process.returncode == 0 |
| 109 | + finally: |
| 110 | + subprocess.run(["fusermount", "-u", mount_dir_path], cwd=WORK_DIR) |
| 111 | + while os.path.ismount(mount_dir_path): |
| 112 | + time.sleep(0.1) |
| 113 | + # avoid device busy, which can happen even after checking if the unmount is complete (there should be a better way) |
| 114 | + time.sleep(0.1) |
| 115 | + subprocess.run(["fusermount", "-u", mounted_image_path], cwd=WORK_DIR) |
0 commit comments