test-user-mode 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 thread_pool
  8. class Main(common.TestCliFunction):
  9. def __init__(self, *args, **kwargs):
  10. if not 'description' in kwargs:
  11. kwargs['description'] = '''\
  12. https://github.com/cirosantilli/linux-kernel-module-cheat#user-mode-tests
  13. TODO: expose all userland relevant ./run args here as well somehow.
  14. '''
  15. if not 'defaults' in kwargs:
  16. kwargs['defaults'] = {}
  17. super().__init__(*args, is_userland=True, **kwargs)
  18. self.add_argument(
  19. 'tests',
  20. nargs='*',
  21. help='''\
  22. If given, run only the given tests. Otherwise, run all tests.
  23. '''
  24. )
  25. def setup_one(self):
  26. self.env['tests'] = self.resolve_targets(
  27. [self.env['userland_source_dir']],
  28. self.env['tests']
  29. )
  30. def timed_main(self):
  31. run_args = self.get_common_args()
  32. if self.env['emulator'] == 'gem5':
  33. run_args['userland_build_id'] = 'static'
  34. had_failure = False
  35. rootdir_abs_len = len(self.env['root_dir'])
  36. with thread_pool.ThreadPool(
  37. self.run_test,
  38. handle_output=self.handle_output_function,
  39. nthreads=self.env['nproc'],
  40. thread_id_arg='thread_id',
  41. submit_raise_exit=self.env['quit_on_fail'],
  42. ) as my_thread_pool:
  43. for test in self.env['tests']:
  44. for path, in_dirnames, in_filenames in self.sh.walk(test):
  45. path_abs = os.path.abspath(path)
  46. dirpath_relative_root = path_abs[rootdir_abs_len + 1:]
  47. for in_filename in in_filenames:
  48. if os.path.splitext(in_filename)[1] in self.env['build_in_exts']:
  49. path_relative_root = os.path.join(dirpath_relative_root, in_filename)
  50. my_path_properties = path_properties.get(path_relative_root)
  51. if my_path_properties.should_be_tested(self.env, is_userland=True):
  52. cur_run_args = run_args.copy()
  53. cur_run_args.update({
  54. 'userland': os.path.relpath(os.path.join(path_abs, in_filename), os.getcwd()),
  55. })
  56. cur_run_args.update(my_path_properties['test_run_args'])
  57. run_test_args = {
  58. 'expected_exit_status': my_path_properties['exit_status'],
  59. 'run_args': cur_run_args,
  60. 'run_obj': lkmc.import_path.import_path_main('run'),
  61. 'test_id': path_relative_root,
  62. }
  63. signal = my_path_properties['signal_received']
  64. if signal is not None:
  65. # Python subprocess reports signals differently from Bash's 128 + signal rule.
  66. run_test_args['expected_exit_status'] = -signal.value
  67. my_thread_pool.submit(run_test_args)
  68. return self._handle_thread_pool_errors(my_thread_pool)
  69. if __name__ == '__main__':
  70. Main().cli()