mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Migrate tests/unit/states/test_pip_state.py::PipStateInstallationErrorTest::test_importable_installation_error
to pytest
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
parent
7d6a97e7ba
commit
022f73aa7c
2 changed files with 78 additions and 93 deletions
75
tests/pytests/integration/states/test_pip_state.py
Normal file
75
tests/pytests/integration/states/test_pip_state.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.version
|
||||
from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
|
||||
from tests.support.helpers import VirtualEnv, dedent
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.skip_if_binaries_missing(*KNOWN_BINARY_NAMES, check_all=False),
|
||||
pytest.mark.requires_network,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def _extra_requirements():
|
||||
extra_requirements = []
|
||||
for name, version in salt.version.dependency_information():
|
||||
if name in ["PyYAML", "packaging", "looseversion"]:
|
||||
extra_requirements.append(f"{name}=={version}")
|
||||
return extra_requirements
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
@pytest.mark.parametrize(
|
||||
"pip_contraint",
|
||||
[
|
||||
# Latest pip 18
|
||||
"<19.0",
|
||||
# Latest pip 19
|
||||
"<20.0",
|
||||
# Latest pip 20
|
||||
"<21.0",
|
||||
# Latest pip
|
||||
None,
|
||||
],
|
||||
)
|
||||
def test_importable_installation_error(_extra_requirements, pip_contraint):
|
||||
code = dedent(
|
||||
"""\
|
||||
import sys
|
||||
import traceback
|
||||
try:
|
||||
import salt.states.pip_state
|
||||
salt.states.pip_state.InstallationError
|
||||
except ImportError as exc:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(1)
|
||||
except AttributeError as exc:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(2)
|
||||
except Exception as exc:
|
||||
traceback.print_exc(exc, file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(3)
|
||||
sys.exit(0)
|
||||
"""
|
||||
)
|
||||
with VirtualEnv() as venv:
|
||||
venv.install(*_extra_requirements)
|
||||
if pip_contraint:
|
||||
venv.install(f"pip{pip_contraint}")
|
||||
try:
|
||||
subprocess.check_output([venv.venv_python, "-c", code])
|
||||
except subprocess.CalledProcessError as exc:
|
||||
if exc.returncode == 1:
|
||||
pytest.fail(f"Failed to import pip:\n{exc.output}")
|
||||
elif exc.returncode == 2:
|
||||
pytest.fail(
|
||||
f"Failed to import InstallationError from pip:\n{exc.output}"
|
||||
)
|
||||
else:
|
||||
pytest.fail(exc.output)
|
|
@ -1,11 +1,5 @@
|
|||
"""
|
||||
tests.unit.states.pip_test
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
@ -13,27 +7,18 @@ import pytest
|
|||
import salt.states.pip_state as pip_state
|
||||
import salt.utils.path
|
||||
import salt.version
|
||||
from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
|
||||
from tests.support.helpers import VirtualEnv, dedent
|
||||
from tests.support.mixins import LoaderModuleMockMixin, SaltReturnAssertsMixin
|
||||
from tests.support.mock import MagicMock, patch
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
try:
|
||||
import pip
|
||||
|
||||
HAS_PIP = True
|
||||
except ImportError:
|
||||
HAS_PIP = False
|
||||
|
||||
pip = pytest.importorskip(
|
||||
"pip", reason="The 'pip' library is not importable(installed system-wide)"
|
||||
)
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not HAS_PIP, reason="The 'pip' library is not importable(installed system-wide)"
|
||||
)
|
||||
class PipStateTest(TestCase, SaltReturnAssertsMixin, LoaderModuleMockMixin):
|
||||
def setup_loader_modules(self):
|
||||
return {
|
||||
|
@ -422,78 +407,3 @@ class PipStateUtilsTest(TestCase):
|
|||
mock_modules.pop("pip", None)
|
||||
with patch("sys.modules", mock_modules):
|
||||
pip_state.purge_pip()
|
||||
|
||||
|
||||
@pytest.mark.skip_if_binaries_missing(*KNOWN_BINARY_NAMES, check_all=False)
|
||||
@pytest.mark.requires_network
|
||||
class PipStateInstallationErrorTest(TestCase):
|
||||
@pytest.mark.slow_test
|
||||
def test_importable_installation_error(self):
|
||||
extra_requirements = []
|
||||
for name, version in salt.version.dependency_information():
|
||||
if name in ["PyYAML", "packaging", "looseversion"]:
|
||||
extra_requirements.append(f"{name}=={version}")
|
||||
failures = {}
|
||||
pip_version_requirements = [
|
||||
# Latest pip 18
|
||||
"<19.0",
|
||||
# Latest pip 19
|
||||
"<20.0",
|
||||
# Latest pip 20
|
||||
"<21.0",
|
||||
# Latest pip
|
||||
None,
|
||||
]
|
||||
code = dedent(
|
||||
"""\
|
||||
import sys
|
||||
import traceback
|
||||
try:
|
||||
import salt.states.pip_state
|
||||
salt.states.pip_state.InstallationError
|
||||
except ImportError as exc:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(1)
|
||||
except AttributeError as exc:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(2)
|
||||
except Exception as exc:
|
||||
traceback.print_exc(exc, file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(3)
|
||||
sys.exit(0)
|
||||
"""
|
||||
)
|
||||
for requirement in list(pip_version_requirements):
|
||||
try:
|
||||
with VirtualEnv() as venv:
|
||||
venv.install(*extra_requirements)
|
||||
if requirement:
|
||||
venv.install(f"pip{requirement}")
|
||||
try:
|
||||
subprocess.check_output([venv.venv_python, "-c", code])
|
||||
except subprocess.CalledProcessError as exc:
|
||||
if exc.returncode == 1:
|
||||
failures[requirement] = "Failed to import pip:\n{}".format(
|
||||
exc.output
|
||||
)
|
||||
elif exc.returncode == 2:
|
||||
failures[
|
||||
requirement
|
||||
] = "Failed to import InstallationError from pip:\n{}".format(
|
||||
exc.output
|
||||
)
|
||||
else:
|
||||
failures[requirement] = exc.output
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
failures[requirement] = str(exc)
|
||||
if failures:
|
||||
errors = ""
|
||||
for requirement, exception in failures.items():
|
||||
errors += "pip{}: {}\n\n".format(requirement or "", exception)
|
||||
self.fail(
|
||||
"Failed to get InstallationError exception under at least one pip"
|
||||
" version:\n{}".format(errors)
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue