test_memcached.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import
  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. from salt.exceptions import CommandExecutionError
  14. # Import Salt Libs
  15. import salt.states.memcached as memcached
  16. class MemcachedTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.memcached
  19. '''
  20. def setup_loader_modules(self):
  21. return {memcached: {}}
  22. # 'managed' function tests: 1
  23. def test_managed(self):
  24. '''
  25. Test to manage a memcached key.
  26. '''
  27. name = 'foo'
  28. ret = {'name': name,
  29. 'result': False,
  30. 'comment': '',
  31. 'changes': {}}
  32. mock_t = MagicMock(side_effect=[CommandExecutionError, 'salt', True,
  33. True, True])
  34. with patch.dict(memcached.__salt__, {'memcached.get': mock_t,
  35. 'memcached.set': mock_t}):
  36. self.assertDictEqual(memcached.managed(name), ret)
  37. comt = ("Key 'foo' does not need to be updated")
  38. ret.update({'comment': comt, 'result': True})
  39. self.assertDictEqual(memcached.managed(name, 'salt'), ret)
  40. with patch.dict(memcached.__opts__, {'test': True}):
  41. comt = ("Value of key 'foo' would be changed")
  42. ret.update({'comment': comt, 'result': None})
  43. self.assertDictEqual(memcached.managed(name, 'salt'), ret)
  44. with patch.dict(memcached.__opts__, {'test': False}):
  45. comt = ("Successfully set key 'foo'")
  46. ret.update({'comment': comt, 'result': True,
  47. 'changes': {'new': 'salt', 'old': True}})
  48. self.assertDictEqual(memcached.managed(name, 'salt'), ret)
  49. # 'absent' function tests: 1
  50. def test_absent(self):
  51. '''
  52. Test to ensure that a memcached key is not present.
  53. '''
  54. name = 'foo'
  55. ret = {'name': name,
  56. 'result': False,
  57. 'comment': '',
  58. 'changes': {}}
  59. mock_t = MagicMock(side_effect=[CommandExecutionError, 'salt', None,
  60. True, True, True])
  61. with patch.dict(memcached.__salt__, {'memcached.get': mock_t,
  62. 'memcached.delete': mock_t}):
  63. self.assertDictEqual(memcached.absent(name), ret)
  64. comt = ("Value of key 'foo' ('salt') is not 'bar'")
  65. ret.update({'comment': comt, 'result': True})
  66. self.assertDictEqual(memcached.absent(name, 'bar'), ret)
  67. comt = ("Key 'foo' does not exist")
  68. ret.update({'comment': comt})
  69. self.assertDictEqual(memcached.absent(name), ret)
  70. with patch.dict(memcached.__opts__, {'test': True}):
  71. comt = ("Key 'foo' would be deleted")
  72. ret.update({'comment': comt, 'result': None})
  73. self.assertDictEqual(memcached.absent(name), ret)
  74. with patch.dict(memcached.__opts__, {'test': False}):
  75. comt = ("Successfully deleted key 'foo'")
  76. ret.update({'comment': comt, 'result': True,
  77. 'changes': {'key deleted': 'foo', 'value': True}})
  78. self.assertDictEqual(memcached.absent(name), ret)