PageRenderTime 69ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/testing/kunit/kunit_kernel.py

https://github.com/kvaneesh/linux
Python | 342 lines | 304 code | 27 blank | 11 comment | 24 complexity | 5a2e024d4c2db5711c04bee8d122b57f MD5 | raw file
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. # Runs UML kernel, collects output, and handles errors.
  4. #
  5. # Copyright (C) 2019, Google LLC.
  6. # Author: Felix Guo <felixguoxiuping@gmail.com>
  7. # Author: Brendan Higgins <brendanhiggins@google.com>
  8. import importlib.util
  9. import logging
  10. import subprocess
  11. import os
  12. import shutil
  13. import signal
  14. from typing import Iterator, Optional, Tuple
  15. from contextlib import ExitStack
  16. from collections import namedtuple
  17. import kunit_config
  18. import kunit_parser
  19. import qemu_config
  20. KCONFIG_PATH = '.config'
  21. KUNITCONFIG_PATH = '.kunitconfig'
  22. DEFAULT_KUNITCONFIG_PATH = 'tools/testing/kunit/configs/default.config'
  23. BROKEN_ALLCONFIG_PATH = 'tools/testing/kunit/configs/broken_on_uml.config'
  24. OUTFILE_PATH = 'test.log'
  25. ABS_TOOL_PATH = os.path.abspath(os.path.dirname(__file__))
  26. QEMU_CONFIGS_DIR = os.path.join(ABS_TOOL_PATH, 'qemu_configs')
  27. def get_file_path(build_dir, default):
  28. if build_dir:
  29. default = os.path.join(build_dir, default)
  30. return default
  31. class ConfigError(Exception):
  32. """Represents an error trying to configure the Linux kernel."""
  33. class BuildError(Exception):
  34. """Represents an error trying to build the Linux kernel."""
  35. class LinuxSourceTreeOperations(object):
  36. """An abstraction over command line operations performed on a source tree."""
  37. def __init__(self, linux_arch: str, cross_compile: Optional[str]):
  38. self._linux_arch = linux_arch
  39. self._cross_compile = cross_compile
  40. def make_mrproper(self) -> None:
  41. try:
  42. subprocess.check_output(['make', 'mrproper'], stderr=subprocess.STDOUT)
  43. except OSError as e:
  44. raise ConfigError('Could not call make command: ' + str(e))
  45. except subprocess.CalledProcessError as e:
  46. raise ConfigError(e.output.decode())
  47. def make_arch_qemuconfig(self, kconfig: kunit_config.Kconfig) -> None:
  48. pass
  49. def make_allyesconfig(self, build_dir, make_options) -> None:
  50. raise ConfigError('Only the "um" arch is supported for alltests')
  51. def make_olddefconfig(self, build_dir, make_options) -> None:
  52. command = ['make', 'ARCH=' + self._linux_arch, 'olddefconfig']
  53. if self._cross_compile:
  54. command += ['CROSS_COMPILE=' + self._cross_compile]
  55. if make_options:
  56. command.extend(make_options)
  57. if build_dir:
  58. command += ['O=' + build_dir]
  59. print('Populating config with:\n$', ' '.join(command))
  60. try:
  61. subprocess.check_output(command, stderr=subprocess.STDOUT)
  62. except OSError as e:
  63. raise ConfigError('Could not call make command: ' + str(e))
  64. except subprocess.CalledProcessError as e:
  65. raise ConfigError(e.output.decode())
  66. def make(self, jobs, build_dir, make_options) -> None:
  67. command = ['make', 'ARCH=' + self._linux_arch, '--jobs=' + str(jobs)]
  68. if make_options:
  69. command.extend(make_options)
  70. if self._cross_compile:
  71. command += ['CROSS_COMPILE=' + self._cross_compile]
  72. if build_dir:
  73. command += ['O=' + build_dir]
  74. print('Building with:\n$', ' '.join(command))
  75. try:
  76. proc = subprocess.Popen(command,
  77. stderr=subprocess.PIPE,
  78. stdout=subprocess.DEVNULL)
  79. except OSError as e:
  80. raise BuildError('Could not call execute make: ' + str(e))
  81. except subprocess.CalledProcessError as e:
  82. raise BuildError(e.output)
  83. _, stderr = proc.communicate()
  84. if proc.returncode != 0:
  85. raise BuildError(stderr.decode())
  86. if stderr: # likely only due to build warnings
  87. print(stderr.decode())
  88. def run(self, params, timeout, build_dir, outfile) -> None:
  89. pass
  90. class LinuxSourceTreeOperationsQemu(LinuxSourceTreeOperations):
  91. def __init__(self, qemu_arch_params: qemu_config.QemuArchParams, cross_compile: Optional[str]):
  92. super().__init__(linux_arch=qemu_arch_params.linux_arch,
  93. cross_compile=cross_compile)
  94. self._kconfig = qemu_arch_params.kconfig
  95. self._qemu_arch = qemu_arch_params.qemu_arch
  96. self._kernel_path = qemu_arch_params.kernel_path
  97. self._kernel_command_line = qemu_arch_params.kernel_command_line + ' kunit_shutdown=reboot'
  98. self._extra_qemu_params = qemu_arch_params.extra_qemu_params
  99. def make_arch_qemuconfig(self, base_kunitconfig: kunit_config.Kconfig) -> None:
  100. kconfig = kunit_config.Kconfig()
  101. kconfig.parse_from_string(self._kconfig)
  102. base_kunitconfig.merge_in_entries(kconfig)
  103. def run(self, params, timeout, build_dir, outfile):
  104. kernel_path = os.path.join(build_dir, self._kernel_path)
  105. qemu_command = ['qemu-system-' + self._qemu_arch,
  106. '-nodefaults',
  107. '-m', '1024',
  108. '-kernel', kernel_path,
  109. '-append', '\'' + ' '.join(params + [self._kernel_command_line]) + '\'',
  110. '-no-reboot',
  111. '-nographic',
  112. '-serial stdio'] + self._extra_qemu_params
  113. print('Running tests with:\n$', ' '.join(qemu_command))
  114. with open(outfile, 'w') as output:
  115. process = subprocess.Popen(' '.join(qemu_command),
  116. stdin=subprocess.PIPE,
  117. stdout=output,
  118. stderr=subprocess.STDOUT,
  119. text=True, shell=True)
  120. try:
  121. process.wait(timeout=timeout)
  122. except Exception as e:
  123. print(e)
  124. process.terminate()
  125. return process
  126. class LinuxSourceTreeOperationsUml(LinuxSourceTreeOperations):
  127. """An abstraction over command line operations performed on a source tree."""
  128. def __init__(self, cross_compile=None):
  129. super().__init__(linux_arch='um', cross_compile=cross_compile)
  130. def make_allyesconfig(self, build_dir, make_options) -> None:
  131. kunit_parser.print_with_timestamp(
  132. 'Enabling all CONFIGs for UML...')
  133. command = ['make', 'ARCH=um', 'allyesconfig']
  134. if make_options:
  135. command.extend(make_options)
  136. if build_dir:
  137. command += ['O=' + build_dir]
  138. process = subprocess.Popen(
  139. command,
  140. stdout=subprocess.DEVNULL,
  141. stderr=subprocess.STDOUT)
  142. process.wait()
  143. kunit_parser.print_with_timestamp(
  144. 'Disabling broken configs to run KUnit tests...')
  145. with ExitStack() as es:
  146. config = open(get_kconfig_path(build_dir), 'a')
  147. disable = open(BROKEN_ALLCONFIG_PATH, 'r').read()
  148. config.write(disable)
  149. kunit_parser.print_with_timestamp(
  150. 'Starting Kernel with all configs takes a few minutes...')
  151. def run(self, params, timeout, build_dir, outfile):
  152. """Runs the Linux UML binary. Must be named 'linux'."""
  153. linux_bin = get_file_path(build_dir, 'linux')
  154. outfile = get_outfile_path(build_dir)
  155. with open(outfile, 'w') as output:
  156. process = subprocess.Popen([linux_bin] + params,
  157. stdin=subprocess.PIPE,
  158. stdout=output,
  159. stderr=subprocess.STDOUT,
  160. text=True)
  161. process.wait(timeout)
  162. def get_kconfig_path(build_dir) -> str:
  163. return get_file_path(build_dir, KCONFIG_PATH)
  164. def get_kunitconfig_path(build_dir) -> str:
  165. return get_file_path(build_dir, KUNITCONFIG_PATH)
  166. def get_outfile_path(build_dir) -> str:
  167. return get_file_path(build_dir, OUTFILE_PATH)
  168. def get_source_tree_ops(arch: str, cross_compile: Optional[str]) -> LinuxSourceTreeOperations:
  169. config_path = os.path.join(QEMU_CONFIGS_DIR, arch + '.py')
  170. if arch == 'um':
  171. return LinuxSourceTreeOperationsUml(cross_compile=cross_compile)
  172. elif os.path.isfile(config_path):
  173. return get_source_tree_ops_from_qemu_config(config_path, cross_compile)[1]
  174. else:
  175. raise ConfigError(arch + ' is not a valid arch')
  176. def get_source_tree_ops_from_qemu_config(config_path: str,
  177. cross_compile: Optional[str]) -> Tuple[
  178. str, LinuxSourceTreeOperations]:
  179. # The module name/path has very little to do with where the actual file
  180. # exists (I learned this through experimentation and could not find it
  181. # anywhere in the Python documentation).
  182. #
  183. # Bascially, we completely ignore the actual file location of the config
  184. # we are loading and just tell Python that the module lives in the
  185. # QEMU_CONFIGS_DIR for import purposes regardless of where it actually
  186. # exists as a file.
  187. module_path = '.' + os.path.join(os.path.basename(QEMU_CONFIGS_DIR), os.path.basename(config_path))
  188. spec = importlib.util.spec_from_file_location(module_path, config_path)
  189. config = importlib.util.module_from_spec(spec)
  190. # TODO(brendanhiggins@google.com): I looked this up and apparently other
  191. # Python projects have noted that pytype complains that "No attribute
  192. # 'exec_module' on _importlib_modulespec._Loader". Disabling for now.
  193. spec.loader.exec_module(config) # pytype: disable=attribute-error
  194. return config.QEMU_ARCH.linux_arch, LinuxSourceTreeOperationsQemu(
  195. config.QEMU_ARCH, cross_compile=cross_compile)
  196. class LinuxSourceTree(object):
  197. """Represents a Linux kernel source tree with KUnit tests."""
  198. def __init__(
  199. self,
  200. build_dir: str,
  201. load_config=True,
  202. kunitconfig_path='',
  203. arch=None,
  204. cross_compile=None,
  205. qemu_config_path=None) -> None:
  206. signal.signal(signal.SIGINT, self.signal_handler)
  207. if qemu_config_path:
  208. self._arch, self._ops = get_source_tree_ops_from_qemu_config(
  209. qemu_config_path, cross_compile)
  210. else:
  211. self._arch = 'um' if arch is None else arch
  212. self._ops = get_source_tree_ops(self._arch, cross_compile)
  213. if not load_config:
  214. return
  215. if kunitconfig_path:
  216. if os.path.isdir(kunitconfig_path):
  217. kunitconfig_path = os.path.join(kunitconfig_path, KUNITCONFIG_PATH)
  218. if not os.path.exists(kunitconfig_path):
  219. raise ConfigError(f'Specified kunitconfig ({kunitconfig_path}) does not exist')
  220. else:
  221. kunitconfig_path = get_kunitconfig_path(build_dir)
  222. if not os.path.exists(kunitconfig_path):
  223. shutil.copyfile(DEFAULT_KUNITCONFIG_PATH, kunitconfig_path)
  224. self._kconfig = kunit_config.Kconfig()
  225. self._kconfig.read_from_file(kunitconfig_path)
  226. def clean(self) -> bool:
  227. try:
  228. self._ops.make_mrproper()
  229. except ConfigError as e:
  230. logging.error(e)
  231. return False
  232. return True
  233. def validate_config(self, build_dir) -> bool:
  234. kconfig_path = get_kconfig_path(build_dir)
  235. validated_kconfig = kunit_config.Kconfig()
  236. validated_kconfig.read_from_file(kconfig_path)
  237. if not self._kconfig.is_subset_of(validated_kconfig):
  238. invalid = self._kconfig.entries() - validated_kconfig.entries()
  239. message = 'Provided Kconfig is not contained in validated .config. Following fields found in kunitconfig, ' \
  240. 'but not in .config: %s' % (
  241. ', '.join([str(e) for e in invalid])
  242. )
  243. logging.error(message)
  244. return False
  245. return True
  246. def build_config(self, build_dir, make_options) -> bool:
  247. kconfig_path = get_kconfig_path(build_dir)
  248. if build_dir and not os.path.exists(build_dir):
  249. os.mkdir(build_dir)
  250. try:
  251. self._ops.make_arch_qemuconfig(self._kconfig)
  252. self._kconfig.write_to_file(kconfig_path)
  253. self._ops.make_olddefconfig(build_dir, make_options)
  254. except ConfigError as e:
  255. logging.error(e)
  256. return False
  257. return self.validate_config(build_dir)
  258. def build_reconfig(self, build_dir, make_options) -> bool:
  259. """Creates a new .config if it is not a subset of the .kunitconfig."""
  260. kconfig_path = get_kconfig_path(build_dir)
  261. if os.path.exists(kconfig_path):
  262. existing_kconfig = kunit_config.Kconfig()
  263. existing_kconfig.read_from_file(kconfig_path)
  264. self._ops.make_arch_qemuconfig(self._kconfig)
  265. if not self._kconfig.is_subset_of(existing_kconfig):
  266. print('Regenerating .config ...')
  267. os.remove(kconfig_path)
  268. return self.build_config(build_dir, make_options)
  269. else:
  270. return True
  271. else:
  272. print('Generating .config ...')
  273. return self.build_config(build_dir, make_options)
  274. def build_kernel(self, alltests, jobs, build_dir, make_options) -> bool:
  275. try:
  276. if alltests:
  277. self._ops.make_allyesconfig(build_dir, make_options)
  278. self._ops.make_olddefconfig(build_dir, make_options)
  279. self._ops.make(jobs, build_dir, make_options)
  280. except (ConfigError, BuildError) as e:
  281. logging.error(e)
  282. return False
  283. return self.validate_config(build_dir)
  284. def run_kernel(self, args=None, build_dir='', filter_glob='', timeout=None) -> Iterator[str]:
  285. if not args:
  286. args = []
  287. args.extend(['mem=1G', 'console=tty', 'kunit_shutdown=halt'])
  288. if filter_glob:
  289. args.append('kunit.filter_glob='+filter_glob)
  290. outfile = get_outfile_path(build_dir)
  291. self._ops.run(args, timeout, build_dir, outfile)
  292. subprocess.call(['stty', 'sane'])
  293. with open(outfile, 'r') as file:
  294. for line in file:
  295. yield line
  296. def signal_handler(self, sig, frame) -> None:
  297. logging.error('Build interruption occurred. Cleaning console.')
  298. subprocess.call(['stty', 'sane'])