1
0

test_rabbitmq_policy.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.rabbitmq_policy as rabbitmq_policy
  16. class RabbitmqPolicyTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.rabbitmq_policy
  19. '''
  20. def setup_loader_modules(self):
  21. return {rabbitmq_policy: {}}
  22. # 'present' function tests: 1
  23. def test_present(self):
  24. '''
  25. Test to ensure the RabbitMQ policy exists.
  26. '''
  27. name = 'HA'
  28. pattern = '.*'
  29. definition = '{"ha-mode":"all"}'
  30. ret = {'name': name,
  31. 'changes': {},
  32. 'result': True,
  33. 'comment': ''}
  34. mock = MagicMock(side_effect=[{'/': {name: {'pattern': pattern,
  35. 'definition': definition,
  36. 'priority': 0}}}, {}])
  37. with patch.dict(rabbitmq_policy.__salt__,
  38. {'rabbitmq.list_policies': mock}):
  39. comt = ('Policy / HA is already present')
  40. ret.update({'comment': comt})
  41. self.assertDictEqual(rabbitmq_policy.present(name, pattern,
  42. definition), ret)
  43. with patch.dict(rabbitmq_policy.__opts__, {'test': True}):
  44. comment = 'Policy / HA is set to be created'
  45. changes = {'new': 'HA', 'old': {}}
  46. ret.update({'comment': comment, 'result': None, 'changes': changes})
  47. self.assertDictEqual(rabbitmq_policy.present(name, pattern,
  48. definition), ret)
  49. # 'absent' function tests: 1
  50. def test_absent(self):
  51. '''
  52. Test to ensure the named policy is absent.
  53. '''
  54. name = 'HA'
  55. ret = {'name': name,
  56. 'changes': {},
  57. 'result': True,
  58. 'comment': ''}
  59. mock = MagicMock(side_effect=[False, True])
  60. with patch.dict(rabbitmq_policy.__salt__,
  61. {'rabbitmq.policy_exists': mock}):
  62. comment = "Policy '/ HA' is not present."
  63. ret.update({'comment': comment})
  64. self.assertDictEqual(rabbitmq_policy.absent(name), ret)
  65. with patch.dict(rabbitmq_policy.__opts__, {'test': True}):
  66. comment = "Policy '/ HA' will be removed."
  67. changes = {'new': '', 'old': 'HA'}
  68. ret.update({'comment': comment, 'result': None, 'changes': changes})
  69. self.assertDictEqual(rabbitmq_policy.absent(name), ret)