aboutsummaryrefslogtreecommitdiff
path: root/tests/test_utils_download.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_utils_download.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_utils_download.py')
-rw-r--r--tests/test_utils_download.py147
1 files changed, 147 insertions, 0 deletions
diff --git a/tests/test_utils_download.py b/tests/test_utils_download.py
new file mode 100644
index 0000000..4f8e2dc
--- /dev/null
+++ b/tests/test_utils_download.py
@@ -0,0 +1,147 @@
+# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+"""Tests for download functionality."""
+from contextlib import ExitStack as does_not_raise
+from pathlib import Path
+from typing import Any
+from typing import Iterable
+from typing import Optional
+from unittest.mock import MagicMock
+from unittest.mock import PropertyMock
+
+import pytest
+import requests
+
+from mlia.utils.download import download
+from mlia.utils.download import DownloadArtifact
+
+
+def response_mock(
+ content_length: Optional[str], content_chunks: Iterable[bytes]
+) -> MagicMock:
+ """Mock response object."""
+ mock = MagicMock(spec=requests.Response)
+ mock.__enter__.return_value = mock
+
+ type(mock).headers = PropertyMock(return_value={"Content-Length": content_length})
+ mock.iter_content.return_value = content_chunks
+
+ return mock
+
+
+@pytest.mark.parametrize("show_progress", [True, False])
+@pytest.mark.parametrize(
+ "content_length, content_chunks, label",
+ [
+ [
+ "5",
+ [bytes(range(5))],
+ "Downloading artifact",
+ ],
+ [
+ "10",
+ [bytes(range(5)), bytes(range(5))],
+ None,
+ ],
+ [
+ None,
+ [bytes(range(5))],
+ "Downlading no size",
+ ],
+ [
+ "abc",
+ [bytes(range(5))],
+ "Downloading wrong size",
+ ],
+ ],
+)
+def test_download(
+ show_progress: bool,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ content_length: Optional[str],
+ content_chunks: Iterable[bytes],
+ label: Optional[str],
+) -> None:
+ """Test function download."""
+ monkeypatch.setattr(
+ "mlia.utils.download.requests.get",
+ MagicMock(return_value=response_mock(content_length, content_chunks)),
+ )
+
+ dest = tmp_path / "sample.bin"
+ download("some_url", dest, show_progress=show_progress, label=label)
+
+ assert dest.is_file()
+ assert dest.read_bytes() == bytes(
+ byte for chunk in content_chunks for byte in chunk
+ )
+
+
+@pytest.mark.parametrize(
+ "content_length, content_chunks, sha256_hash, expected_error",
+ [
+ [
+ "10",
+ [bytes(range(10))],
+ "1f825aa2f0020ef7cf91dfa30da4668d791c5d4824fc8e41354b89ec05795ab3",
+ does_not_raise(),
+ ],
+ [
+ "10",
+ [bytes(range(10))],
+ "bad_hash",
+ pytest.raises(ValueError, match="Digests do not match"),
+ ],
+ ],
+)
+def test_download_artifact_download_to(
+ monkeypatch: pytest.MonkeyPatch,
+ content_length: Optional[str],
+ content_chunks: Iterable[bytes],
+ sha256_hash: str,
+ expected_error: Any,
+ tmp_path: Path,
+) -> None:
+ """Test artifact downloading."""
+ monkeypatch.setattr(
+ "mlia.utils.download.requests.get",
+ MagicMock(return_value=response_mock(content_length, content_chunks)),
+ )
+
+ with expected_error:
+ artifact = DownloadArtifact(
+ "test_artifact",
+ "some_url",
+ "artifact_filename",
+ "1.0",
+ sha256_hash,
+ )
+
+ dest = artifact.download_to(tmp_path)
+ assert isinstance(dest, Path)
+ assert dest.name == "artifact_filename"
+
+
+def test_download_artifact_unable_to_overwrite(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ """Test that download process cannot overwrite file."""
+ monkeypatch.setattr(
+ "mlia.utils.download.requests.get",
+ MagicMock(return_value=response_mock("10", [bytes(range(10))])),
+ )
+
+ artifact = DownloadArtifact(
+ "test_artifact",
+ "some_url",
+ "artifact_filename",
+ "1.0",
+ "sha256_hash",
+ )
+
+ existing_file = tmp_path / "artifact_filename"
+ existing_file.touch()
+
+ with pytest.raises(ValueError, match=f"{existing_file} already exists"):
+ artifact.download_to(tmp_path)