1
0

test_sqs_events.py 3.0 KB

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