aboutsummaryrefslogtreecommitdiff
path: root/tests/aiet/test_cli_tool.py
blob: 45d45c8a86c17de33d4b33890113494d3538bbfc (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
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
# pylint: disable=attribute-defined-outside-init,no-member,line-too-long,too-many-arguments,too-many-locals
"""Module for testing CLI tool subcommand."""
import json
from pathlib import Path
from typing import Any
from typing import List
from typing import Optional
from typing import Sequence
from unittest.mock import MagicMock

import click
import pytest
from click.testing import CliRunner
from click.testing import Result

from aiet.backend.tool import get_unique_tool_names
from aiet.backend.tool import Tool
from aiet.cli.tool import details_cmd
from aiet.cli.tool import execute_cmd
from aiet.cli.tool import list_cmd
from aiet.cli.tool import tool_cmd


def test_tool_cmd() -> None:
    """Test tool commands."""
    commands = ["list", "details", "execute"]
    assert all(command in tool_cmd.commands for command in commands)


@pytest.mark.parametrize("format_", ["json", "cli"])
def test_tool_cmd_context(cli_runner: CliRunner, format_: str) -> None:
    """Test setting command context parameters."""
    result = cli_runner.invoke(tool_cmd, ["--format", format_])
    # command should fail if no subcommand provided
    assert result.exit_code == 2

    result = cli_runner.invoke(tool_cmd, ["--format", format_, "list"])
    assert result.exit_code == 0


@pytest.mark.parametrize(
    "format_, expected_output",
    [
        (
            "json",
            '{"type": "tool", "available": ["tool_1", "tool_2"]}\n',
        ),
        ("cli", "Available tools:\n\ntool_1\ntool_2\n"),
    ],
)
def test_list_cmd(
    cli_runner: CliRunner,
    monkeypatch: Any,
    format_: str,
    expected_output: str,
) -> None:
    """Test available tool commands."""
    # Mock some tools
    mock_tool_1 = MagicMock(spec=Tool)
    mock_tool_1.name = "tool_1"
    mock_tool_2 = MagicMock(spec=Tool)
    mock_tool_2.name = "tool_2"

    # Monkey patch the call get_available_tools
    mock_available_tools = MagicMock()
    mock_available_tools.return_value = [mock_tool_1, mock_tool_2]

    monkeypatch.setattr("aiet.backend.tool.get_available_tools", mock_available_tools)

    obj = {"format": format_}
    args: Sequence[str] = []
    result = cli_runner.invoke(list_cmd, obj=obj, args=args)
    assert result.output == expected_output


def get_details_cmd_json_output() -> List[dict]:
    """Get JSON output for details command."""
    json_output = [
        {
            "type": "tool",
            "name": "tool_1",
            "description": "This is tool 1",
            "supported_systems": ["System 1"],
            "commands": {
                "clean": {"command_strings": ["echo 'clean'"], "user_params": []},
                "build": {"command_strings": ["echo 'build'"], "user_params": []},
                "run": {"command_strings": ["echo 'run'"], "user_params": []},
                "post_run": {"command_strings": ["echo 'post_run'"], "user_params": []},
            },
        }
    ]

    return json_output


def get_details_cmd_console_output() -> str:
    """Get console output for details command."""
    return (
        'Tool "tool_1" details'
        "\nDescription: This is tool 1"
        "\n\nSupported systems: System 1"
        "\n\nclean commands:"
        "\nCommands: [\"echo 'clean'\"]"
        "\n\nbuild commands:"
        "\nCommands: [\"echo 'build'\"]"
        "\n\nrun commands:\nCommands: [\"echo 'run'\"]"
        "\n\npost_run commands:"
        "\nCommands: [\"echo 'post_run'\"]"
        "\n"
    )


@pytest.mark.parametrize(
    [
        "tool_name",
        "format_",
        "expected_success",
        "expected_output",
    ],
    [
        ("tool_1", "json", True, get_details_cmd_json_output()),
        ("tool_1", "cli", True, get_details_cmd_console_output()),
        ("non-existent tool", "json", False, None),
        ("non-existent tool", "cli", False, None),
    ],
)
def test_details_cmd(
    cli_runner: CliRunner,
    tool_name: str,
    format_: str,
    expected_success: bool,
    expected_output: str,
) -> None:
    """Test tool details command."""
    details_cmd.params[0].type = click.Choice(["tool_1", "tool_2", "vela"])
    result = cli_runner.invoke(
        details_cmd, obj={"format": format_}, args=["--name", tool_name]
    )
    success = result.exit_code == 0
    assert success == expected_success, result.output
    if expected_success:
        assert result.exception is None
        output = json.loads(result.output) if format_ == "json" else result.output
        assert output == expected_output


@pytest.mark.parametrize(
    "system_name",
    [
        "",
        "Corstone-300: Cortex-M55+Ethos-U55",
        "Corstone-300: Cortex-M55+Ethos-U65",
        "Corstone-310: Cortex-M85+Ethos-U55",
    ],
)
def test_details_cmd_vela(cli_runner: CliRunner, system_name: str) -> None:
    """Test tool details command for Vela."""
    details_cmd.params[0].type = click.Choice(get_unique_tool_names())
    details_cmd.params[1].type = click.Choice([system_name])
    args = ["--name", "vela"]
    if system_name:
        args += ["--system", system_name]
    result = cli_runner.invoke(details_cmd, obj={"format": "json"}, args=args)
    success = result.exit_code == 0
    assert success, result.output
    result_json = json.loads(result.output)
    assert result_json
    if system_name:
        assert len(result_json) == 1
        tool = result_json[0]
        assert len(tool["supported_systems"]) == 1
        assert system_name == tool["supported_systems"][0]
    else:  # no system specified => list details for all systems
        assert len(result_json) == 3
        assert all(len(tool["supported_systems"]) == 1 for tool in result_json)


@pytest.fixture(scope="session")
def input_model_file(non_optimised_input_model_file: Path) -> Path:
    """Provide the path to a quantized dummy model file in the test_resources_path."""
    return non_optimised_input_model_file


def execute_vela(
    cli_runner: CliRunner,
    tool_name: str = "vela",
    system_name: Optional[str] = None,
    input_model: Optional[Path] = None,
    output_model: Optional[Path] = None,
    mac: Optional[int] = None,
    format_: str = "cli",
) -> Result:
    """Run Vela with different parameters."""
    execute_cmd.params[0].type = click.Choice(get_unique_tool_names())
    execute_cmd.params[2].type = click.Choice([system_name or "dummy_system"])
    args = ["--name", tool_name]
    if system_name is not None:
        args += ["--system", system_name]
    if input_model is not None:
        args += ["--param", "input={}".format(input_model)]
    if output_model is not None:
        args += ["--param", "output={}".format(output_model)]
    if mac is not None:
        args += ["--param", "mac={}".format(mac)]
    result = cli_runner.invoke(
        execute_cmd,
        args=args,
        obj={"format": format_},
    )
    return result


@pytest.mark.parametrize("format_", ["cli, json"])
@pytest.mark.parametrize(
    ["tool_name", "system_name", "mac", "expected_success", "expected_output"],
    [
        ("vela", "System 1", 32, False, None),  # system not supported
        ("vela", "NON-EXISTENT SYSTEM", 128, False, None),  # system does not exist
        ("vela", "Corstone-300: Cortex-M55+Ethos-U55", 32, True, None),
        ("NON-EXISTENT TOOL", "Corstone-300: Cortex-M55+Ethos-U55", 32, False, None),
        ("vela", "Corstone-300: Cortex-M55+Ethos-U55", 64, True, None),
        ("vela", "Corstone-300: Cortex-M55+Ethos-U55", 128, True, None),
        ("vela", "Corstone-300: Cortex-M55+Ethos-U55", 256, True, None),
        (
            "vela",
            "Corstone-300: Cortex-M55+Ethos-U55",
            512,
            False,
            None,
        ),  # mac not supported
        (
            "vela",
            "Corstone-300: Cortex-M55+Ethos-U65",
            32,
            False,
            None,
        ),  # mac not supported
        ("vela", "Corstone-300: Cortex-M55+Ethos-U65", 256, True, None),
        ("vela", "Corstone-300: Cortex-M55+Ethos-U65", 512, True, None),
        (
            "vela",
            None,
            512,
            False,
            "Error: Please specify the system for tool vela.",
        ),  # no system specified
        (
            "NON-EXISTENT TOOL",
            "Corstone-300: Cortex-M55+Ethos-U65",
            512,
            False,
            None,
        ),  # tool does not exist
        ("vela", "Corstone-310: Cortex-M85+Ethos-U55", 128, True, None),
    ],
)
def test_vela_run(
    cli_runner: CliRunner,
    format_: str,
    input_model_file: Path,  # pylint: disable=redefined-outer-name
    tool_name: str,
    system_name: Optional[str],
    mac: int,
    expected_success: bool,
    expected_output: Optional[str],
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
) -> None:
    """Test the execution of the Vela command."""
    monkeypatch.chdir(tmp_path)

    output_file = Path("vela_output.tflite")

    result = execute_vela(
        cli_runner,
        tool_name=tool_name,
        system_name=system_name,
        input_model=input_model_file,
        output_model=output_file,
        mac=mac,
        format_=format_,
    )

    success = result.exit_code == 0
    assert success == expected_success
    if success:
        # Check output file
        output_file = output_file.resolve()
        assert output_file.is_file()
    if expected_output:
        assert result.output.strip() == expected_output


@pytest.mark.parametrize("include_input_model", [True, False])
@pytest.mark.parametrize("include_output_model", [True, False])
@pytest.mark.parametrize("include_mac", [True, False])
def test_vela_run_missing_params(
    cli_runner: CliRunner,
    input_model_file: Path,  # pylint: disable=redefined-outer-name
    include_input_model: bool,
    include_output_model: bool,
    include_mac: bool,
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
) -> None:
    """Test the execution of the Vela command with missing user parameters."""
    monkeypatch.chdir(tmp_path)

    output_model_file = Path("output_model.tflite")
    system_name = "Corstone-300: Cortex-M55+Ethos-U65"
    mac = 256
    # input_model is a required parameters, but mac and output_model have default values.
    expected_success = include_input_model

    result = execute_vela(
        cli_runner,
        tool_name="vela",
        system_name=system_name,
        input_model=input_model_file if include_input_model else None,
        output_model=output_model_file if include_output_model else None,
        mac=mac if include_mac else None,
    )

    success = result.exit_code == 0
    assert success == expected_success, (
        f"Success is {success}, but expected {expected_success}. "
        f"Included params: ["
        f"input_model={include_input_model}, "
        f"output_model={include_output_model}, "
        f"mac={include_mac}]"
    )