Skip to content

Commit 42f4a55

Browse files
committed
Fix dashboard exploration when the annotations.list slot is None
The expected value would have been an empty list instead. This patch makes the gathering process more graceful.
1 parent c7f7c5c commit 42f4a55

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ grafana-wtf changelog
66
in progress
77
===========
88
- CI: Use most recent Grafana 7.5.16, 8.5.5, and 9.0.0-beta3
9+
- Fix dashboard exploration when the ``annotations.list`` slot is ``None``
10+
instead of an empty list. Thanks, @TaylorMutch!
911

1012
2022-03-25 0.13.3
1113
=================

grafana_wtf/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ def index(self):
497497

498498
@staticmethod
499499
def collect_datasource_items(element):
500+
element = element or []
500501
items = []
501502
for node in element:
502503
if "datasource" in node and node["datasource"]:

tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
from io import StringIO
55
from pathlib import Path
6-
from typing import List, Union
6+
from typing import List, Union, Optional
77

88
import grafanalib.core
99
import pytest
@@ -321,12 +321,14 @@ def grafana_version(docker_grafana):
321321
return grafana_version
322322

323323

324-
def mkdashboard(title: str, datasources: List[str]):
324+
def mkdashboard(title: str, datasources: Optional[List[str]] = None):
325325
"""
326326
Build dashboard with multiple panels, each with a different data source.
327327
"""
328328
# datasource = grafanalib.core.DataSourceInput(name="foo", label="foo", pluginId="foo", pluginName="foo")
329329

330+
datasources = datasources or []
331+
330332
# Build dashboard object model.
331333
panels = []
332334
for datasource in datasources:

tests/test_commands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,33 @@ def test_explore_dashboards_grafana7up(grafana_version, ldi_resources, capsys, c
366366
assert dashboard["datasources_missing"][0]["type"] is None
367367

368368

369+
def test_explore_dashboards_empty_annotations(create_datasource, create_dashboard, capsys, caplog):
370+
371+
# Create a dashboard with an anomalous value in the "annotations" slot.
372+
dashboard = mkdashboard(title="foo")
373+
dashboard["annotations"]["list"] = None
374+
create_dashboard(dashboard)
375+
376+
# Compute breakdown.
377+
set_command("explore dashboards", "--format=yaml")
378+
379+
# Proof the output is correct.
380+
with caplog.at_level(logging.DEBUG):
381+
grafana_wtf.commands.run()
382+
assert "Found 1 dashboard(s)" in caplog.messages
383+
384+
captured = capsys.readouterr()
385+
data = yaml.safe_load(captured.out)
386+
387+
# Proof the output is correct.
388+
assert len(data) == 1
389+
dashboard = data[0]
390+
assert dashboard["dashboard"]["title"] == "foo"
391+
assert len(dashboard["dashboard"]["uid"]) == 9
392+
assert "datasources" not in dashboard
393+
assert "datasources_missing" not in dashboard
394+
395+
369396
def find_all_missing_datasources(data):
370397
missing_items = []
371398
for item in data:

0 commit comments

Comments
 (0)