aboutsummaryrefslogtreecommitdiff
path: root/tests/test_backend_executor_runner.py
blob: 36c6e5e91849f85f6509344e083ccaa3de06846f (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
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for module backend/manager."""
from __future__ import annotations

from pathlib import Path
from unittest.mock import MagicMock
from unittest.mock import PropertyMock

import pytest

from mlia.backend.corstone.performance import BackendRunner
from mlia.backend.corstone.performance import ExecutionParams


class TestBackendRunner:
    """Tests for BackendRunner class."""

    @staticmethod
    def _setup_backends(
        monkeypatch: pytest.MonkeyPatch,
        available_systems: list[str] | None = None,
        available_apps: list[str] | None = None,
    ) -> None:
        """Set up backend metadata."""

        def mock_system(system: str) -> MagicMock:
            """Mock the System instance."""
            mock = MagicMock()
            type(mock).name = PropertyMock(return_value=system)
            return mock

        def mock_app(app: str) -> MagicMock:
            """Mock the Application instance."""
            mock = MagicMock()
            type(mock).name = PropertyMock(return_value=app)
            mock.can_run_on.return_value = True
            return mock

        system_mocks = [mock_system(name) for name in (available_systems or [])]
        monkeypatch.setattr(
            "mlia.backend.executor.runner.get_available_systems",
            MagicMock(return_value=system_mocks),
        )

        apps_mock = [mock_app(name) for name in (available_apps or [])]
        monkeypatch.setattr(
            "mlia.backend.executor.runner.get_available_applications",
            MagicMock(return_value=apps_mock),
        )

    @pytest.mark.parametrize(
        "available_systems, system, installed",
        [
            ([], "system1", False),
            (["system1", "system2"], "system1", True),
        ],
    )
    def test_is_system_installed(
        self,
        available_systems: list,
        system: str,
        installed: bool,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        """Test method is_system_installed."""
        backend_runner = BackendRunner()

        self._setup_backends(monkeypatch, available_systems)

        assert backend_runner.is_system_installed(system) == installed

    @pytest.mark.parametrize(
        "available_systems, systems",
        [
            ([], []),
            (["system1"], ["system1"]),
        ],
    )
    def test_installed_systems(
        self,
        available_systems: list[str],
        systems: list[str],
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        """Test method installed_systems."""
        backend_runner = BackendRunner()

        self._setup_backends(monkeypatch, available_systems)
        assert backend_runner.get_installed_systems() == systems

    @staticmethod
    def test_install_system(monkeypatch: pytest.MonkeyPatch) -> None:
        """Test system installation."""
        install_system_mock = MagicMock()
        monkeypatch.setattr(
            "mlia.backend.executor.runner.install_system", install_system_mock
        )

        backend_runner = BackendRunner()
        backend_runner.install_system(Path("test_system_path"))

        install_system_mock.assert_called_once_with(Path("test_system_path"))

    @pytest.mark.parametrize(
        "available_systems, systems, expected_result",
        [
            ([], [], False),
            (["system1"], [], False),
            (["system1"], ["system1"], True),
            (["system1", "system2"], ["system1", "system3"], False),
            (["system1", "system2"], ["system1", "system2"], True),
        ],
    )
    def test_systems_installed(
        self,
        available_systems: list[str],
        systems: list[str],
        expected_result: bool,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        """Test method systems_installed."""
        self._setup_backends(monkeypatch, available_systems)

        backend_runner = BackendRunner()

        assert backend_runner.systems_installed(systems) is expected_result

    @pytest.mark.parametrize(
        "available_apps, applications, expected_result",
        [
            ([], [], False),
            (["app1"], [], False),
            (["app1"], ["app1"], True),
            (["app1", "app2"], ["app1", "app3"], False),
            (["app1", "app2"], ["app1", "app2"], True),
        ],
    )
    def test_applications_installed(
        self,
        available_apps: list[str],
        applications: list[str],
        expected_result: bool,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        """Test method applications_installed."""
        self._setup_backends(monkeypatch, [], available_apps)
        backend_runner = BackendRunner()

        assert backend_runner.applications_installed(applications) is expected_result

    @pytest.mark.parametrize(
        "available_apps, applications",
        [
            ([], []),
            (
                ["application1", "application2"],
                ["application1", "application2"],
            ),
        ],
    )
    def test_get_installed_applications(
        self,
        available_apps: list[str],
        applications: list[str],
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        """Test method get_installed_applications."""
        self._setup_backends(monkeypatch, [], available_apps)

        backend_runner = BackendRunner()
        assert applications == backend_runner.get_installed_applications()

    @staticmethod
    def test_install_application(monkeypatch: pytest.MonkeyPatch) -> None:
        """Test application installation."""
        mock_install_application = MagicMock()
        monkeypatch.setattr(
            "mlia.backend.executor.runner.install_application",
            mock_install_application,
        )

        backend_runner = BackendRunner()
        backend_runner.install_application(Path("test_application_path"))
        mock_install_application.assert_called_once_with(Path("test_application_path"))

    @pytest.mark.parametrize(
        "available_apps, application, installed",
        [
            ([], "system1", False),
            (
                ["application1", "application2"],
                "application1",
                True,
            ),
            (
                [],
                "application1",
                False,
            ),
        ],
    )
    def test_is_application_installed(
        self,
        available_apps: list[str],
        application: str,
        installed: bool,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        """Test method is_application_installed."""
        self._setup_backends(monkeypatch, [], available_apps)

        backend_runner = BackendRunner()
        assert installed == backend_runner.is_application_installed(
            application, "system1"
        )

    @staticmethod
    @pytest.mark.parametrize(
        "execution_params, expected_command",
        [
            (
                ExecutionParams("application_4", "System 4", [], []),
                ["application_4", [], "System 4", []],
            ),
            (
                ExecutionParams(
                    "application_6",
                    "System 6",
                    ["param1=value2"],
                    ["sys-param1=value2"],
                ),
                [
                    "application_6",
                    ["param1=value2"],
                    "System 6",
                    ["sys-param1=value2"],
                ],
            ),
        ],
    )
    def test_run_application_local(
        monkeypatch: pytest.MonkeyPatch,
        execution_params: ExecutionParams,
        expected_command: list[str],
    ) -> None:
        """Test method run_application with local systems."""
        run_app = MagicMock()
        monkeypatch.setattr("mlia.backend.executor.runner.run_application", run_app)

        backend_runner = BackendRunner()
        backend_runner.run_application(execution_params)

        run_app.assert_called_once_with(*expected_command)