test_zk_concurrency.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # Import Salt Libs
  14. import salt.states.zk_concurrency as zk_concurrency
  15. class ZkConcurrencyTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Validate the zk_concurrency state
  18. '''
  19. def setup_loader_modules(self):
  20. return {zk_concurrency: {}}
  21. def test_lock(self):
  22. '''
  23. Test to block state execution until you are able to get the lock
  24. '''
  25. ret = {'name': 'salt',
  26. 'changes': {},
  27. 'result': True,
  28. 'comment': ''}
  29. with patch.dict(zk_concurrency.__opts__, {"test": True}):
  30. ret.update({'comment': 'Attempt to acquire lock', 'result': None})
  31. self.assertDictEqual(zk_concurrency.lock('salt', 'dude'), ret)
  32. with patch.dict(zk_concurrency.__opts__, {"test": False}):
  33. mock = MagicMock(return_value=True)
  34. with patch.dict(zk_concurrency.__salt__,
  35. {"zk_concurrency.lock": mock}):
  36. ret.update({'comment': 'lock acquired', 'result': True})
  37. self.assertDictEqual(zk_concurrency.lock('salt', 'dude',
  38. 'stack'), ret)
  39. def test_unlock(self):
  40. '''
  41. Test to remove lease from semaphore
  42. '''
  43. ret = {'name': 'salt',
  44. 'changes': {},
  45. 'result': True,
  46. 'comment': ''}
  47. with patch.dict(zk_concurrency.__opts__, {"test": True}):
  48. ret.update({'comment': 'Released lock if it is here',
  49. 'result': None})
  50. self.assertDictEqual(zk_concurrency.unlock('salt'), ret)
  51. with patch.dict(zk_concurrency.__opts__, {"test": False}):
  52. mock = MagicMock(return_value=True)
  53. with patch.dict(zk_concurrency.__salt__,
  54. {"zk_concurrency.unlock": mock}):
  55. ret.update({'comment': '', 'result': True})
  56. self.assertDictEqual(zk_concurrency.unlock('salt',
  57. identifier='stack'),
  58. ret)
  59. def test_min_party(self):
  60. '''
  61. Test to ensure min party of nodes and the blocking behavior
  62. '''
  63. ret = {'name': 'salt',
  64. 'changes': {},
  65. 'result': True,
  66. 'comment': ''}
  67. with patch.dict(zk_concurrency.__opts__, {"test": True}):
  68. ret.update({'comment': 'Attempt to ensure min_party', 'result': None})
  69. self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 1), ret)
  70. with patch.dict(zk_concurrency.__opts__, {"test": False}):
  71. mock = MagicMock(return_value=['1', '2', '3'])
  72. with patch.dict(zk_concurrency.__salt__,
  73. {"zk_concurrency.party_members": mock}):
  74. ret.update({'comment': 'Currently 3 nodes, which is >= 2', 'result': True})
  75. self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 2), ret)
  76. ret.update({'comment': 'Blocked until 2 nodes were available. ' +
  77. 'Unblocked after 3 nodes became available', 'result': True})
  78. self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 2, True), ret)
  79. ret.update({'comment': 'Currently 3 nodes, which is < 4', 'result': False})
  80. self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 4), ret)