conftest.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """
  2. tests.multimaster.conftest
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Multimaster PyTest prep routines
  5. """
  6. import logging
  7. import os
  8. import pathlib
  9. import shutil
  10. import pytest
  11. import salt.utils.files
  12. from salt.serializers import yaml
  13. from salt.utils.immutabletypes import freeze
  14. from tests.support.runtests import RUNTIME_VARS
  15. log = logging.getLogger(__name__)
  16. @pytest.fixture(scope="package")
  17. def ext_pillar_file_tree():
  18. pillar_file_tree = {
  19. "root_dir": str(pathlib.Path(RUNTIME_VARS.PILLAR_DIR) / "base" / "file_tree"),
  20. "follow_dir_links": False,
  21. "keep_newline": True,
  22. }
  23. return {"file_tree": pillar_file_tree}
  24. @pytest.fixture(scope="package")
  25. def salt_mm_master(request, salt_factories, ext_pillar_file_tree):
  26. with salt.utils.files.fopen(
  27. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_master")
  28. ) as rfh:
  29. config_defaults = yaml.deserialize(rfh.read())
  30. master_id = "mm-master"
  31. root_dir = salt_factories.get_root_dir_for_daemon(master_id)
  32. config_defaults["root_dir"] = str(root_dir)
  33. config_defaults["ext_pillar"] = [ext_pillar_file_tree]
  34. config_defaults["open_mode"] = True
  35. config_defaults["transport"] = request.config.getoption("--transport")
  36. config_overrides = {
  37. "file_roots": {
  38. "base": [
  39. RUNTIME_VARS.TMP_STATE_TREE,
  40. os.path.join(RUNTIME_VARS.FILES, "file", "base"),
  41. ],
  42. # Alternate root to test __env__ choices
  43. "prod": [
  44. RUNTIME_VARS.TMP_PRODENV_STATE_TREE,
  45. os.path.join(RUNTIME_VARS.FILES, "file", "prod"),
  46. ],
  47. },
  48. "pillar_roots": {
  49. "base": [
  50. RUNTIME_VARS.TMP_PILLAR_TREE,
  51. os.path.join(RUNTIME_VARS.FILES, "pillar", "base"),
  52. ],
  53. "prod": [RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE],
  54. },
  55. }
  56. factory = salt_factories.get_salt_master_daemon(
  57. master_id,
  58. config_defaults=config_defaults,
  59. config_overrides=config_overrides,
  60. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  61. )
  62. with factory.started():
  63. yield factory
  64. @pytest.fixture(scope="package")
  65. def salt_mm_sub_master(salt_factories, salt_mm_master, ext_pillar_file_tree):
  66. with salt.utils.files.fopen(
  67. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_sub_master")
  68. ) as rfh:
  69. config_defaults = yaml.deserialize(rfh.read())
  70. master_id = "mm-sub-master"
  71. root_dir = salt_factories.get_root_dir_for_daemon(master_id)
  72. config_defaults["root_dir"] = str(root_dir)
  73. config_defaults["ext_pillar"] = [ext_pillar_file_tree]
  74. config_defaults["open_mode"] = True
  75. config_defaults["transport"] = salt_mm_master.config["transport"]
  76. config_overrides = {
  77. "file_roots": {
  78. "base": [
  79. RUNTIME_VARS.TMP_STATE_TREE,
  80. os.path.join(RUNTIME_VARS.FILES, "file", "base"),
  81. ],
  82. # Alternate root to test __env__ choices
  83. "prod": [
  84. RUNTIME_VARS.TMP_PRODENV_STATE_TREE,
  85. os.path.join(RUNTIME_VARS.FILES, "file", "prod"),
  86. ],
  87. },
  88. "pillar_roots": {
  89. "base": [
  90. RUNTIME_VARS.TMP_PILLAR_TREE,
  91. os.path.join(RUNTIME_VARS.FILES, "pillar", "base"),
  92. ],
  93. "prod": [RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE],
  94. },
  95. }
  96. factory = salt_factories.get_salt_master_daemon(
  97. master_id,
  98. config_defaults=config_defaults,
  99. config_overrides=config_overrides,
  100. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  101. )
  102. # The secondary salt master depends on the primarily salt master fixture
  103. # because we need to clone the keys
  104. for keyfile in ("master.pem", "master.pub"):
  105. shutil.copyfile(
  106. os.path.join(salt_mm_master.config["pki_dir"], keyfile),
  107. os.path.join(factory.config["pki_dir"], keyfile),
  108. )
  109. with factory.started():
  110. yield factory
  111. @pytest.fixture(scope="package")
  112. def salt_mm_minion(salt_mm_master, salt_mm_sub_master):
  113. with salt.utils.files.fopen(
  114. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_minion")
  115. ) as rfh:
  116. config_defaults = yaml.deserialize(rfh.read())
  117. config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
  118. config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
  119. config_defaults["transport"] = salt_mm_master.config["transport"]
  120. mm_master_port = salt_mm_master.config["ret_port"]
  121. mm_sub_master_port = salt_mm_sub_master.config["ret_port"]
  122. config_overrides = {
  123. "master": [
  124. "localhost:{}".format(mm_master_port),
  125. "localhost:{}".format(mm_sub_master_port),
  126. ],
  127. "test.foo": "baz",
  128. }
  129. factory = salt_mm_master.get_salt_minion_daemon(
  130. "mm-minion",
  131. config_defaults=config_defaults,
  132. config_overrides=config_overrides,
  133. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  134. )
  135. with factory.started():
  136. yield factory
  137. @pytest.fixture(scope="package")
  138. def salt_mm_sub_minion(salt_mm_master, salt_mm_sub_master):
  139. with salt.utils.files.fopen(
  140. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_sub_minion")
  141. ) as rfh:
  142. config_defaults = yaml.deserialize(rfh.read())
  143. config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
  144. config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
  145. config_defaults["transport"] = salt_mm_master.config["transport"]
  146. mm_master_port = salt_mm_master.config["ret_port"]
  147. mm_sub_master_port = salt_mm_sub_master.config["ret_port"]
  148. config_overrides = {
  149. "master": [
  150. "localhost:{}".format(mm_master_port),
  151. "localhost:{}".format(mm_sub_master_port),
  152. ],
  153. "test.foo": "baz",
  154. }
  155. factory = salt_mm_sub_master.get_salt_minion_daemon(
  156. "mm-sub-minion",
  157. config_defaults=config_defaults,
  158. config_overrides=config_overrides,
  159. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  160. )
  161. with factory.started():
  162. yield factory
  163. @pytest.fixture(scope="package", autouse=True)
  164. def bridge_pytest_and_runtests( # pylint: disable=function-redefined
  165. reap_stray_processes,
  166. base_env_state_tree_root_dir,
  167. prod_env_state_tree_root_dir,
  168. base_env_pillar_tree_root_dir,
  169. prod_env_pillar_tree_root_dir,
  170. salt_mm_master,
  171. salt_mm_sub_master,
  172. salt_mm_minion,
  173. salt_mm_sub_minion,
  174. ):
  175. # Make sure unittest2 uses the pytest generated configuration
  176. RUNTIME_VARS.RUNTIME_CONFIGS["mm_master"] = freeze(salt_mm_master.config)
  177. RUNTIME_VARS.RUNTIME_CONFIGS["mm_minion"] = freeze(salt_mm_minion.config)
  178. RUNTIME_VARS.RUNTIME_CONFIGS["mm_sub_master"] = freeze(salt_mm_sub_master.config)
  179. RUNTIME_VARS.RUNTIME_CONFIGS["mm_sub_minion"] = freeze(salt_mm_sub_minion.config)
  180. # Make sure unittest2 classes know their paths
  181. RUNTIME_VARS.TMP_MM_CONF_DIR = os.path.dirname(salt_mm_master.config["conf_file"])
  182. RUNTIME_VARS.TMP_MM_MINION_CONF_DIR = os.path.dirname(
  183. salt_mm_minion.config["conf_file"]
  184. )
  185. RUNTIME_VARS.TMP_MM_SUB_CONF_DIR = os.path.dirname(
  186. salt_mm_sub_master.config["conf_file"]
  187. )
  188. RUNTIME_VARS.TMP_MM_SUB_MINION_CONF_DIR = os.path.dirname(
  189. salt_mm_sub_minion.config["conf_file"]
  190. )