From 3e3dcb9bd5abb88adcd85b4f89e8a81e7f6fa293 Mon Sep 17 00:00:00 2001 From: Dmitrii Agibov Date: Fri, 27 Jan 2023 09:12:50 +0000 Subject: MLIA-595 Remove old backend configuration mechanism - Remove old backend configuration code - Install backends into directory ~/.mlia - Rename targets/backends in registry to make it consistent across codebase. Change-Id: I9c8b012fe863280f1c692940c0dcad3ef638aaae --- tests/test_backend_install.py | 240 +++++++++++++++++++++++++++++++----------- 1 file changed, 176 insertions(+), 64 deletions(-) (limited to 'tests/test_backend_install.py') diff --git a/tests/test_backend_install.py b/tests/test_backend_install.py index c3efe09..dacb1aa 100644 --- a/tests/test_backend_install.py +++ b/tests/test_backend_install.py @@ -3,91 +3,203 @@ """Tests for common management functionality.""" from __future__ import annotations +import tarfile from pathlib import Path +from unittest.mock import ANY +from unittest.mock import MagicMock import pytest from mlia.backend.install import BackendInfo -from mlia.backend.install import get_all_application_names +from mlia.backend.install import BackendInstallation +from mlia.backend.install import CompoundPathChecker +from mlia.backend.install import DownloadAndInstall +from mlia.backend.install import InstallFromPath +from mlia.backend.install import PackagePathChecker from mlia.backend.install import StaticPathChecker -from mlia.backend.registry import get_supported_backends -from mlia.target.registry import is_supported +from mlia.backend.repo import BackendRepository + + +@pytest.fixture(name="backend_repo") +def mock_backend_repo(monkeypatch: pytest.MonkeyPatch) -> MagicMock: + """Mock backend repository.""" + mock = MagicMock(spec=BackendRepository) + monkeypatch.setattr("mlia.backend.install.get_backend_repository", lambda: mock) + + return mock + + +def test_wrong_install_type() -> None: + """Test that installation should fail for wrong install type.""" + installation = BackendInstallation( + "sample_backend", + "Sample backend", + "sample_backend", + None, + None, + lambda path: None, + None, + ) + + assert not installation.supports("some_path") # type: ignore + + with pytest.raises(Exception): + installation.install("some_path") # type: ignore @pytest.mark.parametrize( - "copy_source, system_config", + "supported_platforms, expected_result", [ - (True, "system_config.json"), - (True, None), - (False, "system_config.json"), - (False, None), + [None, True], + [["UNKNOWN"], False], ], ) -def test_static_path_checker( - tmp_path: Path, copy_source: bool, system_config: str +def test_backend_could_be_installed( + supported_platforms: list[str] | None, expected_result: bool +) -> None: + """Test method could_be_installed.""" + installation = BackendInstallation( + "sample_backend", + "Sample backend", + "sample_backend", + None, + supported_platforms, + lambda path: None, + None, + ) + + assert installation.could_be_installed == expected_result + + +@pytest.mark.parametrize("copy_source", [True, False]) +def test_backend_installation_from_path( + tmp_path: Path, backend_repo: MagicMock, copy_source: bool +) -> None: + """Test methods of backend installation.""" + installation = BackendInstallation( + "sample_backend", + "Sample backend", + "sample_backend", + None, + None, + lambda path: BackendInfo(path, copy_source=copy_source), + None, + ) + + assert installation.supports(InstallFromPath(tmp_path)) + assert not installation.supports(DownloadAndInstall()) + + installation.install(InstallFromPath(tmp_path)) + + if copy_source: + backend_repo.copy_backend.assert_called_with( + "sample_backend", tmp_path, "sample_backend", None + ) + backend_repo.add_backend.assert_not_called() + else: + backend_repo.copy_backend.assert_not_called() + backend_repo.add_backend.assert_called_with("sample_backend", tmp_path, None) + + +def test_backend_installation_download_and_install( + tmp_path: Path, backend_repo: MagicMock ) -> None: - """Test static path checker.""" - checker = StaticPathChecker(tmp_path, ["file1.txt"], copy_source, system_config) - tmp_path.joinpath("file1.txt").touch() + """Test methods of backend installation.""" + download_artifact_mock = MagicMock() - result = checker(tmp_path) - assert result == BackendInfo(tmp_path, copy_source, system_config) + tmp_archive = tmp_path.joinpath("sample.tgz") + sample_file = tmp_path.joinpath("sample.txt") + sample_file.touch() + with tarfile.open(tmp_archive, "w:gz") as archive: + archive.add(sample_file) -def test_static_path_checker_invalid_path(tmp_path: Path) -> None: - """Test static path checker with invalid path.""" - checker = StaticPathChecker(tmp_path, ["file1.txt"]) + download_artifact_mock.download_to.return_value = tmp_archive - result = checker(tmp_path) - assert result is None + installation = BackendInstallation( + "sample_backend", + "Sample backend", + "sample_backend", + download_artifact_mock, + None, + lambda path: BackendInfo(path, copy_source=False), + lambda eula_agreement, path: path, + ) - result = checker(tmp_path / "unknown_directory") - assert result is None + assert installation.supports(DownloadAndInstall()) + installation.install(DownloadAndInstall()) + backend_repo.add_backend.assert_called_with("sample_backend", ANY, None) -def test_supported_backends() -> None: - """Test function supported backends.""" - assert get_supported_backends() == [ - "ArmNNTFLiteDelegate", - "Corstone-300", - "Corstone-310", - "TOSA-Checker", - "Vela", - ] +def test_backend_installation_unable_to_download() -> None: + """Test that installation should fail when downloading fails.""" + download_artifact_mock = MagicMock() + download_artifact_mock.download_to.side_effect = Exception("Download error") -@pytest.mark.parametrize( - "backend, expected_result", - [ - ["unknown_backend", False], - ["Corstone-300", True], - ["Corstone-310", True], - ], -) -def test_is_supported(backend: str, expected_result: bool) -> None: - """Test function is_supported.""" - assert is_supported(backend) == expected_result + installation = BackendInstallation( + "sample_backend", + "Sample backend", + "sample_backend", + download_artifact_mock, + None, + lambda path: BackendInfo(path, copy_source=False), + lambda eula_agreement, path: path, + ) + with pytest.raises(Exception, match="Unable to download backend artifact"): + installation.install(DownloadAndInstall()) -@pytest.mark.parametrize( - "backend, expected_result", - [ - [ - "Corstone-300", - [ - "Generic Inference Runner: Ethos-U55", - "Generic Inference Runner: Ethos-U65", - ], - ], - [ - "Corstone-310", - [ - "Generic Inference Runner: Ethos-U55", - "Generic Inference Runner: Ethos-U65", - ], - ], - ], -) -def test_get_all_application_names(backend: str, expected_result: list[str]) -> None: - """Test function get_all_application_names.""" - assert sorted(get_all_application_names(backend)) == expected_result + +def test_static_path_checker(tmp_path: Path) -> None: + """Test for StaticPathChecker.""" + checker1 = StaticPathChecker(tmp_path, []) + assert checker1(tmp_path) == BackendInfo(tmp_path, copy_source=False) + + checker2 = StaticPathChecker(tmp_path / "dist", []) + assert checker2(tmp_path) is None + + checker3 = StaticPathChecker(tmp_path, ["sample.txt"]) + + assert checker3(tmp_path) is None + + sample_file = tmp_path.joinpath("sample.txt") + sample_file.touch() + + assert checker3(tmp_path) == BackendInfo(tmp_path, copy_source=False) + + +def test_compound_path_checker(tmp_path: Path) -> None: + """Test for CompoundPathChecker.""" + static_checker = StaticPathChecker(tmp_path, []) + compound_checker = CompoundPathChecker(static_checker) + + assert compound_checker(tmp_path) == BackendInfo(tmp_path, copy_source=False) + + +def test_package_path_checker(tmp_path: Path) -> None: + """Test PackagePathChecker.""" + sample_dir = tmp_path.joinpath("sample") + sample_dir.mkdir() + + checker1 = PackagePathChecker([], "sample") + assert checker1(tmp_path) == BackendInfo(tmp_path / "sample") + + checker2 = PackagePathChecker(["sample.txt"], "sample") + assert checker2(tmp_path) is None + + +def test_backend_installation_uninstall(backend_repo: MagicMock) -> None: + """Test backend removing process.""" + installation = BackendInstallation( + "sample_backend", + "Sample backend", + "sample_backend", + None, + None, + lambda path: None, + None, + ) + + installation.uninstall() + backend_repo.remove_backend.assert_called_with("sample_backend") -- cgit v1.2.1