test_mdb.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # Copyright (c) 2020 Synopsys
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. import argparse
  5. from os import path
  6. from unittest.mock import patch
  7. from unittest.mock import call
  8. import pytest
  9. from runners.mdb import MdbNsimBinaryRunner, MdbHwBinaryRunner
  10. from conftest import RC_KERNEL_ELF, RC_BOARD_DIR
  11. TEST_DRIVER_CMD = 'mdb'
  12. TEST_NSIM_ARGS='test_nsim.args'
  13. TEST_TARGET = 'test-target'
  14. TEST_BOARD_NSIM_ARGS = '@' + path.join(RC_BOARD_DIR, 'support', TEST_NSIM_ARGS)
  15. # mdb-nsim
  16. TEST_NSIM_FLASH_CASES = [
  17. {
  18. 'i': ['--cores=1', '--nsim=' + TEST_NSIM_ARGS],
  19. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  20. '-toggle=include_local_symbols=1',
  21. '-nsim', TEST_BOARD_NSIM_ARGS,
  22. '-run', '-cl', RC_KERNEL_ELF]
  23. }]
  24. TEST_NSIM_DEBUG_CASES = [
  25. {
  26. 'i': ['--cores=1', '--nsim=' + TEST_NSIM_ARGS],
  27. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  28. '-toggle=include_local_symbols=1',
  29. '-nsim', TEST_BOARD_NSIM_ARGS,
  30. '-OKN', RC_KERNEL_ELF
  31. ]
  32. }]
  33. TEST_NSIM_MULTICORE_CASES = [['--cores=2', '--nsim=' + TEST_NSIM_ARGS]]
  34. TEST_NSIM_CORE1 = [TEST_DRIVER_CMD, '-pset=1', '-psetname=core0', '',
  35. '-nooptions', '-nogoifmain', '-toggle=include_local_symbols=1',
  36. '-nsim', TEST_BOARD_NSIM_ARGS, RC_KERNEL_ELF]
  37. TEST_NSIM_CORE2 = [TEST_DRIVER_CMD, '-pset=2', '-psetname=core1',
  38. '-prop=download=2', '-nooptions', '-nogoifmain',
  39. '-toggle=include_local_symbols=1',
  40. '-nsim', TEST_BOARD_NSIM_ARGS, RC_KERNEL_ELF]
  41. TEST_NSIM_CORES_LAUNCH = [TEST_DRIVER_CMD, '-multifiles=core1,core0',
  42. '-run', '-cl']
  43. # mdb-hw
  44. TEST_HW_FLASH_CASES = [
  45. {
  46. 'i': ['--jtag=digilent', '--cores=1'],
  47. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  48. '-toggle=include_local_symbols=1',
  49. '-digilent', '',
  50. '-run', '-cmd=-nowaitq run', '-cmd=quit', '-cl', RC_KERNEL_ELF]
  51. }, {
  52. 'i': ['--jtag=digilent', '--cores=1', '--dig-device=test'],
  53. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  54. '-toggle=include_local_symbols=1',
  55. '-digilent', '-prop=dig_device=test',
  56. '-run', '-cmd=-nowaitq run', '-cmd=quit', '-cl', RC_KERNEL_ELF]
  57. }, {
  58. 'i': ['--jtag=test_debug', '--cores=1'],
  59. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  60. '-toggle=include_local_symbols=1',
  61. '',
  62. '-run', '-cmd=-nowaitq run', '-cmd=quit', '-cl', RC_KERNEL_ELF]
  63. }]
  64. TEST_HW_DEBUG_CASES = [
  65. {
  66. 'i': ['--jtag=digilent', '--cores=1'],
  67. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  68. '-toggle=include_local_symbols=1',
  69. '-digilent', '',
  70. '-OKN', RC_KERNEL_ELF]
  71. }, {
  72. 'i': ['--jtag=digilent', '--cores=1', '--dig-device=test'],
  73. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  74. '-toggle=include_local_symbols=1',
  75. '-digilent', '-prop=dig_device=test',
  76. '-OKN', RC_KERNEL_ELF]
  77. }, {
  78. 'i': ['--jtag=test_debug', '--cores=1'],
  79. 'o': [TEST_DRIVER_CMD, '-nooptions', '-nogoifmain',
  80. '-toggle=include_local_symbols=1',
  81. '',
  82. '-OKN', RC_KERNEL_ELF]
  83. }]
  84. TEST_HW_MULTICORE_CASES = [['--jtag=digilent', '--cores=2']]
  85. TEST_HW_CORE1 = [TEST_DRIVER_CMD, '-pset=1', '-psetname=core0', '',
  86. '-nooptions', '-nogoifmain', '-toggle=include_local_symbols=1',
  87. '-digilent', '', RC_KERNEL_ELF]
  88. TEST_HW_CORE2 = [TEST_DRIVER_CMD, '-pset=2', '-psetname=core1',
  89. '-prop=download=2', '-nooptions', '-nogoifmain',
  90. '-toggle=include_local_symbols=1',
  91. '-digilent', '', RC_KERNEL_ELF]
  92. TEST_HW_CORES_LAUNCH = [TEST_DRIVER_CMD, '-multifiles=core1,core0', '-run',
  93. '-cmd=-nowaitq run', '-cmd=quit', '-cl']
  94. #
  95. # Fixtures
  96. #
  97. def mdb(runner_config, tmpdir, mdb_runner):
  98. '''MdbBinaryRunner from constructor kwargs or command line parameters'''
  99. # This factory takes either a dict of kwargs to pass to the
  100. # constructor, or a list of command-line arguments to parse and
  101. # use with the create() method.
  102. def _factory(args):
  103. # Ensure kernel binaries exist (as empty files, so commands
  104. # which use them must be patched out).
  105. tmpdir.ensure(RC_KERNEL_ELF)
  106. tmpdir.chdir()
  107. if isinstance(args, dict):
  108. return mdb_runner(runner_config, TEST_TARGET, **args)
  109. elif isinstance(args, list):
  110. parser = argparse.ArgumentParser()
  111. mdb_runner.add_parser(parser)
  112. arg_namespace = parser.parse_args(args)
  113. return mdb_runner.create(runner_config, arg_namespace)
  114. return _factory
  115. @pytest.fixture
  116. def mdb_nsim(runner_config, tmpdir):
  117. return mdb(runner_config, tmpdir, MdbNsimBinaryRunner)
  118. @pytest.fixture
  119. def mdb_hw(runner_config, tmpdir):
  120. return mdb(runner_config, tmpdir, MdbHwBinaryRunner)
  121. #
  122. # Helpers
  123. #
  124. def require_patch(program):
  125. assert program == TEST_DRIVER_CMD
  126. #
  127. # Test cases for runners created by constructor.
  128. #
  129. # mdb-nsim test cases
  130. @pytest.mark.parametrize('test_case', TEST_NSIM_FLASH_CASES)
  131. @patch('runners.mdb.get_cld_pid', return_value=(False, -1))
  132. @patch('time.sleep', return_value=None)
  133. @patch('runners.mdb.MdbNsimBinaryRunner.popen_ignore_int')
  134. @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
  135. def test_flash_nsim(require, cc, t, gcp, test_case, mdb_nsim):
  136. mdb_nsim(test_case['i']).run('flash')
  137. assert require.called
  138. cc.assert_called_once_with(test_case['o'])
  139. @pytest.mark.parametrize('test_case', TEST_NSIM_DEBUG_CASES)
  140. @patch('runners.mdb.get_cld_pid', return_value=(False, -1))
  141. @patch('time.sleep', return_value=None)
  142. @patch('runners.mdb.MdbNsimBinaryRunner.popen_ignore_int')
  143. @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
  144. def test_debug_nsim(require, pii, t, gcp, test_case, mdb_nsim):
  145. mdb_nsim(test_case['i']).run('debug')
  146. assert require.called
  147. pii.assert_called_once_with(test_case['o'])
  148. @pytest.mark.parametrize('test_case', TEST_NSIM_MULTICORE_CASES)
  149. @patch('runners.mdb.get_cld_pid', return_value=(False, -1))
  150. @patch('time.sleep', return_value=None)
  151. @patch('runners.mdb.MdbNsimBinaryRunner.check_call')
  152. @patch('runners.mdb.MdbNsimBinaryRunner.popen_ignore_int')
  153. @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
  154. def test_multicores_nsim(require, pii, cc, t, gcp, test_case, mdb_nsim):
  155. mdb_nsim(test_case).run('flash')
  156. assert require.called
  157. cc_calls = [call(TEST_NSIM_CORE1), call(TEST_NSIM_CORE2)]
  158. cc.assert_has_calls(cc_calls)
  159. pii.assert_called_once_with(TEST_NSIM_CORES_LAUNCH)
  160. # mdb-hw test cases
  161. @pytest.mark.parametrize('test_case', TEST_HW_FLASH_CASES)
  162. @patch('runners.mdb.get_cld_pid', return_value=(False, -1))
  163. @patch('time.sleep', return_value=None)
  164. @patch('runners.mdb.MdbHwBinaryRunner.popen_ignore_int')
  165. @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
  166. def test_flash_hw(require, cc, t, gcp, test_case, mdb_hw):
  167. mdb_hw(test_case['i']).run('flash')
  168. assert require.called
  169. cc.assert_called_once_with(test_case['o'])
  170. @pytest.mark.parametrize('test_case', TEST_HW_DEBUG_CASES)
  171. @patch('runners.mdb.get_cld_pid', return_value=(False, -1))
  172. @patch('time.sleep', return_value=None)
  173. @patch('runners.mdb.MdbHwBinaryRunner.popen_ignore_int')
  174. @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
  175. def test_debug_hw(require, pii, t, gcp, test_case, mdb_hw):
  176. mdb_hw(test_case['i']).run('debug')
  177. assert require.called
  178. pii.assert_called_once_with(test_case['o'])
  179. @pytest.mark.parametrize('test_case', TEST_HW_MULTICORE_CASES)
  180. @patch('runners.mdb.get_cld_pid', return_value=(False, -1))
  181. @patch('time.sleep', return_value=None)
  182. @patch('runners.mdb.MdbHwBinaryRunner.check_call')
  183. @patch('runners.mdb.MdbHwBinaryRunner.popen_ignore_int')
  184. @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
  185. def test_multicores_hw(require, pii, cc, t, gcp, test_case, mdb_hw):
  186. mdb_hw(test_case).run('flash')
  187. assert require.called
  188. cc_calls = [call(TEST_HW_CORE1), call(TEST_HW_CORE2)]
  189. cc.assert_has_calls(cc_calls)
  190. pii.assert_called_once_with(TEST_HW_CORES_LAUNCH)