test_salt_run.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """
  2. Tests for the salt-run command
  3. """
  4. import re
  5. import pytest
  6. import salt.utils.files
  7. import salt.utils.platform
  8. import salt.utils.pycrypto
  9. import salt.utils.yaml
  10. from tests.support.helpers import slowTest
  11. USERA = "saltdev-runner"
  12. USERA_PWD = "saltdev"
  13. pytestmark = pytest.mark.windows_whitelisted
  14. @pytest.fixture(scope="module")
  15. def saltdev_account(sminion):
  16. try:
  17. assert sminion.functions.user.add(USERA, createhome=False)
  18. assert sminion.functions.shadow.set_password(
  19. USERA,
  20. USERA_PWD
  21. if salt.utils.platform.is_darwin()
  22. else salt.utils.pycrypto.gen_hash(password=USERA_PWD),
  23. )
  24. assert USERA in sminion.functions.user.list_users()
  25. # Run tests
  26. yield
  27. finally:
  28. sminion.functions.user.delete(USERA, remove=True)
  29. @pytest.fixture
  30. def salt_run_cli(salt_master):
  31. """
  32. Override salt_run_cli fixture to provide an increased default_timeout to the calls
  33. """
  34. return salt_master.get_salt_run_cli(default_timeout=120)
  35. @slowTest
  36. def test_in_docs(salt_run_cli):
  37. """
  38. test the salt-run docs system
  39. """
  40. ret = salt_run_cli.run("-d")
  41. assert "jobs.active:" in ret.stdout
  42. assert "jobs.list_jobs:" in ret.stdout
  43. assert "jobs.lookup_jid:" in ret.stdout
  44. assert "manage.down:" in ret.stdout
  45. assert "manage.up:" in ret.stdout
  46. assert "network.wol:" in ret.stdout
  47. assert "network.wollist:" in ret.stdout
  48. @slowTest
  49. def test_not_in_docs(salt_run_cli):
  50. """
  51. test the salt-run docs system
  52. """
  53. ret = salt_run_cli.run("-d")
  54. assert "jobs.SaltException:" not in ret.stdout
  55. @slowTest
  56. def test_salt_documentation_too_many_arguments(salt_run_cli):
  57. """
  58. Test to see if passing additional arguments shows an error
  59. """
  60. ret = salt_run_cli.run("-d", "virt.list", "foo")
  61. assert ret.exitcode != 0
  62. assert "You can only get documentation for one method at one time" in ret.stderr
  63. @slowTest
  64. def test_exit_status_unknown_argument(salt_run_cli):
  65. """
  66. Ensure correct exit status when an unknown argument is passed to salt-run.
  67. """
  68. ret = salt_run_cli.run("--unknown-argument")
  69. assert ret.exitcode == salt.defaults.exitcodes.EX_USAGE, ret
  70. assert "Usage" in ret.stderr
  71. assert "no such option: --unknown-argument" in ret.stderr
  72. @slowTest
  73. def test_exit_status_correct_usage(salt_run_cli):
  74. """
  75. Ensure correct exit status when salt-run starts correctly.
  76. """
  77. ret = salt_run_cli.run()
  78. assert ret.exitcode == salt.defaults.exitcodes.EX_OK, ret
  79. @slowTest
  80. @pytest.mark.skip_if_not_root
  81. @pytest.mark.parametrize("flag", ["--auth", "--eauth", "--external-auth", "-a"])
  82. @pytest.mark.skip_on_windows(reason="PAM is not supported on Windows")
  83. def test_salt_run_with_eauth_all_args(salt_run_cli, saltdev_account, flag):
  84. """
  85. test salt-run with eauth
  86. tests all eauth args
  87. """
  88. ret = salt_run_cli.run(
  89. flag,
  90. "pam",
  91. "--username",
  92. USERA,
  93. "--password",
  94. USERA_PWD,
  95. "test.arg",
  96. "arg",
  97. kwarg="kwarg1",
  98. _timeout=240,
  99. )
  100. assert ret.exitcode == 0, ret
  101. assert ret.json, ret
  102. expected = {"args": ["arg"], "kwargs": {"kwarg": "kwarg1"}}
  103. assert ret.json == expected, ret
  104. @slowTest
  105. @pytest.mark.skip_if_not_root
  106. @pytest.mark.skip_on_windows(reason="PAM is not supported on Windows")
  107. def test_salt_run_with_eauth_bad_passwd(salt_run_cli, saltdev_account):
  108. """
  109. test salt-run with eauth and bad password
  110. """
  111. ret = salt_run_cli.run(
  112. "-a",
  113. "pam",
  114. "--username",
  115. USERA,
  116. "--password",
  117. "wrongpassword",
  118. "test.arg",
  119. "arg",
  120. kwarg="kwarg1",
  121. )
  122. assert (
  123. ret.stdout
  124. == 'Authentication failure of type "eauth" occurred for user {}.'.format(USERA)
  125. )
  126. @slowTest
  127. def test_salt_run_with_wrong_eauth(salt_run_cli):
  128. """
  129. test salt-run with wrong eauth parameter
  130. """
  131. ret = salt_run_cli.run(
  132. "-a",
  133. "wrongeauth",
  134. "--username",
  135. USERA,
  136. "--password",
  137. USERA_PWD,
  138. "test.arg",
  139. "arg",
  140. kwarg="kwarg1",
  141. )
  142. assert ret.exitcode == 0, ret
  143. assert re.search(
  144. r"^The specified external authentication system \"wrongeauth\" is not available\nAvailable eauth types: auto, .*",
  145. ret.stdout.replace("\r\n", "\n"),
  146. )