noxfile.py 36 KB

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