test_mysql_grants.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  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.mysql_grants as mysql_grants
  15. class MysqlGrantsTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.mysql_grants
  18. '''
  19. def setup_loader_modules(self):
  20. return {mysql_grants: {}}
  21. # 'present' function tests: 1
  22. def test_present(self):
  23. '''
  24. Test to ensure that the grant is present with the specified properties.
  25. '''
  26. name = 'frank_exampledb'
  27. ret = {'name': name,
  28. 'result': True,
  29. 'comment': '',
  30. 'changes': {}}
  31. mock = MagicMock(side_effect=[True, False, False, False])
  32. mock_t = MagicMock(return_value=True)
  33. mock_str = MagicMock(return_value='salt')
  34. mock_none = MagicMock(return_value=None)
  35. with patch.dict(mysql_grants.__salt__, {'mysql.grant_exists': mock,
  36. 'mysql.grant_add': mock_t}):
  37. comt = ('Grant None on None to None@localhost is already present')
  38. ret.update({'comment': comt})
  39. self.assertDictEqual(mysql_grants.present(name), ret)
  40. with patch.object(mysql_grants, '_get_mysql_error', mock_str):
  41. ret.update({'comment': 'salt', 'result': False})
  42. self.assertDictEqual(mysql_grants.present(name), ret)
  43. with patch.object(mysql_grants, '_get_mysql_error', mock_none):
  44. with patch.dict(mysql_grants.__opts__, {'test': True}):
  45. comt = ('MySQL grant frank_exampledb is set to be created')
  46. ret.update({'comment': comt, 'result': None})
  47. self.assertDictEqual(mysql_grants.present(name), ret)
  48. with patch.dict(mysql_grants.__opts__, {'test': False}):
  49. comt = ('Grant None on None to None@localhost'
  50. ' has been added')
  51. ret.update({'comment': comt, 'result': True,
  52. 'changes': {name: 'Present'}})
  53. self.assertDictEqual(mysql_grants.present(name), ret)
  54. # 'absent' function tests: 1
  55. def test_absent(self):
  56. '''
  57. Test to ensure that the grant is absent.
  58. '''
  59. name = 'frank_exampledb'
  60. ret = {'name': name,
  61. 'result': True,
  62. 'comment': '',
  63. 'changes': {}}
  64. mock = MagicMock(side_effect=[True, False])
  65. mock_t = MagicMock(side_effect=[True, True, True, False, False])
  66. mock_str = MagicMock(return_value='salt')
  67. mock_none = MagicMock(return_value=None)
  68. with patch.dict(mysql_grants.__salt__, {'mysql.grant_exists': mock_t,
  69. 'mysql.grant_revoke': mock}):
  70. with patch.dict(mysql_grants.__opts__, {'test': True}):
  71. comt = ('MySQL grant frank_exampledb is set to be revoked')
  72. ret.update({'comment': comt, 'result': None})
  73. self.assertDictEqual(mysql_grants.absent(name), ret)
  74. with patch.dict(mysql_grants.__opts__, {'test': False}):
  75. comt = ('Grant None on None for None@localhost'
  76. ' has been revoked')
  77. ret.update({'comment': comt, 'result': True,
  78. 'changes': {name: 'Absent'}})
  79. self.assertDictEqual(mysql_grants.absent(name), ret)
  80. with patch.object(mysql_grants, '_get_mysql_error', mock_str):
  81. comt = ('Unable to revoke grant None on None'
  82. ' for None@localhost (salt)')
  83. ret.update({'comment': comt, 'result': False,
  84. 'changes': {}})
  85. self.assertDictEqual(mysql_grants.absent(name), ret)
  86. comt = ('Unable to determine if grant None on '
  87. 'None for None@localhost exists (salt)')
  88. ret.update({'comment': comt})
  89. self.assertDictEqual(mysql_grants.absent(name), ret)
  90. with patch.object(mysql_grants, '_get_mysql_error', mock_none):
  91. comt = ('Grant None on None to None@localhost is not present,'
  92. ' so it cannot be revoked')
  93. ret.update({'comment': comt, 'result': True})
  94. self.assertDictEqual(mysql_grants.absent(name), ret)