runtests.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. .. _runtime_vars:
  5. Runtime Variables
  6. -----------------
  7. :command:`salt-runtests` provides a variable, :py:attr:`RUNTIME_VARS` which has some common paths defined at
  8. startup:
  9. .. autoattribute:: tests.support.runtests.RUNTIME_VARS
  10. :annotation:
  11. :TMP: Tests suite temporary directory
  12. :TMP_CONF_DIR: Configuration directory from where the daemons that :command:`salt-runtests` starts get their
  13. configuration files.
  14. :TMP_CONF_MASTER_INCLUDES: Salt Master configuration files includes directory. See
  15. :salt_conf_master:`default_include`.
  16. :TMP_CONF_MINION_INCLUDES: Salt Minion configuration files includes directory. Seei
  17. :salt_conf_minion:`include`.
  18. :TMP_CONF_CLOUD_INCLUDES: Salt cloud configuration files includes directory. The same as the salt master and
  19. minion includes configuration, though under a different directory name.
  20. :TMP_CONF_CLOUD_PROFILE_INCLUDES: Salt cloud profiles configuration files includes directory. Same as above.
  21. :TMP_CONF_CLOUD_PROVIDER_INCLUDES: Salt cloud providers configuration files includes directory. Same as above.
  22. :TMP_SCRIPT_DIR: Temporary scripts directory from where the Salt CLI tools will be called when running tests.
  23. :TMP_SALT_INTEGRATION_FILES: Temporary directory from where Salt's test suite integration files are copied to.
  24. :TMP_BASEENV_STATE_TREE: Salt master's **base** environment state tree directory
  25. :TMP_PRODENV_STATE_TREE: Salt master's **production** environment state tree directory
  26. :TMP_BASEENV_PILLAR_TREE: Salt master's **base** environment pillar tree directory
  27. :TMP_PRODENV_PILLAR_TREE: Salt master's **production** environment pillar tree directory
  28. Use it on your test case in case of need. As simple as:
  29. .. code-block:: python
  30. import os
  31. from tests.support.runtests import RUNTIME_VARS
  32. # Path to the testing minion configuration file
  33. minion_config_path = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion')
  34. .. _`pytest`: http://pytest.org
  35. .. _`nose`: https://nose.readthedocs.org
  36. '''
  37. # Import Python modules
  38. from __future__ import absolute_import, print_function
  39. import os
  40. import shutil
  41. import logging
  42. import multiprocessing
  43. import salt.utils.json
  44. import salt.utils.path
  45. # Import tests support libs
  46. import tests.support.paths as paths
  47. import tests.support.helpers
  48. # Import 3rd-party libs
  49. from salt.ext import six
  50. try:
  51. import coverage # pylint: disable=import-error
  52. HAS_COVERAGE = True
  53. except ImportError:
  54. HAS_COVERAGE = False
  55. try:
  56. import multiprocessing.util
  57. # Force forked multiprocessing processes to be measured as well
  58. def multiprocessing_stop(coverage_object):
  59. '''
  60. Save the multiprocessing process coverage object
  61. '''
  62. coverage_object.stop()
  63. coverage_object.save()
  64. def multiprocessing_start(obj):
  65. coverage_options = salt.utils.json.loads(os.environ.get('SALT_RUNTESTS_COVERAGE_OPTIONS', '{}'))
  66. if not coverage_options:
  67. return
  68. if coverage_options.get('data_suffix', False) is False:
  69. return
  70. coverage_object = coverage.coverage(**coverage_options)
  71. coverage_object.start()
  72. multiprocessing.util.Finalize(
  73. None,
  74. multiprocessing_stop,
  75. args=(coverage_object,),
  76. exitpriority=1000
  77. )
  78. if HAS_COVERAGE:
  79. multiprocessing.util.register_after_fork(
  80. multiprocessing_start,
  81. multiprocessing_start
  82. )
  83. except ImportError:
  84. pass
  85. RUNNING_TESTS_USER = tests.support.helpers.this_user()
  86. log = logging.getLogger(__name__)
  87. class RootsDict(dict):
  88. def merge(self, data):
  89. for key, values in six.iteritems(data):
  90. if key not in self:
  91. self[key] = values
  92. continue
  93. for value in values:
  94. if value not in self[key]:
  95. self[key].append(value)
  96. return self
  97. def to_dict(self):
  98. return dict(self)
  99. def recursive_copytree(source, destination, overwrite=False):
  100. for root, dirs, files in os.walk(source):
  101. for item in dirs:
  102. src_path = os.path.join(root, item)
  103. dst_path = os.path.join(destination, src_path.replace(source, '').lstrip(os.sep))
  104. if not os.path.exists(dst_path):
  105. log.debug('Creating directory: %s', dst_path)
  106. os.makedirs(dst_path)
  107. for item in files:
  108. src_path = os.path.join(root, item)
  109. dst_path = os.path.join(destination, src_path.replace(source, '').lstrip(os.sep))
  110. if os.path.exists(dst_path) and not overwrite:
  111. if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
  112. log.debug('Copying %s to %s', src_path, dst_path)
  113. shutil.copy2(src_path, dst_path)
  114. else:
  115. if not os.path.isdir(os.path.dirname(dst_path)):
  116. log.debug('Creating directory: %s', os.path.dirname(dst_path))
  117. os.makedirs(os.path.dirname(dst_path))
  118. log.debug('Copying %s to %s', src_path, dst_path)
  119. shutil.copy2(src_path, dst_path)
  120. class RuntimeVars(object):
  121. __self_attributes__ = ('_vars', '_locked', 'lock')
  122. def __init__(self, **kwargs):
  123. self._vars = kwargs
  124. self._locked = False
  125. def lock(self):
  126. # Late import
  127. from salt.utils.immutabletypes import freeze
  128. frozen_vars = freeze(self._vars.copy())
  129. self._vars = frozen_vars
  130. self._locked = True
  131. def __iter__(self):
  132. for name, value in six.iteritems(self._vars):
  133. yield name, value
  134. def __getattribute__(self, name):
  135. if name in object.__getattribute__(self, '_vars'):
  136. return object.__getattribute__(self, '_vars')[name]
  137. return object.__getattribute__(self, name)
  138. def __setattr__(self, name, value):
  139. if getattr(self, '_locked', False) is True:
  140. raise RuntimeError(
  141. 'After {0} is locked, no additional data can be added to it'.format(
  142. self.__class__.__name__
  143. )
  144. )
  145. if name in object.__getattribute__(self, '__self_attributes__'):
  146. object.__setattr__(self, name, value)
  147. return
  148. self._vars[name] = value
  149. # <---- Helper Methods -----------------------------------------------------------------------------------------------
  150. # ----- Global Variables -------------------------------------------------------------------------------------------->
  151. XML_OUTPUT_DIR = os.environ.get('SALT_XML_TEST_REPORTS_DIR', os.path.join(paths.TMP, 'xml-test-reports'))
  152. # <---- Global Variables ---------------------------------------------------------------------------------------------
  153. # ----- Tests Runtime Variables ------------------------------------------------------------------------------------->
  154. RUNTIME_VARS = RuntimeVars(
  155. TMP=paths.TMP,
  156. SYS_TMP_DIR=paths.SYS_TMP_DIR,
  157. FILES=paths.FILES,
  158. CONF_DIR=paths.CONF_DIR,
  159. PILLAR_DIR=paths.PILLAR_DIR,
  160. ENGINES_DIR=paths.ENGINES_DIR,
  161. LOG_HANDLERS_DIR=paths.LOG_HANDLERS_DIR,
  162. TMP_ROOT_DIR=paths.TMP_ROOT_DIR,
  163. TMP_CONF_DIR=paths.TMP_CONF_DIR,
  164. TMP_CONF_MASTER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'master.d'),
  165. TMP_CONF_MINION_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'minion.d'),
  166. TMP_CONF_PROXY_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'proxy.d'),
  167. TMP_CONF_CLOUD_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.conf.d'),
  168. TMP_CONF_CLOUD_PROFILE_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.profiles.d'),
  169. TMP_CONF_CLOUD_PROVIDER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.providers.d'),
  170. TMP_SUB_MINION_CONF_DIR=paths.TMP_SUB_MINION_CONF_DIR,
  171. TMP_SYNDIC_MASTER_CONF_DIR=paths.TMP_SYNDIC_MASTER_CONF_DIR,
  172. TMP_SYNDIC_MINION_CONF_DIR=paths.TMP_SYNDIC_MINION_CONF_DIR,
  173. TMP_SCRIPT_DIR=paths.TMP_SCRIPT_DIR,
  174. TMP_STATE_TREE=paths.TMP_STATE_TREE,
  175. TMP_PILLAR_TREE=paths.TMP_PILLAR_TREE,
  176. TMP_PRODENV_STATE_TREE=paths.TMP_PRODENV_STATE_TREE,
  177. RUNNING_TESTS_USER=RUNNING_TESTS_USER,
  178. RUNTIME_CONFIGS={},
  179. CODE_DIR=paths.CODE_DIR,
  180. BASE_FILES=paths.BASE_FILES,
  181. PROD_FILES=paths.PROD_FILES,
  182. TESTS_DIR=paths.TESTS_DIR,
  183. PYTEST_SESSION=False,
  184. SHELL_TRUE_PATH=salt.utils.path.which('true'),
  185. SHELL_FALSE_PATH=salt.utils.path.which('false'),
  186. )
  187. # <---- Tests Runtime Variables --------------------------------------------------------------------------------------