diff --git a/tests/scenario/conftest.py b/tests/scenario/conftest.py index e658e08c..48266939 100644 --- a/tests/scenario/conftest.py +++ b/tests/scenario/conftest.py @@ -1,5 +1,8 @@ import shutil from pathlib import Path +from ops.testing import Context + +from charm import GrafanaAgentK8sCharm import pytest @@ -12,3 +15,8 @@ def vroot(tmp_path) -> Path: shutil.rmtree(root) shutil.copytree(CHARM_ROOT / "src", root / "src") return root + + +@pytest.fixture +def ctx(): + yield Context(GrafanaAgentK8sCharm) diff --git a/tests/scenario/test_config_reporting_enabled.py b/tests/scenario/test_config_reporting_enabled.py new file mode 100644 index 00000000..52b5d718 --- /dev/null +++ b/tests/scenario/test_config_reporting_enabled.py @@ -0,0 +1,47 @@ +from ops.testing import State, Container +from configparser import ConfigParser + + +containers = [Container(name="agent", can_connect=True)] + + +def test_reporting_enabled(ctx): + # GIVEN the "reporting_enabled" config option is set to True + state = State( + leader=True, config={"reporting_enabled": True}, containers=containers + ) + + # WHEN config-changed fires + out = ctx.run(ctx.on.config_changed(), state) + + # THEN the config file is written WITHOUT the [analytics] section being rendered + simulated_pebble_filesystem = out.get_container("grafana").get_filesystem(ctx) + grafana_config_path = simulated_pebble_filesystem / "etc/grafana/grafana-config.ini" + + config = ConfigParser() + config.read(grafana_config_path) + assert "analytics" not in config + + +def test_reporting_disabled(ctx): + # GIVEN the "reporting_enabled" config option is set to False + state = State(leader=True, config={"reporting_enabled": False}, containers=containers) + # WHEN config-changed fires + out = ctx.run(ctx.on.config_changed(), state) + + # THEN the config file is written WITH the [analytics] section being rendered + simulated_pebble_filesystem = out.get_container("grafana").get_filesystem(ctx) + grafana_config_path = simulated_pebble_filesystem / "etc/grafana/grafana-config.ini" + + config = ConfigParser() + config.read(grafana_config_path) + assert "analytics" in config + assert dict(config["analytics"]) == { + "reporting_enabled": "false", + "check_for_updates": "false", + "check_for_plugin_updates": "false", + } + + # AND the "grafana" service is restarted + # TODO Does it make sense to check this if the charm under test's lifetime is only for the config-changed? + # TODO How to assert this? diff --git a/tox.ini b/tox.ini index abb622ae..654cbbaa 100644 --- a/tox.ini +++ b/tox.ini @@ -85,7 +85,7 @@ deps = -r{toxinidir}/requirements.txt pytest cosl - ops-scenario >= 6.1.5,<7.0.0 + ops[testing] commands = pytest -vv --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path}/scenario --ignore {[vars]tst_path}/scenario/test_k8s