conftest.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.multimaster.conftest
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Multimaster PyTest prep routines
  6. """
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import logging
  9. import os
  10. import shutil
  11. import pytest
  12. import salt.utils.files
  13. from salt.serializers import yaml
  14. from salt.utils.immutabletypes import freeze
  15. from tests.support.runtests import RUNTIME_VARS
  16. log = logging.getLogger(__name__)
  17. @pytest.fixture(scope="package")
  18. def salt_mm_master_config(request, salt_factories):
  19. root_dir = salt_factories._get_root_dir_for_daemon("mm-master")
  20. with salt.utils.files.fopen(
  21. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_master")
  22. ) as rfh:
  23. config_defaults = yaml.deserialize(rfh.read())
  24. config_defaults["root_dir"] = root_dir.strpath
  25. config_overrides = {
  26. "file_roots": {
  27. "base": [
  28. RUNTIME_VARS.TMP_STATE_TREE,
  29. os.path.join(RUNTIME_VARS.FILES, "file", "base"),
  30. ],
  31. # Alternate root to test __env__ choices
  32. "prod": [
  33. RUNTIME_VARS.TMP_PRODENV_STATE_TREE,
  34. os.path.join(RUNTIME_VARS.FILES, "file", "prod"),
  35. ],
  36. },
  37. "pillar_roots": {
  38. "base": [
  39. RUNTIME_VARS.TMP_PILLAR_TREE,
  40. os.path.join(RUNTIME_VARS.FILES, "pillar", "base"),
  41. ],
  42. "prod": [RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE],
  43. },
  44. }
  45. return salt_factories.configure_master(
  46. request,
  47. "mm-master",
  48. config_defaults=config_defaults,
  49. config_overrides=config_overrides,
  50. )
  51. @pytest.fixture(scope="package")
  52. def salt_mm_minion_config(request, salt_factories, salt_mm_master, salt_mm_sub_master):
  53. with salt.utils.files.fopen(
  54. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_minion")
  55. ) as rfh:
  56. config_defaults = yaml.deserialize(rfh.read())
  57. config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
  58. config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
  59. config_defaults["transport"] = request.config.getoption("--transport")
  60. mm_master_port = salt_mm_master.config["ret_port"]
  61. mm_sub_master_port = salt_mm_sub_master.config["ret_port"]
  62. config_overrides = {
  63. "master_port": "",
  64. "master": [
  65. "localhost:{}".format(mm_master_port),
  66. "localhost:{}".format(mm_sub_master_port),
  67. ],
  68. "test.foo": "baz",
  69. }
  70. return salt_factories.configure_minion(
  71. request,
  72. "mm-minion",
  73. master_id=salt_mm_master.config["id"],
  74. config_defaults=config_defaults,
  75. config_overrides=config_overrides,
  76. )
  77. @pytest.fixture(scope="package")
  78. def salt_mm_sub_master_config(request, salt_factories, salt_mm_master):
  79. with salt.utils.files.fopen(
  80. os.path.join(RUNTIME_VARS.CONF_DIR, "mm_sub_master")
  81. ) as rfh:
  82. config_defaults = yaml.deserialize(rfh.read())
  83. root_dir = salt_factories._get_root_dir_for_daemon("mm-master")
  84. config_defaults["root_dir"] = root_dir.strpath
  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(
  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. )