1
0

test_sqs_events.py 2.9 KB

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