Test fixes when running with the onedir build

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
Pedro Algarvio 2022-08-16 13:20:11 +01:00 committed by Gareth J. Greenaway
parent 1ed20998c8
commit 3012dc42d6
6 changed files with 36 additions and 94 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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():

View file

@ -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",

View file

@ -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)