runtime.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. .. _runtime_vars:
  5. Runtime Variables
  6. -----------------
  7. This module provides a variable, :py:attr:`RUNTIME_VARS` which has some common paths defined at
  8. startup:
  9. .. autoattribute:: tests.support.runtime.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. See
  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.runtime 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. '''
  36. # Import Python modules
  37. from __future__ import absolute_import, print_function
  38. import os
  39. import shutil
  40. import logging
  41. import salt.utils.json
  42. import salt.utils.platform
  43. try:
  44. import pwd
  45. except ImportError:
  46. import salt.utils.win_functions
  47. # Import tests support libs
  48. import tests.support.paths as paths
  49. # Import 3rd-party libs
  50. from salt.ext import six
  51. log = logging.getLogger(__name__)
  52. def this_user():
  53. '''
  54. Get the user associated with the current process.
  55. '''
  56. if salt.utils.platform.is_windows():
  57. return salt.utils.win_functions.get_current_user(with_domain=False)
  58. return pwd.getpwuid(os.getuid())[0]
  59. class RootsDict(dict):
  60. def merge(self, data):
  61. for key, values in six.iteritems(data):
  62. if key not in self:
  63. self[key] = values
  64. continue
  65. for value in values:
  66. if value not in self[key]:
  67. self[key].append(value)
  68. return self
  69. def to_dict(self):
  70. return dict(self)
  71. def recursive_copytree(source, destination, overwrite=False):
  72. for root, dirs, files in os.walk(source):
  73. for item in dirs:
  74. src_path = os.path.join(root, item)
  75. dst_path = os.path.join(destination, src_path.replace(source, '').lstrip(os.sep))
  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(destination, src_path.replace(source, '').lstrip(os.sep))
  82. if os.path.exists(dst_path) and not overwrite:
  83. if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
  84. log.debug('Copying %s to %s', src_path, dst_path)
  85. shutil.copy2(src_path, dst_path)
  86. else:
  87. if not os.path.isdir(os.path.dirname(dst_path)):
  88. log.debug('Creating directory: %s', os.path.dirname(dst_path))
  89. os.makedirs(os.path.dirname(dst_path))
  90. log.debug('Copying %s to %s', src_path, dst_path)
  91. shutil.copy2(src_path, dst_path)
  92. class RuntimeVars(object):
  93. __self_attributes__ = ('_vars', '_locked', 'lock')
  94. def __init__(self, **kwargs):
  95. self._vars = kwargs
  96. self._locked = False
  97. def lock(self):
  98. # Late import
  99. from salt.utils.immutabletypes import freeze
  100. frozen_vars = freeze(self._vars.copy())
  101. self._vars = frozen_vars
  102. self._locked = True
  103. def __iter__(self):
  104. for name, value in six.iteritems(self._vars):
  105. yield name, value
  106. def __getattribute__(self, name):
  107. if name in object.__getattribute__(self, '_vars'):
  108. return object.__getattribute__(self, '_vars')[name]
  109. return object.__getattribute__(self, name)
  110. def __setattr__(self, name, value):
  111. if getattr(self, '_locked', False) is True:
  112. raise RuntimeError(
  113. 'After {0} is locked, no additional data can be added to it'.format(
  114. self.__class__.__name__
  115. )
  116. )
  117. if name in object.__getattribute__(self, '__self_attributes__'):
  118. object.__setattr__(self, name, value)
  119. return
  120. self._vars[name] = value
  121. # <---- Helper Methods -----------------------------------------------------------------------------------------------
  122. # ----- Global Variables -------------------------------------------------------------------------------------------->
  123. XML_OUTPUT_DIR = os.environ.get('SALT_XML_TEST_REPORTS_DIR', os.path.join(paths.TMP, 'xml-test-reports'))
  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_CONF_MASTER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'master.d'),
  137. TMP_CONF_MINION_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'minion.d'),
  138. TMP_CONF_PROXY_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'proxy.d'),
  139. TMP_CONF_CLOUD_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.conf.d'),
  140. TMP_CONF_CLOUD_PROFILE_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.profiles.d'),
  141. TMP_CONF_CLOUD_PROVIDER_INCLUDES=os.path.join(paths.TMP_CONF_DIR, 'cloud.providers.d'),
  142. TMP_SUB_MINION_CONF_DIR=paths.TMP_SUB_MINION_CONF_DIR,
  143. TMP_SYNDIC_MASTER_CONF_DIR=paths.TMP_SYNDIC_MASTER_CONF_DIR,
  144. TMP_SYNDIC_MINION_CONF_DIR=paths.TMP_SYNDIC_MINION_CONF_DIR,
  145. TMP_MM_CONF_DIR=paths.TMP_MM_CONF_DIR,
  146. TMP_MM_SUB_CONF_DIR=paths.TMP_MM_SUB_CONF_DIR,
  147. TMP_SCRIPT_DIR=paths.TMP_SCRIPT_DIR,
  148. TMP_STATE_TREE=paths.TMP_STATE_TREE,
  149. TMP_PILLAR_TREE=paths.TMP_PILLAR_TREE,
  150. TMP_PRODENV_STATE_TREE=paths.TMP_PRODENV_STATE_TREE,
  151. RUNNING_TESTS_USER=this_user(),
  152. RUNTIME_CONFIGS={},
  153. CODE_DIR=paths.CODE_DIR,
  154. BASE_FILES=paths.BASE_FILES,
  155. PROD_FILES=paths.PROD_FILES,
  156. TESTS_DIR=paths.TESTS_DIR,
  157. PYTEST_SESSION=False
  158. )
  159. # <---- Tests Runtime Variables --------------------------------------------------------------------------------------