test_salt.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. :codeauthor: Thayne Harbaugh (tharbaug@adobe.com)
  3. tests.integration.shell.saltcli
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. :NOTE: this was named ``saltcli`` rather than ``salt`` because ``salt`` conflates
  6. in the python importer with the expected ``salt`` namespace and breaks imports.
  7. """
  8. import logging
  9. import os
  10. import shutil
  11. import pytest
  12. import salt.defaults.exitcodes
  13. import salt.utils.path
  14. from tests.support.helpers import slowTest
  15. log = logging.getLogger(__name__)
  16. pytestmark = pytest.mark.windows_whitelisted
  17. @slowTest
  18. def test_context_retcode_salt(salt_cli, salt_minion):
  19. """
  20. Test that a nonzero retcode set in the context dunder will cause the
  21. salt CLI to set a nonzero retcode.
  22. """
  23. # test.retcode will set the retcode in the context dunder
  24. ret = salt_cli.run("test.retcode", "0", minion_tgt=salt_minion.id)
  25. assert ret.exitcode == 0, ret
  26. ret = salt_cli.run("test.retcode", "42", minion_tgt=salt_minion.id)
  27. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  28. @slowTest
  29. def test_salt_error(salt_cli, salt_minion):
  30. """
  31. Test that we return the expected retcode when a minion function raises
  32. an exception.
  33. """
  34. ret = salt_cli.run("test.raise_exception", "TypeError", minion_tgt=salt_minion.id)
  35. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  36. ret = salt_cli.run(
  37. "test.raise_exception",
  38. "salt.exceptions.CommandNotFoundError",
  39. minion_tgt=salt_minion.id,
  40. )
  41. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  42. ret = salt_cli.run(
  43. "test.raise_exception",
  44. "salt.exceptions.CommandExecutionError",
  45. minion_tgt=salt_minion.id,
  46. )
  47. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  48. ret = salt_cli.run(
  49. "test.raise_exception",
  50. "salt.exceptions.SaltInvocationError",
  51. minion_tgt=salt_minion.id,
  52. )
  53. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  54. ret = salt_cli.run(
  55. "test.raise_exception",
  56. "OSError",
  57. "2",
  58. '"No such file or directory" /tmp/foo.txt',
  59. minion_tgt=salt_minion.id,
  60. )
  61. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  62. ret = salt_cli.run(
  63. "test.echo", "{foo: bar, result: False}", minion_tgt=salt_minion.id
  64. )
  65. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  66. ret = salt_cli.run(
  67. "test.echo", "{foo: bar, success: False}", minion_tgt=salt_minion.id
  68. )
  69. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  70. @slowTest
  71. def test_missing_minion(salt_cli, salt_master, salt_minion):
  72. """
  73. Test that a minion which doesn't respond results in a nonzeo exit code
  74. """
  75. good = salt.utils.path.join(
  76. salt_master.config["pki_dir"], "minions", salt_minion.id
  77. )
  78. bad = salt.utils.path.join(salt_master.config["pki_dir"], "minions", "minion2")
  79. try:
  80. # Copy the key
  81. shutil.copyfile(good, bad)
  82. ret = salt_cli.run(
  83. "--timeout=5", "test.ping", minion_tgt="minion2", _timeout=120
  84. )
  85. assert ret.exitcode == salt.defaults.exitcodes.EX_GENERIC, ret
  86. finally:
  87. # Now get rid of it
  88. try:
  89. os.remove(bad)
  90. except OSError as exc:
  91. if exc.errno != os.errno.ENOENT:
  92. log.error(
  93. "Failed to remove %s, this may affect other tests: %s", bad, exc
  94. )
  95. @slowTest
  96. def test_exit_status_unknown_argument(salt_cli):
  97. """
  98. Ensure correct exit status when an unknown argument is passed to salt CLI.
  99. """
  100. ret = salt_cli.run("--unknown-argument")
  101. assert ret.exitcode == salt.defaults.exitcodes.EX_USAGE, ret
  102. assert "Usage" in ret.stderr
  103. assert "no such option: --unknown-argument" in ret.stderr
  104. @slowTest
  105. def test_exit_status_correct_usage(salt_cli, salt_minion):
  106. """
  107. Ensure correct exit status when salt CLI starts correctly.
  108. """
  109. ret = salt_cli.run("test.ping", minion_tgt=salt_minion.id)
  110. assert ret.exitcode == salt.defaults.exitcodes.EX_OK, ret