Simplify/Fix tests

This commit is contained in:
Pedro Algarvio 2024-04-04 18:13:19 +01:00
parent e7b735cdac
commit bc8e35bcd4

View file

@ -1,6 +1,7 @@
import glob
import logging
import os
import pathlib
import pprint
import shutil
import sys
@ -14,14 +15,11 @@ import salt.utils.versions
import salt.utils.win_dacl
import salt.utils.win_functions
import salt.utils.win_runas
from tests.support.helpers import SKIP_INITIAL_PHOTONOS_FAILURES, patched_environ
try:
import pwd
HAS_PWD = True
except ImportError:
HAS_PWD = False
from tests.support.helpers import (
SKIP_INITIAL_PHOTONOS_FAILURES,
VirtualEnv,
patched_environ,
)
log = logging.getLogger(__name__)
@ -33,10 +31,27 @@ pytestmark = [
def _win_user_where(username, password, program):
cmd = f"cmd.exe /c where {program}"
ret = salt.utils.win_runas.runas(cmd, username, password)
assert ret["retcode"] == 0, "{} returned {}".format(cmd, ret["retcode"])
assert ret["retcode"] == 0
return ret["stdout"].strip().split("\n")[-1].strip()
@pytest.fixture
def pkg_name():
return "pep8"
@pytest.fixture
def venv(tmp_path):
with VirtualEnv(venv_dir=tmp_path) as venv:
yield venv
@pytest.fixture
def account():
with pytest.helpers.create_account(username="pip-account") as _account:
yield _account
@pytest.fixture(scope="module")
def create_virtualenv(modules):
def run_command(path, **kwargs):
@ -83,28 +98,29 @@ def create_virtualenv(modules):
return run_command
@pytest.fixture
def _skip_if_pep8_installed(modules, pkg_name):
if pkg_name in modules.pip.list():
pytest.skip(f"{pkg_name} is already installed, uninstall to run this test")
@pytest.mark.slow_test
def test_pip_installed_removed(modules, states):
@pytest.mark.usefixtures("_skip_if_pep8_installed")
def test_pip_installed_removed(states, pkg_name):
"""
Tests installed and removed states
"""
name = "pudb"
if name in modules.pip.list():
pytest.skip(f"{name} is already installed, uninstall to run this test")
ret = states.pip.installed(name=name)
ret = states.pip.installed(name=pkg_name)
assert ret.result is True
ret = states.pip.removed(name=name)
ret = states.pip.removed(name=pkg_name)
assert ret.result is True
@pytest.mark.slow_test
def test_pip_installed_removed_venv(tmp_path, create_virtualenv, states):
venv_dir = tmp_path / "pip_installed_removed"
create_virtualenv(str(venv_dir))
name = "pudb"
ret = states.pip.installed(name=name, bin_env=str(venv_dir))
def test_pip_installed_removed_venv(states, venv, pkg_name):
ret = states.pip.installed(name=pkg_name, bin_env=str(venv.venv_dir))
assert ret.result is True
ret = states.pip.removed(name=name, bin_env=str(venv_dir))
ret = states.pip.removed(name=pkg_name, bin_env=str(venv.venv_dir))
assert ret.result is True
@ -117,15 +133,12 @@ def test_pip_installed_errors(tmp_path, modules, state_tree):
# * "Error installing 'pep8': /tmp/pip-installed-errors: not found"
# * "Error installing 'pep8': /bin/sh: 1: /tmp/pip-installed-errors: not found"
# * "Error installing 'pep8': /bin/bash: /tmp/pip-installed-errors: No such file or directory"
sls_contents = """
pep8-pip:
pip.installed:
- name: pep8
- bin_env: '{}'
""".format(
str(venv_dir)
)
sls_contents = f"""
pep8-pip:
pip.installed:
- name: pep8
- bin_env: '{venv_dir}'
"""
with patched_environ(SHELL="/bin/sh"):
with pytest.helpers.temp_file(
"pip-installed-errors.sls", sls_contents, state_tree
@ -136,38 +149,30 @@ pep8-pip:
assert "Error installing 'pep8':" in state_return.comment
# We now create the missing virtualenv
ret = modules.virtualenv.create(str(venv_dir))
assert ret["retcode"] == 0
# The state should not have any issues running now
ret = modules.state.sls(mods="pip-installed-errors")
for state_return in ret:
assert state_return.result is True
with VirtualEnv(venv_dir=venv_dir):
# The state should not have any issues running now
ret = modules.state.sls(mods="pip-installed-errors")
for state_return in ret:
assert state_return.result is True
def test_pip_installed_name_test_mode(tmp_path, create_virtualenv, states):
def test_pip_installed_name_test_mode(states, venv, pkg_name):
"""
Test pip.installed state while test=true
"""
venv_dir = tmp_path / "pip_installed_test_mode_name"
create_virtualenv(str(venv_dir))
name = "pudb"
msg = "Python package(s) set to be installed:\npudb"
ret = states.pip.installed(name=name, bin_env=str(venv_dir), test=True)
assert name in ret.comment
ret = states.pip.installed(name=pkg_name, bin_env=str(venv.venv_dir), test=True)
assert pkg_name in ret.comment
def test_pip_installed_pkgs_test_mode(tmp_path, create_virtualenv, states):
def test_pip_installed_pkgs_test_mode(states, venv):
"""
Test pip.installed state while test=true
"""
venv_dir = tmp_path / "pip_installed_test_mode_pkgs"
create_virtualenv(str(venv_dir))
pkgs = ["boto", "pudb", "black"]
msg = "Python package(s) set to be installed:\nboto\npudb\nblack"
ret = states.pip.installed(name=None, pkgs=pkgs, bin_env=str(venv_dir), test=True)
pkgs = ["boto", "pep8", "black"]
msg = "Python package(s) set to be installed:\nboto\npep8\nblack"
ret = states.pip.installed(
name=None, pkgs=pkgs, bin_env=str(venv.venv_dir), test=True
)
assert msg in ret.comment
@ -177,36 +182,35 @@ def test_issue_2028_pip_installed_state(
):
venv_dir = tmp_path / "issue-2028-pip-installed"
python_executable = get_python_executable
sls_contents = """
{%- set virtualenv_base = salt['pillar.get']('venv_dir') %}
{%- set python_executable = salt['pillar.get']('python_executable') %}
{%- set virtualenv_base = salt['pillar.get']('venv_dir') %}
{%- set python_executable = salt['pillar.get']('python_executable') %}
{{ virtualenv_base }}:
virtualenv.managed:
- system_site_packages: False
- distribute: False
{#- Provide the real path for the python executable in case tests are running inside a virtualenv #}
{%- if python_executable %}
- python: {{ python_executable }}
{%- endif %}
{{ virtualenv_base }}:
virtualenv.managed:
- system_site_packages: False
- distribute: False
{#- Provide the real path for the python executable in case tests are running inside a virtualenv #}
{%- if python_executable %}
- python: {{ python_executable }}
{%- endif %}
install-working-setuptools:
pip.installed:
- name: 'setuptools!=50.*,!=51.*,!=52.*'
- bin_env: {{ virtualenv_base }}
- require:
- virtualenv: {{ virtualenv_base }}
install-working-setuptools:
pip.installed:
- name: 'setuptools!=50.*,!=51.*,!=52.*'
- bin_env: {{ virtualenv_base }}
- require:
- virtualenv: {{ virtualenv_base }}
pep8-pip:
pip.installed:
- name: pep8
- bin_env: {{ virtualenv_base }}
- require:
- pip: install-working-setuptools
- virtualenv: {{ virtualenv_base }}
"""
pep8-pip:
pip.installed:
- name: pep8
- bin_env: {{ virtualenv_base }}
- require:
- pip: install-working-setuptools
- virtualenv: {{ virtualenv_base }}
"""
with pytest.helpers.temp_file(
"issue-2028-pip-installed.sls", sls_contents, state_tree
):
@ -230,109 +234,98 @@ pep8-pip:
@pytest.mark.slow_test
def test_issue_2087_missing_pip(tmp_path, create_virtualenv, modules):
venv_dir = tmp_path / "issue-2087-missing-pip"
def test_issue_2087_missing_pip(modules, venv, pkg_name, state_tree):
sls_name = "issue-2087-missing-pip"
sls_contents = f"""
{pkg_name}-pip:
pip.installed:
- name: {pkg_name}
- bin_env: {venv.venv_dir}
"""
sls_contents = """pep8-pip:
pip.installed:
- name: pep8
- bin_env: {}
""".format(
str(venv_dir)
)
# Let's create the testing virtualenv
ret = create_virtualenv(str(venv_dir))
assert ret["retcode"] == 0
# Let's remove the pip binary
pip_bin = venv_dir / "bin" / "pip"
site_dir = modules.virtualenv.get_distribution_path(str(venv_dir), "pip")
if salt.utils.platform.is_windows():
pip_bin = venv_dir / "Scripts" / "pip.exe"
site_dir = venv_dir / "lib" / "site-packages"
if not os.path.isfile(str(pip_bin)):
pytest.skip("Failed to find the pip binary to the test virtualenv")
os.remove(str(pip_bin))
# Also remove the pip dir from site-packages
# This is needed now that we're using python -m pip instead of the
# pip binary directly. python -m pip will still work even if the
# pip binary is missing
shutil.rmtree(os.path.join(str(site_dir), "pip"))
# Let's run the state which should fail because pip is missing
ret = modules.state.sls(mods="issue-2087-missing-pip")
for state_return in ret:
assert state_return.result is True
assert (
"Error installing 'pep8': Could not find a `pip` binary"
in state_return.comment
with pytest.helpers.temp_file(f"{sls_name}.sls", sls_contents, state_tree):
# Let's remove the pip binary
pip_bin = venv.venv_bin_dir / "pip"
site_dir = pathlib.Path(
modules.virtualenv.get_distribution_path(str(venv.venv_dir), "pip")
)
if salt.utils.platform.is_windows():
pip_bin = venv.venv_dir / "Scripts" / "pip.exe"
site_dir = venv.venv_dir / "lib" / "site-packages"
if not pip_bin.is_file():
pytest.skip("Failed to find the pip binary to the test virtualenv")
pip_bin.unlink()
# Also remove the pip dir from site-packages
# This is needed now that we're using python -m pip instead of the
# pip binary directly. python -m pip will still work even if the
# pip binary is missing
shutil.rmtree(site_dir / "pip")
# Let's run the state which should fail because pip is missing
ret = modules.state.sls(mods=sls_name)
for state_return in ret:
assert state_return.result is False
assert (
f"Error installing '{pkg_name}': Could not find a `pip` binary"
in state_return.comment
)
@SKIP_INITIAL_PHOTONOS_FAILURES
@pytest.mark.destructive_test
@pytest.mark.slow_test
@pytest.mark.skip_if_not_root
def test_issue_6912_wrong_owner(tmp_path, create_virtualenv, modules, states):
def test_issue_6912_wrong_owner(tmp_path, create_virtualenv, states, account):
# Setup virtual environment directory to be used throughout the test
venv_dir = tmp_path / "6912-wrong-owner"
venv_kwargs = {}
with pytest.helpers.create_account(
username="issue-6912", password="PassWord1!"
) as account:
# The virtual environment needs to be in a location that is accessible
# by both the user running the test and the runas user
if salt.utils.platform.is_windows():
salt.utils.win_dacl.set_permissions(
tmp_path, account.username, "full_control"
)
# Make sure we're calling a virtualenv and python
# program that the user has access too.
venv_kwargs["venv_bin"] = _win_user_where(
account.username,
"PassWord1!",
"virtualenv",
)
venv_kwargs["python"] = _win_user_where(
account.username,
"PassWord1!",
"python",
)
else:
uid = modules.file.user_to_uid(account.username)
os.chown(str(tmp_path), uid, -1)
# Create the virtual environment
venv_create = create_virtualenv(
str(venv_dir), user=account.username, password="PassWord1!", **venv_kwargs
# The virtual environment needs to be in a location that is accessible
# by both the user running the test and the runas user
if salt.utils.platform.is_windows():
salt.utils.win_dacl.set_permissions(tmp_path, account.username, "full_control")
# Make sure we're calling a virtualenv and python
# program that the user has access too.
venv_kwargs["venv_bin"] = _win_user_where(
account.username,
account.password,
"virtualenv",
)
if venv_create.get("retcode", 1) > 0:
pytest.skip(f"Failed to create testcase virtual environment: {venv_create}")
# pip install passing the package name in `name`
ret = states.pip.installed(
name="pep8",
user=account.username,
bin_env=str(venv_dir),
password="PassWord1!",
venv_kwargs["python"] = _win_user_where(
account.username,
account.password,
"python",
)
assert ret.result is True
else:
os.chown(str(tmp_path), account.info.uid, -1)
if HAS_PWD:
uid = pwd.getpwnam(account.username).pw_uid
for globmatch in (
os.path.join(str(venv_dir), "**", "pep8*"),
os.path.join(str(venv_dir), "*", "**", "pep8*"),
os.path.join(str(venv_dir), "*", "*", "**", "pep8*"),
):
for path in glob.glob(globmatch):
if HAS_PWD:
assert uid == os.stat(path).st_uid
elif salt.utils.platform.is_windows():
assert salt.utils.win_dacl.get_owner(path) == account.username
# Create the virtual environment
venv_create = create_virtualenv(
str(venv_dir), user=account.username, password=account.password, **venv_kwargs
)
if venv_create.get("retcode", 1) > 0:
pytest.skip(f"Failed to create testcase virtual environment: {venv_create}")
# pip install passing the package name in `name`
ret = states.pip.installed(
name="pep8",
user=account.username,
password=account.password,
bin_env=str(venv_dir),
)
assert ret.result is True
for globmatch in (
os.path.join(str(venv_dir), "**", "pep8*"),
os.path.join(str(venv_dir), "*", "**", "pep8*"),
os.path.join(str(venv_dir), "*", "*", "**", "pep8*"),
):
for path in glob.glob(globmatch):
if salt.utils.platform.is_windows():
assert salt.utils.win_dacl.get_owner(path) == account.username
else:
assert os.stat(path).st_uid == account.info.uid
@SKIP_INITIAL_PHOTONOS_FAILURES
@ -341,70 +334,62 @@ def test_issue_6912_wrong_owner(tmp_path, create_virtualenv, modules, states):
@pytest.mark.slow_test
@pytest.mark.skip_if_not_root
def test_issue_6912_wrong_owner_requirements_file(
tmp_path, create_virtualenv, state_tree, modules, states
tmp_path, create_virtualenv, state_tree, states, account
):
# Setup virtual environment directory to be used throughout the test
venv_dir = tmp_path / "6912-wrong-owner"
venv_kwargs = {}
with pytest.helpers.create_account(
username="issue-6912", password="PassWord1!"
) as account:
# The virtual environment needs to be in a location that is accessible
# by both the user running the test and the runas user
if salt.utils.platform.is_windows():
salt.utils.win_dacl.set_permissions(
str(tmp_path), account.username, "full_control"
)
# Make sure we're calling a virtualenv and python
# program that the user has access too.
venv_kwargs["venv_bin"] = _win_user_where(
account.username,
"PassWord1!",
"virtualenv",
)
venv_kwargs["python"] = _win_user_where(
account.username,
"PassWord1!",
"python",
)
else:
uid = modules.file.user_to_uid(account.username)
os.chown(str(tmp_path), uid, -1)
# Create the virtual environment again as it should have been removed
venv_create = create_virtualenv(
str(venv_dir), user=account.username, password="PassWord1!", **venv_kwargs
# The virtual environment needs to be in a location that is accessible
# by both the user running the test and the runas user
if salt.utils.platform.is_windows():
salt.utils.win_dacl.set_permissions(
str(tmp_path), account.username, "full_control"
)
if venv_create.get("retcode", 1) > 0:
pytest.skip(f"failed to create testcase virtual environment: {venv_create}")
# Make sure we're calling a virtualenv and python
# program that the user has access too.
venv_kwargs["venv_bin"] = _win_user_where(
account.username,
account.password,
"virtualenv",
)
venv_kwargs["python"] = _win_user_where(
account.username,
account.password,
"python",
)
else:
os.chown(str(tmp_path), account.info.uid, -1)
# pip install using a requirements file
contents = "pep8\n"
with pytest.helpers.temp_file(
"issue-6912-requirements.txt", contents, state_tree
):
ret = states.pip.installed(
name="",
user=account.username,
bin_env=str(venv_dir),
requirements="salt://issue-6912-requirements.txt",
password="PassWord1!",
)
assert ret.result is True
# Create the virtual environment again as it should have been removed
venv_create = create_virtualenv(
str(venv_dir), user=account.username, password=account.password, **venv_kwargs
)
if venv_create.get("retcode", 1) > 0:
pytest.skip(f"failed to create testcase virtual environment: {venv_create}")
if HAS_PWD:
uid = pwd.getpwnam(account.username).pw_uid
for globmatch in (
os.path.join(str(venv_dir), "**", "pep8*"),
os.path.join(str(venv_dir), "*", "**", "pep8*"),
os.path.join(str(venv_dir), "*", "*", "**", "pep8*"),
):
for path in glob.glob(globmatch):
if HAS_PWD:
assert uid == os.stat(path).st_uid
elif salt.utils.platform.is_windows():
assert salt.utils.win_dacl.get_owner(path) == account.username
# pip install using a requirements file
contents = "pep8\n"
with pytest.helpers.temp_file("issue-6912-requirements.txt", contents, state_tree):
ret = states.pip.installed(
name="",
user=account.username,
password=account.password,
bin_env=str(venv_dir),
requirements="salt://issue-6912-requirements.txt",
)
assert ret.result is True
for globmatch in (
os.path.join(str(venv_dir), "**", "pep8*"),
os.path.join(str(venv_dir), "*", "**", "pep8*"),
os.path.join(str(venv_dir), "*", "*", "**", "pep8*"),
):
for path in glob.glob(globmatch):
if salt.utils.platform.is_windows():
assert salt.utils.win_dacl.get_owner(path) == account.username
else:
assert os.stat(path).st_uid == account.info.uid
@pytest.mark.destructive_test
@ -465,12 +450,7 @@ def test_issue_6833_pip_upgrade_pip(tmp_path, create_virtualenv, modules, states
@pytest.mark.slow_test
def test_pip_installed_specific_env(
tmp_path, state_tree_prod, states, create_virtualenv
):
# Create the testing virtualenv
venv_dir = tmp_path / "pip-installed-specific-env"
def test_pip_installed_specific_env(state_tree_prod, states, venv):
contents = "pep8\n"
# Let's write a requirements file
@ -478,12 +458,10 @@ def test_pip_installed_specific_env(
"prod-env-requirements.txt", contents, state_tree_prod
):
create_virtualenv(str(venv_dir))
# The requirements file should not be found the base environment
ret = states.pip.installed(
name="",
bin_env=str(venv_dir),
bin_env=str(venv.venv_dir),
requirements="salt://prod-env-requirements.txt",
)
assert ret.result is False
@ -492,7 +470,7 @@ def test_pip_installed_specific_env(
# The requirements file must be found in the prod environment
ret = states.pip.installed(
name="",
bin_env=str(venv_dir),
bin_env=str(venv.venv_dir),
saltenv="prod",
requirements="salt://prod-env-requirements.txt",
)
@ -506,7 +484,7 @@ def test_pip_installed_specific_env(
# environment as an url arg to salt://
ret = states.pip.installed(
name="",
bin_env=str(venv_dir),
bin_env=str(venv.venv_dir),
requirements="salt://prod-env-requirements.txt?saltenv=prod",
)
assert ret.result is True
@ -514,31 +492,20 @@ def test_pip_installed_specific_env(
@pytest.mark.slow_test
def test_22359_pip_installed_unless_does_not_trigger_warnings(
create_virtualenv, tmp_path, states
):
def test_22359_pip_installed_unless_does_not_trigger_warnings(states, venv):
# This test case should be moved to a format_call unit test specific to
# the state internal keywords
venv_dir = str(tmp_path / "pip-installed-unless")
venv_create = create_virtualenv(venv_dir)
if venv_create["retcode"] > 0:
pytest.skip(f"Failed to create testcase virtual environment: {venv_create}")
false_cmd = salt.utils.path.which("false")
if salt.utils.platform.is_windows():
false_cmd = "exit 1 >nul"
try:
ret = states.pip.installed(
name="pep8",
bin_env=str(venv_dir),
unless=false_cmd,
timeout=600,
)
assert ret.result is True
assert "warnings" not in next(iter(ret.raw.values()))
finally:
if os.path.isdir(str(venv_dir)):
shutil.rmtree(str(venv_dir), ignore_errors=True)
ret = states.pip.installed(
name="pep8",
bin_env=str(venv.venv_dir),
unless=false_cmd,
timeout=600,
)
assert ret.result is True
assert "warnings" not in next(iter(ret.raw.values()))
@pytest.mark.windows_whitelisted