aboutsummaryrefslogtreecommitdiff
path: root/tests/test_backend_install.py
blob: 3636fb4669630e4c673e649e094406b826a3c79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# SPDX-FileCopyrightText: Copyright 2022-2024, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for common management functionality."""
from __future__ import annotations

import tarfile
import tempfile
from pathlib import Path
from typing import Callable
from unittest.mock import ANY
from unittest.mock import MagicMock

import pytest

from mlia.backend.install import BackendInfo
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.repo import BackendRepository
from mlia.utils.download import DownloadConfig


@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(
    "supported_platforms, expected_result",
    [
        [None, True],
        [["UNKNOWN"], False],
    ],
)
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, monkeypatch: pytest.MonkeyPatch
) -> None:
    """Test methods of backend installation."""
    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)

    monkeypatch.setattr("mlia.backend.install.download", MagicMock())
    monkeypatch.setattr(
        "mlia.utils.download.DownloadConfig.filename",
        tmp_archive,
    )

    installation = BackendInstallation(
        "sample_backend",
        "Sample backend",
        "sample_backend",
        DownloadConfig(url="NOT_USED", sha256_hash="NOT_USED"),
        None,
        lambda path: BackendInfo(path, copy_source=False),
        lambda eula_agreement, path: path,
    )

    assert installation.supports(DownloadAndInstall())
    installation.install(DownloadAndInstall())

    backend_repo.add_backend.assert_called_with("sample_backend", ANY, None)


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")

    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())


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")


def _gen_rel_file(dir_path: Path) -> Path:
    file_path = dir_path / "test.txt"
    if not file_path.exists():
        file_path.touch()
    return file_path


def _gen_abs_file(dir_path: Path) -> Path:
    return _gen_rel_file(dir_path).resolve()


def _gen_rel_sym(dir_path: Path) -> Path:
    file_path = _gen_rel_file(dir_path)
    lnk_path = dir_path / "symlink-rel"
    lnk_path.symlink_to(file_path.relative_to(dir_path))
    return lnk_path


def _gen_abs_sym(dir_path: Path) -> Path:
    file_path = _gen_abs_file(dir_path)
    lnk_path = dir_path / Path("symlink-abs")
    lnk_path.symlink_to(file_path)
    return lnk_path


def _gen_rel_lnk(dir_path: Path) -> Path:
    file_path = _gen_rel_file(dir_path)
    lnk_path = dir_path / "hardlink-rel"
    lnk_path.symlink_to(file_path.relative_to(dir_path))
    return lnk_path


def _gen_abs_lnk(dir_path: Path) -> Path:
    file_path = _gen_abs_file(dir_path)
    lnk_path = dir_path / Path("hardlink-abs")
    lnk_path.symlink_to(file_path)
    return lnk_path


@pytest.mark.parametrize(
    ("gen_file_func", "num_members_in", "num_members_out"),
    (
        (_gen_rel_file, 1, 1),
        (_gen_rel_sym, 1, 1),
        (_gen_abs_sym, 1, 0),
        (_gen_rel_lnk, 1, 1),
        (_gen_abs_lnk, 1, 0),
    ),
)
def test_filter_tar_members(
    gen_file_func: Callable[[Path], Path],
    num_members_in: int,
    num_members_out: int,
    tmp_path: Path,
) -> None:
    """Test function BackendInstallation._filter_tar_members()."""

    def create_tar(file_to_add: Path, archive_path: Path) -> None:
        with tarfile.open(archive_path, "w") as archive:
            archive.add(file_to_add, arcname=file_to_add, recursive=False)

    with tempfile.TemporaryDirectory() as tmp_dir_2:
        with pytest.raises(ValueError):
            Path(tmp_dir_2).relative_to(tmp_path)

        archive_path = Path(tmp_dir_2) / "test.tar.gz"
        file_path = gen_file_func(tmp_path)
        create_tar(file_path, archive_path)
        with tarfile.open(archive_path) as archive:
            orig_members = list(archive.getmembers())
            assert len(orig_members) == num_members_in
            filtered_members = list(
                # pylint: disable=protected-access
                BackendInstallation._filter_tar_members(orig_members, tmp_path)
            )
            assert len(filtered_members) == num_members_out