test_grafana_datasource.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.grafana_datasource as grafana_datasource
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, Mock, patch
  9. from tests.support.unit import TestCase
  10. profile = {
  11. "grafana_url": "http://grafana",
  12. "grafana_token": "token",
  13. }
  14. def mock_json_response(data):
  15. response = MagicMock()
  16. response.json = MagicMock(return_value=data)
  17. return Mock(return_value=response)
  18. class GrafanaDatasourceTestCase(TestCase, LoaderModuleMockMixin):
  19. def setup_loader_modules(self):
  20. return {grafana_datasource: {}}
  21. def test_present(self):
  22. with patch("requests.get", mock_json_response([])):
  23. with patch("requests.post") as rpost:
  24. ret = grafana_datasource.present("test", "type", "url", profile=profile)
  25. rpost.assert_called_once_with(
  26. "http://grafana/api/datasources",
  27. grafana_datasource._get_json_data("test", "type", "url"),
  28. headers={
  29. "Authorization": "Bearer token",
  30. "Accept": "application/json",
  31. },
  32. timeout=3,
  33. )
  34. self.assertTrue(ret["result"])
  35. self.assertEqual(ret["comment"], "New data source test added")
  36. data = grafana_datasource._get_json_data("test", "type", "url")
  37. data.update({"id": 1, "orgId": 1})
  38. with patch("requests.get", mock_json_response([data])):
  39. with patch("requests.put") as rput:
  40. ret = grafana_datasource.present("test", "type", "url", profile=profile)
  41. rput.assert_called_once_with(
  42. "http://grafana/api/datasources/1",
  43. grafana_datasource._get_json_data("test", "type", "url"),
  44. headers={
  45. "Authorization": "Bearer token",
  46. "Accept": "application/json",
  47. },
  48. timeout=3,
  49. )
  50. self.assertTrue(ret["result"])
  51. self.assertEqual(ret["comment"], "Data source test already up-to-date")
  52. self.assertEqual(ret["changes"], {})
  53. with patch("requests.put") as rput:
  54. ret = grafana_datasource.present(
  55. "test", "type", "newurl", profile=profile
  56. )
  57. rput.assert_called_once_with(
  58. "http://grafana/api/datasources/1",
  59. grafana_datasource._get_json_data("test", "type", "newurl"),
  60. headers={
  61. "Authorization": "Bearer token",
  62. "Accept": "application/json",
  63. },
  64. timeout=3,
  65. )
  66. self.assertTrue(ret["result"])
  67. self.assertEqual(ret["comment"], "Data source test updated")
  68. self.assertEqual(
  69. ret["changes"], {"old": {"url": "url"}, "new": {"url": "newurl"}}
  70. )
  71. def test_absent(self):
  72. with patch("requests.get", mock_json_response([])):
  73. with patch("requests.delete") as rdelete:
  74. ret = grafana_datasource.absent("test", profile=profile)
  75. self.assertTrue(rdelete.call_count == 0)
  76. self.assertTrue(ret["result"])
  77. self.assertEqual(ret["comment"], "Data source test already absent")
  78. with patch("requests.get", mock_json_response([{"name": "test", "id": 1}])):
  79. with patch("requests.delete") as rdelete:
  80. ret = grafana_datasource.absent("test", profile=profile)
  81. rdelete.assert_called_once_with(
  82. "http://grafana/api/datasources/1",
  83. headers={
  84. "Authorization": "Bearer token",
  85. "Accept": "application/json",
  86. },
  87. timeout=3,
  88. )
  89. self.assertTrue(ret["result"])
  90. self.assertEqual(ret["comment"], "Data source test was deleted")