testrunner.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # Class for actually running tests.
  2. #
  3. # Copyright (c) 2020-2021 Virtuozzo International GmbH
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. import os
  19. from pathlib import Path
  20. import datetime
  21. import time
  22. import difflib
  23. import subprocess
  24. import contextlib
  25. import json
  26. import termios
  27. import shutil
  28. import sys
  29. from multiprocessing import Pool
  30. from contextlib import contextmanager
  31. from typing import List, Optional, Iterator, Any, Sequence, Dict, \
  32. ContextManager
  33. from testenv import TestEnv
  34. def silent_unlink(path: Path) -> None:
  35. try:
  36. path.unlink()
  37. except OSError:
  38. pass
  39. def file_diff(file1: str, file2: str) -> List[str]:
  40. with open(file1, encoding="utf-8") as f1, \
  41. open(file2, encoding="utf-8") as f2:
  42. # We want to ignore spaces at line ends. There are a lot of mess about
  43. # it in iotests.
  44. # TODO: fix all tests to not produce extra spaces, fix all .out files
  45. # and use strict diff here!
  46. seq1 = [line.rstrip() for line in f1]
  47. seq2 = [line.rstrip() for line in f2]
  48. res = [line.rstrip()
  49. for line in difflib.unified_diff(seq1, seq2, file1, file2)]
  50. return res
  51. # We want to save current tty settings during test run,
  52. # since an aborting qemu call may leave things screwed up.
  53. @contextmanager
  54. def savetty() -> Iterator[None]:
  55. isterm = sys.stdin.isatty()
  56. if isterm:
  57. fd = sys.stdin.fileno()
  58. attr = termios.tcgetattr(fd)
  59. try:
  60. yield
  61. finally:
  62. if isterm:
  63. termios.tcsetattr(fd, termios.TCSADRAIN, attr)
  64. class LastElapsedTime(ContextManager['LastElapsedTime']):
  65. """ Cache for elapsed time for tests, to show it during new test run
  66. It is safe to use get() at any time. To use update(), you must either
  67. use it inside with-block or use save() after update().
  68. """
  69. def __init__(self, cache_file: str, env: TestEnv) -> None:
  70. self.env = env
  71. self.cache_file = cache_file
  72. self.cache: Dict[str, Dict[str, Dict[str, float]]]
  73. try:
  74. with open(cache_file, encoding="utf-8") as f:
  75. self.cache = json.load(f)
  76. except (OSError, ValueError):
  77. self.cache = {}
  78. def get(self, test: str,
  79. default: Optional[float] = None) -> Optional[float]:
  80. if test not in self.cache:
  81. return default
  82. if self.env.imgproto not in self.cache[test]:
  83. return default
  84. return self.cache[test][self.env.imgproto].get(self.env.imgfmt,
  85. default)
  86. def update(self, test: str, elapsed: float) -> None:
  87. d = self.cache.setdefault(test, {})
  88. d.setdefault(self.env.imgproto, {})[self.env.imgfmt] = elapsed
  89. def save(self) -> None:
  90. with open(self.cache_file, 'w', encoding="utf-8") as f:
  91. json.dump(self.cache, f)
  92. def __enter__(self) -> 'LastElapsedTime':
  93. return self
  94. def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
  95. self.save()
  96. class TestResult:
  97. def __init__(self, status: str, description: str = '',
  98. elapsed: Optional[float] = None, diff: Sequence[str] = (),
  99. casenotrun: str = '', interrupted: bool = False) -> None:
  100. self.status = status
  101. self.description = description
  102. self.elapsed = elapsed
  103. self.diff = diff
  104. self.casenotrun = casenotrun
  105. self.interrupted = interrupted
  106. class TestRunner(ContextManager['TestRunner']):
  107. shared_self = None
  108. @staticmethod
  109. def proc_run_test(test: str, test_field_width: int) -> TestResult:
  110. # We are in a subprocess, we can't change the runner object!
  111. runner = TestRunner.shared_self
  112. assert runner is not None
  113. return runner.run_test(test, test_field_width, mp=True)
  114. def run_tests_pool(self, tests: List[str],
  115. test_field_width: int, jobs: int) -> List[TestResult]:
  116. # passing self directly to Pool.starmap() just doesn't work, because
  117. # it's a context manager.
  118. assert TestRunner.shared_self is None
  119. TestRunner.shared_self = self
  120. with Pool(jobs) as p:
  121. results = p.starmap(self.proc_run_test,
  122. zip(tests, [test_field_width] * len(tests)))
  123. TestRunner.shared_self = None
  124. return results
  125. def __init__(self, env: TestEnv, tap: bool = False,
  126. color: str = 'auto') -> None:
  127. self.env = env
  128. self.tap = tap
  129. self.last_elapsed = LastElapsedTime('.last-elapsed-cache', env)
  130. assert color in ('auto', 'on', 'off')
  131. self.color = (color == 'on') or (color == 'auto' and
  132. sys.stdout.isatty())
  133. self._stack: contextlib.ExitStack
  134. def __enter__(self) -> 'TestRunner':
  135. self._stack = contextlib.ExitStack()
  136. self._stack.enter_context(self.env)
  137. self._stack.enter_context(self.last_elapsed)
  138. self._stack.enter_context(savetty())
  139. return self
  140. def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
  141. self._stack.close()
  142. def test_print_one_line(self, test: str,
  143. test_field_width: int,
  144. starttime: str,
  145. endtime: Optional[str] = None, status: str = '...',
  146. lasttime: Optional[float] = None,
  147. thistime: Optional[float] = None,
  148. description: str = '',
  149. end: str = '\n') -> None:
  150. """ Print short test info before/after test run """
  151. test = os.path.basename(test)
  152. if test_field_width is None:
  153. test_field_width = 8
  154. if self.tap:
  155. if status == 'pass':
  156. print(f'ok {self.env.imgfmt} {test}')
  157. elif status == 'fail':
  158. print(f'not ok {self.env.imgfmt} {test}')
  159. elif status == 'not run':
  160. print(f'ok {self.env.imgfmt} {test} # SKIP')
  161. return
  162. if lasttime:
  163. lasttime_s = f' (last: {lasttime:.1f}s)'
  164. else:
  165. lasttime_s = ''
  166. if thistime:
  167. thistime_s = f'{thistime:.1f}s'
  168. else:
  169. thistime_s = '...'
  170. if endtime:
  171. endtime = f'[{endtime}]'
  172. else:
  173. endtime = ''
  174. if self.color:
  175. if status == 'pass':
  176. col = '\033[32m'
  177. elif status == 'fail':
  178. col = '\033[1m\033[31m'
  179. elif status == 'not run':
  180. col = '\033[33m'
  181. else:
  182. col = ''
  183. col_end = '\033[0m'
  184. else:
  185. col = ''
  186. col_end = ''
  187. print(f'{test:{test_field_width}} {col}{status:10}{col_end} '
  188. f'[{starttime}] {endtime:13}{thistime_s:5} {lasttime_s:14} '
  189. f'{description}', end=end)
  190. def find_reference(self, test: str) -> str:
  191. if self.env.cachemode == 'none':
  192. ref = f'{test}.out.nocache'
  193. if os.path.isfile(ref):
  194. return ref
  195. ref = f'{test}.out.{self.env.imgfmt}'
  196. if os.path.isfile(ref):
  197. return ref
  198. ref = f'{test}.{self.env.qemu_default_machine}.out'
  199. if os.path.isfile(ref):
  200. return ref
  201. return f'{test}.out'
  202. def do_run_test(self, test: str, mp: bool) -> TestResult:
  203. """
  204. Run one test
  205. :param test: test file path
  206. :param mp: if true, we are in a multiprocessing environment, use
  207. personal subdirectories for test run
  208. Note: this method may be called from subprocess, so it does not
  209. change ``self`` object in any way!
  210. """
  211. f_test = Path(test)
  212. f_reference = Path(self.find_reference(test))
  213. if not f_test.exists():
  214. return TestResult(status='fail',
  215. description=f'No such test file: {f_test}')
  216. if not os.access(str(f_test), os.X_OK):
  217. sys.exit(f'Not executable: {f_test}')
  218. if not f_reference.exists():
  219. return TestResult(status='not run',
  220. description='No qualified output '
  221. f'(expected {f_reference})')
  222. args = [str(f_test.resolve())]
  223. env = self.env.prepare_subprocess(args)
  224. if mp:
  225. # Split test directories, so that tests running in parallel don't
  226. # break each other.
  227. for d in ['TEST_DIR', 'SOCK_DIR']:
  228. env[d] = os.path.join(env[d], f_test.name)
  229. Path(env[d]).mkdir(parents=True, exist_ok=True)
  230. test_dir = env['TEST_DIR']
  231. f_bad = Path(test_dir, f_test.name + '.out.bad')
  232. f_notrun = Path(test_dir, f_test.name + '.notrun')
  233. f_casenotrun = Path(test_dir, f_test.name + '.casenotrun')
  234. for p in (f_notrun, f_casenotrun):
  235. silent_unlink(p)
  236. t0 = time.time()
  237. with f_bad.open('w', encoding="utf-8") as f:
  238. with subprocess.Popen(args, cwd=str(f_test.parent), env=env,
  239. stdout=f, stderr=subprocess.STDOUT) as proc:
  240. try:
  241. proc.wait()
  242. except KeyboardInterrupt:
  243. proc.terminate()
  244. proc.wait()
  245. return TestResult(status='not run',
  246. description='Interrupted by user',
  247. interrupted=True)
  248. ret = proc.returncode
  249. elapsed = round(time.time() - t0, 1)
  250. if ret != 0:
  251. return TestResult(status='fail', elapsed=elapsed,
  252. description=f'failed, exit status {ret}',
  253. diff=file_diff(str(f_reference), str(f_bad)))
  254. if f_notrun.exists():
  255. return TestResult(
  256. status='not run',
  257. description=f_notrun.read_text(encoding='utf-8').strip())
  258. casenotrun = ''
  259. if f_casenotrun.exists():
  260. casenotrun = f_casenotrun.read_text(encoding='utf-8')
  261. diff = file_diff(str(f_reference), str(f_bad))
  262. if diff:
  263. if os.environ.get("QEMU_IOTESTS_REGEN", None) is not None:
  264. shutil.copyfile(str(f_bad), str(f_reference))
  265. print("########################################")
  266. print("##### REFERENCE FILE UPDATED #####")
  267. print("########################################")
  268. return TestResult(status='fail', elapsed=elapsed,
  269. description=f'output mismatch (see {f_bad})',
  270. diff=diff, casenotrun=casenotrun)
  271. else:
  272. f_bad.unlink()
  273. return TestResult(status='pass', elapsed=elapsed,
  274. casenotrun=casenotrun)
  275. def run_test(self, test: str,
  276. test_field_width: int,
  277. mp: bool = False) -> TestResult:
  278. """
  279. Run one test and print short status
  280. :param test: test file path
  281. :param test_field_width: width for first field of status format
  282. :param mp: if true, we are in a multiprocessing environment, don't try
  283. to rewrite things in stdout
  284. Note: this method may be called from subprocess, so it does not
  285. change ``self`` object in any way!
  286. """
  287. last_el = self.last_elapsed.get(test)
  288. start = datetime.datetime.now().strftime('%H:%M:%S')
  289. if not self.tap:
  290. self.test_print_one_line(test=test,
  291. test_field_width=test_field_width,
  292. status = 'started' if mp else '...',
  293. starttime=start,
  294. lasttime=last_el,
  295. end = '\n' if mp else '\r')
  296. res = self.do_run_test(test, mp)
  297. end = datetime.datetime.now().strftime('%H:%M:%S')
  298. self.test_print_one_line(test=test,
  299. test_field_width=test_field_width,
  300. status=res.status,
  301. starttime=start, endtime=end,
  302. lasttime=last_el, thistime=res.elapsed,
  303. description=res.description)
  304. if res.casenotrun:
  305. if self.tap:
  306. print('#' + res.casenotrun.replace('\n', '\n#'))
  307. else:
  308. print(res.casenotrun)
  309. return res
  310. def run_tests(self, tests: List[str], jobs: int = 1) -> bool:
  311. n_run = 0
  312. failed = []
  313. notrun = []
  314. casenotrun = []
  315. if self.tap:
  316. self.env.print_env('# ')
  317. else:
  318. self.env.print_env()
  319. test_field_width = max(len(os.path.basename(t)) for t in tests) + 2
  320. if jobs > 1:
  321. results = self.run_tests_pool(tests, test_field_width, jobs)
  322. for i, t in enumerate(tests):
  323. name = os.path.basename(t)
  324. if jobs > 1:
  325. res = results[i]
  326. else:
  327. res = self.run_test(t, test_field_width)
  328. assert res.status in ('pass', 'fail', 'not run')
  329. if res.casenotrun:
  330. casenotrun.append(t)
  331. if res.status != 'not run':
  332. n_run += 1
  333. if res.status == 'fail':
  334. failed.append(name)
  335. if res.diff:
  336. if self.tap:
  337. print('\n'.join(res.diff), file=sys.stderr)
  338. else:
  339. print('\n'.join(res.diff))
  340. elif res.status == 'not run':
  341. notrun.append(name)
  342. elif res.status == 'pass':
  343. assert res.elapsed is not None
  344. self.last_elapsed.update(t, res.elapsed)
  345. sys.stdout.flush()
  346. if res.interrupted:
  347. break
  348. if not self.tap:
  349. if notrun:
  350. print('Not run:', ' '.join(notrun))
  351. if casenotrun:
  352. print('Some cases not run in:', ' '.join(casenotrun))
  353. if failed:
  354. print('Failures:', ' '.join(failed))
  355. print(f'Failed {len(failed)} of {n_run} iotests')
  356. else:
  357. print(f'Passed all {n_run} iotests')
  358. return not failed