aboutsummaryrefslogtreecommitdiff
path: root/tests/test_backend_fs.py
diff options
context:
space:
mode:
authorBenjamin Klimczak <benjamin.klimczak@arm.com>2022-07-11 12:33:42 +0100
committerBenjamin Klimczak <benjamin.klimczak@arm.com>2022-07-26 14:08:21 +0100
commit5d81f37de09efe10f90512e50252be9c36925fcf (patch)
treeb4d7cdfd051da0a6e882bdfcf280fd7ca7b39e57 /tests/test_backend_fs.py
parent7899b908c1fe6d86b92a80f3827ddd0ac05b674b (diff)
downloadmlia-5d81f37de09efe10f90512e50252be9c36925fcf.tar.gz
MLIA-551 Rework remains of AIET architecture
Re-factoring the code base to further merge the old AIET code into MLIA. - Remove last traces of the backend type 'tool' - Controlled systems removed, including SSH protocol, controller, RunningCommand, locks etc. - Build command / build dir and deploy functionality removed from Applications and Systems - Moving working_dir() - Replace module 'output_parser' with new module 'output_consumer' and merge Base64 parsing into it - Change the output consumption to optionally remove (i.e. actually consume) lines - Use Base64 parsing in GenericInferenceOutputParser, replacing the regex-based parsing and remove the now unused regex parsing - Remove AIET reporting - Pre-install applications by moving them to src/mlia/resources/backends - Rename aiet-config.json to backend-config.json - Move tests from tests/mlia/ to tests/ - Adapt unit tests to code changes - Dependencies removed: paramiko, filelock, psutil - Fix bug in corstone.py: The wrong resource directory was used which broke the functionality to download backends. - Use f-string formatting. - Use logging instead of print. Change-Id: I768bc3bb6b2eda57d219ad01be4a8e0a74167d76
Diffstat (limited to 'tests/test_backend_fs.py')
-rw-r--r--tests/test_backend_fs.py134
1 files changed, 134 insertions, 0 deletions
diff --git a/tests/test_backend_fs.py b/tests/test_backend_fs.py
new file mode 100644
index 0000000..7423222
--- /dev/null
+++ b/tests/test_backend_fs.py
@@ -0,0 +1,134 @@
+# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+# pylint: disable=no-self-use
+"""Module for testing fs.py."""
+from contextlib import ExitStack as does_not_raise
+from pathlib import Path
+from typing import Any
+from typing import Union
+from unittest.mock import MagicMock
+
+import pytest
+
+from mlia.backend.fs import get_backends_path
+from mlia.backend.fs import recreate_directory
+from mlia.backend.fs import remove_directory
+from mlia.backend.fs import remove_resource
+from mlia.backend.fs import ResourceType
+from mlia.backend.fs import valid_for_filename
+
+
+@pytest.mark.parametrize(
+ "resource_name,expected_path",
+ [
+ ("systems", does_not_raise()),
+ ("applications", does_not_raise()),
+ ("whaaat", pytest.raises(ResourceWarning)),
+ (None, pytest.raises(ResourceWarning)),
+ ],
+)
+def test_get_backends_path(resource_name: ResourceType, expected_path: Any) -> None:
+ """Test get_resources() with multiple parameters."""
+ with expected_path:
+ resource_path = get_backends_path(resource_name)
+ assert resource_path.exists()
+
+
+def test_remove_resource_wrong_directory(
+ monkeypatch: Any, test_applications_path: Path
+) -> None:
+ """Test removing resource with wrong directory."""
+ mock_get_resources = MagicMock(return_value=test_applications_path)
+ monkeypatch.setattr("mlia.backend.fs.get_backends_path", mock_get_resources)
+
+ mock_shutil_rmtree = MagicMock()
+ monkeypatch.setattr("mlia.backend.fs.shutil.rmtree", mock_shutil_rmtree)
+
+ with pytest.raises(Exception, match="Resource .* does not exist"):
+ remove_resource("unknown", "applications")
+ mock_shutil_rmtree.assert_not_called()
+
+ with pytest.raises(Exception, match="Wrong resource .*"):
+ remove_resource("readme.txt", "applications")
+ mock_shutil_rmtree.assert_not_called()
+
+
+def test_remove_resource(monkeypatch: Any, test_applications_path: Path) -> None:
+ """Test removing resource data."""
+ mock_get_resources = MagicMock(return_value=test_applications_path)
+ monkeypatch.setattr("mlia.backend.fs.get_backends_path", mock_get_resources)
+
+ mock_shutil_rmtree = MagicMock()
+ monkeypatch.setattr("mlia.backend.fs.shutil.rmtree", mock_shutil_rmtree)
+
+ remove_resource("application1", "applications")
+ mock_shutil_rmtree.assert_called_once()
+
+
+def test_remove_directory(tmpdir: Any) -> None:
+ """Test directory removal."""
+ tmpdir_path = Path(tmpdir)
+ tmpfile = tmpdir_path / "temp.txt"
+
+ for item in [None, tmpfile]:
+ with pytest.raises(Exception, match="No directory path provided"):
+ remove_directory(item)
+
+ newdir = tmpdir_path / "newdir"
+ newdir.mkdir()
+
+ assert newdir.is_dir()
+ remove_directory(newdir)
+ assert not newdir.exists()
+
+
+def test_recreate_directory(tmpdir: Any) -> None:
+ """Test directory recreation."""
+ with pytest.raises(Exception, match="No directory path provided"):
+ recreate_directory(None)
+
+ tmpdir_path = Path(tmpdir)
+ tmpfile = tmpdir_path / "temp.txt"
+ tmpfile.touch()
+ with pytest.raises(Exception, match="Path .* does exist and it is not a directory"):
+ recreate_directory(tmpfile)
+
+ newdir = tmpdir_path / "newdir"
+ newdir.mkdir()
+ newfile = newdir / "newfile"
+ newfile.touch()
+ assert list(newdir.iterdir()) == [newfile]
+ recreate_directory(newdir)
+ assert not list(newdir.iterdir())
+
+ newdir2 = tmpdir_path / "newdir2"
+ assert not newdir2.exists()
+ recreate_directory(newdir2)
+ assert newdir2.is_dir()
+
+
+def write_to_file(
+ write_directory: Any, write_mode: str, write_text: Union[str, bytes]
+) -> Path:
+ """Write some text to a temporary test file."""
+ tmpdir_path = Path(write_directory)
+ tmpfile = tmpdir_path / "file_name.txt"
+ with open(tmpfile, write_mode) as file: # pylint: disable=unspecified-encoding
+ file.write(write_text)
+ return tmpfile
+
+
+@pytest.mark.parametrize(
+ "value, replacement, expected_result",
+ [
+ ["", "", ""],
+ ["123", "", "123"],
+ ["123", "_", "123"],
+ ["/some_folder/some_script.sh", "", "some_foldersome_script.sh"],
+ ["/some_folder/some_script.sh", "_", "_some_folder_some_script.sh"],
+ ["!;'some_name$%^!", "_", "___some_name____"],
+ ],
+)
+def test_valid_for_filename(value: str, replacement: str, expected_result: str) -> None:
+ """Test function valid_for_filename."""
+ assert valid_for_filename(value, replacement) == expected_result