aboutsummaryrefslogtreecommitdiff
path: root/tests/test_backend_manager.py
blob: 879353ea186e894af7b671a8894aac72634fa6ed (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for installation manager."""
from __future__ import annotations

from pathlib import Path
from typing import Any
from unittest.mock import call
from unittest.mock import MagicMock
from unittest.mock import PropertyMock

import pytest

from mlia.backend.install import DownloadAndInstall
from mlia.backend.install import Installation
from mlia.backend.install import InstallationType
from mlia.backend.install import InstallFromPath
from mlia.backend.manager import DefaultInstallationManager
from mlia.core.errors import ConfigurationError
from mlia.core.errors import InternalError


def get_default_installation_manager_mock(
    name: str,
    already_installed: bool = False,
) -> MagicMock:
    """Get mock instance for DefaultInstallationManager."""
    mock = MagicMock(spec=DefaultInstallationManager)

    props = {
        "name": name,
        "already_installed": already_installed,
    }
    for prop, value in props.items():
        setattr(type(mock), prop, PropertyMock(return_value=value))

    return mock


def _ready_for_uninstall_mock() -> MagicMock:
    return get_default_installation_manager_mock(
        name="already_installed",
        already_installed=True,
    )


def get_installation_mock(
    name: str,
    already_installed: bool = False,
    could_be_installed: bool = False,
    supported_install_type: type | tuple | None = None,
) -> MagicMock:
    """Get mock instance for the installation."""
    mock = MagicMock(spec=Installation)

    def supports(install_type: InstallationType) -> bool:
        if supported_install_type is None:
            return False

        return isinstance(install_type, supported_install_type)

    mock.supports.side_effect = supports

    props = {
        "name": name,
        "already_installed": already_installed,
        "could_be_installed": could_be_installed,
    }
    for prop, value in props.items():
        setattr(type(mock), prop, PropertyMock(return_value=value))

    return mock


def _already_installed_mock() -> MagicMock:
    return get_installation_mock(
        name="already_installed",
        already_installed=True,
        supported_install_type=(DownloadAndInstall, InstallFromPath),
    )


def _ready_for_installation_mock() -> MagicMock:
    return get_installation_mock(
        name="ready_for_installation",
        already_installed=False,
        could_be_installed=True,
    )


def _could_be_downloaded_and_installed_mock() -> MagicMock:
    return get_installation_mock(
        name="could_be_downloaded_and_installed",
        already_installed=False,
        could_be_installed=True,
        supported_install_type=DownloadAndInstall,
    )


def _could_be_installed_from_mock() -> MagicMock:
    return get_installation_mock(
        name="could_be_installed_from",
        already_installed=False,
        could_be_installed=True,
        supported_install_type=InstallFromPath,
    )


def get_installation_manager(
    noninteractive: bool,
    installations: list[Any],
    monkeypatch: pytest.MonkeyPatch,
    yes_response: bool = True,
) -> DefaultInstallationManager:
    """Get installation manager instance."""
    if not noninteractive:
        monkeypatch.setattr(
            "mlia.backend.manager.yes", MagicMock(return_value=yes_response)
        )

    return DefaultInstallationManager(installations, noninteractive=noninteractive)


def test_installation_manager_filtering() -> None:
    """Test default installation manager."""
    already_installed = _already_installed_mock()
    ready_for_installation = _ready_for_installation_mock()
    could_be_downloaded_and_installed = _could_be_downloaded_and_installed_mock()

    manager = DefaultInstallationManager(
        [
            already_installed,
            ready_for_installation,
            could_be_downloaded_and_installed,
        ]
    )
    assert manager.already_installed("already_installed") == [already_installed]
    assert manager.ready_for_installation() == [
        ready_for_installation,
        could_be_downloaded_and_installed,
    ]


@pytest.mark.parametrize("noninteractive", [True, False])
@pytest.mark.parametrize(
    "install_mock, eula_agreement, backend_name, force, expected_call",
    [
        [
            _could_be_downloaded_and_installed_mock(),
            True,
            "could_be_downloaded_and_installed",
            False,
            [call(DownloadAndInstall(eula_agreement=True))],
        ],
        [
            _could_be_downloaded_and_installed_mock(),
            False,
            "could_be_downloaded_and_installed",
            True,
            [call(DownloadAndInstall(eula_agreement=False))],
        ],
        [
            _already_installed_mock(),
            False,
            "already_installed",
            True,
            [call(DownloadAndInstall(eula_agreement=False))],
        ],
        [
            _could_be_downloaded_and_installed_mock(),
            False,
            "unknown",
            True,
            [],
        ],
    ],
)
def test_installation_manager_download_and_install(
    install_mock: MagicMock,
    noninteractive: bool,
    eula_agreement: bool,
    backend_name: str,
    force: bool,
    expected_call: Any,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test installation process."""
    install_mock.reset_mock()

    manager = get_installation_manager(noninteractive, [install_mock], monkeypatch)

    manager.download_and_install(
        backend_name, eula_agreement=eula_agreement, force=force
    )

    assert install_mock.install.mock_calls == expected_call
    if force and install_mock.already_installed:
        install_mock.uninstall.assert_called_once()
    else:
        install_mock.uninstall.assert_not_called()


@pytest.mark.parametrize("noninteractive", [True, False])
@pytest.mark.parametrize(
    "install_mock, backend_name, force, expected_call",
    [
        [
            _could_be_installed_from_mock(),
            "could_be_installed_from",
            False,
            [call(InstallFromPath(Path("some_path")))],
        ],
        [
            _could_be_installed_from_mock(),
            "unknown",
            False,
            [],
        ],
        [
            _could_be_installed_from_mock(),
            "unknown",
            True,
            [],
        ],
        [
            _already_installed_mock(),
            "already_installed",
            False,
            [],
        ],
        [
            _already_installed_mock(),
            "already_installed",
            True,
            [call(InstallFromPath(Path("some_path")))],
        ],
    ],
)
def test_installation_manager_install_from(
    install_mock: MagicMock,
    noninteractive: bool,
    backend_name: str,
    force: bool,
    expected_call: Any,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test installation process."""
    install_mock.reset_mock()

    manager = get_installation_manager(noninteractive, [install_mock], monkeypatch)
    manager.install_from(Path("some_path"), backend_name, force=force)

    assert install_mock.install.mock_calls == expected_call
    if force and install_mock.already_installed:
        install_mock.uninstall.assert_called_once()
    else:
        install_mock.uninstall.assert_not_called()


def test_installation_manager_unsupported_install_type(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
) -> None:
    """Test that installation could not be installed via unsupported type."""
    download_install_mock = _could_be_downloaded_and_installed_mock()
    install_from_mock = _could_be_installed_from_mock()
    install_mocks = [download_install_mock, install_from_mock]

    manager = get_installation_manager(False, install_mocks, monkeypatch)
    manager.install_from(tmp_path, "could_be_downloaded_and_installed")

    manager.download_and_install("could_be_installed_from")

    for mock in install_mocks:
        mock.install.assert_not_called()
        mock.uninstall.assert_not_called()


@pytest.mark.parametrize("noninteractive", [True, False])
@pytest.mark.parametrize(
    "install_mock, backend_name, expected_call",
    [
        [
            _ready_for_uninstall_mock(),
            "already_installed",
            [call()],
        ],
    ],
)
def test_installation_manager_uninstall(
    install_mock: MagicMock,
    noninteractive: bool,
    backend_name: str,
    expected_call: Any,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test uninstallation."""
    install_mock.reset_mock()

    manager = get_installation_manager(noninteractive, [install_mock], monkeypatch)
    manager.uninstall(backend_name)

    assert install_mock.uninstall.mock_calls == expected_call


def test_installation_internal_error(monkeypatch: pytest.MonkeyPatch) -> None:
    """Test that manager should be able to detect wrong state."""
    install_mock = _ready_for_uninstall_mock()
    manager = get_installation_manager(False, [install_mock, install_mock], monkeypatch)

    with pytest.raises(
        InternalError,
        match="More than one installed backend with name already_installed found",
    ):
        manager.uninstall("already_installed")


def test_uninstall_unknown_backend(monkeypatch: pytest.MonkeyPatch) -> None:
    """Test that uninstall should fail for uknown backend."""
    install_mock = _ready_for_uninstall_mock()
    manager = get_installation_manager(False, [install_mock, install_mock], monkeypatch)

    with pytest.raises(
        ConfigurationError, match="Backend 'some_backend' is not installed"
    ):
        manager.uninstall("some_backend")


def test_show_env_details(monkeypatch: pytest.MonkeyPatch) -> None:
    """Test method show_env_details."""
    ready_to_install_mock = _ready_for_installation_mock()
    could_be_installed_mock = _could_be_installed_from_mock()

    manager = get_installation_manager(
        False,
        [ready_to_install_mock, could_be_installed_mock],
        monkeypatch,
    )
    manager.show_env_details()