test-executables 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import common
  5. import lkmc.import_path
  6. import path_properties
  7. import signal
  8. import thread_pool
  9. class Main(common.TestCliFunction):
  10. def __init__(self, *args, **kwargs):
  11. if not 'description' in kwargs:
  12. kwargs['description'] = '''\
  13. Test userland executables in user mode, or baremetal executables in full system
  14. depending on the value of the --mode option. See also:
  15. * https://cirosantilli.com/linux-kernel-module-cheat#user-mode-tests
  16. * https://cirosantilli.com/linux-kernel-module-cheat#baremetal-tests
  17. * https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
  18. '''
  19. if not 'defaults' in kwargs:
  20. kwargs['defaults'] = {}
  21. if not 'mode' in kwargs['defaults']:
  22. kwargs['defaults']['mode'] = 'userland'
  23. super().__init__(*args, **kwargs)
  24. self.add_argument(
  25. 'tests',
  26. nargs='*',
  27. help='''\
  28. If given, run only the given tests. Otherwise, run all tests.
  29. '''
  30. )
  31. def setup_one(self):
  32. self.env['tests'] = self.resolve_targets(
  33. [
  34. self.env['baremetal_source_dir'],
  35. self.env['userland_source_dir']
  36. ],
  37. self.env['tests']
  38. )
  39. def timed_main(self):
  40. run_args = self.get_common_args()
  41. rootdir_abs_len = len(self.env['root_dir'])
  42. with thread_pool.ThreadPool(
  43. self.run_test,
  44. handle_output=self.handle_output_function,
  45. nthreads=self.env['nproc'],
  46. thread_id_arg='thread_id',
  47. submit_raise_exit=self.env['quit_on_fail'],
  48. ) as my_thread_pool:
  49. for test in self.env['tests']:
  50. for path, in_dirnames, in_filenames in self.sh.walk(test):
  51. path_abs = os.path.abspath(path)
  52. dirpath_relative_root = path_abs[rootdir_abs_len + 1:]
  53. for in_filename in in_filenames:
  54. if os.path.splitext(in_filename)[1] in self.env['build_in_exts']:
  55. path_relative_root = os.path.join(dirpath_relative_root, in_filename)
  56. my_path_properties = path_properties.get(path_relative_root)
  57. if my_path_properties.should_be_tested(self.env):
  58. cur_run_args = run_args.copy()
  59. cur_run_args.update({
  60. self.env['mode']: [os.path.relpath(
  61. os.path.join(path_abs, in_filename),
  62. os.getcwd()
  63. )],
  64. })
  65. cur_run_args.update(my_path_properties['test_run_args'])
  66. if my_path_properties['test_stdin_data'] is not None:
  67. cur_run_args['stdin_file'] = os.path.join(
  68. path_abs,
  69. 'test_data',
  70. my_path_properties['test_stdin_data'] + '.i'
  71. )
  72. run_test_args = {
  73. 'expected_exit_status': my_path_properties['exit_status'],
  74. 'run_args': cur_run_args,
  75. 'run_obj': lkmc.import_path.import_path_main('run'),
  76. 'test_id': '{} {}'.format(self.env['mode'], path_relative_root),
  77. }
  78. if (my_path_properties['qemu_unimplemented_instruction'] and
  79. self.env['emulator'] == 'qemu' and
  80. self.env['mode'] == 'userland'
  81. ):
  82. run_test_args['expected_exit_status'] = -signal.Signals.SIGILL.value
  83. my_signal = my_path_properties['signal_received']
  84. if my_signal is not None:
  85. if self.env['mode'] == 'baremetal':
  86. run_test_args['expected_exit_status'] = 128 + my_signal.value
  87. elif self.env['mode'] == 'userland':
  88. # Python subprocess reports signals differently from Bash's 128 + signal rule.
  89. run_test_args['expected_exit_status'] = -my_signal.value
  90. my_thread_pool.submit(run_test_args)
  91. return self._handle_thread_pool_errors(my_thread_pool)
  92. if __name__ == '__main__':
  93. Main().cli()