test_postgres_privileges.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Andrew Colin Kissa <andrew@topdog.za.net>
  4. '''
  5. from __future__ import absolute_import, unicode_literals, print_function
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.unit import TestCase
  8. from tests.support.mock import (
  9. MagicMock,
  10. patch
  11. )
  12. import salt.states.postgres_privileges as postgres_privileges
  13. class PostgresPrivilegesTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test cases for salt.states.postgres_privileges
  16. '''
  17. def setup_loader_modules(self):
  18. return {postgres_privileges: {}}
  19. def setUp(self):
  20. '''
  21. Setup data for the tests
  22. '''
  23. self.table_name = 'awl'
  24. self.group_name = 'admins'
  25. self.name = 'baruwa'
  26. self.ret = {'name': self.name,
  27. 'changes': {},
  28. 'result': False,
  29. 'comment': ''}
  30. self.mock_true = MagicMock(return_value=True)
  31. self.mock_false = MagicMock(return_value=False)
  32. def tearDown(self):
  33. del self.ret
  34. del self.mock_true
  35. del self.mock_false
  36. def test_present_table(self):
  37. '''
  38. Test present
  39. '''
  40. with patch.dict(postgres_privileges.__salt__,
  41. {'postgres.has_privileges': self.mock_true}):
  42. comt = 'The requested privilege(s) are already set'
  43. self.ret.update({'comment': comt, 'result': True})
  44. self.assertDictEqual(
  45. postgres_privileges.present(
  46. self.name,
  47. self.table_name,
  48. 'table'),
  49. self.ret)
  50. with patch.dict(postgres_privileges.__salt__,
  51. {'postgres.has_privileges': self.mock_false,
  52. 'postgres.privileges_grant': self.mock_true}):
  53. with patch.dict(postgres_privileges.__opts__, {'test': True}):
  54. comt = ('The privilege(s): {0} are'
  55. ' set to be granted to {1}').format('ALL', self.name)
  56. self.ret.update({'comment': comt, 'result': None})
  57. self.assertDictEqual(
  58. postgres_privileges.present(self.name,
  59. self.table_name, 'table', privileges=['ALL']), self.ret)
  60. with patch.dict(postgres_privileges.__opts__, {'test': False}):
  61. comt = ('The privilege(s): {0} have '
  62. 'been granted to {1}').format('ALL', self.name)
  63. self.ret.update({'comment': comt,
  64. 'result': True,
  65. 'changes': {'baruwa': 'Present'}})
  66. self.assertDictEqual(
  67. postgres_privileges.present(self.name,
  68. self.table_name, 'table', privileges=['ALL']), self.ret)
  69. def test_present_group(self):
  70. '''
  71. Test present group
  72. '''
  73. with patch.dict(postgres_privileges.__salt__,
  74. {'postgres.has_privileges': self.mock_false,
  75. 'postgres.privileges_grant': self.mock_true}):
  76. with patch.dict(postgres_privileges.__opts__, {'test': True}):
  77. comt = ('The privilege(s): {0} are'
  78. ' set to be granted to {1}').format(self.group_name,
  79. self.name)
  80. self.ret.update({'comment': comt, 'result': None})
  81. self.assertDictEqual(
  82. postgres_privileges.present(self.name,
  83. self.group_name, 'group'), self.ret)
  84. with patch.dict(postgres_privileges.__opts__, {'test': False}):
  85. comt = ('The privilege(s): {0} have '
  86. 'been granted to {1}').format(self.group_name,
  87. self.name)
  88. self.ret.update({'comment': comt,
  89. 'result': True,
  90. 'changes': {'baruwa': 'Present'}})
  91. self.assertDictEqual(
  92. postgres_privileges.present(self.name,
  93. self.group_name, 'group'), self.ret)
  94. def test_absent_table(self):
  95. '''
  96. Test absent
  97. '''
  98. with patch.dict(postgres_privileges.__salt__,
  99. {'postgres.has_privileges': self.mock_false}):
  100. with patch.dict(postgres_privileges.__opts__, {'test': True}):
  101. comt = ('The requested privilege(s)'
  102. ' are not set so cannot be revoked')
  103. self.ret.update({'comment': comt, 'result': True})
  104. self.assertDictEqual(
  105. postgres_privileges.absent(
  106. self.name,
  107. self.table_name,
  108. 'table'),
  109. self.ret)
  110. with patch.dict(postgres_privileges.__salt__,
  111. {'postgres.has_privileges': self.mock_true,
  112. 'postgres.privileges_revoke': self.mock_true}):
  113. with patch.dict(postgres_privileges.__opts__, {'test': True}):
  114. comt = ('The privilege(s): {0} are'
  115. ' set to be revoked from {1}').format('ALL', self.name)
  116. self.ret.update({'comment': comt, 'result': None})
  117. self.assertDictEqual(
  118. postgres_privileges.absent(self.name,
  119. self.table_name, 'table', privileges=['ALL']), self.ret)
  120. with patch.dict(postgres_privileges.__opts__, {'test': False}):
  121. comt = ('The privilege(s): {0} have '
  122. 'been revoked from {1}').format('ALL', self.name)
  123. self.ret.update({'comment': comt,
  124. 'result': True,
  125. 'changes': {'baruwa': 'Absent'}})
  126. self.assertDictEqual(
  127. postgres_privileges.absent(self.name,
  128. self.table_name, 'table', privileges=['ALL']), self.ret)
  129. def test_absent_group(self):
  130. '''
  131. Test absent group
  132. '''
  133. with patch.dict(postgres_privileges.__salt__,
  134. {'postgres.has_privileges': self.mock_true,
  135. 'postgres.privileges_revoke': self.mock_true}):
  136. with patch.dict(postgres_privileges.__opts__, {'test': True}):
  137. comt = ('The privilege(s): {0} are'
  138. ' set to be revoked from {1}').format(self.group_name,
  139. self.name)
  140. self.ret.update({'comment': comt, 'result': None})
  141. self.assertDictEqual(
  142. postgres_privileges.absent(self.name,
  143. self.group_name, 'group'), self.ret)
  144. with patch.dict(postgres_privileges.__opts__, {'test': False}):
  145. comt = ('The privilege(s): {0} have '
  146. 'been revoked from {1}').format(self.group_name,
  147. self.name)
  148. self.ret.update({'comment': comt,
  149. 'result': True,
  150. 'changes': {'baruwa': 'Absent'}})
  151. self.assertDictEqual(
  152. postgres_privileges.absent(self.name,
  153. self.group_name, 'group'), self.ret)