Tools to make Grafana dashboards from templates
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_builder.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2015 Red Hat, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import os
  15. import mock
  16. from grafana_dashboards import builder
  17. from tests.base import TestCase
  18. class TestCaseBuilder(TestCase):
  19. def setUp(self):
  20. super(TestCaseBuilder, self).setUp()
  21. self.builder = builder.Builder(self.config)
  22. @mock.patch('grafana_dashboards.grafana.Dashboard.delete')
  23. def test_delete_dashboard(self, mock_grafana):
  24. path = os.path.join(
  25. os.path.dirname(__file__), 'fixtures/builder/dashboard-0001.yaml')
  26. # Create a dashboard.
  27. self._update_dashboard(path)
  28. # Create a new builder to avoid duplicate dashboards.
  29. builder2 = builder.Builder(self.config)
  30. # Delete same dashboard, ensure we delete it from grafana.
  31. builder2.delete_dashboard(path)
  32. self.assertEqual(mock_grafana.call_count, 1)
  33. def test_grafana_defaults(self):
  34. self.assertEqual(
  35. self.builder.grafana.server, 'http://grafana.example.org')
  36. self.assertEqual(self.builder.grafana.auth, None)
  37. @mock.patch('grafana_dashboards.grafana.Dashboard.create')
  38. def test_update_dashboard(self, mock_grafana):
  39. path = os.path.join(
  40. os.path.dirname(__file__), 'fixtures/builder/dashboard-0001.yaml')
  41. # Create a dashboard.
  42. self._update_dashboard(path)
  43. # Create a new builder to avoid duplicate dashboards.
  44. builder2 = builder.Builder(self.config)
  45. # Update again with same dashboard, ensure we don't update grafana.
  46. builder2.update_dashboard(path)
  47. self.assertEqual(mock_grafana.call_count, 0)
  48. @mock.patch('grafana_dashboards.grafana.Dashboard.create')
  49. def _update_dashboard(self, path, mock_grafana):
  50. self.builder.update_dashboard(path)
  51. # Cache is empty, so we should update grafana.
  52. self.assertEqual(mock_grafana.call_count, 1)