|  | 
|  | 1 | +"""pytest tests testing subclasses of xcp.accessor.MountingAccessor using pyfakefs""" | 
|  | 2 | +import sys | 
|  | 3 | +from io import BytesIO | 
|  | 4 | +from typing import cast | 
|  | 5 | + | 
|  | 6 | +from mock import patch | 
|  | 7 | +from pyfakefs.fake_filesystem import FakeFileOpen, FakeFilesystem | 
|  | 8 | + | 
|  | 9 | +import xcp.accessor | 
|  | 10 | + | 
|  | 11 | +binary_data = b"\x00\x1b\x5b\x95\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xcc\xdd\xee\xff" | 
|  | 12 | + | 
|  | 13 | + | 
|  | 14 | +def test_device_accessor(fs): | 
|  | 15 | +    # type: (FakeFilesystem) -> None | 
|  | 16 | +    accessor = xcp.accessor.createAccessor("dev:///dev/device", False) | 
|  | 17 | +    check_mounting_accessor(accessor, fs) | 
|  | 18 | + | 
|  | 19 | + | 
|  | 20 | +def test_nfs_accessor(fs): | 
|  | 21 | +    # type: (FakeFilesystem) -> None | 
|  | 22 | +    accessor = xcp.accessor.createAccessor("nfs://server/path", False) | 
|  | 23 | +    check_mounting_accessor(accessor, fs) | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +def check_mounting_accessor(accessor, fs): | 
|  | 27 | +    # type: (xcp.accessor.MountingAccessor, FakeFilesystem) -> None | 
|  | 28 | +    """Test subclasses of MountingAccessor (with xcp.cmd.runCmd in xcp.mount mocked)""" | 
|  | 29 | + | 
|  | 30 | +    with patch("xcp.cmd.runCmd") as mount_runcmd: | 
|  | 31 | +        mount_runcmd.return_value = (0, "", "") | 
|  | 32 | +        accessor.start() | 
|  | 33 | + | 
|  | 34 | +    assert accessor.location | 
|  | 35 | +    assert fs.isdir(accessor.location) | 
|  | 36 | + | 
|  | 37 | +    location = accessor.location | 
|  | 38 | + | 
|  | 39 | +    if sys.version_info.major >= 3: | 
|  | 40 | +        fs.add_mount_point(location) | 
|  | 41 | + | 
|  | 42 | +    assert check_binary_read(accessor, location, fs) | 
|  | 43 | +    assert check_binary_write(accessor, location, fs) | 
|  | 44 | + | 
|  | 45 | +    if sys.version_info.major >= 3: | 
|  | 46 | +        fs.mount_points.pop(location) | 
|  | 47 | + | 
|  | 48 | +    with patch("xcp.cmd.runCmd"): | 
|  | 49 | +        accessor.finish() | 
|  | 50 | + | 
|  | 51 | +    assert not fs.exists(location) | 
|  | 52 | + | 
|  | 53 | +    assert not accessor.location | 
|  | 54 | + | 
|  | 55 | + | 
|  | 56 | +def check_binary_read(accessor, location, fs): | 
|  | 57 | +    # type: (xcp.accessor.MountingAccessor, str, FakeFilesystem) -> bool | 
|  | 58 | +    """Test the openAddress() method of subclasses of xcp.accessor.MountingAccessor""" | 
|  | 59 | + | 
|  | 60 | +    name = "binary_file" | 
|  | 61 | +    path = location + "/" + name | 
|  | 62 | + | 
|  | 63 | +    assert fs.create_file(path, contents=cast(str, binary_data)) | 
|  | 64 | + | 
|  | 65 | +    assert accessor.access(name) | 
|  | 66 | + | 
|  | 67 | +    binary_file = accessor.openAddress(name) | 
|  | 68 | +    assert not isinstance(binary_file, bool) | 
|  | 69 | + | 
|  | 70 | +    fs.remove(path) | 
|  | 71 | +    return cast(bytes, binary_file.read()) == binary_data | 
|  | 72 | + | 
|  | 73 | + | 
|  | 74 | +def check_binary_write(accessor, location, fs): | 
|  | 75 | +    # type: (xcp.accessor.MountingAccessor, str, FakeFilesystem) -> bool | 
|  | 76 | +    """Test the writeFile() method of subclasses of xcp.accessor.MountingAccessor""" | 
|  | 77 | + | 
|  | 78 | +    name = "binary_file_written_by_accessor" | 
|  | 79 | +    accessor.writeFile(BytesIO(binary_data), name) | 
|  | 80 | + | 
|  | 81 | +    assert accessor.access(name) | 
|  | 82 | + | 
|  | 83 | +    with FakeFileOpen(fs, delete_on_close=True)(location + "/" + name, "rb") as written: | 
|  | 84 | +        return cast(bytes, written.read()) == binary_data | 
0 commit comments