diff --git a/config.yaml b/config.yaml index 58220e41..85cf2538 100644 --- a/config.yaml +++ b/config.yaml @@ -7,7 +7,12 @@ options: default: "" description: > Alertmanager configuration file (yaml), with the exclusion of the templates section. - Refer to https://www.prometheus.io/docs/alerting/latest/configuration/ for full details. + To send the contents of a file to this configuration option, the symbol `@` must be used. + + Usage: `juju config alertmanager config_file=@alertmanager.yaml` + + For more information on configuring the Alertmanager, refer to: + https://www.prometheus.io/docs/alerting/latest/configuration/ templates_file: type: string default: "" diff --git a/src/charm.py b/src/charm.py index a4b8b787..8758a6ff 100755 --- a/src/charm.py +++ b/src/charm.py @@ -351,6 +351,15 @@ def _get_local_config(self) -> Optional[Tuple[Optional[dict], Optional[str]]]: config = self.config["config_file"] if config: local_config = yaml.safe_load(cast(str, config)) + + # If `juju config` is executed like this `config_file=am.yaml` instead of + # `config_file=@am.yaml` local_config will be the string `am.yaml` instead + # of its content (dict). + if not isinstance(local_config, dict): + msg = f"Unable to set config from file. Use juju config {self.unit.name} config_file=@FILENAME" + logger.error(msg) + raise ConfigUpdateFailure(msg) + local_templates = cast(str, self.config["templates_file"]) or None return local_config, local_templates return None diff --git a/tests/integration/am_config.yaml b/tests/integration/am_config.yaml new file mode 100644 index 00000000..0f6cd721 --- /dev/null +++ b/tests/integration/am_config.yaml @@ -0,0 +1,9 @@ +route: + receiver: test_receiver + group_by: + - alertname + group_wait: 1234s + group_interval: 4321s + repeat_interval: 1111h +receivers: +- name: test_receiver diff --git a/tests/integration/test_remote_configuration.py b/tests/integration/test_remote_configuration.py index 4fd1b025..ca8f09c8 100644 --- a/tests/integration/test_remote_configuration.py +++ b/tests/integration/test_remote_configuration.py @@ -16,6 +16,7 @@ import helpers import pytest +import sh import yaml from deepdiff import DeepDiff # type: ignore[import] from pytest_operator.plugin import OpsTest @@ -105,6 +106,26 @@ async def test_remote_configuration_applied_on_relation_created(ops_test: OpsTes ) +@pytest.mark.abort_on_fail +async def test_remote_configuration_file_wrongly_applied(ops_test: OpsTest, setup): + sh.juju( + [ + "config", + f"{APP_NAME}", + "-m", + ops_test.model_name, + "config_file=tests/integration/am_config.yaml", + ] + ) + + await ops_test.model.wait_for_idle( + apps=[APP_NAME], + status="blocked", + timeout=1000, + idle_period=5, + ) + + def _copy_alertmanager_remote_configuration_library_into_tester_charm(): """Ensure that the tester charm uses the current Alertmanager Remote Configuration library.""" library_path = "lib/charms/alertmanager_k8s/v0/alertmanager_remote_configuration.py"