File: conftest.py

package info (click to toggle)
pytest-salt-factories 0.121.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,068 kB
  • sloc: python: 9,193; makefile: 15
file content (128 lines) | stat: -rw-r--r-- 3,992 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import functools
import logging
import os
import pathlib
import stat
import tempfile
import textwrap

import pkg_resources
import pytest
import salt.version

log = logging.getLogger(__name__)

pytest_plugins = ["pytester"]

TESTS_PATH = pathlib.Path(__file__).resolve().parent


def pytest_report_header():
    return "salt-version: {}".format(salt.version.__version__)


class Tempfiles:
    """
    Class which generates temporary files and cleans them when done
    """

    def __init__(self, request):
        self.request = request

    def makepyfile(self, contents, prefix=None, executable=False):
        """
        Creates a python file and returns it's path
        """
        tfile = tempfile.NamedTemporaryFile("w", prefix=prefix or "tmp", suffix=".py", delete=False)
        contents = textwrap.dedent(contents.lstrip("\n")).strip()
        tfile.write(contents)
        tfile.close()
        if executable is True:
            st = os.stat(tfile.name)
            os.chmod(tfile.name, st.st_mode | stat.S_IEXEC)
        self.request.addfinalizer(functools.partial(self._delete_temp_file, tfile.name))
        with open(tfile.name) as rfh:
            log.debug(
                "Created python file with contents:\n>>>>> %s >>>>>\n%s\n<<<<< %s <<<<<\n",
                tfile.name,
                rfh.read(),
                tfile.name,
            )
        return tfile.name

    def makeslsfile(self, contents, name=None):
        """
        Creates an sls file and returns it's path
        """
        if name is None:
            tfile = tempfile.NamedTemporaryFile("w", suffix=".sls", delete=False)
            name = tfile.name
        with open(name, "w") as wfh:
            contents = textwrap.dedent(contents.lstrip("\n")).strip()
            wfh.write(contents)
        self.request.addfinalizer(functools.partial(self._delete_temp_file, name))
        with open(name) as rfh:
            log.debug(
                "Created SLS file with contents:\n>>>>> %s >>>>>\n%s\n<<<<< %s <<<<<\n",
                name,
                rfh.read(),
                name,
            )
        return name

    def _delete_temp_file(self, fpath):
        """
        Cleanup the temporary path
        """
        if os.path.exists(fpath):
            os.unlink(fpath)


@pytest.fixture
def tempfiles(request):
    """
    Temporary files fixture
    """
    return Tempfiles(request)


@pytest.fixture(scope="session")
def salt_version():
    return pkg_resources.get_distribution("salt").version


@pytest.mark.trylast
def pytest_configure(config):
    """
    called after command line options have been parsed
    and all plugins and initial conftest files been loaded.
    """
    # Expose the markers we use to pytest CLI
    config.addinivalue_line(
        "markers",
        "skip_on_salt_system_install: Marker to skip tests when testing"
        "against salt installed in the system.",
    )


@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
    salt_factories_fixture = item._request.getfixturevalue("salt_factories")
    if salt_factories_fixture.system_install is False:
        return
    system_install_skip_paths = (
        # There's no point on running these tests against a system install of salt
        str(TESTS_PATH / "unit"),
        str(TESTS_PATH / "functional"),
        str(TESTS_PATH / "integration" / "factories" / "cli"),
        str(TESTS_PATH / "integration" / "factories" / "daemons" / "sshd"),
        str(TESTS_PATH / "integration" / "factories" / "daemons" / "container"),
    )
    if str(item.fspath).startswith(system_install_skip_paths):
        item._skipped_by_mark = True
        pytest.skip("Test should not run against system install of Salt")

    skip_on_salt_system_install_marker = item.get_closest_marker("skip_on_salt_system_install")
    if skip_on_salt_system_install_marker is not None:
        item._skipped_by_mark = True
        pytest.skip("Test should not run against system install of Salt")