Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasource interface tests #361

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/interface/conftest.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tests/interface/test_grafana_datasource.py
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 16 additions & 17 deletions tests/scenario/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import ExitStack
from unittest.mock import patch

import pytest
Expand All @@ -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)
Loading