test_salt_proxy.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. :codeauthor: Thayne Harbaugh (tharbaug@adobe.com)
  3. tests.pytests.integration.cli.test_proxy
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Various integration tests for the salt-proxy executable.
  6. """
  7. import logging
  8. import time
  9. import pytest
  10. import salt.defaults.exitcodes
  11. from saltfactories.exceptions import FactoryNotStarted
  12. from saltfactories.utils import random_string
  13. from tests.support.helpers import PRE_PYTEST_SKIP_REASON, slowTest
  14. log = logging.getLogger(__name__)
  15. @pytest.fixture
  16. def proxy_minion_id(salt_factories, salt_master):
  17. _proxy_minion_id = random_string("proxy-minion-")
  18. try:
  19. yield _proxy_minion_id
  20. finally:
  21. # Remove stale key if it exists
  22. pytest.helpers.remove_stale_minion_key(salt_master, _proxy_minion_id)
  23. @slowTest
  24. def test_exit_status_no_proxyid(salt_master, proxy_minion_id):
  25. """
  26. Ensure correct exit status when --proxyid argument is missing.
  27. """
  28. with pytest.raises(FactoryNotStarted) as exc:
  29. factory = salt_master.get_salt_proxy_minion_daemon(
  30. proxy_minion_id, include_proxyid_cli_flag=False
  31. )
  32. factory.start(start_timeout=10, max_start_attempts=1)
  33. assert exc.value.exitcode == salt.defaults.exitcodes.EX_USAGE, exc.value
  34. assert "Usage" in exc.value.stderr, exc.value
  35. assert "error: salt-proxy requires --proxyid" in exc.value.stderr, exc.value
  36. # Hangs on Windows. You can add a timeout to the proxy.run command, but then
  37. # it just times out.
  38. @pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
  39. def test_exit_status_unknown_user(salt_master, proxy_minion_id):
  40. """
  41. Ensure correct exit status when the proxy is configured to run as an
  42. unknown user.
  43. """
  44. with pytest.raises(FactoryNotStarted) as exc:
  45. factory = salt_master.get_salt_proxy_minion_daemon(
  46. proxy_minion_id, config_overrides={"user": "unknown-user"}
  47. )
  48. factory.start(start_timeout=10, max_start_attempts=1)
  49. assert exc.value.exitcode == salt.defaults.exitcodes.EX_NOUSER, exc.value
  50. assert "The user is not available." in exc.value.stderr, exc.value
  51. @slowTest
  52. def test_exit_status_unknown_argument(salt_master, proxy_minion_id):
  53. """
  54. Ensure correct exit status when an unknown argument is passed to
  55. salt-proxy.
  56. """
  57. with pytest.raises(FactoryNotStarted) as exc:
  58. factory = salt_master.get_salt_proxy_minion_daemon(proxy_minion_id)
  59. factory.start("--unknown-argument", start_timeout=10, max_start_attempts=1)
  60. assert exc.value.exitcode == salt.defaults.exitcodes.EX_USAGE, exc.value
  61. assert "Usage" in exc.value.stderr, exc.value
  62. assert "no such option: --unknown-argument" in exc.value.stderr, exc.value
  63. # Hangs on Windows. You can add a timeout to the proxy.run command, but then
  64. # it just times out.
  65. @pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
  66. def test_exit_status_correct_usage(salt_master, proxy_minion_id):
  67. """
  68. Ensure correct exit status when salt-proxy starts correctly.
  69. Skip on Windows because daemonization not supported
  70. """
  71. factory = salt_master.get_salt_proxy_minion_daemon(
  72. proxy_minion_id,
  73. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  74. config_defaults={"transport": salt_master.config["transport"]},
  75. )
  76. factory.start()
  77. assert factory.is_running()
  78. time.sleep(0.5)
  79. ret = factory.terminate()
  80. assert ret.exitcode == salt.defaults.exitcodes.EX_OK, ret