test_grafana.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.states.grafana as grafana
  8. # Import Salt Libs
  9. import salt.utils.json
  10. from salt.exceptions import SaltInvocationError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.unit import TestCase
  15. class GrafanaTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.states.grafana
  18. """
  19. def setup_loader_modules(self):
  20. return {grafana: {}}
  21. # 'dashboard_present' function tests: 1
  22. def test_dashboard_present(self):
  23. """
  24. Test to ensure the grafana dashboard exists and is managed.
  25. """
  26. name = "myservice"
  27. rows = ["systemhealth", "requests", "title"]
  28. row = [{"panels": [{"id": "a"}], "title": "systemhealth"}]
  29. ret = {"name": name, "result": None, "changes": {}, "comment": ""}
  30. comt1 = (
  31. "Dashboard myservice is set to be updated. The following rows "
  32. "set to be updated: {0}".format(["systemhealth"])
  33. )
  34. self.assertRaises(
  35. SaltInvocationError, grafana.dashboard_present, name, profile=False
  36. )
  37. self.assertRaises(
  38. SaltInvocationError, grafana.dashboard_present, name, True, True
  39. )
  40. mock = MagicMock(
  41. side_effect=[
  42. {"hosts": True, "index": False},
  43. {"hosts": True, "index": True},
  44. {"hosts": True, "index": True},
  45. {"hosts": True, "index": True},
  46. {"hosts": True, "index": True},
  47. {"hosts": True, "index": True},
  48. {"hosts": True, "index": True},
  49. ]
  50. )
  51. mock_f = MagicMock(side_effect=[False, False, True, True, True, True])
  52. mock_t = MagicMock(return_value="")
  53. mock_i = MagicMock(return_value=False)
  54. source = {"dashboard": '["rows", {"rows":["baz", null, 1.0, 2]}]'}
  55. mock_dict = MagicMock(return_value={"_source": source})
  56. with patch.dict(
  57. grafana.__salt__,
  58. {
  59. "config.option": mock,
  60. "elasticsearch.exists": mock_f,
  61. "pillar.get": mock_t,
  62. "elasticsearch.get": mock_dict,
  63. "elasticsearch.index": mock_i,
  64. },
  65. ):
  66. self.assertRaises(SaltInvocationError, grafana.dashboard_present, name)
  67. with patch.dict(grafana.__opts__, {"test": True}):
  68. self.assertRaises(SaltInvocationError, grafana.dashboard_present, name)
  69. comt = "Dashboard {0} is set to be created.".format(name)
  70. ret.update({"comment": comt})
  71. self.assertDictEqual(grafana.dashboard_present(name, True), ret)
  72. mock = MagicMock(
  73. return_value={"rows": [{"panels": "b", "title": "systemhealth"}]}
  74. )
  75. with patch.object(salt.utils.json, "loads", mock):
  76. ret.update({"comment": comt1, "result": None})
  77. self.assertDictEqual(
  78. grafana.dashboard_present(name, True, rows=row), ret
  79. )
  80. with patch.object(
  81. salt.utils.json, "loads", MagicMock(return_value={"rows": {}})
  82. ):
  83. self.assertRaises(
  84. SaltInvocationError,
  85. grafana.dashboard_present,
  86. name,
  87. rows_from_pillar=rows,
  88. )
  89. comt = "Dashboard myservice is up to date"
  90. ret.update({"comment": comt, "result": True})
  91. self.assertDictEqual(grafana.dashboard_present(name, True), ret)
  92. mock = MagicMock(
  93. return_value={"rows": [{"panels": "b", "title": "systemhealth"}]}
  94. )
  95. with patch.dict(grafana.__opts__, {"test": False}):
  96. with patch.object(salt.utils.json, "loads", mock):
  97. comt = "Failed to update dashboard myservice."
  98. ret.update({"comment": comt, "result": False})
  99. self.assertDictEqual(
  100. grafana.dashboard_present(name, True, rows=row), ret
  101. )
  102. # 'dashboard_absent' function tests: 1
  103. def test_dashboard_absent(self):
  104. """
  105. Test to ensure the named grafana dashboard is deleted.
  106. """
  107. name = "myservice"
  108. ret = {"name": name, "result": None, "changes": {}, "comment": ""}
  109. mock = MagicMock(
  110. side_effect=[
  111. {"hosts": True, "index": False},
  112. {"hosts": True, "index": True},
  113. {"hosts": True, "index": True},
  114. ]
  115. )
  116. mock_f = MagicMock(side_effect=[True, False])
  117. with patch.dict(
  118. grafana.__salt__, {"config.option": mock, "elasticsearch.exists": mock_f}
  119. ):
  120. self.assertRaises(SaltInvocationError, grafana.dashboard_absent, name)
  121. with patch.dict(grafana.__opts__, {"test": True}):
  122. comt = "Dashboard myservice is set to be removed."
  123. ret.update({"comment": comt, "result": None})
  124. self.assertDictEqual(grafana.dashboard_absent(name), ret)
  125. comt = "Dashboard myservice does not exist."
  126. ret.update({"comment": comt, "result": True})
  127. self.assertDictEqual(grafana.dashboard_absent(name), ret)