Skip to content

Commit

Permalink
Fetch Grafana version from /api/frontend/settings (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz authored Sep 28, 2024
1 parent a5db83e commit 9153a57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
16 changes: 10 additions & 6 deletions grafana_wtf/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,28 +321,32 @@ def info(self):
return response

@property
def health(self):
def build_info(self):
response = None
error = None
error_template = f"The request to {self.grafana_url.rstrip('/')}/api/health failed"
error_template = f"The request to {self.grafana_url.rstrip('/')}/api/frontend/settings failed"
try:
response = self.grafana.client.GET("/health")
response = self.grafana.client.GET("/frontend/settings")
if not isinstance(response, dict):
error = f"{error_template}: Invalid response, content was: {response}"

response = Munch(response)
response = response.get("buildInfo")
if not response:
error = f"{error_template}: No buildInfo found in the settings response"

except Exception as ex:
error = f"{error_template}: {ex}"

if error:
log.critical(error)
raise ConnectionError(error)

if response:
return Munch(response)
return response

@property
def version(self):
return self.health.get("version")
return self.build_info.get("version")

def dashboard_details(self):
for dashboard in self.data.dashboards:
Expand Down
17 changes: 8 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,28 @@ def test_collect_datasource_items_variable_all():

def test_connect_success():
wtf = GrafanaWtf("https://play.grafana.org")
health = wtf.health
assert "commit" in health
assert "version" in health
assert health.database == "ok"
build_info = wtf.build_info
assert "commit" in build_info
assert "version" in build_info


def test_connect_failure():
wtf = GrafanaWtf("http://localhost:1234")
with pytest.raises(ConnectionError) as ex:
_ = wtf.health
assert ex.match("The request to http://localhost:1234/api/health failed")
_ = wtf.build_info
assert ex.match("The request to http://localhost:1234/api/frontend/settings failed")


@patch("grafana_client.client.GrafanaClient.__getattr__")
def test_connect_version(mock_get):
mock_get.return_value = Mock()
mock_get.return_value.return_value = {"commit": "14e988bd22", "database": "ok", "version": "9.0.1"}
mock_get.return_value.return_value = {"buildInfo": {"version": "9.0.1", "commit": "14e988bd22"}}
wtf = GrafanaWtf("http://localhost:1234")
assert wtf.version == "9.0.1"


def test_connect_non_json_response():
wtf = GrafanaWtf("https://example.org/")
with pytest.raises(ConnectionError) as ex:
_ = wtf.health
assert ex.match("The request to https://example.org/api/health failed")
_ = wtf.build_info
assert ex.match("The request to https://example.org/api/frontend/settings failed")

0 comments on commit 9153a57

Please sign in to comment.