From a3d80a0b94bf5cf0a340efaa0fa463f96fef858f Mon Sep 17 00:00:00 2001 From: Ryan Barry Date: Fri, 20 May 2022 09:18:20 -0400 Subject: [PATCH] Fix the duration validator so it matches what prom actually uses (#272) * Fix the duration validator so it matches what prom actually uses * Add comments referencing the regexp definition and validation --- src/charm.py | 10 +++++++++- tests/unit/test_charm.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/charm.py b/src/charm.py index c9fe18ca..c137a608 100755 --- a/src/charm.py +++ b/src/charm.py @@ -256,7 +256,15 @@ def _is_valid_timespec(self, timeval) -> bool: Returns: True if time specification is valid and False otherwise. """ - if not (matched := re.match(r"[1-9][0-9]*[ymwdhs]", timeval)): + # Prometheus checks here: + # https://github.com/prometheus/common/blob/627089d3a7af73be778847aa577192b937b8d89a/model/time.go#L186 + # Which is where this regex is sourced from. The validation is done + # when parsing flags as part of binary invocation here: + # https://github.com/prometheus/prometheus/blob/c40e269c3e514953299e9ba1f6265e067ab43e64/cmd/prometheus/main.go#L302 + timespec_re = re.compile( + r"^((([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?|0)$" + ) + if not (matched := timespec_re.search(timeval)): self.unit.status = BlockedStatus(f"Invalid time spec : {timeval}") return bool(matched) diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index ada743cf..019af550 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -156,7 +156,7 @@ def test_invalid_metrics_retention_times_can_not_be_set(self): self.assertEqual(cli_arg(plan, "--storage.tsdb.retention.time"), None) # invalid time value - retention_time = "0d" + retention_time = "5m1y2d" retention_time_config["metrics_retention_time"] = retention_time self.harness.update_config(retention_time_config)