1
0

test_salt_minion.py 2.6 KB

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