|
| 1 | +# Copyright (c) 2024 Advanced Micro Devices, Inc. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import argparse |
| 6 | +from unittest.mock import patch, call |
| 7 | +import pytest |
| 8 | +from runners.xsdb import XSDBBinaryRunner |
| 9 | +from conftest import RC_KERNEL_ELF |
| 10 | + |
| 11 | +TEST_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 | +@pytest.mark.parametrize("tc", TEST_CASES) |
| 45 | +@patch("runners.xsdb.os.path.exists", return_value=True) |
| 46 | +@patch("runners.xsdb.XSDBBinaryRunner.check_call") |
| 47 | +def test_xsdbbinaryrunner_init(check_call, path_exists, tc, runner_config): |
| 48 | + '''Test actions using a runner created by constructor.''' |
| 49 | + # Mock the default config path |
| 50 | + with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"): |
| 51 | + runner = XSDBBinaryRunner( |
| 52 | + cfg=runner_config, |
| 53 | + config=tc["config"], |
| 54 | + bitstream=tc["bitstream"], |
| 55 | + fsbl=tc["fsbl"], |
| 56 | + ) |
| 57 | + |
| 58 | + runner.do_run("flash") |
| 59 | + |
| 60 | + assert check_call.call_args_list == [call(tc["expected_cmd"])] |
| 61 | + |
| 62 | +@pytest.mark.parametrize("tc", TEST_CASES) |
| 63 | +@patch("runners.xsdb.os.path.exists", return_value=True) |
| 64 | +@patch("runners.xsdb.XSDBBinaryRunner.check_call") |
| 65 | +def test_xsdbbinaryrunner_create(check_call, path_exists, tc, runner_config): |
| 66 | + '''Test actions using a runner created from action line parameters.''' |
| 67 | + args = [] |
| 68 | + if tc["config"]: |
| 69 | + args.extend(["--config", tc["config"]]) |
| 70 | + if tc["bitstream"]: |
| 71 | + args.extend(["--bitstream", tc["bitstream"]]) |
| 72 | + if tc["fsbl"]: |
| 73 | + args.extend(["--fsbl", tc["fsbl"]]) |
| 74 | + |
| 75 | + parser = argparse.ArgumentParser(allow_abbrev=False) |
| 76 | + XSDBBinaryRunner.add_parser(parser) |
| 77 | + arg_namespace = parser.parse_args(args) |
| 78 | + |
| 79 | + # Mock the default config path |
| 80 | + with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"): |
| 81 | + runner = XSDBBinaryRunner.create(runner_config, arg_namespace) |
| 82 | + |
| 83 | + runner.do_run("flash") |
| 84 | + |
| 85 | + assert check_call.call_args_list == [call(tc["expected_cmd"])] |
0 commit comments