1
0

test_runner.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for the salt-run command
  4. """
  5. from __future__ import absolute_import
  6. import re
  7. import time
  8. import pytest
  9. import salt.utils.files
  10. import salt.utils.platform
  11. import salt.utils.yaml
  12. USERA = "saltdev-runner"
  13. USERA_PWD = "saltdev"
  14. HASHED_USERA_PWD = "$6$SALTsalt$ZZFD90fKFWq8AGmmX0L3uBtS9fXL62SrTk5zcnQ6EkD6zoiM3kB88G1Zvs0xm/gZ7WXJRs5nsTBybUvGSqZkT."
  15. @pytest.fixture(scope="module")
  16. def saltdev_account(sminion):
  17. try:
  18. assert sminion.functions.user.add(USERA, createhome=False)
  19. assert sminion.functions.shadow.set_password(
  20. USERA, USERA_PWD if salt.utils.platform.is_darwin() else HASHED_USERA_PWD
  21. )
  22. assert USERA in sminion.functions.user.list_users()
  23. # Run tests
  24. yield
  25. finally:
  26. sminion.functions.user.delete(USERA, remove=True)
  27. @pytest.fixture
  28. def salt_run_cli(salt_factories, salt_minion, salt_master):
  29. """
  30. Override salt_run_cli fixture to provide an increased default_timeout to the calls
  31. """
  32. return salt_factories.get_salt_run_cli(
  33. salt_master.config["id"], default_timeout=120
  34. )
  35. @pytest.mark.windows_whitelisted
  36. class TestSaltRun(object):
  37. """
  38. Test the salt-run command
  39. """
  40. def test_in_docs(self, salt_run_cli):
  41. """
  42. test the salt-run docs system
  43. """
  44. ret = salt_run_cli.run("-d")
  45. assert "jobs.active:" in ret.stdout
  46. assert "jobs.list_jobs:" in ret.stdout
  47. assert "jobs.lookup_jid:" in ret.stdout
  48. assert "manage.down:" in ret.stdout
  49. assert "manage.up:" in ret.stdout
  50. assert "network.wol:" in ret.stdout
  51. assert "network.wollist:" in ret.stdout
  52. def test_not_in_docs(self, salt_run_cli):
  53. """
  54. test the salt-run docs system
  55. """
  56. ret = salt_run_cli.run("-d")
  57. assert "jobs.SaltException:" not in ret.stdout
  58. def test_salt_documentation_too_many_arguments(self, salt_run_cli):
  59. """
  60. Test to see if passing additional arguments shows an error
  61. """
  62. ret = salt_run_cli.run("-d", "virt.list", "foo")
  63. assert ret.exitcode != 0
  64. assert "You can only get documentation for one method at one time" in ret.stderr
  65. def test_exit_status_unknown_argument(self, salt_run_cli):
  66. """
  67. Ensure correct exit status when an unknown argument is passed to salt-run.
  68. """
  69. ret = salt_run_cli.run("--unknown-argument")
  70. assert ret.exitcode == salt.defaults.exitcodes.EX_USAGE, ret
  71. assert "Usage" in ret.stderr
  72. assert "no such option: --unknown-argument" in ret.stderr
  73. def test_exit_status_correct_usage(self, 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. @pytest.mark.skip_if_not_root
  80. @pytest.mark.parametrize("flag", ["--auth", "--eauth", "--external-auth", "-a"])
  81. @pytest.mark.skip_on_windows(reason="PAM is not supported on Windows")
  82. def test_salt_run_with_eauth_all_args(self, salt_run_cli, saltdev_account, flag):
  83. """
  84. test salt-run with eauth
  85. tests all eauth args
  86. """
  87. time.sleep(1)
  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. @pytest.mark.skip_if_not_root
  105. @pytest.mark.skip_on_windows(reason="PAM is not supported on Windows")
  106. def test_salt_run_with_eauth_bad_passwd(self, salt_run_cli, saltdev_account):
  107. """
  108. test salt-run with eauth and bad password
  109. """
  110. ret = salt_run_cli.run(
  111. "-a",
  112. "pam",
  113. "--username",
  114. USERA,
  115. "--password",
  116. "wrongpassword",
  117. "test.arg",
  118. "arg",
  119. kwarg="kwarg1",
  120. )
  121. assert (
  122. ret.stdout
  123. == 'Authentication failure of type "eauth" occurred for user {}.'.format(
  124. USERA
  125. )
  126. )
  127. def test_salt_run_with_wrong_eauth(self, 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. )