1
0

conftest.py 7.5 KB

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