1# Copyright (c) 2024 Advanced Micro Devices, Inc.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5import argparse
6from unittest.mock import patch, call
7import pytest
8from runners.xsdb import XSDBBinaryRunner
9from conftest import RC_KERNEL_ELF
10
11TEST_CASES = [
12    {
13        "config": None,
14        "bitstream": None,
15        "fsbl": None,
16        "expected_cmd": ["xsdb", "default_cfg_path"],
17    },
18    {
19        "config": "custom_cfg_path",
20        "bitstream": None,
21        "fsbl": None,
22        "expected_cmd": ["xsdb", "custom_cfg_path"],
23    },
24    {
25        "config": None,
26        "bitstream": "bitstream_path",
27        "fsbl": None,
28        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "bitstream_path"],
29    },
30    {
31        "config": None,
32        "bitstream": None,
33        "fsbl": "fsbl_path",
34        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "fsbl_path"],
35    },
36    {
37        "config": None,
38        "bitstream": "bitstream_path",
39        "fsbl": "fsbl_path",
40        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "bitstream_path", "fsbl_path"],
41    },
42]
43
44
45@pytest.mark.parametrize("tc", TEST_CASES)
46@patch("runners.xsdb.os.path.exists", return_value=True)
47@patch("runners.xsdb.XSDBBinaryRunner.check_call")
48def test_xsdbbinaryrunner_init(check_call, path_exists, tc, runner_config):
49    '''Test actions using a runner created by constructor.'''
50    # Mock the default config path
51    with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"):
52        runner = XSDBBinaryRunner(
53            cfg=runner_config,
54            config=tc["config"],
55            bitstream=tc["bitstream"],
56            fsbl=tc["fsbl"],
57        )
58
59    runner.do_run("flash")
60
61    assert check_call.call_args_list == [call(tc["expected_cmd"])]
62
63
64@pytest.mark.parametrize("tc", TEST_CASES)
65@patch("runners.xsdb.os.path.exists", return_value=True)
66@patch("runners.xsdb.XSDBBinaryRunner.check_call")
67def test_xsdbbinaryrunner_create(check_call, path_exists, tc, runner_config):
68    '''Test actions using a runner created from action line parameters.'''
69    args = []
70    if tc["config"]:
71        args.extend(["--config", tc["config"]])
72    if tc["bitstream"]:
73        args.extend(["--bitstream", tc["bitstream"]])
74    if tc["fsbl"]:
75        args.extend(["--fsbl", tc["fsbl"]])
76
77    parser = argparse.ArgumentParser(allow_abbrev=False)
78    XSDBBinaryRunner.add_parser(parser)
79    arg_namespace = parser.parse_args(args)
80
81    # Mock the default config path
82    with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"):
83        runner = XSDBBinaryRunner.create(runner_config, arg_namespace)
84
85    runner.do_run("flash")
86
87    assert check_call.call_args_list == [call(tc["expected_cmd"])]
88