test_minion.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.shell.minion
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. """
  7. from __future__ import absolute_import
  8. import logging
  9. import time
  10. import pytest
  11. import salt.defaults.exitcodes
  12. from saltfactories.exceptions import ProcessNotStarted
  13. from tests.support.helpers import PRE_PYTEST_SKIP_REASON
  14. log = logging.getLogger(__name__)
  15. @pytest.fixture(scope="module")
  16. def shell_tests_salt_master(request, salt_factories):
  17. return salt_factories.spawn_master(request, "minion-shell-tests")
  18. @pytest.fixture(scope="module")
  19. def shell_tests_salt_minion_config(request, salt_factories, shell_tests_salt_master):
  20. return salt_factories.configure_minion(
  21. request,
  22. shell_tests_salt_master.config["id"],
  23. master_id=shell_tests_salt_master.config["id"],
  24. config_overrides={"user": "unknown-user"},
  25. )
  26. @pytest.mark.windows_whitelisted
  27. class TestSaltMinionCLI(object):
  28. @pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
  29. def test_exit_status_unknown_user(
  30. self, request, salt_factories, shell_tests_salt_minion_config
  31. ):
  32. """
  33. Ensure correct exit status when the minion is configured to run as an unknown user.
  34. """
  35. with pytest.raises(ProcessNotStarted) as exc:
  36. salt_factories.spawn_minion(
  37. request,
  38. shell_tests_salt_minion_config["id"],
  39. master_id=shell_tests_salt_minion_config["id"],
  40. max_start_attempts=1,
  41. )
  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. def test_exit_status_unknown_argument(
  45. self, request, salt_factories, shell_tests_salt_minion_config, tempdir
  46. ):
  47. """
  48. Ensure correct exit status when an unknown argument is passed to salt-minion.
  49. """
  50. # We pass root_dir in order not to hit the max length socket path issue
  51. root_dir = tempdir.join("ex-st-unkn-arg-minion").ensure(dir=True)
  52. with pytest.raises(ProcessNotStarted) as exc:
  53. salt_factories.spawn_minion(
  54. request,
  55. shell_tests_salt_minion_config["id"],
  56. master_id=shell_tests_salt_minion_config["id"],
  57. max_start_attempts=1,
  58. base_script_args=["--unknown-argument"],
  59. config_defaults={"root_dir": root_dir},
  60. )
  61. assert exc.value.exitcode == salt.defaults.exitcodes.EX_USAGE, exc.value
  62. assert "Usage" in exc.value.stderr, exc.value
  63. assert "no such option: --unknown-argument" in exc.value.stderr, exc.value
  64. @pytest.mark.skip_on_windows(reason=PRE_PYTEST_SKIP_REASON)
  65. def test_exit_status_correct_usage(
  66. self, request, salt_factories, shell_tests_salt_master
  67. ):
  68. proc = salt_factories.spawn_minion(
  69. request,
  70. shell_tests_salt_master.config["id"] + "-2",
  71. master_id=shell_tests_salt_master.config["id"],
  72. )
  73. assert proc.is_alive()
  74. time.sleep(1)
  75. ret = proc.terminate()
  76. assert ret.exitcode == salt.defaults.exitcodes.EX_OK, ret