runtests.py 8.2 KB

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