diff --git a/swh/fuse/cli.py b/swh/fuse/cli.py --- a/swh/fuse/cli.py +++ b/swh/fuse/cli.py @@ -194,7 +194,7 @@ def rm_cache(conf, cache_name): try: - conf["cache"][cache_name]["path"].unlink(missing_ok=True) + Path(conf["cache"][cache_name]["path"]).unlink(missing_ok=True) except KeyError: pass diff --git a/swh/fuse/tests/conftest.py b/swh/fuse/tests/conftest.py --- a/swh/fuse/tests/conftest.py +++ b/swh/fuse/tests/conftest.py @@ -7,7 +7,6 @@ from multiprocessing import Process import os from pathlib import Path -import subprocess from tempfile import NamedTemporaryFile, TemporaryDirectory import time @@ -42,22 +41,20 @@ # Run FUSE in foreground mode but in a separate process, so it does not # block execution and remains easy to kill during teardown - def fuse_process(tmpdir, tmpfile): - with tmpdir as mntdir, tmpfile as config_path: - config_path = Path(config_path.name) - config_path.write_text(yaml.dump(config)) - CliRunner().invoke( - cli.fuse, - args=[ - "--config-file", - str(config_path), - "mount", - mntdir, - "--foreground", - ], - ) + def fuse_process(mntdir: Path, config_path: Path): + config_path.write_text(yaml.dump(config)) + CliRunner().invoke( + cli.fuse, + args=[ + "--config-file", + str(config_path), + "mount", + str(mntdir), + "--foreground", + ], + ) - fuse = Process(target=fuse_process, args=[tmpdir, tmpfile]) + fuse = Process(target=fuse_process, args=[Path(tmpdir.name), Path(tmpfile.name)]) fuse.start() # Wait max 3 seconds for the FUSE to correctly mount for i in range(30): @@ -73,5 +70,5 @@ yield Path(tmpdir.name) - subprocess.run(["fusermount", "-u", tmpdir.name], check=True) + CliRunner().invoke(cli.umount, [tmpdir.name]) fuse.join() diff --git a/swh/fuse/tests/test_cli.py b/swh/fuse/tests/test_cli.py --- a/swh/fuse/tests/test_cli.py +++ b/swh/fuse/tests/test_cli.py @@ -3,16 +3,32 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -import os +from pathlib import Path +from tempfile import NamedTemporaryFile -from swh.fuse.tests.data.config import REGULAR_FILE +from click.testing import CliRunner +import yaml +import swh.fuse.cli as cli -def test_mountpoint(fuse_mntdir): - archive_dir = fuse_mntdir / "archive" - meta_dir = fuse_mntdir / "meta" - assert os.listdir(archive_dir) == [] - assert os.listdir(meta_dir) == [] - # On the fly mounting - file_path = archive_dir / REGULAR_FILE - assert file_path.is_file() +# mount/umount commands are already tested when setting up the fuse_mntdir fixture + + +def test_clean_command(): + fake_metadata_cache = NamedTemporaryFile(delete=False) + fake_blob_cache = NamedTemporaryFile(delete=False) + config = { + "cache": { + "metadata": {"path": fake_metadata_cache.name}, + "blob": {"path": fake_blob_cache.name}, + }, + } + + with NamedTemporaryFile(suffix=".yml") as config_path: + Path(config_path.name).write_text(yaml.dump(config)) + CliRunner().invoke( + cli.fuse, args=["--config-file", config_path.name, "clean",], + ) + + assert not Path(fake_metadata_cache.name).exists() + assert not Path(fake_blob_cache.name).exists() diff --git a/swh/fuse/tests/test_mountpoint.py b/swh/fuse/tests/test_mountpoint.py new file mode 100644 --- /dev/null +++ b/swh/fuse/tests/test_mountpoint.py @@ -0,0 +1,22 @@ +# Copyright (C) 2020 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import os + +from swh.fuse.tests.data.config import ORIGIN_URL_ENCODED, REGULAR_FILE + + +def test_mountpoint(fuse_mntdir): + assert os.listdir(fuse_mntdir) == ["archive", "meta", "origin"] + + +def test_on_the_fly_mounting(fuse_mntdir): + assert os.listdir(fuse_mntdir / "archive") == [] + assert os.listdir(fuse_mntdir / "meta") == [] + assert (fuse_mntdir / "archive" / REGULAR_FILE).is_file() + assert (fuse_mntdir / "meta" / (REGULAR_FILE + ".json")).is_file() + + assert os.listdir(fuse_mntdir / "origin") == [] + assert (fuse_mntdir / "origin" / ORIGIN_URL_ENCODED).is_dir()