diff --git a/tests/interface/conftest.py b/tests/interface/conftest.py new file mode 100644 index 00000000..1a6e8f43 --- /dev/null +++ b/tests/interface/conftest.py @@ -0,0 +1,49 @@ +from contextlib import ExitStack +from unittest.mock import patch + +import pytest +from scenario import State, Container + +from charm import GrafanaCharm + + +@pytest.fixture +def containers(): + """Mocks for standard containers grafana needs to work.""" + return [ + Container(name="grafana", can_connect=True), + Container(name="litestream", can_connect=True), + ] + + +@pytest.fixture(autouse=True, scope="module") +def apply_all_patches(): + patches = ( + patch.multiple( + "charm.KubernetesComputeResourcesPatch", + _namespace="test-namespace", + _patch=lambda *_a, **_k: True, + is_ready=lambda *_a, **_k: True, + ), + patch("charm.GrafanaCharm._push_sqlite_static", new=lambda _: None), + patch("lightkube.core.client.GenericSyncClient"), + patch("socket.getfqdn", new=lambda *args: "grafana-k8s-0.testmodel.svc.cluster.local"), + patch("socket.gethostbyname", new=lambda *args: "1.2.3.4"), + patch.object(GrafanaCharm, "grafana_version", "0.1.0"), + ) + with ExitStack() as stack: + for _patch in patches: + stack.enter_context(_patch) + yield + + +@pytest.fixture +def grafana_source_tester(interface_tester, containers): + interface_tester.configure( + charm_type=GrafanaCharm, + state_template=State( + leader=True, + containers=containers, + ), + ) + yield interface_tester diff --git a/tests/interface/test_grafana_datasource.py b/tests/interface/test_grafana_datasource.py new file mode 100644 index 00000000..c6984cfd --- /dev/null +++ b/tests/interface/test_grafana_datasource.py @@ -0,0 +1,9 @@ +from interface_tester import InterfaceTester + + +def test_grafana_datasource_v0_interface(grafana_source_tester: InterfaceTester): + grafana_source_tester.configure( + interface_name="grafana_datasource", + interface_version=0, + ) + grafana_source_tester.run() diff --git a/tests/scenario/conftest.py b/tests/scenario/conftest.py index 68c4d2b3..8b4ebdb8 100644 --- a/tests/scenario/conftest.py +++ b/tests/scenario/conftest.py @@ -1,3 +1,4 @@ +from contextlib import ExitStack from unittest.mock import patch import pytest @@ -6,29 +7,27 @@ from charm import GrafanaCharm -def tautology(*_, **__) -> bool: - return True - - -@pytest.fixture -def ctx(): +@pytest.fixture(autouse=True, scope="module") +def apply_all_patches(): patches = ( - patch("charm.GrafanaCharm._push_sqlite_static", new=lambda _: None), - patch("lightkube.core.client.GenericSyncClient"), - patch("socket.getfqdn", new=lambda *args: "grafana-k8s-0.testmodel.svc.cluster.local"), - patch("socket.gethostbyname", new=lambda *args: "1.2.3.4"), patch.multiple( "charm.KubernetesComputeResourcesPatch", _namespace="test-namespace", - _patch=tautology, - is_ready=tautology, + _patch=lambda *_a, **_k: True, + is_ready=lambda *_a, **_k: True, ), + patch("charm.GrafanaCharm._push_sqlite_static", new=lambda _: None), + patch("lightkube.core.client.GenericSyncClient"), + patch("socket.getfqdn", new=lambda *args: "grafana-k8s-0.testmodel.svc.cluster.local"), + patch("socket.gethostbyname", new=lambda *args: "1.2.3.4"), patch.object(GrafanaCharm, "grafana_version", "0.1.0"), ) - for p in patches: - p.__enter__() + with ExitStack() as stack: + for _patch in patches: + stack.enter_context(_patch) + yield - yield Context(GrafanaCharm) - for p in patches: - p.__exit__(None, None, None) +@pytest.fixture +def ctx(): + yield Context(GrafanaCharm)