Untangle test/integration/shell/test_proxy.py

This commit is contained in:
Pedro Algarvio 2020-04-08 18:47:16 +01:00
parent ea0482296e
commit b536d2dc85
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
6 changed files with 102 additions and 189 deletions

View file

@ -594,6 +594,7 @@ def groups_collection_modifyitems(config, items):
# <---- Test Groups Selection ----------------------------------------------------------------------------------------
# ----- Fixtures Overrides ------------------------------------------------------------------------------------------>
@pytest.fixture(scope="session")
def salt_factories_config():
@ -610,7 +611,7 @@ def salt_factories_config():
}
# <---- Pytest Helpers -----------------------------------------------------------------------------------------------
# <---- Fixtures Overrides -------------------------------------------------------------------------------------------
# ----- Salt Factories ---------------------------------------------------------------------------------------------->

View file

@ -1,165 +0,0 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: Thayne Harbaugh (tharbaug@adobe.com)
tests.integration.shell.proxy
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
from __future__ import absolute_import, print_function, unicode_literals
import logging
import pytest
import salt.utils.platform
import tests.integration.utils
from tests.integration.utils import testprogram
from tests.support.helpers import slowTest
from tests.support.unit import skipIf
log = logging.getLogger(__name__)
@pytest.mark.windows_whitelisted
class ProxyTest(testprogram.TestProgramCase):
"""
Various integration tests for the salt-proxy executable.
"""
@slowTest
def test_exit_status_no_proxyid(self):
"""
Ensure correct exit status when --proxyid argument is missing.
"""
proxy = testprogram.TestDaemonSaltProxy(
name="proxy-no_proxyid", parent_dir=self._test_dir,
)
# Call setup here to ensure config and script exist
proxy.setup()
# Needed due to verbatim_args=True
args = ["--config-dir", proxy.abs_path(proxy.config_dir)]
if not salt.utils.platform.is_windows():
args.append("-d")
stdout, stderr, status = proxy.run(
args=args,
# verbatim_args prevents --proxyid from being added automatically
verbatim_args=True,
catch_stderr=True,
with_retcode=True,
# The proxy minion had a bug where it would loop forever
# without daemonizing - protect that with a timeout.
timeout=60,
)
try:
self.assert_exit_status(
status,
"EX_USAGE",
message="no --proxyid specified",
stdout=stdout,
stderr=tests.integration.utils.decode_byte_list(stderr),
)
finally:
# Although the start-up should fail, call shutdown() to set the
# internal _shutdown flag and avoid the registered atexit calls to
# cause timeout exceptions and respective traceback
proxy.shutdown()
# Hangs on Windows. You can add a timeout to the proxy.run command, but then
# it just times out.
@skipIf(salt.utils.platform.is_windows(), "Test hangs on Windows")
@slowTest
def test_exit_status_unknown_user(self):
"""
Ensure correct exit status when the proxy is configured to run as an
unknown user.
"""
proxy = testprogram.TestDaemonSaltProxy(
name="proxy-unknown_user",
config_base={"user": "some_unknown_user_xyz"},
parent_dir=self._test_dir,
)
# Call setup here to ensure config and script exist
proxy.setup()
stdout, stderr, status = proxy.run(
args=["-d"] if not salt.utils.platform.is_windows() else [],
catch_stderr=True,
with_retcode=True,
)
try:
self.assert_exit_status(
status,
"EX_NOUSER",
message="unknown user not on system",
stdout=stdout,
stderr=tests.integration.utils.decode_byte_list(stderr),
)
finally:
# Although the start-up should fail, call shutdown() to set the
# internal _shutdown flag and avoid the registered atexit calls to
# cause timeout exceptions and respective traceback
proxy.shutdown()
@slowTest
def test_exit_status_unknown_argument(self):
"""
Ensure correct exit status when an unknown argument is passed to
salt-proxy.
"""
proxy = testprogram.TestDaemonSaltProxy(
name="proxy-unknown_argument", parent_dir=self._test_dir,
)
# Call setup here to ensure config and script exist
proxy.setup()
args = ["--unknown-argument"]
if not salt.utils.platform.is_windows():
args.append("-b")
stdout, stderr, status = proxy.run(
args=args, catch_stderr=True, with_retcode=True,
)
try:
self.assert_exit_status(
status,
"EX_USAGE",
message="unknown argument",
stdout=stdout,
stderr=stderr,
)
finally:
# Although the start-up should fail, call shutdown() to set the
# internal _shutdown flag and avoid the registered atexit calls to
# cause timeout exceptions and respective traceback
proxy.shutdown()
# Hangs on Windows. You can add a timeout to the proxy.run command, but then
# it just times out.
@skipIf(salt.utils.platform.is_windows(), "Test hangs on Windows")
@slowTest
def test_exit_status_correct_usage(self):
"""
Ensure correct exit status when salt-proxy starts correctly.
Skip on Windows because daemonization not supported
"""
proxy = testprogram.TestDaemonSaltProxy(
name="proxy-correct_usage", parent_dir=self._test_dir,
)
# Call setup here to ensure config and script exist
proxy.setup()
stdout, stderr, status = proxy.run(
args=["-d"] if not salt.utils.platform.is_windows() else [],
catch_stderr=True,
with_retcode=True,
)
try:
self.assert_exit_status(
status,
"EX_OK",
message="correct usage",
stdout=stdout,
stderr=tests.integration.utils.decode_byte_list(stderr),
)
finally:
proxy.shutdown(wait_for_orphans=3)

