-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
grafana_datasource_exchange
(#476)
* add datasource_exchange endpoint * fetch lib * fetch lib * add raise_on_error=false * PR comments
- Loading branch information
1 parent
8721dce
commit 54f7b1d
Showing
11 changed files
with
277 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
cosl>=0.0.12 | ||
cosl>=0.0.47 | ||
# pinned to 2.16 as 2.17 breaks our unittests | ||
ops | ||
kubernetes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
import json | ||
from contextlib import ExitStack | ||
from unittest.mock import MagicMock, patch | ||
|
||
import ops | ||
import pytest | ||
from charms.tempo_coordinator_k8s.v0.charm_tracing import charm_tracing_disabled | ||
from interface_tester import InterfaceTester | ||
from ops import ActiveStatus | ||
from scenario.state import Container, Exec, Relation, State | ||
|
||
from charm import LokiOperatorCharm | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
def patch_all(): | ||
with ExitStack() as stack: | ||
stack.enter_context(patch("lightkube.core.client.GenericSyncClient")) | ||
stack.enter_context( | ||
patch.multiple( | ||
"charms.observability_libs.v0.kubernetes_compute_resources_patch.KubernetesComputeResourcesPatch", | ||
_namespace="test-namespace", | ||
_patch=lambda _: None, | ||
is_ready=MagicMock(return_value=True), | ||
get_status=lambda _: ActiveStatus(""), | ||
) | ||
) | ||
stack.enter_context(charm_tracing_disabled()) | ||
|
||
yield | ||
|
||
|
||
loki_container = Container( | ||
name="loki", | ||
can_connect=True, | ||
execs={Exec(["update-ca-certificates", "--fresh"], return_code=0)}, | ||
layers={"loki": ops.pebble.Layer({"services": {"loki": {}}})}, | ||
service_statuses={"loki": ops.pebble.ServiceStatus.ACTIVE}, | ||
) | ||
|
||
grafana_source_relation = Relation( | ||
"grafana-source", | ||
remote_app_data={ | ||
"datasource_uids": json.dumps({"loki/0": "01234"}), | ||
"grafana_uid": "5678", | ||
}, | ||
) | ||
|
||
grafana_datasource_exchange_relation = Relation( | ||
"send-datasource", | ||
remote_app_data={ | ||
"datasources": json.dumps([{"type": "loki", "uid": "01234", "grafana_uid": "5678"}]) | ||
}, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def grafana_datasource_tester(interface_tester: InterfaceTester): | ||
interface_tester.configure( | ||
charm_type=LokiOperatorCharm, | ||
state_template=State( | ||
leader=True, containers=[loki_container], relations=[grafana_source_relation] | ||
), | ||
) | ||
yield interface_tester | ||
|
||
|
||
@pytest.fixture | ||
def grafana_datasource_exchange_tester(interface_tester: InterfaceTester): | ||
interface_tester.configure( | ||
charm_type=LokiOperatorCharm, | ||
state_template=State( | ||
leader=True, | ||
containers=[loki_container], | ||
relations=[grafana_source_relation, grafana_datasource_exchange_relation], | ||
), | ||
) | ||
yield interface_tester |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
from interface_tester import InterfaceTester | ||
|
||
|
||
def test_grafana_datasource_exchange_v0_interface( | ||
grafana_datasource_exchange_tester: InterfaceTester, | ||
): | ||
grafana_datasource_exchange_tester.configure( | ||
interface_name="grafana_datasource_exchange", | ||
interface_version=0, | ||
) | ||
grafana_datasource_exchange_tester.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
from interface_tester import InterfaceTester | ||
|
||
|
||
def test_grafana_datasource_v0_interface(grafana_datasource_tester: InterfaceTester): | ||
grafana_datasource_tester.configure( | ||
interface_name="grafana_datasource", | ||
interface_version=0, | ||
) | ||
grafana_datasource_tester.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
import json | ||
|
||
import pytest | ||
from cosl.interfaces.datasource_exchange import ( | ||
DatasourceExchange, | ||
DSExchangeAppData, | ||
GrafanaDatasource, | ||
) | ||
from scenario import Relation, State | ||
|
||
from charm import LokiOperatorCharm | ||
|
||
ds_tempo = [ | ||
{"type": "tempo", "uid": "3", "grafana_uid": "4"}, | ||
] | ||
|
||
ds_mimir = [ | ||
{"type": "prometheus", "uid": "8", "grafana_uid": "9"}, | ||
] | ||
|
||
mimir_dsx = Relation( | ||
"send-datasource", | ||
remote_app_data=DSExchangeAppData(datasources=json.dumps(ds_mimir)).dump(), | ||
) | ||
tempo_dsx = Relation( | ||
"send-datasource", | ||
remote_app_data=DSExchangeAppData(datasources=json.dumps(ds_tempo)).dump(), | ||
) | ||
|
||
ds = Relation( | ||
"grafana-source", | ||
remote_app_data={ | ||
"grafana_uid": "9", | ||
"datasource_uids": json.dumps({"loki/0": "1234"}), | ||
}, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("event_type", ("changed", "created", "joined")) | ||
@pytest.mark.parametrize("relation_to_observe", (ds, mimir_dsx, tempo_dsx)) | ||
def test_datasource_send(context, loki_container, relation_to_observe, event_type): | ||
|
||
state_in = State( | ||
relations=[ | ||
ds, | ||
mimir_dsx, | ||
tempo_dsx, | ||
], | ||
containers=[loki_container], | ||
leader=True, | ||
) | ||
|
||
# WHEN we receive a datasource-related event | ||
with context( | ||
getattr(context.on, f"relation_{event_type}")(relation_to_observe), state_in | ||
) as mgr: | ||
charm: LokiOperatorCharm = mgr.charm | ||
# THEN we can find all received datasource uids | ||
dsx: DatasourceExchange = charm.datasource_exchange | ||
received = dsx.received_datasources | ||
assert received == ( | ||
GrafanaDatasource(type="tempo", uid="3", grafana_uid="4"), | ||
GrafanaDatasource(type="prometheus", uid="8", grafana_uid="9"), | ||
) | ||
state_out = mgr.run() | ||
|
||
# AND THEN we publish our own datasource information to mimir and tempo | ||
published_dsx_mimir = state_out.get_relation(mimir_dsx.id).local_app_data | ||
published_dsx_tempo = state_out.get_relation(tempo_dsx.id).local_app_data | ||
assert published_dsx_tempo == published_dsx_mimir | ||
assert json.loads(published_dsx_tempo["datasources"])[0] == { | ||
"type": "loki", | ||
"uid": "1234", | ||
"grafana_uid": "9", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters