diff --git a/requirements.txt b/requirements.txt index 86ed5997..29ba8115 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ lightkube >= 0.11 lightkube-models >= 1.22.0.4 # From PYDEPS importlib-metadata~=6.0.0 +opentelemetry-exporter-otlp-proto-http diff --git a/tests/interface/conftest.py b/tests/interface/conftest.py new file mode 100644 index 00000000..3a7ab9ac --- /dev/null +++ b/tests/interface/conftest.py @@ -0,0 +1,48 @@ +# # Copyright 2022 Canonical Ltd. +# # See LICENSE file for licensing details. +# from unittest.mock import patch + +from unittest.mock import patch + +import pytest +from charm import PrometheusCharm +from interface_tester import InterfaceTester +from scenario import Container, ExecOutput, State + + +def tautology(*_, **__) -> bool: + return True + + +@pytest.fixture +def prometheus_charm(): + with patch("lightkube.core.client.GenericSyncClient"), patch.multiple( + "charm.KubernetesComputeResourcesPatch", + _namespace="test-namespace", + _patch=tautology, + is_ready=tautology, + ), patch("prometheus_client.Prometheus.reload_configuration"), patch.multiple( + "charm.PrometheusCharm", + _promtool_check_config=lambda *_: ("stdout", ""), + _prometheus_version="0.1.0", + ): + yield PrometheusCharm + + +def begin_with_initial_hooks_isolated() -> State: + container = Container( + "prometheus", + can_connect=True, + exec_mock={("update-ca-certificates", "--fresh"): ExecOutput(return_code=0, stdout="")}, + ) + state = State(containers=[container], leader=True) + return state + + +@pytest.fixture +def interface_tester(interface_tester: InterfaceTester, prometheus_charm): + interface_tester.configure( + charm_type=prometheus_charm, + state_template=begin_with_initial_hooks_isolated(), + ) + yield interface_tester diff --git a/tests/interface/test_prometheus_scrape.py b/tests/interface/test_prometheus_scrape.py new file mode 100644 index 00000000..9a67eb01 --- /dev/null +++ b/tests/interface/test_prometheus_scrape.py @@ -0,0 +1,11 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +from interface_tester import InterfaceTester + + +def test_prometheus_scrape_v0_interface(interface_tester: InterfaceTester): + interface_tester.configure( + interface_name="prometheus_scrape", + ) + interface_tester.run() diff --git a/tox.ini b/tox.ini index 0ca3cdca..c34c1fc9 100644 --- a/tox.ini +++ b/tox.ini @@ -102,6 +102,17 @@ deps = commands = pytest -v --tb native {[vars]tst_path}/scenario --log-cli-level=INFO -s {posargs} + +[testenv:interface] +description = Run interface tests +deps = + pytest + ops-scenario>=5.3.1 + pytest-interface-tester > 2.0.0 + -r{toxinidir}/requirements.txt +commands = + pytest -v --tb native {[vars]tst_path}/interface --log-cli-level=INFO -s {posargs} + [testenv:integration] description = Run integration tests deps =