From 1af7a4e0106db5bc1b0c760e9a18d1975180251e Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Thu, 14 Apr 2016 14:10:19 -0400 Subject: [PATCH] Add support for influxdb datasource Change-Id: I8d4b073cdc3fec4e55d10fac014874003d3739b6 --- grafana_dashboards/schema/datasource.py | 3 +- ...asource.py => test_graphite_datasource.py} | 0 tests/grafana/test_influxdb_datasource.py | 99 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) rename tests/grafana/{test_datasource.py => test_graphite_datasource.py} (100%) create mode 100644 tests/grafana/test_influxdb_datasource.py diff --git a/grafana_dashboards/schema/datasource.py b/grafana_dashboards/schema/datasource.py index 2a27f3b..9e24ea2 100644 --- a/grafana_dashboards/schema/datasource.py +++ b/grafana_dashboards/schema/datasource.py @@ -22,7 +22,8 @@ class Datasource(object): v.Required('access', default='direct'): v.Any('direct', 'proxy'), v.Required('isDefault', default=False): v.All(bool), v.Required('name'): v.All(str, v.Length(min=1)), - v.Required('type', default='graphite'): v.Any('graphite'), + v.Required('type', default='graphite'): v.Any('graphite', + 'influxdb'), v.Required('url'): v.All(str, v.Length(min=1)), v.Optional('orgId'): int, } diff --git a/tests/grafana/test_datasource.py b/tests/grafana/test_graphite_datasource.py similarity index 100% rename from tests/grafana/test_datasource.py rename to tests/grafana/test_graphite_datasource.py diff --git a/tests/grafana/test_influxdb_datasource.py b/tests/grafana/test_influxdb_datasource.py new file mode 100644 index 0000000..2c5bd0f --- /dev/null +++ b/tests/grafana/test_influxdb_datasource.py @@ -0,0 +1,99 @@ +# Copyright 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import requests_mock +from testtools import TestCase + +from grafana_dashboards.grafana import Grafana + +DATASOURCE001 = { + "id": 1, + "orgId": 1, + "name": "foobar", + "type": "fluentd", + "access": "direct", + "url": "http://example.org:8080", + "password": "", + "user": "", + "database": "", + "basicAuth": False, + "basicAuthUser": "", + "basicAuthPassword": "", + "isDefault": True, + "jsonData": None, +} + +DATASOURCE_NOT_FOUND = { + "message": "Failed to query datasources" +} + + +class TestCaseDatasource(TestCase): + + def setUp(self): + super(TestCaseDatasource, self).setUp() + self.url = 'http://localhost' + self.grafana = Grafana(self.url) + + @requests_mock.Mocker() + def test_create_new(self, mock_requests): + mock_requests.post('/api/datasources/', json=DATASOURCE001) + mock_requests.get('/api/datasources/', json=[]) + res = self.grafana.datasource.create('foobar', DATASOURCE001) + self.assertEqual(res, DATASOURCE001) + + @requests_mock.Mocker() + def test_get_not_found(self, mock_requests): + mock_requests.get( + '/api/datasources/1', json=DATASOURCE_NOT_FOUND, + status_code=404) + res = self.grafana.datasource.get(1) + self.assertEqual(res, None) + + @requests_mock.Mocker() + def test_get_success(self, mock_requests): + mock_requests.get('/api/datasources/1', json=DATASOURCE001) + res = self.grafana.datasource.get(1) + self.assertEqual(res, DATASOURCE001) + + @requests_mock.Mocker() + def test_get_all(self, mock_requests): + mock_requests.get( + '/api/datasources/', json=[DATASOURCE001]) + res = self.grafana.datasource.get_all() + self.assertEqual(res, [DATASOURCE001]) + + @requests_mock.Mocker() + def test_get_all_empty(self, mock_requests): + mock_requests.get('/api/datasources/', json=[]) + res = self.grafana.datasource.get_all() + self.assertEqual(res, []) + + @requests_mock.Mocker() + def test_is_datasource_empty(self, mock_requests): + mock_requests.get('/api/datasources/', json=[]) + res = self.grafana.datasource.is_datasource('foobar') + self.assertFalse(res) + + @requests_mock.Mocker() + def test_is_datasource_false(self, mock_requests): + mock_requests.get('/api/datasources/', json=[DATASOURCE001]) + res = self.grafana.datasource.is_datasource('new') + self.assertFalse(res) + + @requests_mock.Mocker() + def test_is_datasource_true(self, mock_requests): + mock_requests.get('/api/datasources/', json=[DATASOURCE001]) + res = self.grafana.datasource.is_datasource('foobar') + self.assertTrue(res)