Filesystem: Manipulating Files and Directories

The pytest_mh.utils.fs module provides access to remote files and directories: reading and writing files, creating directories, making temporary directories and files and more.

A backup is created for every path that is changed during a test and it is restored after the test is finished. Therefore, you don’t have to worry about touching any paths, the original content and state (including ownership, mode and context) are fully restored.

See also

See the API reference of LinuxFileSystem for more information.

Note

Currently, we only provide LinuxFileSystem to manipulate files and directions on Linux systems. Contributions for Windows world are welcomed.

Example: Adding fs utility to your role
from pytest_mh import MultihostHost
from pytest_mh.utils.fs import LinuxFileSystem

class ExampleRole(MultihostHost[ExampleDomain]):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

    self.fs: LinuxFileSystem = LinuxFileSystem(self.host)
    """
    File system manipulation.
    """
Example: Writing contents to a file
@pytest.mark.topology(...)
def test_fs(client: ClientRole):
    ...
    client.fs.write("/etc/my.conf", "configuration", mode="600")
    ...
Example: Writing contents to a temporary file
@pytest.mark.topology(...)
def test_fs(client: ClientRole):
    ...
    tmp_path = client.fs.mktmp("contents")
    ...