View file

@ -908,28 +908,6 @@ class TestDaemonSaltSyndic(TestSaltDaemon):
super().__init__(*args, **kwargs)
@pytest.mark.windows_whitelisted
class TestDaemonSaltProxy(TestSaltDaemon):
"""
Manager for salt-proxy daemon.
"""
pid_file = "salt-minion.pid"
configs = {"proxy": {}}
def __init__(self, *args, **kwargs):
cfgb = kwargs.setdefault("config_base", {})
_ = cfgb.setdefault("user", getpass.getuser())
super().__init__(*args, **kwargs)
def run(self, **kwargs):
if not kwargs.get("verbatim_args"):
args = kwargs.setdefault("args", [])
if "--proxyid" not in args:
args.extend(["--proxyid", self.name])
return super().run(**kwargs)
@pytest.mark.windows_whitelisted
class TestProgramCase(TestCase):
"""

View file

@ -0,0 +1,100 @@
"""
:codeauthor: Thayne Harbaugh (tharbaug@adobe.com)
tests.pytests.integration.cli.test_proxy
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Various integration tests for the salt-proxy executable.
"""
import logging
import time
import pytest
import salt.defaults.exitcodes
from saltfactories.exceptions import FactoryNotStarted
from saltfactories.utils import random_string
from tests.support.helpers import PRE_PYTEST_SKIP_REASON, slowTest
log = logging.getLogger(__name__)
@pytest.fixture
def proxy_minion_id(salt_factories, salt_master):
_proxy_minion_id = random_string("proxy-minion-")
try:
yield _proxy_minion_id
finally:
# Remove stale key if it exists
pytest.helpers.remove_stale_minion_key(salt_master, _proxy_minion_id)
@slowTest
def test_exit_status_no_proxyid(salt_master, proxy_minion_id):
"""
Ensure correct exit status when --proxyid argument is missing.
"""
with pytest.raises(FactoryNotStarted) as exc:
factory = salt_master.get_salt_proxy_minion_daemon(
proxy_minion_id, include_proxyid_cli_flag=False
)
factory.start(start_timeout=10, max_start_attempts=1)
assert exc.value.exitcode == salt.defaults.exitcodes.EX_USAGE, exc.value
assert "Usage" in exc.value.stderr, exc.value
assert "error: salt-proxy requires --proxyid" in exc.value.stderr, exc.value
# Hangs on Windows. You can add a timeout to the proxy.run command, but then
# it just times out.
@pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
def test_exit_status_unknown_user(salt_master, proxy_minion_id):
"""
Ensure correct exit status when the proxy is configured to run as an
unknown user.
"""
with pytest.raises(FactoryNotStarted) as exc:
factory = salt_master.get_salt_proxy_minion_daemon(
proxy_minion_id, config_overrides={"user": "unknown-user"}
)
factory.start(start_timeout=10, max_start_attempts=1)
assert exc.value.exitcode == salt.defaults.exitcodes.EX_NOUSER, exc.value
assert "The user is not available." in exc.value.stderr, exc.value
@slowTest
def test_exit_status_unknown_argument(salt_master, proxy_minion_id):
"""
Ensure correct exit status when an unknown argument is passed to
salt-proxy.
"""
with pytest.raises(FactoryNotStarted) as exc:
factory = salt_master.get_salt_proxy_minion_daemon(proxy_minion_id)
factory.start("--unknown-argument", start_timeout=10, max_start_attempts=1)
assert exc.value.exitcode == salt.defaults.exitcodes.EX_USAGE, exc.value
assert "Usage" in exc.value.stderr, exc.value
assert "no such option: --unknown-argument" in exc.value.stderr, exc.value
# Hangs on Windows. You can add a timeout to the proxy.run command, but then
# it just times out.
@pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
def test_exit_status_correct_usage(salt_master, proxy_minion_id):
"""
Ensure correct exit status when salt-proxy starts correctly.
Skip on Windows because daemonization not supported
"""
factory = salt_master.get_salt_proxy_minion_daemon(
proxy_minion_id,
extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
config_defaults={"transport": salt_master.config["transport"]},
)
factory.start()
assert factory.is_running()
time.sleep(0.5)
ret = factory.terminate()
assert ret.exitcode == salt.defaults.exitcodes.EX_OK, ret

View file

@ -81,7 +81,6 @@ integration.shell.test_master
integration.shell.test_master_tops
integration.shell.test_matcher
integration.shell.test_minion
integration.shell.test_proxy
integration.shell.test_runner
integration.shell.test_saltcli
integration.shell.test_spm