test_postgres_user.py 3.1 KB

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