Upgrade to pytest-salt-factories 0.10.x

This commit is contained in:
Pedro Algarvio 2020-05-07 21:22:00 +01:00 committed by Daniel Wozniak
parent 433e75fea7
commit ef9237e056
7 changed files with 63 additions and 66 deletions

View file

@ -727,6 +727,26 @@ def temp_pillar_file(name, contents, saltenv="base", strip_first_newline=True):
# <---- Pytest Helpers -----------------------------------------------------------------------------------------------
# ----- Fixtures Overrides ------------------------------------------------------------------------------------------>
@pytest.fixture(scope="session")
def salt_factories_config():
"""
Return a dictionary with the keyworkd arguments for SaltFactoriesManager
"""
return {
"executable": sys.executable,
"code_dir": CODE_DIR,
"inject_coverage": MAYBE_RUN_COVERAGE,
"inject_sitecustomize": MAYBE_RUN_COVERAGE,
"start_timeout": 120
if (os.environ.get("JENKINS_URL") or os.environ.get("CI"))
else 60,
}
# <---- Pytest Helpers -----------------------------------------------------------------------------------------------
# ----- Fixtures Overrides ------------------------------------------------------------------------------------------>
def _get_virtualenv_binary_path():
try:
@ -945,6 +965,7 @@ def salt_syndic_config(request, salt_factories, salt_syndic_master_config):
@pytest.fixture(scope="session")
def salt_master_config(request, salt_factories, salt_syndic_master_config):
root_dir = salt_factories._get_root_dir_for_daemon("master")
conf_dir = root_dir.join("conf").ensure(dir=True)
with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.CONF_DIR, "master")) as rfh:
config_defaults = yaml.deserialize(rfh.read())
@ -1028,6 +1049,17 @@ def salt_master_config(request, salt_factories, salt_syndic_master_config):
}
)
# Let's copy over the test cloud config files and directories into the running master config directory
for entry in os.listdir(RUNTIME_VARS.CONF_DIR):
if not entry.startswith("cloud"):
continue
source = os.path.join(RUNTIME_VARS.CONF_DIR, entry)
dest = conf_dir.join(entry).strpath
if os.path.isdir(source):
shutil.copytree(source, dest)
else:
shutil.copyfile(source, dest)
return salt_factories.configure_master(
request,
"master",
@ -1205,17 +1237,6 @@ def bridge_pytest_and_runtests(
salt_syndic_config["conf_file"]
)
# Let's copy over the test cloud config files and directories into the running master config directory
for entry in os.listdir(RUNTIME_VARS.CONF_DIR):
if not entry.startswith("cloud"):
continue
source = os.path.join(RUNTIME_VARS.CONF_DIR, entry)
dest = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, entry)
if os.path.isdir(source):
shutil.copytree(source, dest)
else:
shutil.copyfile(source, dest)
# <---- Salt Configuration -------------------------------------------------------------------------------------------
# <---- Fixtures Overrides -------------------------------------------------------------------------------------------

View file

@ -9,7 +9,8 @@ import time
import salt.utils.stringutils
import zmq
from salt.log.handlers.logstash_mod import DatagramLogstashHandler, ZMQLogstashHander
from tests.support.helpers import get_unused_localhost_port, slowTest
from saltfactories.utils.ports import get_unused_localhost_port
from tests.support.helpers import slowTest
from tests.support.unit import TestCase
log = logging.getLogger(__name__)

View file

@ -19,13 +19,9 @@ import salt.utils.files
import salt.utils.path
import salt.utils.platform
import salt.utils.stringutils
from saltfactories.utils.ports import get_unused_localhost_port
from tests.support.case import ModuleCase
from tests.support.helpers import (
get_unused_localhost_port,
skip_if_not_root,
slowTest,
with_tempfile,
)
from tests.support.helpers import skip_if_not_root, slowTest, with_tempfile
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import skipIf

View file

