test_postgres_group.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.postgres_group as postgres_group
  16. class PostgresGroupTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.postgres_group
  19. '''
  20. def setup_loader_modules(self):
  21. return {postgres_group: {}}
  22. # 'present' function tests: 1
  23. def test_present(self):
  24. '''
  25. Test to ensure that the named group is present
  26. with the specified privileges.
  27. '''
  28. name = 'frank'
  29. ret = {'name': name,
  30. 'changes': {},
  31. 'result': False,
  32. 'comment': ''}
  33. mock_t = MagicMock(return_value=True)
  34. mock = MagicMock(return_value=None)
  35. with patch.dict(postgres_group.__salt__,
  36. {'postgres.role_get': mock,
  37. 'postgres.group_create': mock_t}):
  38. with patch.dict(postgres_group.__opts__, {'test': True}):
  39. comt = ('Group {0} is set to be created'.format(name))
  40. ret.update({'comment': comt, 'result': None})
  41. self.assertDictEqual(postgres_group.present(name), ret)
  42. with patch.dict(postgres_group.__opts__, {'test': False}):
  43. comt = ('The group {0} has been created'.format(name))
  44. ret.update({'comment': comt, 'result': True})
  45. self.assertDictEqual(postgres_group.present(name), ret)
  46. # 'absent' function tests: 1
  47. def test_absent(self):
  48. '''
  49. Test to ensure that the named group is absent.
  50. '''
  51. name = 'frank'
  52. ret = {'name': name,
  53. 'changes': {},
  54. 'result': False,
  55. 'comment': ''}
  56. mock_t = MagicMock(return_value=True)
  57. mock = MagicMock(side_effect=[True, True, False])
  58. with patch.dict(postgres_group.__salt__,
  59. {'postgres.user_exists': mock,
  60. 'postgres.group_remove': mock_t}):
  61. with patch.dict(postgres_group.__opts__, {'test': True}):
  62. comt = ('Group {0} is set to be removed'.format(name))
  63. ret.update({'comment': comt, 'result': None})
  64. self.assertDictEqual(postgres_group.absent(name), ret)
  65. with patch.dict(postgres_group.__opts__, {'test': False}):
  66. comt = ('Group {0} has been removed'.format(name))
  67. ret.update({'comment': comt, 'result': True,
  68. 'changes': {name: 'Absent'}})
  69. self.assertDictEqual(postgres_group.absent(name), ret)
  70. comt = ('Group {0} is not present, so it cannot be removed'
  71. .format(name))
  72. ret.update({'comment': comt, 'result': True, 'changes': {}})
  73. self.assertDictEqual(postgres_group.absent(name), ret)