Skip to content

Commit

Permalink
Tests: Mimic Grafana 7/8 on datasource references within dashboards
Browse files Browse the repository at this point in the history
Newer versions have objects (uid, type) instead of bare names.

This improvement reveals an implementation flaw reported at #32.

  TypeError: '<' not supported between instances of 'dict' and 'dict'
  • Loading branch information
amotl committed Mar 24, 2022
1 parent 7c9e1d3 commit 3d7dc3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ in progress
- Tests: Improve test quality, specifically for ``explore dashboards`` on
Grafana 6 vs. Grafana >= 7
- Tests: Make test case for `explore datasources` use _two_ data sources
- Tests: Mimic Grafana 7/8 on datasource references within dashboards, newer
versions have objects (uid, type) instead of bare names


2022-02-03 0.13.1
Expand Down
12 changes: 10 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest
from grafana_client.client import GrafanaClientError
from grafanalib._gen import write_dashboard
from packaging import version

from grafana_wtf.core import GrafanaWtf

Expand Down Expand Up @@ -53,7 +54,7 @@ def docker_grafana(docker_services):


@pytest.fixture
def create_datasource(docker_grafana):
def create_datasource(docker_grafana, grafana_version):
"""
Create a Grafana data source from a test case.
After the test case finished, it will remove the data source again.
Expand Down Expand Up @@ -100,14 +101,20 @@ def create_datasource(docker_grafana):
# Keep track of the datasource ids in order to delete them afterwards.
datasource_ids = []

def mkresponse(response):
if version.parse(grafana_version) < version.parse("8"):
return response["name"]
else:
return {"uid": response["uid"], "type": response["type"]}

def _create_datasource(name: str, type: str = "testdata", access: str = "proxy", **kwargs):

# Reuse existing datasource.
try:
response = grafana.datasource.get_datasource_by_name(name)
datasource_id = response["id"]
datasource_ids.append(datasource_id)
return
return mkresponse(response)
except GrafanaClientError as ex:
if ex.status_code != 404:
raise
Expand All @@ -119,6 +126,7 @@ def _create_datasource(name: str, type: str = "testdata", access: str = "proxy",
response = grafana.datasource.create_datasource(datasource)
datasource_id = response["datasource"]["id"]
datasource_ids.append(datasource_id)
return mkresponse(response["datasource"])
except GrafanaClientError as ex:
# TODO: Mimic the original response in order to make the removal work.
# `{'datasource': {'id': 5, 'uid': 'u9wNRyEnk', 'orgId': 1, ...`.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ def test_log_tabular_success(ldi_resources, capsys, caplog):
def test_explore_datasources_used(create_datasource, create_dashboard, capsys, caplog):

# Create two data sources and a dashboard which uses them.
create_datasource(name="foo")
create_datasource(name="bar")
create_dashboard(mkdashboard(title="baz", datasources=["foo", "bar"]))
ds_foo = create_datasource(name="foo")
ds_bar = create_datasource(name="bar")
create_dashboard(mkdashboard(title="baz", datasources=[ds_foo, ds_bar]))

# Compute breakdown.
set_command("explore datasources", "--format=yaml")
Expand Down

0 comments on commit 3d7dc3c

Please sign in to comment.