Skip to content

Commit

Permalink
Adding Prometheus scrape interface tests (#575)
Browse files Browse the repository at this point in the history
* adding interface_tester base fixture

* pointing to main interface tester

* remove temp branch
  • Loading branch information
IbraAoad authored Feb 13, 2024
1 parent d7a20a6 commit ede3eea
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions tests/interface/conftest.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/interface/test_prometheus_scrape.py
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_prometheus_scrape_v0_interface(interface_tester: InterfaceTester):
interface_tester.configure(
interface_name="prometheus_scrape",
)
interface_tester.run()
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit ede3eea

Please sign in to comment.