1
0

conftest.py 7.5 KB

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