aboutsummaryrefslogtreecommitdiff
path: root/tests/aiet/test_cli_system.py
blob: fd39f316313fff43b6960c5458103aab38b47a14 (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
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Module for testing CLI system subcommand."""
import json
from pathlib import Path
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
from unittest.mock import MagicMock

import click
import pytest
from click.testing import CliRunner

from aiet.backend.config import SystemConfig
from aiet.backend.system import load_system
from aiet.backend.system import System
from aiet.cli.system import details_cmd
from aiet.cli.system import install_cmd
from aiet.cli.system import list_cmd
from aiet.cli.system import remove_cmd
from aiet.cli.system import system_cmd


def test_system_cmd() -> None:
    """Test system commands."""
    commands = ["list", "details", "install", "remove"]
    assert all(command in system_cmd.commands for command in commands)


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

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


@pytest.mark.parametrize(
    "format_,expected_output",
    [
        ("json", '{"type": "system", "available": ["system1", "system2"]}\n'),
        ("cli", "Available systems:\n\nsystem1\nsystem2\n"),
    ],
)
def test_list_cmd_with_format(
    cli_runner: CliRunner, monkeypatch: Any, format_: str, expected_output: str
) -> None:
    """Test available systems command with different formats output."""
    # Mock some systems
    mock_system1 = MagicMock()
    mock_system1.name = "system1"
    mock_system2 = MagicMock()
    mock_system2.name = "system2"

    # Monkey patch the call get_available_systems
    mock_available_systems = MagicMock()
    mock_available_systems.return_value = [mock_system1, mock_system2]
    monkeypatch.setattr("aiet.cli.system.get_available_systems", mock_available_systems)

    obj = {"format": format_}
    result = cli_runner.invoke(list_cmd, obj=obj)
    assert result.output == expected_output


def get_test_system(
    annotations: Optional[Dict[str, Union[str, List[str]]]] = None
) -> System:
    """Return test system details."""
    config = SystemConfig(
        name="system",
        description="test",
        data_transfer={
            "protocol": "ssh",
            "username": "root",
            "password": "root",
            "hostname": "localhost",
            "port": "8022",
        },
        commands={
            "clean": ["clean"],
            "build": ["build"],
            "run": ["run"],
            "post_run": ["post_run"],
        },
        annotations=annotations or {},
    )

    return load_system(config)


def get_details_cmd_json_output(
    annotations: Optional[Dict[str, Union[str, List[str]]]] = None
) -> str:
    """Test JSON output for details command."""
    ann_str = ""
    if annotations is not None:
        ann_str = '"annotations":{},'.format(json.dumps(annotations))

    json_output = (
        """
{
  "type": "system",
  "name": "system",
  "description": "test",
  "data_transfer_protocol": "ssh",
  "commands": {
    "clean":
      {
        "command_strings": ["clean"],
        "user_params": []
      },
    "build":
      {
        "command_strings": ["build"],
        "user_params": []
      },
    "run":
      {
        "command_strings": ["run"],
        "user_params": []
      },
    "post_run":
      {
        "command_strings": ["post_run"],
        "user_params": []
      }
  },
"""
        + ann_str
        + """
  "available_application" : []
  }
"""
    )
    return json.dumps(json.loads(json_output)) + "\n"


def get_details_cmd_console_output(
    annotations: Optional[Dict[str, Union[str, List[str]]]] = None
) -> str:
    """Test console output for details command."""
    ann_str = ""
    if annotations:
        val_str = "".join(
            "\n\t{}: {}".format(ann_name, ann_value)
            for ann_name, ann_value in annotations.items()
        )
        ann_str = "\nAnnotations:{}".format(val_str)
    return (
        'System "system" details'
        + "\nDescription: test"
        + "\nData Transfer Protocol: ssh"
        + "\nAvailable Applications: "
        + ann_str
        + "\n\nclean commands:"
        + "\nCommands: ['clean']"
        + "\n\nbuild commands:"
        + "\nCommands: ['build']"
        + "\n\nrun commands:"
        + "\nCommands: ['run']"
        + "\n\npost_run commands:"
        + "\nCommands: ['post_run']"
        + "\n"
    )


@pytest.mark.parametrize(
    "format_,system,expected_output",
    [
        (
            "json",
            get_test_system(annotations={"ann1": "annotation1", "ann2": ["a1", "a2"]}),
            get_details_cmd_json_output(
                annotations={"ann1": "annotation1", "ann2": ["a1", "a2"]}
            ),
        ),
        (
            "cli",
            get_test_system(annotations={"ann1": "annotation1", "ann2": ["a1", "a2"]}),
            get_details_cmd_console_output(
                annotations={"ann1": "annotation1", "ann2": ["a1", "a2"]}
            ),
        ),
        (
            "json",
            get_test_system(annotations={}),
            get_details_cmd_json_output(annotations={}),
        ),
        (
            "cli",
            get_test_system(annotations={}),
            get_details_cmd_console_output(annotations={}),
        ),
    ],
)
def test_details_cmd(
    cli_runner: CliRunner,
    monkeypatch: Any,
    format_: str,
    system: System,
    expected_output: str,
) -> None:
    """Test details command with different formats output."""
    mock_get_system = MagicMock()
    mock_get_system.return_value = system
    monkeypatch.setattr("aiet.cli.system.get_system", mock_get_system)

    args = ["--name", "system"]
    obj = {"format": format_}
    details_cmd.params[0].type = click.Choice(["system"])

    result = cli_runner.invoke(details_cmd, args=args, obj=obj)
    assert result.output == expected_output


def test_install_cmd(cli_runner: CliRunner, monkeypatch: Any) -> None:
    """Test install system command."""
    mock_install_system = MagicMock()
    monkeypatch.setattr("aiet.cli.system.install_system", mock_install_system)

    args = ["--source", "test"]
    cli_runner.invoke(install_cmd, args=args)
    mock_install_system.assert_called_once_with(Path("test"))


def test_remove_cmd(cli_runner: CliRunner, monkeypatch: Any) -> None:
    """Test remove system command."""
    mock_remove_system = MagicMock()
    monkeypatch.setattr("aiet.cli.system.remove_system", mock_remove_system)
    remove_cmd.params[0].type = click.Choice(["test"])

    args = ["--directory_name", "test"]
    cli_runner.invoke(remove_cmd, args=args)
    mock_remove_system.assert_called_once_with("test")