From 3d7dc3c8d981d5641073115a58e7ad71a31da0eb Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 25 Mar 2022 00:49:49 +0100 Subject: [PATCH] Tests: Mimic Grafana 7/8 on datasource references within dashboards 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' --- CHANGES.rst | 2 ++ tests/conftest.py | 12 ++++++++++-- tests/test_commands.py | 6 +++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 8428b42..431e351 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 90023df..723535f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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. @@ -100,6 +101,12 @@ 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. @@ -107,7 +114,7 @@ def _create_datasource(name: str, type: str = "testdata", access: str = "proxy", 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 @@ -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, ...`. diff --git a/tests/test_commands.py b/tests/test_commands.py index 699048f..b2df0f8 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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")