1
0

test_sqs_events.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for the sqs_events engine
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.engines.sqs_events as sqs_events
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase, skipIf
  13. @skipIf(sqs_events.HAS_BOTO is False, "The boto library is not installed")
  14. class EngineSqsEventTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.engine.sqs_events
  17. """
  18. def setup_loader_modules(self):
  19. patcher = patch("salt.engines.sqs_events.boto.sqs")
  20. self.mock_sqs = patcher.start()
  21. self.addCleanup(patcher.stop)
  22. self.addCleanup(delattr, self, "mock_sqs")
  23. return {sqs_events: {}}
  24. def sample_msg(self):
  25. fake_msg = MagicMock()
  26. fake_msg.get_body.return_value = "This is a test message"
  27. fake_msg.delete.return_value = True
  28. return fake_msg
  29. # 'present' function tests: 1
  30. def test_no_queue_present(self):
  31. """
  32. Test to ensure the SQS engine logs a warning when queue not present
  33. """
  34. with patch("salt.engines.sqs_events.log") as mock_logging:
  35. with patch("time.sleep", return_value=None) as mock_sleep:
  36. q = None
  37. q_name = "mysqs"
  38. mock_fire = MagicMock(return_value=True)
  39. sqs_events._process_queue(q, q_name, mock_fire)
  40. self.assertTrue(mock_logging.warning.called)
  41. self.assertFalse(self.mock_sqs.queue.Queue().get_messages.called)
  42. def test_minion_message_fires(self):
  43. """
  44. Test SQS engine correctly gets and fires messages on minion
  45. """
  46. msgs = [self.sample_msg(), self.sample_msg()]
  47. self.mock_sqs.queue.Queue().get_messages.return_value = msgs
  48. q = self.mock_sqs.queue.Queue()
  49. q_name = "mysqs"
  50. mock_event = MagicMock(return_value=True)
  51. mock_fire = MagicMock(return_value=True)
  52. with patch.dict(sqs_events.__salt__, {"event.send": mock_event}):
  53. sqs_events._process_queue(q, q_name, mock_fire)
  54. self.assertTrue(self.mock_sqs.queue.Queue().get_messages.called)
  55. self.assertTrue(all(x.delete.called for x in msgs))
  56. def test_master_message_fires(self):
  57. """
  58. Test SQS engine correctly gets and fires messages on master
  59. """
  60. msgs = [self.sample_msg(), self.sample_msg()]
  61. self.mock_sqs.queue.Queue().get_messages.return_value = msgs
  62. q = self.mock_sqs.queue.Queue()
  63. q_name = "mysqs"
  64. mock_fire = MagicMock(return_value=True)
  65. sqs_events._process_queue(q, q_name, mock_fire)
  66. self.assertTrue(self.mock_sqs.queue.Queue().get_messages.called, len(msgs))
  67. self.assertTrue(mock_fire.called, len(msgs))