1
0

test_pre_flight.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. Test for ssh_pre_flight roster option
  3. """
  4. import os
  5. import salt.utils.files
  6. from tests.support.case import SSHCase
  7. from tests.support.helpers import slowTest
  8. from tests.support.runtests import RUNTIME_VARS
  9. class SSHPreFlightTest(SSHCase):
  10. """
  11. Test ssh_pre_flight roster option
  12. """
  13. def setUp(self):
  14. super().setUp()
  15. self.roster = os.path.join(RUNTIME_VARS.TMP, "pre_flight_roster")
  16. self.data = {
  17. "ssh_pre_flight": os.path.join(RUNTIME_VARS.TMP, "ssh_pre_flight.sh")
  18. }
  19. self.test_script = os.path.join(
  20. RUNTIME_VARS.TMP, "test-pre-flight-script-worked.txt"
  21. )
  22. def _create_roster(self):
  23. self.custom_roster(self.roster, self.data)
  24. with salt.utils.files.fopen(self.data["ssh_pre_flight"], "w") as fp_:
  25. fp_.write("touch {}".format(self.test_script))
  26. @slowTest
  27. def test_ssh_pre_flight(self):
  28. """
  29. test ssh when ssh_pre_flight is set
  30. ensure the script runs successfully
  31. """
  32. self._create_roster()
  33. assert self.run_function("test.ping", roster_file=self.roster)
  34. assert os.path.exists(self.test_script)
  35. @slowTest
  36. def test_ssh_run_pre_flight(self):
  37. """
  38. test ssh when --pre-flight is passed to salt-ssh
  39. to ensure the script runs successfully
  40. """
  41. self._create_roster()
  42. # make sure we previously ran a command so the thin dir exists
  43. self.run_function("test.ping", wipe=False)
  44. assert not os.path.exists(self.test_script)
  45. assert self.run_function(
  46. "test.ping", ssh_opts="--pre-flight", roster_file=self.roster, wipe=False
  47. )
  48. assert os.path.exists(self.test_script)
  49. @slowTest
  50. def test_ssh_run_pre_flight_failure(self):
  51. """
  52. test ssh_pre_flight when there is a failure
  53. in the script.
  54. """
  55. self._create_roster()
  56. with salt.utils.files.fopen(self.data["ssh_pre_flight"], "w") as fp_:
  57. fp_.write("exit 2")
  58. ret = self.run_function(
  59. "test.ping", ssh_opts="--pre-flight", roster_file=self.roster, wipe=False
  60. )
  61. assert ret["retcode"] == 2
  62. def tearDown(self):
  63. """
  64. make sure to clean up any old ssh directories
  65. """
  66. files = [self.roster, self.data["ssh_pre_flight"], self.test_script]
  67. for fp_ in files:
  68. if os.path.exists(fp_):
  69. os.remove(fp_)