1
0

noxfile.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. # -*- coding: utf-8 -*-
  2. '''
  3. noxfile
  4. ~~~~~~~
  5. Nox configuration script
  6. '''
  7. # pylint: disable=resource-leakage
  8. # Import Python libs
  9. from __future__ import absolute_import, unicode_literals, print_function
  10. import os
  11. import sys
  12. import glob
  13. import json
  14. import pprint
  15. import shutil
  16. import tempfile
  17. import datetime
  18. if __name__ == '__main__':
  19. sys.stderr.write('Do not execute this file directly. Use nox instead, it will know how to handle this file\n')
  20. sys.stderr.flush()
  21. exit(1)
  22. # Import 3rd-party libs
  23. import nox
  24. from nox.command import CommandFailed
  25. IS_PY3 = sys.version_info > (2,)
  26. # Be verbose when runing under a CI context
  27. PIP_INSTALL_SILENT = (os.environ.get('JENKINS_URL') or os.environ.get('CI') or os.environ.get('DRONE')) is None
  28. # Global Path Definitions
  29. REPO_ROOT = os.path.abspath(os.path.dirname(__file__))
  30. SITECUSTOMIZE_DIR = os.path.join(REPO_ROOT, 'tests', 'support', 'coverage')
  31. IS_DARWIN = sys.platform.lower().startswith('darwin')
  32. IS_WINDOWS = sys.platform.lower().startswith('win')
  33. # Python versions to run against
  34. _PYTHON_VERSIONS = ('2', '2.7', '3', '3.4', '3.5', '3.6', '3.7')
  35. # Nox options
  36. # Reuse existing virtualenvs
  37. nox.options.reuse_existing_virtualenvs = True
  38. # Don't fail on missing interpreters
  39. nox.options.error_on_missing_interpreters = False
  40. # Change current directory to REPO_ROOT
  41. os.chdir(REPO_ROOT)
  42. RUNTESTS_LOGFILE = os.path.join(
  43. 'artifacts', 'logs',
  44. 'runtests-{}.log'.format(datetime.datetime.now().strftime('%Y%m%d%H%M%S.%f'))
  45. )
  46. # Prevent Python from writing bytecode
  47. os.environ[str('PYTHONDONTWRITEBYTECODE')] = str('1')
  48. def _create_ci_directories():
  49. for dirname in ('logs', 'coverage', 'xml-unittests-output'):
  50. path = os.path.join('artifacts', dirname)
  51. if not os.path.exists(path):
  52. os.makedirs(path)
  53. def _get_session_python_version_info(session):
  54. try:
  55. version_info = session._runner._real_python_version_info
  56. except AttributeError:
  57. old_install_only_value = session._runner.global_config.install_only
  58. try:
  59. # Force install only to be false for the following chunk of code
  60. # For additional information as to why see:
  61. # https://github.com/theacodes/nox/pull/181
  62. session._runner.global_config.install_only = False
  63. session_py_version = session.run(
  64. 'python', '-c'
  65. 'import sys; sys.stdout.write("{}.{}.{}".format(*sys.version_info))',
  66. silent=True,
  67. log=False,
  68. )
  69. version_info = tuple(int(part) for part in session_py_version.split('.') if part.isdigit())
  70. session._runner._real_python_version_info = version_info
  71. finally:
  72. session._runner.global_config.install_only = old_install_only_value
  73. return version_info
  74. def _get_session_python_site_packages_dir(session):
  75. try:
  76. site_packages_dir = session._runner._site_packages_dir
  77. except AttributeError:
  78. old_install_only_value = session._runner.global_config.install_only
  79. try:
  80. # Force install only to be false for the following chunk of code
  81. # For additional information as to why see:
  82. # https://github.com/theacodes/nox/pull/181
  83. session._runner.global_config.install_only = False
  84. site_packages_dir = session.run(
  85. 'python', '-c'
  86. 'import sys; from distutils.sysconfig import get_python_lib; sys.stdout.write(get_python_lib())',
  87. silent=True,
  88. log=False,
  89. )
  90. session._runner._site_packages_dir = site_packages_dir
  91. finally:
  92. session._runner.global_config.install_only = old_install_only_value
  93. return site_packages_dir
  94. def _get_pydir(session):
  95. version_info = _get_session_python_version_info(session)
  96. if version_info < (2, 7):
  97. session.error('Only Python >= 2.7 is supported')
  98. return 'py{}.{}'.format(*version_info)
  99. def _get_distro_info(session):
  100. try:
  101. distro = session._runner._distro
  102. except AttributeError:
  103. # The distro package doesn't output anything for Windows
  104. old_install_only_value = session._runner.global_config.install_only
  105. try:
  106. # Force install only to be false for the following chunk of code
  107. # For additional information as to why see:
  108. # https://github.com/theacodes/nox/pull/181
  109. session._runner.global_config.install_only = False
  110. session.install('--progress-bar=off', 'distro', silent=PIP_INSTALL_SILENT)
  111. output = session.run('distro', '-j', silent=True)
  112. distro = json.loads(output.strip())
  113. session.log('Distro information:\n%s', pprint.pformat(distro))
  114. session._runner._distro = distro
  115. finally:
  116. session._runner.global_config.install_only = old_install_only_value
  117. return distro
  118. def _install_system_packages(session):
  119. '''
  120. Because some python packages are provided by the distribution and cannot
  121. be pip installed, and because we don't want the whole system python packages
  122. on our virtualenvs, we copy the required system python packages into
  123. the virtualenv
  124. '''
  125. system_python_packages = {
  126. '__debian_based_distros__': [
  127. '/usr/lib/python{py_version}/dist-packages/*apt*'
  128. ]
  129. }
  130. for key in ('ubuntu-14.04', 'ubuntu-16.04', 'ubuntu-18.04', 'debian-8', 'debian-9'):
  131. system_python_packages[key] = system_python_packages['__debian_based_distros__']
  132. distro = _get_distro_info(session)
  133. distro_keys = [
  134. '{id}'.format(**distro),
  135. '{id}-{version}'.format(**distro),
  136. '{id}-{version_parts[major]}'.format(**distro)
  137. ]
  138. version_info = _get_session_python_version_info(session)
  139. py_version_keys = [
  140. '{}'.format(*version_info),
  141. '{}.{}'.format(*version_info)
  142. ]
  143. session_site_packages_dir = _get_session_python_site_packages_dir(session)
  144. for distro_key in distro_keys:
  145. if distro_key not in system_python_packages:
  146. continue
  147. patterns = system_python_packages[distro_key]
  148. for pattern in patterns:
  149. for py_version in py_version_keys:
  150. matches = set(glob.glob(pattern.format(py_version=py_version)))
  151. if not matches:
  152. continue
  153. for match in matches:
  154. src = os.path.realpath(match)
  155. dst = os.path.join(session_site_packages_dir, os.path.basename(match))
  156. if os.path.exists(dst):
  157. session.log('Not overwritting already existing %s with %s', dst, src)
  158. continue
  159. session.log('Copying %s into %s', src, dst)
  160. if os.path.isdir(src):
  161. shutil.copytree(src, dst)
  162. else:
  163. shutil.copyfile(src, dst)
  164. def _get_distro_pip_constraints(session, transport):
  165. # Install requirements
  166. distro_constraints = []
  167. if transport == 'tcp':
  168. # The TCP requirements are the exact same requirements as the ZeroMQ ones
  169. transport = 'zeromq'
  170. pydir = _get_pydir(session)
  171. if IS_WINDOWS:
  172. _distro_constraints = os.path.join('requirements',
  173. 'static',
  174. pydir,
  175. '{}-windows.txt'.format(transport))
  176. if os.path.exists(_distro_constraints):
  177. distro_constraints.append(_distro_constraints)
  178. _distro_constraints = os.path.join('requirements',
  179. 'static',
  180. pydir,
  181. 'windows.txt')
  182. if os.path.exists(_distro_constraints):
  183. distro_constraints.append(_distro_constraints)
  184. _distro_constraints = os.path.join('requirements',
  185. 'static',
  186. pydir,
  187. 'windows-crypto.txt')
  188. if os.path.exists(_distro_constraints):
  189. distro_constraints.append(_distro_constraints)
  190. elif IS_DARWIN:
  191. _distro_constraints = os.path.join('requirements',
  192. 'static',
  193. pydir,
  194. '{}-darwin.txt'.format(transport))
  195. if os.path.exists(_distro_constraints):
  196. distro_constraints.append(_distro_constraints)
  197. _distro_constraints = os.path.join('requirements',
  198. 'static',
  199. pydir,
  200. 'darwin.txt')
  201. if os.path.exists(_distro_constraints):
  202. distro_constraints.append(_distro_constraints)
  203. _distro_constraints = os.path.join('requirements',
  204. 'static',
  205. pydir,
  206. 'darwin-crypto.txt')
  207. if os.path.exists(_distro_constraints):
  208. distro_constraints.append(_distro_constraints)
  209. else:
  210. _install_system_packages(session)
  211. distro = _get_distro_info(session)
  212. distro_keys = [
  213. 'linux',
  214. '{id}'.format(**distro),
  215. '{id}-{version}'.format(**distro),
  216. '{id}-{version_parts[major]}'.format(**distro)
  217. ]
  218. for distro_key in distro_keys:
  219. _distro_constraints = os.path.join('requirements',
  220. 'static',
  221. pydir,
  222. '{}.txt'.format(distro_key))
  223. if os.path.exists(_distro_constraints):
  224. distro_constraints.append(_distro_constraints)
  225. _distro_constraints = os.path.join('requirements',
  226. 'static',
  227. pydir,
  228. '{}-crypto.txt'.format(distro_key))
  229. if os.path.exists(_distro_constraints):
  230. distro_constraints.append(_distro_constraints)
  231. _distro_constraints = os.path.join('requirements',
  232. 'static',
  233. pydir,
  234. '{}-{}.txt'.format(transport, distro_key))
  235. if os.path.exists(_distro_constraints):
  236. distro_constraints.append(_distro_constraints)
  237. distro_constraints.append(_distro_constraints)
  238. _distro_constraints = os.path.join('requirements',
  239. 'static',
  240. pydir,
  241. '{}-{}-crypto.txt'.format(transport, distro_key))
  242. if os.path.exists(_distro_constraints):
  243. distro_constraints.append(_distro_constraints)
  244. return distro_constraints
  245. def _install_requirements(session, transport, *extra_requirements):
  246. # Install requirements
  247. distro_constraints = _get_distro_pip_constraints(session, transport)
  248. _requirements_files = [
  249. os.path.join('requirements', 'base.txt'),
  250. os.path.join('requirements', 'zeromq.txt'),
  251. os.path.join('requirements', 'pytest.txt')
  252. ]
  253. if sys.platform.startswith('linux'):
  254. requirements_files = [
  255. os.path.join('requirements', 'static', 'linux.in')
  256. ]
  257. elif sys.platform.startswith('win'):
  258. requirements_files = [
  259. os.path.join('pkg', 'windows', 'req.txt'),
  260. os.path.join('requirements', 'static', 'windows.in')
  261. ]
  262. elif sys.platform.startswith('darwin'):
  263. requirements_files = [
  264. os.path.join('pkg', 'osx', 'req.txt'),
  265. os.path.join('pkg', 'osx', 'req_ext.txt'),
  266. os.path.join('requirements', 'static', 'darwin.in')
  267. ]
  268. while True:
  269. if not requirements_files:
  270. break
  271. requirements_file = requirements_files.pop(0)
  272. if requirements_file not in _requirements_files:
  273. _requirements_files.append(requirements_file)
  274. session.log('Processing {}'.format(requirements_file))
  275. with open(requirements_file) as rfh: # pylint: disable=resource-leakage
  276. for line in rfh:
  277. line = line.strip()
  278. if not line:
  279. continue
  280. if line.startswith('-r'):
  281. reqfile = os.path.join(os.path.dirname(requirements_file), line.strip().split()[-1])
  282. if reqfile in _requirements_files:
  283. continue
  284. _requirements_files.append(reqfile)
  285. continue
  286. for requirements_file in _requirements_files:
  287. install_command = [
  288. '--progress-bar=off', '-r', requirements_file
  289. ]
  290. for distro_constraint in distro_constraints:
  291. install_command.extend([
  292. '--constraint', distro_constraint
  293. ])
  294. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  295. if extra_requirements:
  296. install_command = [
  297. '--progress-bar=off',
  298. ]
  299. for distro_constraint in distro_constraints:
  300. install_command.extend([
  301. '--constraint', distro_constraint
  302. ])
  303. install_command += list(extra_requirements)
  304. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  305. def _run_with_coverage(session, *test_cmd):
  306. session.install('--progress-bar=off', 'coverage==4.5.3', silent=PIP_INSTALL_SILENT)
  307. session.run('coverage', 'erase')
  308. python_path_env_var = os.environ.get('PYTHONPATH') or None
  309. if python_path_env_var is None:
  310. python_path_env_var = SITECUSTOMIZE_DIR
  311. else:
  312. python_path_entries = python_path_env_var.split(os.pathsep)
  313. if SITECUSTOMIZE_DIR in python_path_entries:
  314. python_path_entries.remove(SITECUSTOMIZE_DIR)
  315. python_path_entries.insert(0, SITECUSTOMIZE_DIR)
  316. python_path_env_var = os.pathsep.join(python_path_entries)
  317. env = {
  318. # The updated python path so that sitecustomize is importable
  319. 'PYTHONPATH': python_path_env_var,
  320. # The full path to the .coverage data file. Makes sure we always write
  321. # them to the same directory
  322. 'COVERAGE_FILE': os.path.abspath(os.path.join(REPO_ROOT, '.coverage')),
  323. # Instruct sub processes to also run under coverage
  324. 'COVERAGE_PROCESS_START': os.path.join(REPO_ROOT, '.coveragerc')
  325. }
  326. if IS_DARWIN:
  327. # Don't nuke our multiprocessing efforts objc!
  328. # https://stackoverflow.com/questions/50168647/multiprocessing-causes-python-to-crash-and-gives-an-error-may-have-been-in-progr
  329. env['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'
  330. try:
  331. session.run(*test_cmd, env=env)
  332. finally:
  333. # Always combine and generate the XML coverage report
  334. try:
  335. session.run('coverage', 'combine')
  336. except CommandFailed:
  337. # Sometimes some of the coverage files are corrupt which would trigger a CommandFailed
  338. # exception
  339. pass
  340. # Generate report for salt code coverage
  341. session.run(
  342. 'coverage', 'xml',
  343. '-o', os.path.join('artifacts', 'coverage', 'salt.xml'),
  344. '--omit=tests/*',
  345. '--include=salt/*'
  346. )
  347. # Generate report for tests code coverage
  348. session.run(
  349. 'coverage', 'xml',
  350. '-o', os.path.join('artifacts', 'coverage', 'tests.xml'),
  351. '--omit=salt/*',
  352. '--include=tests/*'
  353. )
  354. def _runtests(session, coverage, cmd_args):
  355. # Create required artifacts directories
  356. _create_ci_directories()
  357. try:
  358. if coverage is True:
  359. _run_with_coverage(session, 'coverage', 'run', os.path.join('tests', 'runtests.py'), *cmd_args)
  360. else:
  361. cmd_args = ['python', os.path.join('tests', 'runtests.py')] + list(cmd_args)
  362. env = None
  363. if IS_DARWIN:
  364. # Don't nuke our multiprocessing efforts objc!
  365. # https://stackoverflow.com/questions/50168647/multiprocessing-causes-python-to-crash-and-gives-an-error-may-have-been-in-progr
  366. env = {'OBJC_DISABLE_INITIALIZE_FORK_SAFETY': 'YES'}
  367. session.run(*cmd_args, env=env)
  368. except CommandFailed:
  369. # Disabling re-running failed tests for the time being
  370. raise
  371. # pylint: disable=unreachable
  372. names_file_path = os.path.join('artifacts', 'failed-tests.txt')
  373. session.log('Re-running failed tests if possible')
  374. session.install('--progress-bar=off', 'xunitparser==1.3.3', silent=PIP_INSTALL_SILENT)
  375. session.run(
  376. 'python',
  377. os.path.join('tests', 'support', 'generate-names-file-from-failed-test-reports.py'),
  378. names_file_path
  379. )
  380. if not os.path.exists(names_file_path):
  381. session.log(
  382. 'Failed tests file(%s) was not found. Not rerunning failed tests.',
  383. names_file_path
  384. )
  385. # raise the original exception
  386. raise
  387. with open(names_file_path) as rfh:
  388. contents = rfh.read().strip()
  389. if not contents:
  390. session.log(
  391. 'The failed tests file(%s) is empty. Not rerunning failed tests.',
  392. names_file_path
  393. )
  394. # raise the original exception
  395. raise
  396. failed_tests_count = len(contents.splitlines())
  397. if failed_tests_count > 500:
  398. # 500 test failures?! Something else must have gone wrong, don't even bother
  399. session.error(
  400. 'Total failed tests({}) > 500. No point on re-running the failed tests'.format(
  401. failed_tests_count
  402. )
  403. )
  404. for idx, flag in enumerate(cmd_args[:]):
  405. if '--names-file=' in flag:
  406. cmd_args.pop(idx)
  407. break
  408. elif flag == '--names-file':
  409. cmd_args.pop(idx) # pop --names-file
  410. cmd_args.pop(idx) # pop the actual names file
  411. break
  412. cmd_args.append('--names-file={}'.format(names_file_path))
  413. if coverage is True:
  414. _run_with_coverage(session, 'coverage', 'run', '-m', 'tests.runtests', *cmd_args)
  415. else:
  416. session.run('python', os.path.join('tests', 'runtests.py'), *cmd_args)
  417. # pylint: enable=unreachable
  418. @nox.session(python=_PYTHON_VERSIONS, name='runtests-parametrized')
  419. @nox.parametrize('coverage', [False, True])
  420. @nox.parametrize('transport', ['zeromq', 'tcp'])
  421. @nox.parametrize('crypto', [None, 'm2crypto', 'pycryptodomex'])
  422. def runtests_parametrized(session, coverage, transport, crypto):
  423. # Install requirements
  424. _install_requirements(session, transport, 'unittest-xml-reporting==2.2.1')
  425. if crypto:
  426. if crypto == 'm2crypto':
  427. session.run('pip', 'uninstall', '-y', 'pycrypto', 'pycryptodome', 'pycryptodomex', silent=True)
  428. else:
  429. session.run('pip', 'uninstall', '-y', 'm2crypto', silent=True)
  430. distro_constraints = _get_distro_pip_constraints(session, transport)
  431. install_command = [
  432. '--progress-bar=off',
  433. ]
  434. for distro_constraint in distro_constraints:
  435. install_command.extend([
  436. '--constraint', distro_constraint
  437. ])
  438. install_command.append(crypto)
  439. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  440. cmd_args = [
  441. '--tests-logfile={}'.format(RUNTESTS_LOGFILE),
  442. '--transport={}'.format(transport)
  443. ] + session.posargs
  444. _runtests(session, coverage, cmd_args)
  445. @nox.session(python=_PYTHON_VERSIONS)
  446. @nox.parametrize('coverage', [False, True])
  447. def runtests(session, coverage):
  448. '''
  449. runtests.py session with zeromq transport and default crypto
  450. '''
  451. session.notify(
  452. 'runtests-parametrized-{}(coverage={}, crypto=None, transport=\'zeromq\')'.format(
  453. session.python,
  454. coverage
  455. )
  456. )
  457. @nox.session(python=_PYTHON_VERSIONS, name='runtests-tcp')
  458. @nox.parametrize('coverage', [False, True])
  459. def runtests_tcp(session, coverage):
  460. '''
  461. runtests.py session with TCP transport and default crypto
  462. '''
  463. session.notify(
  464. 'runtests-parametrized-{}(coverage={}, crypto=None, transport=\'tcp\')'.format(
  465. session.python,
  466. coverage
  467. )
  468. )
  469. @nox.session(python=_PYTHON_VERSIONS, name='runtests-zeromq')
  470. @nox.parametrize('coverage', [False, True])
  471. def runtests_zeromq(session, coverage):
  472. '''
  473. runtests.py session with zeromq transport and default crypto
  474. '''
  475. session.notify(
  476. 'runtests-parametrized-{}(coverage={}, crypto=None, transport=\'zeromq\')'.format(
  477. session.python,
  478. coverage
  479. )
  480. )
  481. @nox.session(python=_PYTHON_VERSIONS, name='runtests-m2crypto')
  482. @nox.parametrize('coverage', [False, True])
  483. def runtests_m2crypto(session, coverage):
  484. '''
  485. runtests.py session with zeromq transport and m2crypto
  486. '''
  487. session.notify(
  488. 'runtests-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'zeromq\')'.format(
  489. session.python,
  490. coverage
  491. )
  492. )
  493. @nox.session(python=_PYTHON_VERSIONS, name='runtests-tcp-m2crypto')
  494. @nox.parametrize('coverage', [False, True])
  495. def runtests_tcp_m2crypto(session, coverage):
  496. '''
  497. runtests.py session with TCP transport and m2crypto
  498. '''
  499. session.notify(
  500. 'runtests-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'tcp\')'.format(
  501. session.python,
  502. coverage
  503. )
  504. )
  505. @nox.session(python=_PYTHON_VERSIONS, name='runtests-zeromq-m2crypto')
  506. @nox.parametrize('coverage', [False, True])
  507. def runtests_zeromq_m2crypto(session, coverage):
  508. '''
  509. runtests.py session with zeromq transport and m2crypto
  510. '''
  511. session.notify(
  512. 'runtests-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'zeromq\')'.format(
  513. session.python,
  514. coverage
  515. )
  516. )
  517. @nox.session(python=_PYTHON_VERSIONS, name='runtests-pycryptodomex')
  518. @nox.parametrize('coverage', [False, True])
  519. def runtests_pycryptodomex(session, coverage):
  520. '''
  521. runtests.py session with zeromq transport and pycryptodomex
  522. '''
  523. session.notify(
  524. 'runtests-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'zeromq\')'.format(
  525. session.python,
  526. coverage
  527. )
  528. )
  529. @nox.session(python=_PYTHON_VERSIONS, name='runtests-tcp-pycryptodomex')
  530. @nox.parametrize('coverage', [False, True])
  531. def runtests_tcp_pycryptodomex(session, coverage):
  532. '''
  533. runtests.py session with TCP transport and pycryptodomex
  534. '''
  535. session.notify(
  536. 'runtests-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'tcp\')'.format(
  537. session.python,
  538. coverage
  539. )
  540. )
  541. @nox.session(python=_PYTHON_VERSIONS, name='runtests-zeromq-pycryptodomex')
  542. @nox.parametrize('coverage', [False, True])
  543. def runtests_zeromq_pycryptodomex(session, coverage):
  544. '''
  545. runtests.py session with zeromq transport and pycryptodomex
  546. '''
  547. session.notify(
  548. 'runtests-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'zeromq\')'.format(
  549. session.python,
  550. coverage
  551. )
  552. )
  553. @nox.session(python=_PYTHON_VERSIONS, name='runtests-cloud')
  554. @nox.parametrize('coverage', [False, True])
  555. def runtests_cloud(session, coverage):
  556. # Install requirements
  557. _install_requirements(session, 'zeromq', 'unittest-xml-reporting==2.2.1')
  558. pydir = _get_pydir(session)
  559. cloud_requirements = os.path.join('requirements', 'static', pydir, 'cloud.txt')
  560. session.install('--progress-bar=off', '-r', cloud_requirements, silent=PIP_INSTALL_SILENT)
  561. cmd_args = [
  562. '--tests-logfile={}'.format(RUNTESTS_LOGFILE),
  563. '--cloud-provider-tests'
  564. ] + session.posargs
  565. _runtests(session, coverage, cmd_args)
  566. @nox.session(python=_PYTHON_VERSIONS, name='runtests-tornado')
  567. @nox.parametrize('coverage', [False, True])
  568. def runtests_tornado(session, coverage):
  569. # Install requirements
  570. _install_requirements(session, 'zeromq', 'unittest-xml-reporting==2.2.1')
  571. session.install('--progress-bar=off', 'tornado==5.0.2', silent=PIP_INSTALL_SILENT)
  572. session.install('--progress-bar=off', 'pyzmq==17.0.0', silent=PIP_INSTALL_SILENT)
  573. cmd_args = [
  574. '--tests-logfile={}'.format(RUNTESTS_LOGFILE)
  575. ] + session.posargs
  576. _runtests(session, coverage, cmd_args)
  577. @nox.session(python=_PYTHON_VERSIONS, name='pytest-parametrized')
  578. @nox.parametrize('coverage', [False, True])
  579. @nox.parametrize('transport', ['zeromq', 'tcp'])
  580. @nox.parametrize('crypto', [None, 'm2crypto', 'pycryptodomex'])
  581. def pytest_parametrized(session, coverage, transport, crypto):
  582. # Install requirements
  583. _install_requirements(session, transport)
  584. if crypto:
  585. if crypto == 'm2crypto':
  586. session.run('pip', 'uninstall', '-y', 'pycrypto', 'pycryptodome', 'pycryptodomex', silent=True)
  587. else:
  588. session.run('pip', 'uninstall', '-y', 'm2crypto', silent=True)
  589. distro_constraints = _get_distro_pip_constraints(session, transport)
  590. install_command = [
  591. '--progress-bar=off',
  592. ]
  593. for distro_constraint in distro_constraints:
  594. install_command.extend([
  595. '--constraint', distro_constraint
  596. ])
  597. install_command.append(crypto)
  598. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  599. cmd_args = [
  600. '--rootdir', REPO_ROOT,
  601. '--log-file={}'.format(RUNTESTS_LOGFILE),
  602. '--log-file-level=debug',
  603. '--no-print-logs',
  604. '-ra',
  605. '-s',
  606. '--transport={}'.format(transport)
  607. ] + session.posargs
  608. _pytest(session, coverage, cmd_args)
  609. @nox.session(python=_PYTHON_VERSIONS)
  610. @nox.parametrize('coverage', [False, True])
  611. def pytest(session, coverage):
  612. '''
  613. pytest session with zeromq transport and default crypto
  614. '''
  615. session.notify(
  616. 'pytest-parametrized-{}(coverage={}, crypto=None, transport=\'zeromq\')'.format(
  617. session.python,
  618. coverage
  619. )
  620. )
  621. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tcp')
  622. @nox.parametrize('coverage', [False, True])
  623. def pytest_tcp(session, coverage):
  624. '''
  625. pytest session with TCP transport and default crypto
  626. '''
  627. session.notify(
  628. 'pytest-parametrized-{}(coverage={}, crypto=None, transport=\'tcp\')'.format(
  629. session.python,
  630. coverage
  631. )
  632. )
  633. @nox.session(python=_PYTHON_VERSIONS, name='pytest-zeromq')
  634. @nox.parametrize('coverage', [False, True])
  635. def pytest_zeromq(session, coverage):
  636. '''
  637. pytest session with zeromq transport and default crypto
  638. '''
  639. session.notify(
  640. 'pytest-parametrized-{}(coverage={}, crypto=None, transport=\'zeromq\')'.format(
  641. session.python,
  642. coverage
  643. )
  644. )
  645. @nox.session(python=_PYTHON_VERSIONS, name='pytest-m2crypto')
  646. @nox.parametrize('coverage', [False, True])
  647. def pytest_m2crypto(session, coverage):
  648. '''
  649. pytest session with zeromq transport and m2crypto
  650. '''
  651. session.notify(
  652. 'pytest-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'zeromq\')'.format(
  653. session.python,
  654. coverage
  655. )
  656. )
  657. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tcp-m2crypto')
  658. @nox.parametrize('coverage', [False, True])
  659. def pytest_tcp_m2crypto(session, coverage):
  660. '''
  661. pytest session with TCP transport and m2crypto
  662. '''
  663. session.notify(
  664. 'pytest-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'tcp\')'.format(
  665. session.python,
  666. coverage
  667. )
  668. )
  669. @nox.session(python=_PYTHON_VERSIONS, name='pytest-zeromq-m2crypto')
  670. @nox.parametrize('coverage', [False, True])
  671. def pytest_zeromq_m2crypto(session, coverage):
  672. '''
  673. pytest session with zeromq transport and m2crypto
  674. '''
  675. session.notify(
  676. 'pytest-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'zeromq\')'.format(
  677. session.python,
  678. coverage
  679. )
  680. )
  681. @nox.session(python=_PYTHON_VERSIONS, name='pytest-pycryptodomex')
  682. @nox.parametrize('coverage', [False, True])
  683. def pytest_pycryptodomex(session, coverage):
  684. '''
  685. pytest session with zeromq transport and pycryptodomex
  686. '''
  687. session.notify(
  688. 'pytest-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'zeromq\')'.format(
  689. session.python,
  690. coverage
  691. )
  692. )
  693. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tcp-pycryptodomex')
  694. @nox.parametrize('coverage', [False, True])
  695. def pytest_tcp_pycryptodomex(session, coverage):
  696. '''
  697. pytest session with TCP transport and pycryptodomex
  698. '''
  699. session.notify(
  700. 'pytest-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'tcp\')'.format(
  701. session.python,
  702. coverage
  703. )
  704. )
  705. @nox.session(python=_PYTHON_VERSIONS, name='pytest-zeromq-pycryptodomex')
  706. @nox.parametrize('coverage', [False, True])
  707. def pytest_zeromq_pycryptodomex(session, coverage):
  708. '''
  709. pytest session with zeromq transport and pycryptodomex
  710. '''
  711. session.notify(
  712. 'pytest-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'zeromq\')'.format(
  713. session.python,
  714. coverage
  715. )
  716. )
  717. @nox.session(python=_PYTHON_VERSIONS, name='pytest-cloud')
  718. @nox.parametrize('coverage', [False, True])
  719. def pytest_cloud(session, coverage):
  720. # Install requirements
  721. _install_requirements(session, 'zeromq')
  722. pydir = _get_pydir(session)
  723. cloud_requirements = os.path.join('requirements', 'static', pydir, 'cloud.txt')
  724. session.install('--progress-bar=off', '-r', cloud_requirements, silent=PIP_INSTALL_SILENT)
  725. cmd_args = [
  726. '--rootdir', REPO_ROOT,
  727. '--log-file={}'.format(RUNTESTS_LOGFILE),
  728. '--log-file-level=debug',
  729. '--no-print-logs',
  730. '-ra',
  731. '-s',
  732. os.path.join('tests', 'integration', 'cloud', 'providers')
  733. ] + session.posargs
  734. _pytest(session, coverage, cmd_args)
  735. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tornado')
  736. @nox.parametrize('coverage', [False, True])
  737. def pytest_tornado(session, coverage):
  738. # Install requirements
  739. _install_requirements(session, 'zeromq')
  740. session.install('--progress-bar=off', 'tornado==5.0.2', silent=PIP_INSTALL_SILENT)
  741. session.install('--progress-bar=off', 'pyzmq==17.0.0', silent=PIP_INSTALL_SILENT)
  742. cmd_args = [
  743. '--rootdir', REPO_ROOT,
  744. '--log-file={}'.format(RUNTESTS_LOGFILE),
  745. '--log-file-level=debug',
  746. '--no-print-logs',
  747. '-ra',
  748. '-s',
  749. ] + session.posargs
  750. _pytest(session, coverage, cmd_args)
  751. def _pytest(session, coverage, cmd_args):
  752. # Create required artifacts directories
  753. _create_ci_directories()
  754. env = None
  755. if IS_DARWIN:
  756. # Don't nuke our multiprocessing efforts objc!
  757. # https://stackoverflow.com/questions/50168647/multiprocessing-causes-python-to-crash-and-gives-an-error-may-have-been-in-progr
  758. env = {'OBJC_DISABLE_INITIALIZE_FORK_SAFETY': 'YES'}
  759. try:
  760. if coverage is True:
  761. _run_with_coverage(session, 'coverage', 'run', '-m', 'py.test', *cmd_args)
  762. else:
  763. session.run('py.test', *cmd_args, env=env)
  764. except CommandFailed:
  765. # Re-run failed tests
  766. session.log('Re-running failed tests')
  767. cmd_args.append('--lf')
  768. if coverage is True:
  769. _run_with_coverage(session, 'coverage', 'run', '-m', 'py.test', *cmd_args)
  770. else:
  771. session.run('py.test', *cmd_args, env=env)
  772. def _lint(session, rcfile, flags, paths):
  773. _install_requirements(session, 'zeromq')
  774. requirements_file = 'requirements/static/lint.in'
  775. distro_constraints = [
  776. 'requirements/static/{}/lint.txt'.format(_get_pydir(session))
  777. ]
  778. install_command = [
  779. '--progress-bar=off', '-r', requirements_file
  780. ]
  781. for distro_constraint in distro_constraints:
  782. install_command.extend([
  783. '--constraint', distro_constraint
  784. ])
  785. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  786. session.run('pylint', '--version')
  787. pylint_report_path = os.environ.get('PYLINT_REPORT')
  788. cmd_args = [
  789. 'pylint',
  790. '--rcfile={}'.format(rcfile)
  791. ] + list(flags) + list(paths)
  792. stdout = tempfile.TemporaryFile(mode='w+b')
  793. lint_failed = False
  794. try:
  795. session.run(*cmd_args, stdout=stdout)
  796. except CommandFailed:
  797. lint_failed = True
  798. raise
  799. finally:
  800. stdout.seek(0)
  801. contents = stdout.read()
  802. if contents:
  803. if IS_PY3:
  804. contents = contents.decode('utf-8')
  805. else:
  806. contents = contents.encode('utf-8')
  807. sys.stdout.write(contents)
  808. sys.stdout.flush()
  809. if pylint_report_path:
  810. # Write report
  811. with open(pylint_report_path, 'w') as wfh:
  812. wfh.write(contents)
  813. session.log('Report file written to %r', pylint_report_path)
  814. stdout.close()
  815. @nox.session(python='2.7')
  816. def lint(session):
  817. '''
  818. Run PyLint against Salt and it's test suite. Set PYLINT_REPORT to a path to capture output.
  819. '''
  820. session.notify('lint-salt-{}'.format(session.python))
  821. session.notify('lint-tests-{}'.format(session.python))
  822. @nox.session(python='2.7', name='lint-salt')
  823. def lint_salt(session):
  824. '''
  825. Run PyLint against Salt. Set PYLINT_REPORT to a path to capture output.
  826. '''
  827. flags = [
  828. '--disable=I,W1307,C0411,C0413,W8410,str-format-in-logging'
  829. ]
  830. if session.posargs:
  831. paths = session.posargs
  832. else:
  833. paths = ['setup.py', 'noxfile.py', 'salt/']
  834. _lint(session, '.testing.pylintrc', flags, paths)
  835. @nox.session(python='2.7', name='lint-tests')
  836. def lint_tests(session):
  837. '''
  838. Run PyLint against Salt and it's test suite. Set PYLINT_REPORT to a path to capture output.
  839. '''
  840. flags = [
  841. '--disable=I,W0232,E1002,W1307,C0411,C0413,W8410,str-format-in-logging'
  842. ]
  843. if session.posargs:
  844. paths = session.posargs
  845. else:
  846. paths = ['tests/']
  847. _lint(session, '.testing.pylintrc', flags, paths)
  848. @nox.session(python='3')
  849. @nox.parametrize('update', [False, True])
  850. @nox.parametrize('compress', [False, True])
  851. def docs(session, compress, update):
  852. '''
  853. Build Salt's Documentation
  854. '''
  855. session.notify('docs-html(compress={})'.format(compress))
  856. session.notify('docs-man(compress={}, update={})'.format(compress, update))
  857. @nox.session(name='docs-html', python='3')
  858. @nox.parametrize('compress', [False, True])
  859. def docs_html(session, compress):
  860. '''
  861. Build Salt's HTML Documentation
  862. '''
  863. pydir = _get_pydir(session)
  864. if pydir == 'py3.4':
  865. session.error('Sphinx only runs on Python >= 3.5')
  866. requirements_file = 'requirements/static/docs.in'
  867. distro_constraints = [
  868. 'requirements/static/{}/docs.txt'.format(_get_pydir(session))
  869. ]
  870. install_command = [
  871. '--progress-bar=off', '-r', requirements_file
  872. ]
  873. for distro_constraint in distro_constraints:
  874. install_command.extend([
  875. '--constraint', distro_constraint
  876. ])
  877. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  878. os.chdir('doc/')
  879. session.run('make', 'clean', external=True)
  880. session.run('make', 'html', 'SPHINXOPTS=-W', external=True)
  881. if compress:
  882. session.run('tar', '-cJvf', 'html-archive.tar.xz', '_build/html', external=True)
  883. os.chdir('..')
  884. @nox.session(name='docs-man', python='3')
  885. @nox.parametrize('update', [False, True])
  886. @nox.parametrize('compress', [False, True])
  887. def docs_man(session, compress, update):
  888. '''
  889. Build Salt's Manpages Documentation
  890. '''
  891. pydir = _get_pydir(session)
  892. if pydir == 'py3.4':
  893. session.error('Sphinx only runs on Python >= 3.5')
  894. requirements_file = 'requirements/static/docs.in'
  895. distro_constraints = [
  896. 'requirements/static/{}/docs.txt'.format(_get_pydir(session))
  897. ]
  898. install_command = [
  899. '--progress-bar=off', '-r', requirements_file
  900. ]
  901. for distro_constraint in distro_constraints:
  902. install_command.extend([
  903. '--constraint', distro_constraint
  904. ])
  905. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  906. os.chdir('doc/')
  907. session.run('make', 'clean', external=True)
  908. session.run('make', 'man', 'SPHINXOPTS=-W', external=True)
  909. if update:
  910. session.run('rm', '-rf', 'man/', external=True)
  911. session.run('cp', '-Rp', '_build/man', 'man/', external=True)
  912. if compress:
  913. session.run('tar', '-cJvf', 'man-archive.tar.xz', '_build/man', external=True)
  914. os.chdir('..')