runtests.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. from __future__ import absolute_import, print_function
  38. import logging
  39. import os
  40. import shutil
  41. import salt.utils.path
  42. import salt.utils.platform
  43. import tests.support.paths as paths
  44. from salt.ext import six
  45. try:
  46. import pwd
  47. except ImportError:
  48. import salt.utils.win_functions
  49. log = logging.getLogger(__name__)
  50. def this_user():
  51. """
  52. Get the user associated with the current process.
  53. """
  54. if salt.utils.platform.is_windows():
  55. return salt.utils.win_functions.get_current_user(with_domain=False)
  56. return pwd.getpwuid(os.getuid())[0]
  57. class RootsDict(dict):
  58. def merge(self, data):
  59. for key, values in six.iteritems(data):
  60. if key not in self:
  61. self[key] = values
  62. continue
  63. for value in values:
  64. if value not in self[key]:
  65. self[key].append(value)
  66. return self
  67. def to_dict(self):
  68. return dict(self)
  69. def recursive_copytree(source, destination, overwrite=False):
  70. for root, dirs, files in os.walk(source):
  71. for item in dirs:
  72. src_path = os.path.join(root, item)
  73. dst_path = os.path.join(
  74. destination, src_path.replace(source, "").lstrip(os.sep)
  75. )
  76. if not os.path.exists(dst_path):
  77. log.debug("Creating directory: %s", dst_path)
  78. os.makedirs(dst_path)
  79. for item in files:
  80. src_path = os.path.join(root, item)
  81. dst_path = os.path.join(
  82. destination, src_path.replace(source, "").lstrip(os.sep)
  83. )
  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(
  126. "SALT_XML_TEST_REPORTS_DIR", os.path.join(paths.TMP, "xml-test-reports")
  127. )
  128. # <---- Global Variables ---------------------------------------------------------------------------------------------
  129. # ----- Tests Runtime Variables ------------------------------------------------------------------------------------->
  130. RUNTIME_VARS = RuntimeVars(
  131. TMP=paths.TMP,
  132. SYS_TMP_DIR=paths.SYS_TMP_DIR,
  133. FILES=paths.FILES,
  134. CONF_DIR=paths.CONF_DIR,
  135. PILLAR_DIR=paths.PILLAR_DIR,
  136. ENGINES_DIR=paths.ENGINES_DIR,
  137. LOG_HANDLERS_DIR=paths.LOG_HANDLERS_DIR,
  138. TMP_ROOT_DIR=paths.TMP_ROOT_DIR,
  139. TMP_CONF_DIR=paths.TMP_CONF_DIR,
  140. TMP_MINION_CONF_DIR=paths.TMP_MINION_CONF_DIR,
  141. TMP_CONF_MASTER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, "master.d"),
  142. TMP_CONF_MINION_INCLUDES=os.path.join(paths.TMP_CONF_DIR, "minion.d"),
  143. TMP_CONF_PROXY_INCLUDES=os.path.join(paths.TMP_CONF_DIR, "proxy.d"),
  144. TMP_CONF_CLOUD_INCLUDES=os.path.join(paths.TMP_CONF_DIR, "cloud.conf.d"),
  145. TMP_CONF_CLOUD_PROFILE_INCLUDES=os.path.join(
  146. paths.TMP_CONF_DIR, "cloud.profiles.d"
  147. ),
  148. TMP_CONF_CLOUD_PROVIDER_INCLUDES=os.path.join(
  149. paths.TMP_CONF_DIR, "cloud.providers.d"
  150. ),
  151. TMP_SUB_MINION_CONF_DIR=paths.TMP_SUB_MINION_CONF_DIR,
  152. TMP_SYNDIC_MASTER_CONF_DIR=paths.TMP_SYNDIC_MASTER_CONF_DIR,
  153. TMP_SYNDIC_MINION_CONF_DIR=paths.TMP_SYNDIC_MINION_CONF_DIR,
  154. TMP_PROXY_CONF_DIR=paths.TMP_PROXY_CONF_DIR,
  155. TMP_MM_CONF_DIR=paths.TMP_MM_CONF_DIR,
  156. TMP_MM_MINION_CONF_DIR=paths.TMP_MM_MINION_CONF_DIR,
  157. TMP_MM_SUB_CONF_DIR=paths.TMP_MM_SUB_CONF_DIR,
  158. TMP_MM_SUB_MINION_CONF_DIR=paths.TMP_MM_SUB_CONF_DIR,
  159. TMP_SCRIPT_DIR=paths.TMP_SCRIPT_DIR,
  160. TMP_STATE_TREE=paths.TMP_STATE_TREE,
  161. TMP_BASEENV_STATE_TREE=paths.TMP_STATE_TREE,
  162. TMP_PILLAR_TREE=paths.TMP_PILLAR_TREE,
  163. TMP_BASEENV_PILLAR_TREE=paths.TMP_PILLAR_TREE,
  164. TMP_PRODENV_STATE_TREE=paths.TMP_PRODENV_STATE_TREE,
  165. TMP_PRODENV_PILLAR_TREE=paths.TMP_PRODENV_PILLAR_TREE,
  166. SHELL_TRUE_PATH=salt.utils.path.which("true")
  167. if not salt.utils.platform.is_windows()
  168. else "cmd /c exit 0 > nul",
  169. SHELL_FALSE_PATH=salt.utils.path.which("false")
  170. if not salt.utils.platform.is_windows()
  171. else "cmd /c exit 1 > nul",
  172. RUNNING_TESTS_USER=this_user(),
  173. RUNTIME_CONFIGS={},
  174. CODE_DIR=paths.CODE_DIR,
  175. BASE_FILES=paths.BASE_FILES,
  176. PROD_FILES=paths.PROD_FILES,
  177. TESTS_DIR=paths.TESTS_DIR,
  178. PYTEST_SESSION=False,
  179. )
  180. # <---- Tests Runtime Variables --------------------------------------------------------------------------------------