conftest.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. # Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
  4. #
  5. """
  6. Kconfig unit testing framework.
  7. This provides fixture functions commonly used from test files.
  8. """
  9. import os
  10. import pytest
  11. import shutil
  12. import subprocess
  13. import tempfile
  14. CONF_PATH = os.path.abspath(os.path.join('scripts', 'kconfig', 'conf'))
  15. class Conf:
  16. """Kconfig runner and result checker.
  17. This class provides methods to run text-based interface of Kconfig
  18. (scripts/kconfig/conf) and retrieve the resulted configuration,
  19. stdout, and stderr. It also provides methods to compare those
  20. results with expectations.
  21. """
  22. def __init__(self, request):
  23. """Create a new Conf instance.
  24. request: object to introspect the requesting test module
  25. """
  26. # the directory of the test being run
  27. self._test_dir = os.path.dirname(str(request.fspath))
  28. # runners
  29. def _run_conf(self, mode, dot_config=None, out_file='.config',
  30. interactive=False, in_keys=None, extra_env={}):
  31. """Run text-based Kconfig executable and save the result.
  32. mode: input mode option (--oldaskconfig, --defconfig=<file> etc.)
  33. dot_config: .config file to use for configuration base
  34. out_file: file name to contain the output config data
  35. interactive: flag to specify the interactive mode
  36. in_keys: key inputs for interactive modes
  37. extra_env: additional environments
  38. returncode: exit status of the Kconfig executable
  39. """
  40. command = [CONF_PATH, mode, 'Kconfig']
  41. # Override 'srctree' environment to make the test as the top directory
  42. extra_env['srctree'] = self._test_dir
  43. # Run Kconfig in a temporary directory.
  44. # This directory is automatically removed when done.
  45. with tempfile.TemporaryDirectory() as temp_dir:
  46. # if .config is given, copy it to the working directory
  47. if dot_config:
  48. shutil.copyfile(os.path.join(self._test_dir, dot_config),
  49. os.path.join(temp_dir, '.config'))
  50. ps = subprocess.Popen(command,
  51. stdin=subprocess.PIPE,
  52. stdout=subprocess.PIPE,
  53. stderr=subprocess.PIPE,
  54. cwd=temp_dir,
  55. env=dict(os.environ, **extra_env))
  56. # If input key sequence is given, feed it to stdin.
  57. if in_keys:
  58. ps.stdin.write(in_keys.encode('utf-8'))
  59. while ps.poll() is None:
  60. # For interactive modes such as oldaskconfig, oldconfig,
  61. # send 'Enter' key until the program finishes.
  62. if interactive:
  63. ps.stdin.write(b'\n')
  64. self.retcode = ps.returncode
  65. self.stdout = ps.stdout.read().decode()
  66. self.stderr = ps.stderr.read().decode()
  67. # Retrieve the resulted config data only when .config is supposed
  68. # to exist. If the command fails, the .config does not exist.
  69. # 'listnewconfig' does not produce .config in the first place.
  70. if self.retcode == 0 and out_file:
  71. with open(os.path.join(temp_dir, out_file)) as f:
  72. self.config = f.read()
  73. else:
  74. self.config = None
  75. # Logging:
  76. # Pytest captures the following information by default. In failure
  77. # of tests, the captured log will be displayed. This will be useful to
  78. # figure out what has happened.
  79. print("[command]\n{}\n".format(' '.join(command)))
  80. print("[retcode]\n{}\n".format(self.retcode))
  81. print("[stdout]")
  82. print(self.stdout)
  83. print("[stderr]")
  84. print(self.stderr)
  85. if self.config is not None:
  86. print("[output for '{}']".format(out_file))
  87. print(self.config)
  88. return self.retcode
  89. def oldaskconfig(self, dot_config=None, in_keys=None):
  90. """Run oldaskconfig.
  91. dot_config: .config file to use for configuration base (optional)
  92. in_key: key inputs (optional)
  93. returncode: exit status of the Kconfig executable
  94. """
  95. return self._run_conf('--oldaskconfig', dot_config=dot_config,
  96. interactive=True, in_keys=in_keys)
  97. def oldconfig(self, dot_config=None, in_keys=None):
  98. """Run oldconfig.
  99. dot_config: .config file to use for configuration base (optional)
  100. in_key: key inputs (optional)
  101. returncode: exit status of the Kconfig executable
  102. """
  103. return self._run_conf('--oldconfig', dot_config=dot_config,
  104. interactive=True, in_keys=in_keys)
  105. def olddefconfig(self, dot_config=None):
  106. """Run olddefconfig.
  107. dot_config: .config file to use for configuration base (optional)
  108. returncode: exit status of the Kconfig executable
  109. """
  110. return self._run_conf('--olddefconfig', dot_config=dot_config)
  111. def defconfig(self, defconfig):
  112. """Run defconfig.
  113. defconfig: defconfig file for input
  114. returncode: exit status of the Kconfig executable
  115. """
  116. defconfig_path = os.path.join(self._test_dir, defconfig)
  117. return self._run_conf('--defconfig={}'.format(defconfig_path))
  118. def _allconfig(self, mode, all_config):
  119. if all_config:
  120. all_config_path = os.path.join(self._test_dir, all_config)
  121. extra_env = {'KCONFIG_ALLCONFIG': all_config_path}
  122. else:
  123. extra_env = {}
  124. return self._run_conf('--{}config'.format(mode), extra_env=extra_env)
  125. def allyesconfig(self, all_config=None):
  126. """Run allyesconfig.
  127. all_config: fragment config file for KCONFIG_ALLCONFIG (optional)
  128. returncode: exit status of the Kconfig executable
  129. """
  130. return self._allconfig('allyes', all_config)
  131. def allmodconfig(self, all_config=None):
  132. """Run allmodconfig.
  133. all_config: fragment config file for KCONFIG_ALLCONFIG (optional)
  134. returncode: exit status of the Kconfig executable
  135. """
  136. return self._allconfig('allmod', all_config)
  137. def allnoconfig(self, all_config=None):
  138. """Run allnoconfig.
  139. all_config: fragment config file for KCONFIG_ALLCONFIG (optional)
  140. returncode: exit status of the Kconfig executable
  141. """
  142. return self._allconfig('allno', all_config)
  143. def alldefconfig(self, all_config=None):
  144. """Run alldefconfig.
  145. all_config: fragment config file for KCONFIG_ALLCONFIG (optional)
  146. returncode: exit status of the Kconfig executable
  147. """
  148. return self._allconfig('alldef', all_config)
  149. def randconfig(self, all_config=None):
  150. """Run randconfig.
  151. all_config: fragment config file for KCONFIG_ALLCONFIG (optional)
  152. returncode: exit status of the Kconfig executable
  153. """
  154. return self._allconfig('rand', all_config)
  155. def savedefconfig(self, dot_config):
  156. """Run savedefconfig.
  157. dot_config: .config file for input
  158. returncode: exit status of the Kconfig executable
  159. """
  160. return self._run_conf('--savedefconfig', out_file='defconfig')
  161. def listnewconfig(self, dot_config=None):
  162. """Run listnewconfig.
  163. dot_config: .config file to use for configuration base (optional)
  164. returncode: exit status of the Kconfig executable
  165. """
  166. return self._run_conf('--listnewconfig', dot_config=dot_config,
  167. out_file=None)
  168. # checkers
  169. def _read_and_compare(self, compare, expected):
  170. """Compare the result with expectation.
  171. compare: function to compare the result with expectation
  172. expected: file that contains the expected data
  173. """
  174. with open(os.path.join(self._test_dir, expected)) as f:
  175. expected_data = f.read()
  176. return compare(self, expected_data)
  177. def _contains(self, attr, expected):
  178. return self._read_and_compare(
  179. lambda s, e: getattr(s, attr).find(e) >= 0,
  180. expected)
  181. def _matches(self, attr, expected):
  182. return self._read_and_compare(lambda s, e: getattr(s, attr) == e,
  183. expected)
  184. def config_contains(self, expected):
  185. """Check if resulted configuration contains expected data.
  186. expected: file that contains the expected data
  187. returncode: True if result contains the expected data, False otherwise
  188. """
  189. return self._contains('config', expected)
  190. def config_matches(self, expected):
  191. """Check if resulted configuration exactly matches expected data.
  192. expected: file that contains the expected data
  193. returncode: True if result matches the expected data, False otherwise
  194. """
  195. return self._matches('config', expected)
  196. def stdout_contains(self, expected):
  197. """Check if resulted stdout contains expected data.
  198. expected: file that contains the expected data
  199. returncode: True if result contains the expected data, False otherwise
  200. """
  201. return self._contains('stdout', expected)
  202. def stdout_matches(self, expected):
  203. """Check if resulted stdout exactly matches expected data.
  204. expected: file that contains the expected data
  205. returncode: True if result matches the expected data, False otherwise
  206. """
  207. return self._matches('stdout', expected)
  208. def stderr_contains(self, expected):
  209. """Check if resulted stderr contains expected data.
  210. expected: file that contains the expected data
  211. returncode: True if result contains the expected data, False otherwise
  212. """
  213. return self._contains('stderr', expected)
  214. def stderr_matches(self, expected):
  215. """Check if resulted stderr exactly matches expected data.
  216. expected: file that contains the expected data
  217. returncode: True if result matches the expected data, False otherwise
  218. """
  219. return self._matches('stderr', expected)
  220. @pytest.fixture(scope="module")
  221. def conf(request):
  222. """Create a Conf instance and provide it to test functions."""
  223. return Conf(request)