test_postgres_group.py 3.0 KB

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