conftest.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.e2e.compat.conftest
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Salt Compatibility PyTest Fixtures
  6. """
  7. import logging
  8. import pytest
  9. import salt.utils.path
  10. from saltfactories.factories.manager import SaltFactoriesManager
  11. from saltfactories.utils.log_server import log_server_listener
  12. from saltfactories.utils.ports import get_unused_localhost_port
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.sminion import create_sminion
  15. docker = pytest.importorskip("docker")
  16. log = logging.getLogger(__name__)
  17. @pytest.fixture(scope="session")
  18. def docker_client():
  19. if salt.utils.path.which("docker") is None:
  20. pytest.skip("The docker binary is not available")
  21. return docker.from_env()
  22. @pytest.fixture(scope="package")
  23. def host_docker_network_ip_address(docker_client):
  24. sminion = create_sminion()
  25. network_name = "salt-e2e"
  26. network_subnet = "10.0.20.0/24"
  27. network_gateway = "10.0.20.1"
  28. try:
  29. ret = sminion.states.docker_network.present(
  30. network_name,
  31. driver="bridge",
  32. ipam_pools=[{"subnet": network_subnet, "gateway": network_gateway}],
  33. )
  34. assert isinstance(ret, dict), ret
  35. assert ret["result"], "Failed to create docker network: {}".format(ret)
  36. yield network_gateway
  37. finally:
  38. sminion.states.docker_network.absent(network_name)
  39. # @pytest.fixture(scope="package")
  40. # def log_server_host(host_docker_network_ip_address):
  41. # return host_docker_network_ip_address
  42. # @pytest.fixture(scope="package")
  43. # def log_server_port():
  44. # return get_unused_localhost_port()
  45. # @pytest.fixture(scope="package")
  46. # def log_server(log_server_host, log_server_port):
  47. # log.info("Starting log server")
  48. # with log_server_listener("0.0.0.0", log_server_port):
  49. # log.info("Log Server Started")
  50. # # Run tests
  51. # yield
  52. # @pytest.fixture(scope="package")
  53. # def salt_factories_config(log_server_host, log_server_port, log_server_level):
  54. # """
  55. # Return a dictionary with the keyworkd arguments for SaltFactoriesManager
  56. # """
  57. # return {
  58. # "code_dir": RUNTIME_VARS.CODE_DIR,
  59. # "inject_coverage": True,
  60. # "inject_sitecustomize": True,
  61. # "log_server_host": log_server_host,
  62. # "log_server_port": log_server_port,
  63. # "log_server_level": log_server_level,
  64. # "start_timeout": 120,
  65. # }
  66. # @pytest.fixture(scope="package")
  67. # def salt_factories(
  68. # request, pytestconfig, tempdir, log_server, salt_factories_config,
  69. # ):
  70. # if not isinstance(salt_factories_config, dict):
  71. # raise RuntimeError(
  72. # "The 'salt_factories_config' fixture MUST return a dictionary"
  73. # )
  74. # _manager = SaltFactoriesManager(
  75. # pytestconfig,
  76. # tempdir,
  77. # stats_processes=request.session.stats_processes,
  78. # **salt_factories_config
  79. # )
  80. # yield _manager
  81. # _manager.event_listener.stop()
  82. @pytest.fixture(scope="package")
  83. @pytest.mark.skip_if_binaries_missing("docker")
  84. def salt_master(request, salt_factories, host_docker_network_ip_address):
  85. config_overrides = {
  86. "interface": host_docker_network_ip_address,
  87. "file_roots": {
  88. "base": [RUNTIME_VARS.TMP_STATE_TREE],
  89. # Alternate root to test __env__ choices
  90. "prod": [RUNTIME_VARS.TMP_PRODENV_STATE_TREE],
  91. },
  92. "pillar_roots": {
  93. "base": [RUNTIME_VARS.TMP_PILLAR_TREE],
  94. "prod": [RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE],
  95. },
  96. }
  97. return salt_factories.spawn_master(
  98. request, "master", config_overrides=config_overrides,
  99. )
  100. @pytest.fixture
  101. def salt_cli(salt_factories, salt_minion, salt_master):
  102. return salt_factories.get_salt_cli(salt_master.config["id"])
  103. @pytest.fixture
  104. def salt_cp_cli(salt_factories, salt_minion, salt_master):
  105. return salt_factories.get_salt_cp_cli(salt_master.config["id"])