1
0

runtests.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Discover all instances of unittest.TestCase in this directory.
  5. '''
  6. # pylint: disable=file-perms
  7. # Import python libs
  8. from __future__ import absolute_import, print_function
  9. import os
  10. import sys
  11. import time
  12. import warnings
  13. import collections
  14. TESTS_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
  15. if os.name == 'nt':
  16. TESTS_DIR = TESTS_DIR.replace('\\', '\\\\')
  17. CODE_DIR = os.path.dirname(TESTS_DIR)
  18. # Let's inject CODE_DIR so salt is importable if not there already
  19. if '' in sys.path:
  20. sys.path.remove('')
  21. if TESTS_DIR in sys.path:
  22. sys.path.remove(TESTS_DIR)
  23. if CODE_DIR in sys.path and sys.path[0] != CODE_DIR:
  24. sys.path.remove(CODE_DIR)
  25. if CODE_DIR not in sys.path:
  26. sys.path.insert(0, CODE_DIR)
  27. if TESTS_DIR not in sys.path:
  28. sys.path.insert(1, TESTS_DIR)
  29. try:
  30. import tests
  31. if not tests.__file__.startswith(CODE_DIR):
  32. print('Found tests module not from salt in {}'.format(tests.__file__))
  33. sys.modules.pop('tests')
  34. module_dir = os.path.dirname(tests.__file__)
  35. if module_dir in sys.path:
  36. sys.path.remove(module_dir)
  37. del tests
  38. except ImportError:
  39. pass
  40. # Import salt libs
  41. from salt.ext import six
  42. try:
  43. from tests.support.paths import TMP, SYS_TMP_DIR, INTEGRATION_TEST_DIR
  44. from tests.support.paths import CODE_DIR as SALT_ROOT
  45. except ImportError as exc:
  46. try:
  47. import tests
  48. print('Found tests module not from salt in {}'.format(tests.__file__))
  49. except ImportError:
  50. print('Unable to import salt test module')
  51. print('PYTHONPATH:', os.environ.get('PYTHONPATH'))
  52. print('Current sys.path:')
  53. import pprint
  54. pprint.pprint(sys.path)
  55. six.reraise(*sys.exc_info())
  56. from tests.integration import TestDaemon # pylint: disable=W0403
  57. import salt.utils.platform
  58. if not salt.utils.platform.is_windows():
  59. import resource
  60. # Import Salt Testing libs
  61. from tests.support.parser import PNUM, print_header
  62. from tests.support.parser.cover import SaltCoverageTestingParser
  63. XML_OUTPUT_DIR = os.environ.get(
  64. 'SALT_XML_TEST_REPORTS_DIR',
  65. os.path.join(TMP, 'xml-test-reports')
  66. )
  67. HTML_OUTPUT_DIR = os.environ.get(
  68. 'SALT_HTML_TEST_REPORTS_DIR',
  69. os.path.join(TMP, 'html-test-reports')
  70. )
  71. TEST_DIR = os.path.dirname(INTEGRATION_TEST_DIR)
  72. try:
  73. if SALT_ROOT:
  74. os.chdir(SALT_ROOT)
  75. except OSError as err:
  76. print('Failed to change directory to salt\'s source: {0}'.format(err))
  77. # Soft and hard limits on max open filehandles
  78. MAX_OPEN_FILES = {
  79. 'integration': {
  80. 'soft_limit': 3072,
  81. 'hard_limit': 4096,
  82. },
  83. 'unit': {
  84. 'soft_limit': 1024,
  85. 'hard_limit': 2048,
  86. },
  87. }
  88. # Combine info from command line options and test suite directories. A test
  89. # suite is a python package of test modules relative to the tests directory.
  90. TEST_SUITES_UNORDERED = {
  91. 'unit':
  92. {'display_name': 'Unit',
  93. 'path': 'unit'},
  94. 'kitchen':
  95. {'display_name': 'Kitchen',
  96. 'path': 'kitchen'},
  97. 'module':
  98. {'display_name': 'Module',
  99. 'path': 'integration/modules'},
  100. 'state':
  101. {'display_name': 'State',
  102. 'path': 'integration/states'},
  103. 'cli':
  104. {'display_name': 'CLI',
  105. 'path': 'integration/cli'},
  106. 'client':
  107. {'display_name': 'Client',
  108. 'path': 'integration/client'},
  109. 'doc':
  110. {'display_name': 'Documentation',
  111. 'path': 'integration/doc'},
  112. 'ext_pillar':
  113. {'display_name': 'External Pillar',
  114. 'path': 'integration/pillar'},
  115. 'grains':
  116. {'display_name': 'Grains',
  117. 'path': 'integration/grains'},
  118. 'shell':
  119. {'display_name': 'Shell',
  120. 'path': 'integration/shell'},
  121. 'runners':
  122. {'display_name': 'Runners',
  123. 'path': 'integration/runners'},
  124. 'renderers':
  125. {'display_name': 'Renderers',
  126. 'path': 'integration/renderers'},
  127. 'returners':
  128. {'display_name': 'Returners',
  129. 'path': 'integration/returners'},
  130. 'ssh-int':
  131. {'display_name': 'SSH Integration',
  132. 'path': 'integration/ssh'},
  133. 'spm':
  134. {'display_name': 'SPM',
  135. 'path': 'integration/spm'},
  136. 'loader':
  137. {'display_name': 'Loader',
  138. 'path': 'integration/loader'},
  139. 'outputter':
  140. {'display_name': 'Outputter',
  141. 'path': 'integration/output'},
  142. 'fileserver':
  143. {'display_name': 'Fileserver',
  144. 'path': 'integration/fileserver'},
  145. 'wheel':
  146. {'display_name': 'Wheel',
  147. 'path': 'integration/wheel'},
  148. 'api':
  149. {'display_name': 'NetAPI',
  150. 'path': 'integration/netapi'},
  151. 'cloud_provider':
  152. {'display_name': 'Cloud Provider',
  153. 'path': 'integration/cloud/clouds'},
  154. 'minion':
  155. {'display_name': 'Minion',
  156. 'path': 'integration/minion'},
  157. 'reactor':
  158. {'display_name': 'Reactor',
  159. 'path': 'integration/reactor'},
  160. 'proxy':
  161. {'display_name': 'Proxy',
  162. 'path': 'integration/proxy'},
  163. 'external_api':
  164. {'display_name': 'ExternalAPIs',
  165. 'path': 'integration/externalapi'},
  166. 'daemons':
  167. {'display_name': 'Daemon',
  168. 'path': 'integration/daemons'},
  169. 'scheduler':
  170. {'display_name': 'Scheduler',
  171. 'path': 'integration/scheduler'},
  172. }
  173. TEST_SUITES = collections.OrderedDict(sorted(TEST_SUITES_UNORDERED.items(),
  174. key=lambda x: x[0]))
  175. class SaltTestsuiteParser(SaltCoverageTestingParser):
  176. support_docker_execution = True
  177. support_destructive_tests_selection = True
  178. source_code_basedir = SALT_ROOT
  179. def _get_suites(self, include_unit=False, include_cloud_provider=False,
  180. include_proxy=False, include_kitchen=False):
  181. '''
  182. Return a set of all test suites except unit and cloud provider tests
  183. unless requested
  184. '''
  185. suites = set(TEST_SUITES.keys())
  186. if not include_unit:
  187. suites -= set(['unit'])
  188. if not include_cloud_provider:
  189. suites -= set(['cloud_provider'])
  190. if not include_proxy:
  191. suites -= set(['proxy'])
  192. if not include_kitchen:
  193. suites -= set(['kitchen'])
  194. return suites
  195. def _check_enabled_suites(self, include_unit=False,
  196. include_cloud_provider=False,
  197. include_proxy=False,
  198. include_kitchen=False):
  199. '''
  200. Query whether test suites have been enabled
  201. '''
  202. suites = self._get_suites(include_unit=include_unit,
  203. include_cloud_provider=include_cloud_provider,
  204. include_proxy=include_proxy,
  205. include_kitchen=include_kitchen)
  206. return any([getattr(self.options, suite) for suite in suites])
  207. def _enable_suites(self, include_unit=False, include_cloud_provider=False,
  208. include_proxy=False, include_kitchen=False):
  209. '''
  210. Enable test suites for current test run
  211. '''
  212. suites = self._get_suites(include_unit=include_unit,
  213. include_cloud_provider=include_cloud_provider,
  214. include_proxy=include_proxy,
  215. include_kitchen=include_kitchen)
  216. for suite in suites:
  217. setattr(self.options, suite, True)
  218. def setup_additional_options(self):
  219. self.add_option(
  220. '--sysinfo',
  221. default=False,
  222. action='store_true',
  223. help='Print some system information.'
  224. )
  225. self.add_option(
  226. '--transport',
  227. default='zeromq',
  228. choices=('zeromq', 'raet', 'tcp'),
  229. help=('Select which transport to run the integration tests with, '
  230. 'zeromq, raet, or tcp. Default: %default')
  231. )
  232. self.add_option(
  233. '--interactive',
  234. default=False,
  235. action='store_true',
  236. help='Do not run any tests. Simply start the daemons.'
  237. )
  238. self.output_options_group.add_option(
  239. '--no-colors',
  240. '--no-colours',
  241. default=False,
  242. action='store_true',
  243. help='Disable colour printing.'
  244. )
  245. self.test_selection_group.add_option(
  246. '-m',
  247. '--module',
  248. '--module-tests',
  249. dest='module',
  250. default=False,
  251. action='store_true',
  252. help='Run tests for modules'
  253. )
  254. self.test_selection_group.add_option(
  255. '-S',
  256. '--state',
  257. '--state-tests',
  258. dest='state',
  259. default=False,
  260. action='store_true',
  261. help='Run tests for states'
  262. )
  263. self.test_selection_group.add_option(
  264. '-C',
  265. '--cli',
  266. '--cli-tests',
  267. dest='cli',
  268. default=False,
  269. action='store_true',
  270. help='Run tests for cli'
  271. )
  272. self.test_selection_group.add_option(
  273. '-c',
  274. '--client',
  275. '--client-tests',
  276. dest='client',
  277. default=False,
  278. action='store_true',
  279. help='Run tests for client'
  280. )
  281. self.test_selection_group.add_option(
  282. '-d',
  283. '--doc',
  284. '--doc-tests',
  285. dest='doc',
  286. default=False,
  287. action='store_true',
  288. help='Run tests for documentation'
  289. )
  290. self.test_selection_group.add_option(
  291. '-I',
  292. '--ext-pillar',
  293. '--ext-pillar-tests',
  294. dest='ext_pillar',
  295. default=False,
  296. action='store_true',
  297. help='Run ext_pillar tests'
  298. )
  299. self.test_selection_group.add_option(
  300. '-G',
  301. '--grains',
  302. '--grains-tests',
  303. dest='grains',
  304. default=False,
  305. action='store_true',
  306. help='Run tests for grains'
  307. )
  308. self.test_selection_group.add_option(
  309. '-s',
  310. '--shell',
  311. '--shell-tests',
  312. dest='shell',
  313. default=False,
  314. action='store_true',
  315. help='Run shell tests'
  316. )
  317. self.test_selection_group.add_option(
  318. '-r',
  319. '--runners',
  320. '--runner-tests',
  321. dest='runners',
  322. default=False,
  323. action='store_true',
  324. help='Run salt/runners/*.py tests'
  325. )
  326. self.test_selection_group.add_option(
  327. '-R',
  328. '--renderers',
  329. '--renderer-tests',
  330. dest='renderers',
  331. default=False,
  332. action='store_true',
  333. help='Run salt/renderers/*.py tests'
  334. )
  335. self.test_selection_group.add_option(
  336. '--reactor',
  337. dest='reactor',
  338. default=False,
  339. action='store_true',
  340. help='Run salt/reactor/*.py tests'
  341. )
  342. self.test_selection_group.add_option(
  343. '--minion',
  344. '--minion-tests',
  345. dest='minion',
  346. default=False,
  347. action='store_true',
  348. help='Run tests for minion'
  349. )
  350. self.test_selection_group.add_option(
  351. '--returners',
  352. dest='returners',
  353. default=False,
  354. action='store_true',
  355. help='Run salt/returners/*.py tests'
  356. )
  357. self.test_selection_group.add_option(
  358. '--spm',
  359. dest='spm',
  360. default=False,
  361. action='store_true',
  362. help='Run spm integration tests'
  363. )
  364. self.test_selection_group.add_option(
  365. '-l',
  366. '--loader',
  367. '--loader-tests',
  368. dest='loader',
  369. default=False,
  370. action='store_true',
  371. help='Run loader tests'
  372. )
  373. self.test_selection_group.add_option(
  374. '-u',
  375. '--unit',
  376. '--unit-tests',
  377. dest='unit',
  378. default=False,
  379. action='store_true',
  380. help='Run unit tests'
  381. )
  382. self.test_selection_group.add_option(
  383. '-k',
  384. '--kitchen',
  385. '--kitchen-tests',
  386. dest='kitchen',
  387. default=False,
  388. action='store_true',
  389. help='Run kitchen tests'
  390. )
  391. self.test_selection_group.add_option(
  392. '--fileserver',
  393. '--fileserver-tests',
  394. dest='fileserver',
  395. default=False,
  396. action='store_true',
  397. help='Run Fileserver tests'
  398. )
  399. self.test_selection_group.add_option(
  400. '-w',
  401. '--wheel',
  402. '--wheel-tests',
  403. dest='wheel',
  404. action='store_true',
  405. default=False,
  406. help='Run wheel tests'
  407. )
  408. self.test_selection_group.add_option(
  409. '-o',
  410. '--outputter',
  411. '--outputter-tests',
  412. dest='outputter',
  413. action='store_true',
  414. default=False,
  415. help='Run outputter tests'
  416. )
  417. self.test_selection_group.add_option(
  418. '--cloud-provider',
  419. '--cloud-provider-tests',
  420. dest='cloud_provider',
  421. action='store_true',
  422. default=False,
  423. help=('Run cloud provider tests. These tests create and delete '
  424. 'instances on cloud providers. Must provide valid credentials '
  425. 'in salt/tests/integration/files/conf/cloud.*.d to run tests.')
  426. )
  427. self.test_selection_group.add_option(
  428. '--ssh',
  429. '--ssh-tests',
  430. dest='ssh',
  431. action='store_true',
  432. default=False,
  433. help='Run salt-ssh tests. These tests will spin up a temporary '
  434. 'SSH server on your machine. In certain environments, this '
  435. 'may be insecure! Default: False'
  436. )
  437. self.test_selection_group.add_option(
  438. '--ssh-int',
  439. dest='ssh-int',
  440. action='store_true',
  441. default=False,
  442. help='Run salt-ssh integration tests. Requires to be run with --ssh'
  443. 'to spin up the SSH server on your machine.'
  444. )
  445. self.test_selection_group.add_option(
  446. '-A',
  447. '--api',
  448. '--api-tests',
  449. dest='api',
  450. action='store_true',
  451. default=False,
  452. help='Run salt-api tests'
  453. )
  454. self.test_selection_group.add_option(
  455. '-P',
  456. '--proxy',
  457. '--proxy-tests',
  458. dest='proxy',
  459. action='store_true',
  460. default=False,
  461. help='Run salt-proxy tests'
  462. )
  463. self.test_selection_group.add_option(
  464. '--external',
  465. '--external-api',
  466. '--external-api-tests',
  467. dest='external_api',
  468. action='store_true',
  469. default=False,
  470. help='Run venafi runner tests'
  471. )
  472. self.test_selection_group.add_option(
  473. '--daemons',
  474. '--daemon-tests',
  475. dest='daemons',
  476. action='store_true',
  477. default=False,
  478. help='Run salt/daemons/*.py tests'
  479. )
  480. self.test_selection_group.add_option(
  481. '--scheduler',
  482. dest='scheduler',
  483. action='store_true',
  484. default=False,
  485. help='Run scheduler integration tests'
  486. )
  487. def validate_options(self):
  488. if self.options.cloud_provider or self.options.external_api:
  489. # Turn on expensive tests execution
  490. os.environ['EXPENSIVE_TESTS'] = 'True'
  491. # This fails even with salt.utils.platform imported in the global
  492. # scope, unless we import it again here.
  493. import salt.utils.platform
  494. if salt.utils.platform.is_windows():
  495. import salt.utils.win_functions
  496. current_user = salt.utils.win_functions.get_current_user()
  497. if current_user == 'SYSTEM':
  498. is_admin = True
  499. else:
  500. is_admin = salt.utils.win_functions.is_admin(current_user)
  501. if self.options.coverage and any((
  502. self.options.name,
  503. not is_admin,
  504. not self.options.run_destructive)) \
  505. and self._check_enabled_suites(include_unit=True):
  506. warnings.warn("Test suite not running with elevated priviledges")
  507. else:
  508. is_admin = os.geteuid() == 0
  509. if self.options.coverage and any((
  510. self.options.name,
  511. not is_admin,
  512. not self.options.run_destructive)) \
  513. and self._check_enabled_suites(include_unit=True):
  514. self.error(
  515. 'No sense in generating the tests coverage report when '
  516. 'not running the full test suite, including the '
  517. 'destructive tests, as \'root\'. It would only produce '
  518. 'incorrect results.'
  519. )
  520. # When no tests are specifically enumerated on the command line, setup
  521. # a default run: +unit -cloud_provider
  522. if not self.options.name and not \
  523. self._check_enabled_suites(include_unit=True,
  524. include_cloud_provider=True,
  525. include_proxy=True,
  526. include_kitchen=True):
  527. self._enable_suites(include_unit=True)
  528. self.start_coverage(
  529. branch=True,
  530. source=[os.path.join(SALT_ROOT, 'salt')],
  531. )
  532. # Print out which version of python this test suite is running on
  533. print(' * Python Version: {0}'.format(' '.join(sys.version.split())))
  534. # Transplant configuration
  535. TestDaemon.transplant_configs(transport=self.options.transport)
  536. def post_execution_cleanup(self):
  537. SaltCoverageTestingParser.post_execution_cleanup(self)
  538. if self.options.clean:
  539. TestDaemon.clean()
  540. def run_integration_suite(self, path='', display_name=''):
  541. '''
  542. Run an integration test suite
  543. '''
  544. full_path = os.path.join(TEST_DIR, path)
  545. return self.run_suite(full_path, display_name, suffix='test_*.py')
  546. def start_daemons_only(self):
  547. if not salt.utils.platform.is_windows():
  548. self.set_filehandle_limits('integration')
  549. try:
  550. print_header(
  551. ' * Setting up Salt daemons for interactive use',
  552. top=False, width=getattr(self.options, 'output_columns', PNUM)
  553. )
  554. except TypeError:
  555. print_header(' * Setting up Salt daemons for interactive use', top=False)
  556. with TestDaemon(self):
  557. print_header(' * Salt daemons started')
  558. master_conf = TestDaemon.config('master')
  559. minion_conf = TestDaemon.config('minion')
  560. proxy_conf = TestDaemon.config('proxy')
  561. sub_minion_conf = TestDaemon.config('sub_minion')
  562. syndic_conf = TestDaemon.config('syndic')
  563. syndic_master_conf = TestDaemon.config('syndic_master')
  564. print_header(' * Syndic master configuration values (MoM)', top=False)
  565. print('interface: {0}'.format(syndic_master_conf['interface']))
  566. print('publish port: {0}'.format(syndic_master_conf['publish_port']))
  567. print('return port: {0}'.format(syndic_master_conf['ret_port']))
  568. print('\n')
  569. print_header(' * Syndic configuration values', top=True)
  570. print('interface: {0}'.format(syndic_conf['interface']))
  571. print('syndic master: {0}'.format(syndic_conf['syndic_master']))
  572. print('syndic master port: {0}'.format(syndic_conf['syndic_master_port']))
  573. print('\n')
  574. print_header(' * Master configuration values', top=True)
  575. print('interface: {0}'.format(master_conf['interface']))
  576. print('publish port: {0}'.format(master_conf['publish_port']))
  577. print('return port: {0}'.format(master_conf['ret_port']))
  578. print('\n')
  579. print_header(' * Minion configuration values', top=True)
  580. print('interface: {0}'.format(minion_conf['interface']))
  581. print('master: {0}'.format(minion_conf['master']))
  582. print('master port: {0}'.format(minion_conf['master_port']))
  583. if minion_conf['ipc_mode'] == 'tcp':
  584. print('tcp pub port: {0}'.format(minion_conf['tcp_pub_port']))
  585. print('tcp pull port: {0}'.format(minion_conf['tcp_pull_port']))
  586. print('\n')
  587. print_header(' * Sub Minion configuration values', top=True)
  588. print('interface: {0}'.format(sub_minion_conf['interface']))
  589. print('master: {0}'.format(sub_minion_conf['master']))
  590. print('master port: {0}'.format(sub_minion_conf['master_port']))
  591. if sub_minion_conf['ipc_mode'] == 'tcp':
  592. print('tcp pub port: {0}'.format(sub_minion_conf['tcp_pub_port']))
  593. print('tcp pull port: {0}'.format(sub_minion_conf['tcp_pull_port']))
  594. print('\n')
  595. print_header(' * Proxy Minion configuration values', top=True)
  596. print('interface: {0}'.format(proxy_conf['interface']))
  597. print('master: {0}'.format(proxy_conf['master']))
  598. print('master port: {0}'.format(proxy_conf['master_port']))
  599. if minion_conf['ipc_mode'] == 'tcp':
  600. print('tcp pub port: {0}'.format(proxy_conf['tcp_pub_port']))
  601. print('tcp pull port: {0}'.format(proxy_conf['tcp_pull_port']))
  602. print('\n')
  603. print_header(' Your client configuration is at {0}'.format(TestDaemon.config_location()))
  604. print('To access the minion: salt -c {0} minion test.ping'.format(TestDaemon.config_location()))
  605. while True:
  606. time.sleep(1)
  607. def set_filehandle_limits(self, limits='integration'):
  608. '''
  609. Set soft and hard limits on open file handles at required thresholds
  610. for integration tests or unit tests
  611. '''
  612. # Get current limits
  613. if salt.utils.platform.is_windows():
  614. import win32file
  615. prev_hard = win32file._getmaxstdio()
  616. prev_soft = 512
  617. else:
  618. prev_soft, prev_hard = resource.getrlimit(resource.RLIMIT_NOFILE)
  619. # Get required limits
  620. min_soft = MAX_OPEN_FILES[limits]['soft_limit']
  621. min_hard = MAX_OPEN_FILES[limits]['hard_limit']
  622. # Check minimum required limits
  623. set_limits = False
  624. if prev_soft < min_soft:
  625. soft = min_soft
  626. set_limits = True
  627. else:
  628. soft = prev_soft
  629. if prev_hard < min_hard:
  630. hard = min_hard
  631. set_limits = True
  632. else:
  633. hard = prev_hard
  634. # Increase limits
  635. if set_limits:
  636. print(
  637. ' * Max open files settings is too low (soft: {0}, hard: {1}) '
  638. 'for running the tests'.format(prev_soft, prev_hard)
  639. )
  640. print(
  641. ' * Trying to raise the limits to soft: '
  642. '{0}, hard: {1}'.format(soft, hard)
  643. )
  644. try:
  645. if salt.utils.platform.is_windows():
  646. hard = 2048 if hard > 2048 else hard
  647. win32file._setmaxstdio(hard)
  648. else:
  649. resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard))
  650. except Exception as err:
  651. print(
  652. 'ERROR: Failed to raise the max open files settings -> '
  653. '{0}'.format(err)
  654. )
  655. print('Please issue the following command on your console:')
  656. print(' ulimit -n {0}'.format(soft))
  657. self.exit()
  658. finally:
  659. print('~' * getattr(self.options, 'output_columns', PNUM))
  660. def run_integration_tests(self):
  661. '''
  662. Execute the integration tests suite
  663. '''
  664. named_tests = []
  665. named_unit_test = []
  666. if self.options.name:
  667. for test in self.options.name:
  668. if test.startswith(('tests.unit.', 'unit.', 'test.kitchen.', 'kitchen.')):
  669. named_unit_test.append(test)
  670. continue
  671. named_tests.append(test)
  672. if (self.options.unit or self.options.kitchen or named_unit_test) \
  673. and not named_tests \
  674. and (self.options.from_filenames or
  675. not self._check_enabled_suites(include_cloud_provider=True)):
  676. # We're either not running any integration test suites, or we're
  677. # only running unit tests by passing --unit or by passing only
  678. # `unit.<whatever>` to --name. We don't need the tests daemon
  679. # running
  680. return [True]
  681. if not salt.utils.platform.is_windows():
  682. self.set_filehandle_limits('integration')
  683. try:
  684. print_header(
  685. ' * Setting up Salt daemons to execute tests',
  686. top=False, width=getattr(self.options, 'output_columns', PNUM)
  687. )
  688. except TypeError:
  689. print_header(' * Setting up Salt daemons to execute tests', top=False)
  690. status = []
  691. # Return an empty status if no tests have been enabled
  692. if not self._check_enabled_suites(include_cloud_provider=True, include_proxy=True) and not self.options.name:
  693. return status
  694. with TestDaemon(self):
  695. if self.options.name:
  696. for name in self.options.name:
  697. name = name.strip()
  698. if not name:
  699. continue
  700. if os.path.isfile(name):
  701. if not name.endswith('.py'):
  702. continue
  703. if name.startswith(os.path.join('tests', 'unit')):
  704. continue
  705. results = self.run_suite(os.path.dirname(name),
  706. name,
  707. suffix=os.path.basename(name),
  708. load_from_name=False)
  709. status.append(results)
  710. continue
  711. if name.startswith(('tests.unit.', 'unit.')):
  712. continue
  713. results = self.run_suite('', name, suffix='test_*.py', load_from_name=True)
  714. status.append(results)
  715. return status
  716. for suite in TEST_SUITES:
  717. if suite != 'unit' and getattr(self.options, suite):
  718. status.append(self.run_integration_suite(**TEST_SUITES[suite]))
  719. return status
  720. def run_unit_tests(self):
  721. '''
  722. Execute the unit tests
  723. '''
  724. named_unit_test = []
  725. if self.options.name:
  726. for test in self.options.name:
  727. if not test.startswith(('tests.unit.', 'unit.')):
  728. continue
  729. named_unit_test.append(test)
  730. if not named_unit_test \
  731. and (self.options.from_filenames or not self.options.unit):
  732. # We are not explicitly running the unit tests and none of the
  733. # names passed to --name (or derived via --from-filenames) is a
  734. # unit test.
  735. return [True]
  736. status = []
  737. if self.options.unit:
  738. # MacOS needs more open filehandles for running unit test suite
  739. self.set_filehandle_limits('unit')
  740. results = self.run_suite(
  741. os.path.join(TEST_DIR, 'unit'), 'Unit', suffix='test_*.py'
  742. )
  743. status.append(results)
  744. # We executed ALL unittests, we can skip running unittests by name
  745. # below
  746. return status
  747. for name in named_unit_test:
  748. results = self.run_suite(
  749. os.path.join(TEST_DIR, 'unit'), name, suffix='test_*.py', load_from_name=True
  750. )
  751. status.append(results)
  752. return status
  753. def run_kitchen_tests(self):
  754. '''
  755. Execute the kitchen tests
  756. '''
  757. named_kitchen_test = []
  758. if self.options.name:
  759. for test in self.options.name:
  760. if not test.startswith(('tests.kitchen.', 'kitchen.')):
  761. continue
  762. named_kitchen_test.append(test)
  763. if not self.options.kitchen and not named_kitchen_test:
  764. # We are not explicitly running the unit tests and none of the
  765. # names passed to --name is a unit test.
  766. return [True]
  767. status = []
  768. if self.options.kitchen:
  769. results = self.run_suite(
  770. os.path.join(TEST_DIR, 'kitchen'), 'Kitchen', suffix='test_*.py'
  771. )
  772. status.append(results)
  773. # We executed ALL unittests, we can skip running unittests by name
  774. # below
  775. return status
  776. for name in named_kitchen_test:
  777. results = self.run_suite(
  778. os.path.join(TEST_DIR, 'kitchen'), name, suffix='test_*.py', load_from_name=True
  779. )
  780. status.append(results)
  781. return status
  782. def main(**kwargs):
  783. '''
  784. Parse command line options for running specific tests
  785. '''
  786. try:
  787. parser = SaltTestsuiteParser(
  788. TEST_DIR,
  789. xml_output_dir=XML_OUTPUT_DIR,
  790. tests_logfile=os.path.join(SYS_TMP_DIR, 'salt-runtests.log')
  791. )
  792. parser.parse_args()
  793. # Override parser options (helpful when importing runtests.py and
  794. # running from within a REPL). Using kwargs.items() to avoid importing
  795. # six, as this feature will rarely be used.
  796. for key, val in kwargs.items():
  797. setattr(parser.options, key, val)
  798. overall_status = []
  799. if parser.options.interactive:
  800. parser.start_daemons_only()
  801. status = parser.run_integration_tests()
  802. overall_status.extend(status)
  803. status = parser.run_unit_tests()
  804. overall_status.extend(status)
  805. status = parser.run_kitchen_tests()
  806. overall_status.extend(status)
  807. false_count = overall_status.count(False)
  808. if false_count > 0:
  809. parser.finalize(1)
  810. parser.finalize(0)
  811. except KeyboardInterrupt:
  812. print('\nCaught keyboard interrupt. Exiting.\n')
  813. exit(0)
  814. if __name__ == '__main__':
  815. main()