diff --git a/swh/fuse/cli.py b/swh/fuse/cli.py --- a/swh/fuse/cli.py +++ b/swh/fuse/cli.py @@ -194,8 +194,8 @@ def rm_cache(conf, cache_name): try: - conf["cache"][cache_name]["path"].unlink(missing_ok=True) - except KeyError: + Path(conf["cache"][cache_name]["path"]).unlink() + except (FileNotFoundError, KeyError): pass conf = ctx.obj["config"] 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 @@ -32,7 +31,6 @@ @pytest.fixture def fuse_mntdir(web_api_mock): tmpdir = TemporaryDirectory(suffix=".swh-fuse-test") - tmpfile = NamedTemporaryFile(suffix=".swh-fuse-test.yml") config = { "cache": {"metadata": {"in-memory": True}, "blob": {"in-memory": True},}, @@ -42,9 +40,9 @@ # 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) + def fuse_process(mntdir: Path): + with NamedTemporaryFile(suffix=".swh-fuse-test.yml") as tmpfile: + config_path = Path(tmpfile.name) config_path.write_text(yaml.dump(config)) CliRunner().invoke( cli.fuse, @@ -52,12 +50,12 @@ "--config-file", str(config_path), "mount", - mntdir, + str(mntdir), "--foreground", ], ) - fuse = Process(target=fuse_process, args=[tmpdir, tmpfile]) + fuse = Process(target=fuse_process, args=[Path(tmpdir.name)]) fuse.start() # Wait max 3 seconds for the FUSE to correctly mount for i in range(30): @@ -73,5 +71,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,35 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -import os +from click.testing import CliRunner +import yaml -from swh.fuse.tests.data.config import REGULAR_FILE +import swh.fuse.cli as cli +# mount/umount commands are already tested when setting up the fuse_mntdir fixture -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() + +def test_clean_command(tmp_path): + fake_metadata_db = tmp_path / "metadata.sqlite" + fake_blob_db = tmp_path / "blob.sqlite" + config_path = tmp_path / "config.yml" + config = { + "cache": { + "metadata": {"path": str(fake_metadata_db)}, + "blob": {"path": str(fake_blob_db)}, + }, + } + + fake_metadata_db.touch() + fake_blob_db.touch() + config_path.write_text(yaml.dump(config)) + + assert fake_metadata_db.exists() + assert fake_blob_db.exists() + + CliRunner().invoke( + cli.fuse, args=["--config-file", str(config_path), "clean",], + ) + + assert not fake_metadata_db.exists() + assert not fake_blob_db.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()