1
0

test_salt_syndic.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  3. tests.integration.shell.syndic
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. """
  6. import logging
  7. import os
  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, PRE_PYTEST_SKIP_REASON, slowTest
  14. log = logging.getLogger(__name__)
  15. pytestmark = pytest.mark.windows_whitelisted
  16. @pytest.fixture
  17. def syndic_id(salt_factories, salt_master):
  18. _syndic_id = random_string("syndic-")
  19. try:
  20. yield _syndic_id
  21. finally:
  22. # Remove stale key if it exists
  23. syndic_key_file = os.path.join(
  24. salt_master.config["pki_dir"], "syndics", _syndic_id
  25. )
  26. if os.path.exists(syndic_key_file):
  27. log.debug("syndic %r KEY FILE: %s", _syndic_id, syndic_key_file)
  28. os.unlink(syndic_key_file)
  29. @slowTest
  30. @PRE_PYTEST_SKIP
  31. @pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
  32. def test_exit_status_unknown_user(salt_master, syndic_id):
  33. """
  34. Ensure correct exit status when the syndic is configured to run as an unknown user.
  35. """
  36. with pytest.raises(FactoryNotStarted) as exc:
  37. factory = salt_master.get_salt_syndic_daemon(
  38. syndic_id, config_overrides={"user": "unknown-user"}
  39. )
  40. factory.before_start_callbacks.clear()
  41. factory.start(start_timeout=10, max_start_attempts=1)
  42. assert exc.value.exitcode == salt.defaults.exitcodes.EX_NOUSER, exc.value
  43. assert "The user is not available." in exc.value.stderr, exc.value
  44. @slowTest
  45. @PRE_PYTEST_SKIP
  46. def test_exit_status_unknown_argument(salt_master, syndic_id):
  47. """
  48. Ensure correct exit status when an unknown argument is passed to salt-syndic.
  49. """
  50. with pytest.raises(FactoryNotStarted) as exc:
  51. factory = salt_master.get_salt_syndic_daemon(syndic_id)
  52. factory.before_start_callbacks.clear()
  53. factory.start("--unknown-argument", start_timeout=10, max_start_attempts=1)
  54. assert exc.value.exitcode == salt.defaults.exitcodes.EX_USAGE, exc.value
  55. assert "Usage" in exc.value.stderr, exc.value
  56. assert "no such option: --unknown-argument" in exc.value.stderr, exc.value
  57. @slowTest
  58. @PRE_PYTEST_SKIP
  59. @pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
  60. def test_exit_status_correct_usage(salt_master, syndic_id):
  61. factory = salt_master.get_salt_syndic_daemon(
  62. syndic_id,
  63. extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
  64. config_defaults={"transport": salt_master.config["transport"]},
  65. )
  66. factory.start()
  67. assert factory.is_running()
  68. time.sleep(0.5)
  69. ret = factory.terminate()
  70. assert ret.exitcode == salt.defaults.exitcodes.EX_OK, ret