@ -20,38 +20,26 @@ log = logging.getLogger(__name__)
@pytest.fixture(scope="package", autouse=True)
def salt_proxy(request, salt_factories, salt_master):
yield salt_factories.spawn_proxy_minion(request, "proxytest", master_id="master")
proxy_minion_id = "proxytest"
root_dir = salt_factories._get_root_dir_for_daemon(proxy_minion_id)
conf_dir = root_dir.join("conf").ensure(dir=True)
RUNTIME_VARS.TMP_PROXY_CONF_DIR = conf_dir.strpath
proxy_key_file = os.path.join(salt_master.config["pki_dir"], "minions", "proxytest")
log.warning("KEY FILE: %s", proxy_key_file)
with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.CONF_DIR, "proxy")) as rfh:
config_defaults = yaml.deserialize(rfh.read())
config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
config_defaults["transport"] = request.config.getoption("--transport")
yield salt_factories.spawn_proxy_minion(
request, proxy_minion_id, master_id="master", config_defaults=config_defaults
)
proxy_key_file = os.path.join(
salt_master.config["pki_dir"], "minions", proxy_minion_id
)
log.debug("Proxy minion %r KEY FILE: %s", proxy_minion_id, proxy_key_file)
if os.path.exists(proxy_key_file):
os.unlink(proxy_key_file)
else:
log.warning("The proxy minion key was not found at %s", proxy_key_file)
def pytest_saltfactories_proxy_minion_configuration_defaults(
request, factories_manager, root_dir, proxy_minion_id, master_port
):
"""
Hook which should return a dictionary tailored for the provided proxy_minion_id
Stops at the first non None result
"""
if proxy_minion_id == "proxytest":
with salt.utils.files.fopen(
os.path.join(RUNTIME_VARS.CONF_DIR, "proxy")
) as rfh:
opts = yaml.deserialize(rfh.read())
else:
raise RuntimeError(
"Not prepared to handle proxy_minion_id '{}'".format(proxy_minion_id)
)
opts["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts")
opts["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases")
opts["transport"] = request.config.getoption("--transport")
RUNTIME_VARS.TMP_PROXY_CONF_DIR = root_dir.join("conf").strpath
return opts

View file

@ -21,15 +21,12 @@ import salt.utils.path
import salt.utils.yaml
from salt.fileserver import gitfs
from salt.pillar import git_pillar
from saltfactories.utils.ports import get_unused_localhost_port
from saltfactories.utils.processes.bases import FactoryDaemonScriptBase
from saltfactories.utils.processes.helpers import start_daemon, terminate_process
from saltfactories.utils.processes.sshd import SshdDaemon
from tests.support.case import ModuleCase
from tests.support.helpers import (
get_unused_localhost_port,
patched_environ,
requires_system_grains,
)
from tests.support.helpers import patched_environ, requires_system_grains
from tests.support.mixins import (
AdaptedConfigurationTestCaseMixin,
LoaderModuleMockMixin,
@ -183,7 +180,7 @@ class SSHDMixin(SaltClientMixin, SaltReturnAssertsMixin):
cls.sshd_bin = salt.utils.path.which("sshd")
cls.sshd_config_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
cls.sshd_config = os.path.join(cls.sshd_config_dir, "sshd_config")
cls.sshd_port = get_unused_localhost_port()
cls.sshd_port = get_unused_localhost_port(cached_seconds=120)
cls.url = "ssh://{username}@127.0.0.1:{port}/~/repo.git".format(
username=cls.username, port=cls.sshd_port
)
@ -333,11 +330,8 @@ class WebserverMixin(SaltClientMixin, SaltReturnAssertsMixin):
cls.repo_dir = os.path.join(cls.git_dir, "repos")
cls.venv_dir = os.path.join(cls.root_dir, "venv")
cls.uwsgi_bin = os.path.join(cls.venv_dir, "bin", "uwsgi")
cls.nginx_port = cls.uwsgi_port = get_unused_localhost_port()
while cls.uwsgi_port == cls.nginx_port:
# Ensure we don't hit a corner case in which two sucessive calls to
# get_unused_localhost_port() return identical port numbers.
cls.uwsgi_port = get_unused_localhost_port()
cls.nginx_port = cls.uwsgi_port = get_unused_localhost_port(cached_seconds=120)
cls.uwsgi_port = get_unused_localhost_port(cached_seconds=120)
cls.url = "http://127.0.0.1:{port}/repo.git".format(port=cls.nginx_port)
cls.url_extra_repo = "http://127.0.0.1:{port}/extra_repo.git".format(
port=cls.nginx_port

View file

@ -25,7 +25,8 @@ from salt.transport.tcp import (
SaltMessageClientPool,
TCPPubServerChannel,
)
from tests.support.helpers import flaky, get_unused_localhost_port, slowTest
from saltfactories.utils.ports import get_unused_localhost_port
from tests.support.helpers import flaky, slowTest
from tests.support.mixins import AdaptedConfigurationTestCaseMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase, skipIf

View file

@ -25,12 +25,8 @@ from salt.ext import six
from salt.ext.six.moves import range
from salt.ext.tornado.testing import AsyncTestCase
from salt.transport.zeromq import AsyncReqMessageClientPool
from tests.support.helpers import (
flaky,
get_unused_localhost_port,
not_runs_on,
slowTest,
)
from saltfactories.utils.ports import get_unused_localhost_port
from tests.support.helpers import flaky, not_runs_on, slowTest
from tests.support.mixins import AdaptedConfigurationTestCaseMixin
from tests.support.mock import MagicMock, patch
from tests.support.runtests import RUNTIME_VARS