runtests.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 Salt libs
  43. import salt.utils.path
  44. import salt.utils.platform
  45. try:
  46. import pwd
  47. except ImportError:
  48. import salt.utils.win_functions
  49. # Import tests support libs
  50. import tests.support.paths as paths
  51. # Import 3rd-party libs
  52. from salt.ext import six
  53. log = logging.getLogger(__name__)
  54. def this_user():
  55. '''
  56. Get the user associated with the current process.
  57. '''
  58. if salt.utils.platform.is_windows():
  59. return salt.utils.win_functions.get_current_user(with_domain=False)
  60. return pwd.getpwuid(os.getuid())[0]
  61. class RootsDict(dict):
  62. def merge(self, data):
  63. for key, values in six.iteritems(data):
  64. if key not in self:
  65. self[key] = values
  66. continue
  67. for value in values:
  68. if value not in self[key]:
  69. self[key].append(value)
  70. return self
  71. def to_dict(self):
  72. return dict(self)
  73. def recursive_copytree(source, destination, overwrite=False):
  74. for root, dirs, files in os.walk(source):
  75. for item in dirs:
  76. src_path = os.path.join(root, item)
  77. dst_path = os.path.join(destination, src_path.replace(source, '').lstrip(os.sep))
  78. if not os.path.exists(dst_path):
  79. log.debug('Creating directory: %s', dst_path)
  80. os.makedirs(dst_path)
  81. for item in files:
  82. src_path = os.path.join(root, item)
  83. dst_path = os.path.join(destination, src_path.replace(source, '').lstrip(os.sep))
  84. if os.path.exists(dst_path) and not overwrite:
  85. if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
  86. log.debug('Copying %s to %s', src_path, dst_path)
  87. shutil.copy2(src_path, dst_path)
  88. else:
  89. if not os.path.isdir(os.path.dirname(dst_path)):
  90. log.debug('Creating directory: %s', os.path.dirname(dst_path))
  91. os.makedirs(os.path.dirname(dst_path))
  92. log.debug('Copying %s to %s', src_path, dst_path)
  93. shutil.copy2(src_path, dst_path)
  94. class RuntimeVars(object):
  95. __self_attributes__ = ('_vars', '_locked', 'lock')
  96. def __init__(self, **kwargs):
  97. self._vars = kwargs
  98. self._locked = False
  99. def lock(self):
  100. # Late import
  101. from salt.utils.immutabletypes import freeze
  102. frozen_vars = freeze(self._vars.copy())
  103. self._vars = frozen_vars
  104. self._locked = True
  105. def __iter__(self):
  106. for name, value in six.iteritems(self._vars):
  107. yield name, value
  108. def __getattribute__(self, name):
  109. if name in object.__getattribute__(self, '_vars'):
  110. return object.__getattribute__(self, '_vars')[name]
  111. return object.__getattribute__(self, name)
  112. def __setattr__(self, name, value):
  113. if getattr(self, '_locked', False) is True:
  114. raise RuntimeError(
  115. 'After {0} is locked, no additional data can be added to it'.format(
  116. self.__class__.__name__
  117. )
  118. )
  119. if name in object.__getattribute__(self, '__self_attributes__'):
  120. object.__setattr__(self, name, value)
  121. return
  122. self._vars[name] = value
  123. # <---- Helper Methods -----------------------------------------------------------------------------------------------
  124. # ----- Global Variables -------------------------------------------------------------------------------------------->
  125. XML_OUTPUT_DIR = os.environ.get('SALT_XML_TEST_REPORTS_DIR', os.path.join(paths.TMP, 'xml-test-reports'))
  126. # <---- Global Variables ---------------------------------------------------------------------------------------------
  127. # ----- Tests Runtime Variables ------------------------------------------------------------------------------------->
  128. RUNTIME_VARS = RuntimeVars(
  129. TMP=paths.TMP,
  130. SYS_TMP_DIR=paths.SYS_TMP_DIR,
  131. FILES=paths.FILES,
  132. CONF_DIR=paths.CONF_DIR,
  133. PILLAR_DIR=paths.PILLAR_DIR,
  134. ENGINES_DIR=paths.ENGINES_DIR,
  135. LOG_HANDLERS_DIR=paths.LOG_HANDLERS_DIR,
  136. TMP_ROOT_DIR=paths.TMP_ROOT_DIR,
  137. TMP_CONF_DIR=paths.TMP_CONF_DIR,
  138. TMP_CONF_MASTER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'master.d'),
  139. TMP_CONF_MINION_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'minion.d'),
  140. TMP_CONF_PROXY_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'proxy.d'),
  141. TMP_CONF_CLOUD_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.conf.d'),
  142. TMP_CONF_CLOUD_PROFILE_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.profiles.d'),
  143. TMP_CONF_CLOUD_PROVIDER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.providers.d'),
  144. TMP_SUB_MINION_CONF_DIR=paths.TMP_SUB_MINION_CONF_DIR,
  145. TMP_SYNDIC_MASTER_CONF_DIR=paths.TMP_SYNDIC_MASTER_CONF_DIR,
  146. TMP_SYNDIC_MINION_CONF_DIR=paths.TMP_SYNDIC_MINION_CONF_DIR,
  147. TMP_MM_CONF_DIR=paths.TMP_MM_CONF_DIR,
  148. TMP_MM_SUB_CONF_DIR=paths.TMP_MM_SUB_CONF_DIR,
  149. TMP_SCRIPT_DIR=paths.TMP_SCRIPT_DIR,
  150. TMP_STATE_TREE=paths.TMP_STATE_TREE,
  151. TMP_PILLAR_TREE=paths.TMP_PILLAR_TREE,
  152. TMP_PRODENV_STATE_TREE=paths.TMP_PRODENV_STATE_TREE,
  153. SHELL_TRUE_PATH=salt.utils.path.which('true') if not salt.utils.platform.is_windows() else 'cmd /c exit 0 > nul',
  154. SHELL_FALSE_PATH=salt.utils.path.which('false') if not salt.utils.platform.is_windows() else 'cmd /c exit 1 > nul',
  155. RUNNING_TESTS_USER=this_user(),
  156. RUNTIME_CONFIGS={},
  157. CODE_DIR=paths.CODE_DIR,
  158. BASE_FILES=paths.BASE_FILES,
  159. PROD_FILES=paths.PROD_FILES,
  160. TESTS_DIR=paths.TESTS_DIR,
  161. PYTEST_SESSION=False
  162. )
  163. # <---- Tests Runtime Variables --------------------------------------------------------------------------------------