conftest.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """
  2. tests.pytests.conftest
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. """
  5. import logging
  6. import os
  7. import shutil
  8. import stat
  9. import pytest
  10. import salt.utils.files
  11. import salt.utils.platform
  12. from salt.serializers import yaml
  13. from saltfactories.utils import random_string
  14. from tests.support.helpers import get_virtualenv_binary_path
  15. from tests.support.runtests import RUNTIME_VARS
  16. log = logging.getLogger(__name__)
  17. @pytest.fixture(scope="session")
  18. def salt_master_factory(
  19. request,
  20. salt_factories,
  21. base_env_state_tree_root_dir,
  22. base_env_pillar_tree_root_dir,
  23. prod_env_state_tree_root_dir,
  24. prod_env_pillar_tree_root_dir,
  25. ):
  26. master_id = random_string("master-")
  27. root_dir = salt_factories.get_root_dir_for_daemon(master_id)
  28. conf_dir = root_dir / "conf"
  29. conf_dir.mkdir(exist_ok=True)
  30. with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.CONF_DIR, "master")) as rfh:
  31. config_defaults = yaml.deserialize(rfh.read())
  32. tests_known_hosts_file = str(root_dir / "salt_ssh_known_hosts")
  33. with salt.utils.files.fopen(tests_known_hosts_file, "w") as known_hosts:
  34. known_hosts.write("")
  35. config_defaults["root_dir"] = str(root_dir)
  36. config_defaults["known_hosts_file"] = tests_known_hosts_file
  37. config_defaults["syndic_master"] = "localhost"
  38. config_defaults["transport"] = request.config.getoption("--transport")
  39. config_defaults["reactor"] = [
  40. {"salt/test/reactor": [os.path.join(RUNTIME_VARS.FILES, "reactor-test.sls")]}
  41. ]
  42. config_overrides = {}
  43. ext_pillar = []
  44. if salt.utils.platform.is_windows():
  45. ext_pillar.append(
  46. {"cmd_yaml": "type {}".format(os.path.join(RUNTIME_VARS.FILES, "ext.yaml"))}
  47. )
  48. else:
  49. ext_pillar.append(
  50. {"cmd_yaml": "cat {}".format(os.path.join(RUNTIME_VARS.FILES, "ext.yaml"))}
  51. )
  52. ext_pillar.append(
  53. {
  54. "file_tree": {
  55. "root_dir": os.path.join(RUNTIME_VARS.PILLAR_DIR, "base", "file_tree"),
  56. "follow_dir_links": False,
  57. "keep_newline": True,
  58. }
  59. }
  60. )
  61. config_overrides["pillar_opts"] = True
  62. # We need to copy the extension modules into the new master root_dir or
  63. # it will be prefixed by it
  64. extension_modules_path = str(root_dir / "extension_modules")
  65. if not os.path.exists(extension_modules_path):
  66. shutil.copytree(
  67. os.path.join(RUNTIME_VARS.FILES, "extension_modules"),
  68. extension_modules_path,
  69. )
  70. # Copy the autosign_file to the new master root_dir
  71. autosign_file_path = str(root_dir / "autosign_file")
  72. shutil.copyfile(
  73. os.path.join(RUNTIME_VARS.FILES, "autosign_file"), autosign_file_path
  74. )
  75. # all read, only owner write
  76. autosign_file_permissions = (
  77. stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR
  78. )
  79. os.chmod(autosign_file_path, autosign_file_permissions)
  80. config_overrides.update(
  81. {
  82. "ext_pillar": ext_pillar,
  83. "extension_modules": extension_modules_path,
  84. "file_roots": {
  85. "base": [
  86. str(base_env_state_tree_root_dir),
  87. os.path.join(RUNTIME_VARS.FILES, "file", "base"),
  88. ],
  89. # Alternate root to test __env__ choices
  90. "prod": [
  91. str(prod_env_state_tree_root_dir),
  92. os.path.join(RUNTIME_VARS.FILES, "file", "prod"),
  93. ],
  94. },
  95. "pillar_roots": {
  96. "base": [
  97. str(base_env_pillar_tree_root_dir),
  98. os.path.join(RUNTIME_VARS.FILES, "pillar", "base"),
  99. ],
  100. "prod": [str(prod_env_pillar_tree_root_dir)],
  101. },
  102. }
  103. )
  104. # Let's copy over the test cloud config files and directories into the running master config directory
  105. for entry in os.listdir(RUNTIME_VARS.CONF_DIR):
  106. if not entry.startswith("cloud"):
  107. continue
  108. source = os.path.join(RUNTIME_VARS.CONF_DIR, entry)
  109. dest = str(conf_dir / entry)
  110. if os.path.isdir(source):
  111. shutil.copytree(source, dest)
  112. else:
  113. shutil.copyfile(source, dest)
  114. factory = salt_factories.get_salt_master_daemon(
  115. master_id,
  116. config_defaults=config_defaults,
  117. config_overrides=config_overrides,
  118. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  119. )
  120. return factory
  121. @pytest.fixture(scope="session")
  122. def salt_minion_factory(salt_master_factory):
  123. with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.CONF_DIR, "minion")) as rfh:
  124. config_defaults = yaml.deserialize(rfh.read())
  125. config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
  126. config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
  127. config_defaults["transport"] = salt_master_factory.config["transport"]
  128. config_overrides = {
  129. "file_roots": salt_master_factory.config["file_roots"].copy(),
  130. "pillar_roots": salt_master_factory.config["pillar_roots"].copy(),
  131. }
  132. virtualenv_binary = get_virtualenv_binary_path()
  133. if virtualenv_binary:
  134. config_overrides["venv_bin"] = virtualenv_binary
  135. factory = salt_master_factory.get_salt_minion_daemon(
  136. random_string("minion-"),
  137. config_defaults=config_defaults,
  138. config_overrides=config_overrides,
  139. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  140. )
  141. factory.register_after_terminate_callback(
  142. pytest.helpers.remove_stale_minion_key, salt_master_factory, factory.id
  143. )
  144. return factory
  145. @pytest.fixture(scope="session")
  146. def salt_sub_minion_factory(salt_master_factory):
  147. with salt.utils.files.fopen(
  148. os.path.join(RUNTIME_VARS.CONF_DIR, "sub_minion")
  149. ) as rfh:
  150. config_defaults = yaml.deserialize(rfh.read())
  151. config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
  152. config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
  153. config_defaults["transport"] = salt_master_factory.config["transport"]
  154. config_overrides = {
  155. "file_roots": salt_master_factory.config["file_roots"].copy(),
  156. "pillar_roots": salt_master_factory.config["pillar_roots"].copy(),
  157. }
  158. virtualenv_binary = get_virtualenv_binary_path()
  159. if virtualenv_binary:
  160. config_overrides["venv_bin"] = virtualenv_binary
  161. factory = salt_master_factory.get_salt_minion_daemon(
  162. random_string("sub-minion-"),
  163. config_defaults=config_defaults,
  164. config_overrides=config_overrides,
  165. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  166. )
  167. factory.register_after_terminate_callback(
  168. pytest.helpers.remove_stale_minion_key, salt_master_factory, factory.id
  169. )
  170. return factory
  171. @pytest.fixture(scope="session")
  172. def salt_proxy_factory(salt_factories, salt_master_factory):
  173. proxy_minion_id = random_string("proxytest-")
  174. root_dir = salt_factories.get_root_dir_for_daemon(proxy_minion_id)
  175. conf_dir = root_dir / "conf"
  176. conf_dir.mkdir(parents=True, exist_ok=True)
  177. RUNTIME_VARS.TMP_PROXY_CONF_DIR = str(conf_dir)
  178. with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.CONF_DIR, "proxy")) as rfh:
  179. config_defaults = yaml.deserialize(rfh.read())
  180. config_defaults["root_dir"] = str(root_dir)
  181. config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
  182. config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
  183. config_defaults["transport"] = salt_master_factory.config["transport"]
  184. config_defaults["user"] = salt_master_factory.config["user"]
  185. factory = salt_master_factory.get_salt_proxy_minion_daemon(
  186. proxy_minion_id,
  187. config_defaults=config_defaults,
  188. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  189. start_timeout=240,
  190. )
  191. factory.register_after_terminate_callback(
  192. pytest.helpers.remove_stale_minion_key, salt_master_factory, factory.id
  193. )
  194. return factory
  195. @pytest.fixture(scope="session")
  196. def bridge_pytest_and_runtests():
  197. """
  198. We're basically overriding the same fixture defined in tests/conftest.py
  199. """