Skip to content

Commit

Permalink
added test for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Nov 25, 2024
1 parent 46061af commit e37749a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/charms/grafana_k8s/v0/grafana_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,13 @@ def update_source(self, source_url: Optional[str] = ""):
continue
self._set_sources(rel)

def get_source_uids(self) -> Dict[Relation, str]:
def get_source_uids(self) -> Dict[Relation, Dict[str, str]]:
"""Get the datasource UID(s) assigned by the remote end(s) to this datasource."""
uids = {}
for rel in self._charm.model.relations.get(self._relation_name, []):
if not rel:
continue
uids[rel] = rel.data[rel.app]["datasource_uid"]
uids[rel] = json.loads(rel.data[rel.app]["datasource_uids"])
return uids

def _set_sources_from_event(self, event: RelationJoinedEvent) -> None:
Expand Down
46 changes: 45 additions & 1 deletion tests/scenario/test_datasources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json

from ops import CharmBase, Framework
from ops.testing import Container, State
from scenario import Relation, PeerRelation
from scenario import Relation, PeerRelation, Context

from charms.grafana_k8s.v0.grafana_source import GrafanaSourceProvider

containers = [
Container(name="grafana", can_connect=True),
Expand Down Expand Up @@ -39,3 +42,44 @@ def test_datasource_sharing(ctx):
"remote_host/0": "juju_foo_bar_baz_0",
"remote_host/1": "juju_foo_bar_baz_1",
}

def test_datasource_get():
# GIVEN a datasource relation with two remote units
local_ds_uids = {
"prometheus/0": "some-datasource-uid",
"prometheus/1": "some-datasource-uid",
}
datasource = Relation(
"grafana-source",
remote_app_name="remote_host",
local_unit_data={"grafana_source_host": "somehost:80"},
local_app_data={
"grafana_source_data": json.dumps(
{"model": "foo", "model_uuid": "bar", "application": "baz", "type": "tempo"}
)
},
remote_app_data={
"datasource_uids": json.dumps(local_ds_uids)
}
)
state = State(
leader=True, relations={datasource}
)

# WHEN relation-changed fires for a datasource relation
class MyProviderCharm(CharmBase):
META={"name":"edgar",
"provides":{"grafana-source": {"interface": "grafana_datasource"}}}

def __init__(self, framework: Framework):
super().__init__(framework)
self.source_provider = GrafanaSourceProvider(self, "tempo",
source_url="somehost",
source_port="80")

ctx = Context(MyProviderCharm, MyProviderCharm.META)
with ctx(ctx.on.relation_changed(datasource), state) as mgr:
charm = mgr.charm
# THEN we can see our datasource uids via the provider
assert list(charm.source_provider.get_source_uids().values())[0] == local_ds_uids

0 comments on commit e37749a

Please sign in to comment.