From 3012dc42d6da92b93697aa5f3d195876fad0d1d2 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Tue, 16 Aug 2022 13:20:11 +0100 Subject: [PATCH] Test fixes when running with the onedir build Signed-off-by: Pedro Algarvio --- MANIFEST.in | 1 + tests/conftest.py | 2 + .../files/file/base/_modules/mantest.py | 75 ------------------- .../file/base/_modules/runtests_decorators.py | 16 ++-- .../modules/saltutil/test_modules.py | 2 - tests/support/helpers.py | 34 ++++++--- 6 files changed, 36 insertions(+), 94 deletions(-) delete mode 100644 tests/integration/files/file/base/_modules/mantest.py diff --git a/MANIFEST.in b/MANIFEST.in index c845ca82e29..9886100b685 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -6,6 +6,7 @@ include NOTICE include README.rst include SUPPORT.rst include run.py +include pyproject.toml include tests/*.py recursive-include tests * recursive-include requirements *.txt diff --git a/tests/conftest.py b/tests/conftest.py index 8c9b564c939..d55cdf5828c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -59,6 +59,8 @@ if str(CODE_DIR) in sys.path: sys.path.remove(str(CODE_DIR)) sys.path.insert(0, str(CODE_DIR)) +os.environ["REPO_ROOT_DIR"] = str(CODE_DIR) + # Coverage if "COVERAGE_PROCESS_START" in os.environ: MAYBE_RUN_COVERAGE = True diff --git a/tests/integration/files/file/base/_modules/mantest.py b/tests/integration/files/file/base/_modules/mantest.py deleted file mode 100644 index 707c7c9ea2e..00000000000 --- a/tests/integration/files/file/base/_modules/mantest.py +++ /dev/null @@ -1,75 +0,0 @@ -""" -Helpers for testing man pages -""" - -import logging -import os -import sys - -import salt.utils.files -import salt.utils.path -import salt.utils.stringutils -from salt.exceptions import CommandExecutionError -from tests.support.runtests import RUNTIME_VARS - -log = logging.getLogger(__name__) - - -def install(rootdir): - if not os.path.exists(rootdir): - os.makedirs(rootdir) - return __salt__["cmd.run_all"]( - [ - sys.executable, - os.path.join(RUNTIME_VARS.CODE_DIR, "setup.py"), - "install", - "--root={}".format(rootdir), - ], - redirect_stderr=True, - ) - - -def search(manpages, rootdir): - manpage_fns = set(manpages) - manpage_paths = {} - for root, _, files in os.walk(rootdir): - if not manpage_fns: - # All manpages found, no need to keep walking - break - # Using list because we will be modifying the set during iteration - for manpage_fn in list(manpage_fns): - if manpage_fn in files: - manpage_path = salt.utils.path.join(root, manpage_fn) - manpage_paths[manpage_fn] = manpage_path - manpage_fns.remove(manpage_fn) - - if manpage_fns: - raise CommandExecutionError( - "The following manpages were not found under {}: {}".format( - rootdir, ", ".join(sorted(manpage_fns)) - ) - ) - - failed = {} - for manpage in sorted(manpages): - with salt.utils.files.fopen(manpage_paths[manpage]) as fp_: - contents = salt.utils.stringutils.to_unicode(fp_.read()) - # Check for search string in contents - for search_string in manpages[manpage]: - if search_string not in contents: - failed.setdefault(manpage, []).append( - "No match for search string '{}' found in {}".format( - search_string, manpage_paths[manpage] - ) - ) - # Check for correct install dir - path = "/man{}/".format(manpage.rsplit(".", 1)[-1]) - if path not in manpage_paths[manpage]: - failed.setdefault(manpage, []).append( - "{} not found in manpage path {}".format(path, manpage_paths[manpage]) - ) - - if failed: - raise CommandExecutionError("One or more manpages failed", info=failed) - - return True diff --git a/tests/integration/files/file/base/_modules/runtests_decorators.py b/tests/integration/files/file/base/_modules/runtests_decorators.py index fdb4528e643..a005b32a1bc 100644 --- a/tests/integration/files/file/base/_modules/runtests_decorators.py +++ b/tests/integration/files/file/base/_modules/runtests_decorators.py @@ -1,18 +1,24 @@ +import logging import os +import pathlib import time import salt.utils.decorators -from tests.support.runtests import RUNTIME_VARS -EXIT_CODE_SH = os.path.join(RUNTIME_VARS.BASE_FILES, "exit_code.sh") -EXIT_CODE_CMD = os.path.join(RUNTIME_VARS.BASE_FILES, "exit_code.cmd") +log = logging.getLogger(__name__) + +REPO_ROOT_DIR = pathlib.Path(os.environ["REPO_ROOT_DIR"]).resolve() +STATE_BASE_DIR = REPO_ROOT_DIR / "tests" / "integration" / "files" / "file" / "base" +EXIT_CODE_SH = STATE_BASE_DIR / "exit_code.sh" +EXIT_CODE_CMD = STATE_BASE_DIR / "exit_code.cmd" def _exit_code(code): if os.name == "nt": - return "cmd /c {} {}".format(EXIT_CODE_CMD, code) + cmd = "cmd /c {} {}".format(EXIT_CODE_CMD, code) else: - return "/usr/bin/env sh {} {}".format(EXIT_CODE_SH, code) + cmd = "/usr/bin/env sh {} {}".format(EXIT_CODE_SH, code) + return cmd def _fallbackfunc(): diff --git a/tests/pytests/integration/modules/saltutil/test_modules.py b/tests/pytests/integration/modules/saltutil/test_modules.py index 783bce62634..9d10189bb30 100644 --- a/tests/pytests/integration/modules/saltutil/test_modules.py +++ b/tests/pytests/integration/modules/saltutil/test_modules.py @@ -41,7 +41,6 @@ def test_sync_all(salt_call_cli): "modules": [ "modules.depends_versioned", "modules.depends_versionless", - "modules.mantest", "modules.override_test", "modules.runtests_decorators", "modules.runtests_helpers", @@ -109,7 +108,6 @@ def test_sync_all_blacklist(salt_call_cli): "utils": [], "returners": [], "modules": [ - "modules.mantest", "modules.override_test", "modules.runtests_helpers", "modules.salttest", diff --git a/tests/support/helpers.py b/tests/support/helpers.py index 8f9930c0c2c..a9635410d76 100644 --- a/tests/support/helpers.py +++ b/tests/support/helpers.py @@ -47,7 +47,6 @@ import salt.utils.stringutils import salt.utils.versions from tests.support.mock import patch from tests.support.runtests import RUNTIME_VARS -from tests.support.sminion import create_sminion from tests.support.unit import SkipTest, _id, skip log = logging.getLogger(__name__) @@ -1722,12 +1721,16 @@ class VirtualEnv: def run(self, *args, **kwargs): check = kwargs.pop("check", True) - kwargs.setdefault("cwd", str(self.venv_dir)) + kwargs.setdefault("cwd", tempfile.gettempdir()) kwargs.setdefault("stdout", subprocess.PIPE) kwargs.setdefault("stderr", subprocess.PIPE) kwargs.setdefault("universal_newlines", True) - kwargs.setdefault("env", self.environ) - proc = subprocess.run(args, check=False, **kwargs) + env = kwargs.pop("env", None) + if env: + env = self.environ.copy().update(env) + else: + env = self.environ + proc = subprocess.run(args, check=False, env=env, **kwargs) ret = ProcessResult( returncode=proc.returncode, stdout=proc.stdout, @@ -1778,14 +1781,16 @@ class VirtualEnv: except AttributeError: return sys.executable - def run_code(self, code_string, **kwargs): + def run_code(self, code_string, python=None, **kwargs): if code_string.startswith("\n"): code_string = code_string[1:] code_string = textwrap.dedent(code_string).rstrip() log.debug( "Code to run passed to python:\n>>>>>>>>>>\n%s\n<<<<<<<<<<", code_string ) - return self.run(str(self.venv_python), "-c", code_string, **kwargs) + if python is None: + python = str(self.venv_python) + return self.run(python, "-c", code_string, **kwargs) def get_installed_packages(self): data = {} @@ -1795,12 +1800,17 @@ class VirtualEnv: return data def _create_virtualenv(self): - sminion = create_sminion() - sminion.functions.virtualenv.create( - str(self.venv_dir), - python=self.get_real_python(), - system_site_packages=self.system_site_packages, - ) + virtualenv = shutil.which("virtualenv") + if not virtualenv: + pytest.fail("'virtualenv' binary not found") + cmd = [ + virtualenv, + "--python={}".format(self.get_real_python()), + ] + if self.system_site_packages: + cmd.append("--system-site-packages") + cmd.append(str(self.venv_dir)) + self.run(*cmd, cwd=str(self.venv_dir.parent)) self.install("-U", self.pip_requirement, self.setuptools_requirement) log.debug("Created virtualenv in %s", self.venv_dir)