test_standard.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import os
  4. import pytest
  5. import salt.utils.files
  6. import salt.utils.platform
  7. from tests.support.case import ModuleCase
  8. @pytest.mark.windows_whitelisted
  9. class StdTest(ModuleCase):
  10. """
  11. Test standard client calls
  12. """
  13. def setUp(self):
  14. self.TIMEOUT = 600 if salt.utils.platform.is_windows() else 10
  15. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  16. def test_cli(self):
  17. """
  18. Test cli function
  19. """
  20. cmd_iter = self.client.cmd_cli("minion", "test.ping", timeout=20,)
  21. for ret in cmd_iter:
  22. self.assertTrue(ret["minion"])
  23. # make sure that the iter waits for long running jobs too
  24. cmd_iter = self.client.cmd_cli("minion", "test.sleep", [6], timeout=20,)
  25. num_ret = 0
  26. for ret in cmd_iter:
  27. num_ret += 1
  28. self.assertTrue(ret["minion"])
  29. assert num_ret > 0
  30. # ping a minion that doesn't exist, to make sure that it doesn't hang forever
  31. # create fake minion
  32. key_file = os.path.join(self.master_opts["pki_dir"], "minions", "footest")
  33. # touch the file
  34. with salt.utils.files.fopen(key_file, "a"):
  35. pass
  36. # ping that minion and ensure it times out
  37. try:
  38. cmd_iter = self.client.cmd_cli("footest", "test.ping", timeout=20,)
  39. num_ret = 0
  40. for ret in cmd_iter:
  41. num_ret += 1
  42. self.assertTrue(ret["minion"])
  43. assert num_ret == 0
  44. finally:
  45. os.unlink(key_file)
  46. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  47. def test_iter(self):
  48. """
  49. test cmd_iter
  50. """
  51. cmd_iter = self.client.cmd_iter("minion", "test.ping",)
  52. for ret in cmd_iter:
  53. self.assertTrue(ret["minion"])
  54. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  55. def test_iter_no_block(self):
  56. """
  57. test cmd_iter_no_block
  58. """
  59. cmd_iter = self.client.cmd_iter_no_block("minion", "test.ping",)
  60. for ret in cmd_iter:
  61. if ret is None:
  62. continue
  63. self.assertTrue(ret["minion"])
  64. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  65. def test_batch(self):
  66. """
  67. test cmd_batch
  68. """
  69. cmd_batch = self.client.cmd_batch("minion", "test.ping",)
  70. for ret in cmd_batch:
  71. self.assertTrue(ret["minion"])
  72. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  73. def test_batch_raw(self):
  74. """
  75. test cmd_batch with raw option
  76. """
  77. cmd_batch = self.client.cmd_batch("minion", "test.ping", raw=True,)
  78. for ret in cmd_batch:
  79. self.assertTrue(ret["data"]["success"])
  80. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  81. def test_full_returns(self):
  82. """
  83. test cmd_iter
  84. """
  85. ret = self.client.cmd_full_return("minion", "test.ping", timeout=20,)
  86. self.assertIn("minion", ret)
  87. self.assertEqual({"ret": True, "success": True}, ret["minion"])
  88. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  89. def test_disconnected_return(self):
  90. """
  91. Test return/messaging on a disconnected minion
  92. """
  93. test_ret = {"ret": "Minion did not return. [No response]", "out": "no_return"}
  94. # Create a minion key, but do not start the "fake" minion. This mimics
  95. # a disconnected minion.
  96. key_file = os.path.join(self.master_opts["pki_dir"], "minions", "disconnected")
  97. with salt.utils.files.fopen(key_file, "a"):
  98. pass
  99. # ping disconnected minion and ensure it times out and returns with correct message
  100. try:
  101. cmd_iter = self.client.cmd_cli(
  102. "disconnected", "test.ping", show_timeout=True
  103. )
  104. num_ret = 0
  105. for ret in cmd_iter:
  106. num_ret += 1
  107. self.assertEqual(ret["disconnected"]["ret"], test_ret["ret"])
  108. self.assertEqual(ret["disconnected"]["out"], test_ret["out"])
  109. # Ensure that we entered the loop above
  110. self.assertEqual(num_ret, 1)
  111. finally:
  112. os.unlink(key_file)
  113. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  114. def test_missing_minion_list(self):
  115. """
  116. test cmd with missing minion in nodegroup
  117. """
  118. ret = self.client.cmd(
  119. "minion,ghostminion", "test.ping", tgt_type="list", timeout=self.TIMEOUT
  120. )
  121. self.assertIn("minion", ret)
  122. self.assertIn("ghostminion", ret)
  123. self.assertEqual(True, ret["minion"])
  124. self.assertEqual("Minion did not return. [No response]", ret["ghostminion"])
  125. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  126. def test_missing_minion_nodegroup(self):
  127. """
  128. test cmd with missing minion in nodegroup
  129. """
  130. ret = self.client.cmd("missing_minion", "test.ping", tgt_type="nodegroup")
  131. self.assertIn("minion", ret)
  132. self.assertIn("ghostminion", ret)
  133. self.assertEqual(True, ret["minion"])
  134. self.assertEqual("Minion did not return. [No response]", ret["ghostminion"])