1
0

test_mysql_grants.py 4.6 KB

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