test_postgres_extension.py 3.2 KB

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