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        "pdi": None,
17        "bl31": None,
18        "dtb": None,
19        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF],
20    },
21    {
22        "config": "custom_cfg_path",
23        "bitstream": None,
24        "fsbl": None,
25        "pdi": None,
26        "bl31": None,
27        "dtb": None,
28        "expected_cmd": ["xsdb", "custom_cfg_path", RC_KERNEL_ELF],
29    },
30    {
31        "config": None,
32        "bitstream": "bitstream_path",
33        "fsbl": None,
34        "pdi": None,
35        "bl31": None,
36        "dtb": None,
37        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "bitstream_path"],
38    },
39    {
40        "config": None,
41        "bitstream": None,
42        "fsbl": "fsbl_path",
43        "pdi": None,
44        "bl31": None,
45        "dtb": None,
46        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "fsbl_path"],
47    },
48    {
49        "config": None,
50        "bitstream": "bitstream_path",
51        "fsbl": "fsbl_path",
52        "pdi": None,
53        "bl31": None,
54        "dtb": None,
55        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "bitstream_path", "fsbl_path"],
56    },
57    {
58        "config": None,
59        "bitstream": None,
60        "fsbl": None,
61        "pdi": "pdi_path",
62        "bl31": None,
63        "dtb": None,
64        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "pdi_path"],
65    },
66    {
67        "config": None,
68        "bitstream": None,
69        "fsbl": None,
70        "pdi": "pdi_path",
71        "bl31": "bl31_path",
72        "dtb": None,
73        "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "pdi_path", "bl31_path"],
74    },
75    {
76        "config": None,
77        "bitstream": None,
78        "fsbl": None,
79        "pdi": "pdi_path",
80        "bl31": "bl31_path",
81        "dtb": "dtb_path",
82        "expected_cmd": [
83            "xsdb",
84            "default_cfg_path",
85            RC_KERNEL_ELF,
86            "pdi_path",
87            "bl31_path",
88            "dtb_path",
89        ],
90    },
91]
92
93
94@pytest.mark.parametrize("tc", TEST_CASES)
95@patch("runners.xsdb.os.path.exists", return_value=True)
96@patch("runners.xsdb.XSDBBinaryRunner.check_call")
97def test_xsdbbinaryrunner_init(check_call, path_exists, tc, runner_config):
98    '''Test actions using a runner created by constructor.'''
99    # Mock the default config path
100    with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"):
101        runner = XSDBBinaryRunner(
102            cfg=runner_config,
103            config=tc["config"],
104            bitstream=tc["bitstream"],
105            fsbl=tc["fsbl"],
106            pdi=tc["pdi"],
107            bl31=tc["bl31"],
108            dtb=tc["dtb"],
109        )
110
111    runner.do_run("flash")
112
113    assert check_call.call_args_list == [call(tc["expected_cmd"])]
114
115
116@pytest.mark.parametrize("tc", TEST_CASES)
117@patch("runners.xsdb.os.path.exists", return_value=True)
118@patch("runners.xsdb.XSDBBinaryRunner.check_call")
119def test_xsdbbinaryrunner_create(check_call, path_exists, tc, runner_config):
120    '''Test actions using a runner created from action line parameters.'''
121    args = []
122    if tc["config"]:
123        args.extend(["--config", tc["config"]])
124    if tc["bitstream"]:
125        args.extend(["--bitstream", tc["bitstream"]])
126    if tc["fsbl"]:
127        args.extend(["--fsbl", tc["fsbl"]])
128    if tc["pdi"]:
129        args.extend(["--pdi", tc["pdi"]])
130    if tc["bl31"]:
131        args.extend(["--bl31", tc["bl31"]])
132    if tc["dtb"]:
133        args.extend(["--system-dtb", tc["dtb"]])
134
135    parser = argparse.ArgumentParser(allow_abbrev=False)
136    XSDBBinaryRunner.add_parser(parser)
137    arg_namespace = parser.parse_args(args)
138
139    # Mock the default config path
140    with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"):
141        runner = XSDBBinaryRunner.create(runner_config, arg_namespace)
142
143    runner.do_run("flash")
144
145    assert check_call.call_args_list == [call(tc["expected_cmd"])]
146