test_postgres_cluster.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Logilab <contact@logilab.fr>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.postgres_cluster as postgres_cluster
  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 PostgresClusterTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.postgres_cluster
  16. """
  17. def setup_loader_modules(self):
  18. return {postgres_cluster: {}}
  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 = "main"
  26. version = "9.4"
  27. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  28. mock_t = MagicMock(return_value=True)
  29. mock_f = MagicMock(return_value=False)
  30. infos = {"{0}/{1}".format(version, name): {}}
  31. mock = MagicMock(return_value=infos)
  32. with patch.dict(
  33. postgres_cluster.__salt__,
  34. {
  35. "postgres.cluster_list": mock,
  36. "postgres.cluster_exists": mock_t,
  37. "postgres.cluster_create": mock_t,
  38. },
  39. ):
  40. comt = "Cluster {0}/{1} is already present".format(version, name)
  41. ret.update({"comment": comt, "result": True})
  42. self.assertDictEqual(postgres_cluster.present(version, name), ret)
  43. infos["{0}/{1}".format(version, name)]["port"] = 5433
  44. comt = (
  45. "Cluster {0}/{1} has wrong parameters "
  46. "which couldn't be changed on fly.".format(version, name)
  47. )
  48. ret.update({"comment": comt, "result": False})
  49. self.assertDictEqual(
  50. postgres_cluster.present(version, name, port=5434), ret
  51. )
  52. infos["{0}/{1}".format(version, name)]["datadir"] = "/tmp/"
  53. comt = (
  54. "Cluster {0}/{1} has wrong parameters "
  55. "which couldn't be changed on fly.".format(version, name)
  56. )
  57. ret.update({"comment": comt, "result": False})
  58. self.assertDictEqual(
  59. postgres_cluster.present(version, name, port=5434), ret
  60. )
  61. with patch.dict(
  62. postgres_cluster.__salt__,
  63. {
  64. "postgres.cluster_list": mock,
  65. "postgres.cluster_exists": mock_f,
  66. "postgres.cluster_create": mock_t,
  67. },
  68. ):
  69. comt = "The cluster {0}/{1} has been created".format(version, name)
  70. ret.update(
  71. {
  72. "comment": comt,
  73. "result": True,
  74. "changes": {"{0}/{1}".format(version, name): "Present"},
  75. }
  76. )
  77. self.assertDictEqual(postgres_cluster.present(version, name), ret)
  78. with patch.dict(postgres_cluster.__opts__, {"test": True}):
  79. comt = "Cluster {0}/{1} is set to be created".format(version, name)
  80. ret.update({"comment": comt, "result": None, "changes": {}})
  81. self.assertDictEqual(postgres_cluster.present(version, name), ret)
  82. with patch.dict(
  83. postgres_cluster.__salt__,
  84. {
  85. "postgres.cluster_list": mock,
  86. "postgres.cluster_exists": mock_f,
  87. "postgres.cluster_create": mock_f,
  88. },
  89. ):
  90. comt = "Failed to create cluster {0}/{1}".format(version, name)
  91. ret.update({"comment": comt, "result": False})
  92. self.assertDictEqual(postgres_cluster.present(version, name), ret)
  93. # 'absent' function tests: 1
  94. def test_absent(self):
  95. """
  96. Test to ensure that the named database is absent.
  97. """
  98. name = "main"
  99. version = "9.4"
  100. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  101. mock_t = MagicMock(return_value=True)
  102. mock = MagicMock(side_effect=[True, True, False])
  103. with patch.dict(
  104. postgres_cluster.__salt__,
  105. {"postgres.cluster_exists": mock, "postgres.cluster_remove": mock_t},
  106. ):
  107. with patch.dict(postgres_cluster.__opts__, {"test": True}):
  108. comt = "Cluster {0}/{1} is set to be removed".format(version, name)
  109. ret.update({"comment": comt, "result": None})
  110. self.assertDictEqual(postgres_cluster.absent(version, name), ret)
  111. with patch.dict(postgres_cluster.__opts__, {"test": False}):
  112. comt = "Cluster {0}/{1} has been removed".format(version, name)
  113. ret.update(
  114. {"comment": comt, "result": True, "changes": {name: "Absent"}}
  115. )
  116. self.assertDictEqual(postgres_cluster.absent(version, name), ret)
  117. comt = "Cluster {0}/{1} is not present, so it cannot be removed".format(
  118. version, name
  119. )
  120. ret.update({"comment": comt, "result": True, "changes": {}})
  121. self.assertDictEqual(postgres_cluster.absent(version, name), ret)