test_postgres_database.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_database as postgres_database
  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 PostgresDatabaseTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.postgres_database
  16. """
  17. def setup_loader_modules(self):
  18. return {postgres_database: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure that the named database is present
  23. with the specified properties.
  24. """
  25. name = "frank"
  26. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  27. mock_t = MagicMock(return_value=True)
  28. mock = MagicMock(return_value={name: {}})
  29. with patch.dict(
  30. postgres_database.__salt__,
  31. {"postgres.db_list": mock, "postgres.db_alter": mock_t},
  32. ):
  33. comt = "Database {0} is already present".format(name)
  34. ret.update({"comment": comt, "result": True})
  35. self.assertDictEqual(postgres_database.present(name), ret)
  36. comt = (
  37. "Database frank has wrong parameters "
  38. "which couldn't be changed on fly."
  39. )
  40. ret.update({"comment": comt, "result": False})
  41. self.assertDictEqual(
  42. postgres_database.present(name, tablespace="A", lc_collate=True), ret
  43. )
  44. with patch.dict(postgres_database.__opts__, {"test": True}):
  45. comt = "Database frank exists, " "but parameters need to be changed"
  46. ret.update({"comment": comt, "result": None})
  47. self.assertDictEqual(
  48. postgres_database.present(name, tablespace="A"), ret
  49. )
  50. with patch.dict(postgres_database.__opts__, {"test": False}):
  51. comt = "Parameters for database frank have been changed"
  52. ret.update(
  53. {
  54. "comment": comt,
  55. "result": True,
  56. "changes": {name: "Parameters changed"},
  57. }
  58. )
  59. self.assertDictEqual(
  60. postgres_database.present(name, tablespace="A"), ret
  61. )
  62. # 'absent' function tests: 1
  63. def test_absent(self):
  64. """
  65. Test to ensure that the named database is absent.
  66. """
  67. name = "frank"
  68. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  69. mock_t = MagicMock(return_value=True)
  70. mock = MagicMock(side_effect=[True, True, False])
  71. with patch.dict(
  72. postgres_database.__salt__,
  73. {"postgres.db_exists": mock, "postgres.db_remove": mock_t},
  74. ):
  75. with patch.dict(postgres_database.__opts__, {"test": True}):
  76. comt = "Database {0} is set to be removed".format(name)
  77. ret.update({"comment": comt, "result": None})
  78. self.assertDictEqual(postgres_database.absent(name), ret)
  79. with patch.dict(postgres_database.__opts__, {"test": False}):
  80. comt = "Database {0} has been removed".format(name)
  81. ret.update(
  82. {"comment": comt, "result": True, "changes": {name: "Absent"}}
  83. )
  84. self.assertDictEqual(postgres_database.absent(name), ret)
  85. comt = "Database {0} is not present, so it cannot be removed".format(
  86. name
  87. )
  88. ret.update({"comment": comt, "result": True, "changes": {}})
  89. self.assertDictEqual(postgres_database.absent(name), ret)