grafyaml/tests/test_grafana.py
Ian Wienand 2c3823432d Generate and use UID for acessing dashboards
We are relying on the "slug" field being the same as our "url
friendly" title that we create.  For whatever reason the slug field
was deprecated in Grafana v5.0, and now with v8.3.4 it has stopped
working.

We can turn the name/slug into a UID by hashing, and then use this in
the various API calls.

Change-Id: I13d3162c917e094684756e51836d12000621fefa
2022-01-25 10:59:28 +11:00

128 lines
4.2 KiB
Python

# 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
import re
from testtools import TestCase
from grafana_dashboards.grafana import Grafana
CREATE_NEW_DASHBOARD = {
"meta": {
"canSave": True,
"created": "0001-01-01T00:00:00Z",
"canStar": True,
"expires": "0001-01-01T00:00:00Z",
"slug": "new-dashboard",
"type": "db",
"canEdit": True
},
"dashboard": {
"rows": [],
"id": 1,
"version": 0,
"title": "New dashboard"
}
}
DASHBOARD_NOT_FOUND = {
"message": "Dashboard not found"
}
UID_URL_MATCHER = re.compile(r'api/dashboards/uid/[a-fA-F\d]{10}')
class TestCaseGrafana(TestCase):
def setUp(self):
super(TestCaseGrafana, self).setUp()
self.url = 'http://localhost'
self.grafana = Grafana(self.url)
def test_init(self):
grafana = Grafana(self.url)
self.assertNotIn('Authorization', grafana.dashboard.session.headers)
def test_init_apikey(self):
apikey = 'eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk'
grafana = Grafana(self.url, apikey)
headers = grafana.dashboard.session.headers
self.assertIn('Authorization', headers)
self.assertEqual(headers['Authorization'], 'Bearer %s' % apikey)
@requests_mock.Mocker()
def test_create_dashboard_new(self, mock_requests):
def post_callback(request, context):
mock_requests.get(UID_URL_MATCHER, json=CREATE_NEW_DASHBOARD)
return True
mock_requests.post('/api/dashboards/db/', json=post_callback)
mock_requests.get(
UID_URL_MATCHER, json=DASHBOARD_NOT_FOUND, status_code=404)
data = {
"dashboard": {
"title": "New dashboard",
},
"slug": 'new-dashboard',
}
self.grafana.dashboard.create(
name=data['slug'], data=data['dashboard'])
self.assertEqual(mock_requests.call_count, 3)
@requests_mock.Mocker()
def test_create_dashboard_overwrite(self, mock_requests):
mock_requests.post('/api/dashboards/db/')
mock_requests.get(UID_URL_MATCHER, json=CREATE_NEW_DASHBOARD)
data = {
"dashboard": {
"title": "New dashboard",
},
"slug": 'new-dashboard',
}
self.grafana.dashboard.create(
name=data['slug'], data=data['dashboard'], overwrite=True)
self.assertEqual(mock_requests.call_count, 2)
@requests_mock.Mocker()
def test_create_dashboard_existing(self, mock_requests):
mock_requests.post('/api/dashboards/db/')
mock_requests.get(UID_URL_MATCHER, json=CREATE_NEW_DASHBOARD)
data = {
"dashboard": {
"title": "New dashboard",
},
"slug": 'new-dashboard',
}
self.assertRaises(
Exception, self.grafana.dashboard.create, name=data['slug'],
data=data['dashboard'], overwrite=False)
self.assertEqual(mock_requests.call_count, 1)
@requests_mock.Mocker()
def test_delete_dashboard(self, mock_requests):
mock_requests.delete(UID_URL_MATCHER)
mock_requests.get(
UID_URL_MATCHER, json=DASHBOARD_NOT_FOUND, status_code=404)
self.grafana.dashboard.delete('new-dashboard')
@requests_mock.Mocker()
def test_delete_dashboard_failure(self, mock_requests):
mock_requests.delete('/api/dashboards/db/new-dashboard')
mock_requests.get(
'/api/dashboards/db/new-dashboard', json=CREATE_NEW_DASHBOARD)
self.assertRaises(
Exception, self.grafana.dashboard.delete, name='new-dashboard')