From a064acd183993c3b597ebf87f6e319d30559dd10 Mon Sep 17 00:00:00 2001 From: Chi Wai Chan Date: Thu, 25 Jul 2024 11:47:37 +0800 Subject: [PATCH] Fix tempest unittests The new version of croniter introduces year fields, and that breaks the unittest for tempest charm. In tempest charm, we only support vixie cron, and that means exactly 5 fields are required. Change-Id: I0da988c820ac0b691c8b51a56cfa756c90783499 --- charms/tempest-k8s/src/utils/validators.py | 10 +++++----- charms/tempest-k8s/tests/unit/test_validators.py | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/charms/tempest-k8s/src/utils/validators.py b/charms/tempest-k8s/src/utils/validators.py index ec198c26..b22898f8 100644 --- a/charms/tempest-k8s/src/utils/validators.py +++ b/charms/tempest-k8s/src/utils/validators.py @@ -54,12 +54,12 @@ def validated_schedule(schedule: str) -> Schedule: if not schedule: return Schedule(value=schedule, valid=True, err="") - # croniter supports second repeats, but vixie cron does not. - if len(schedule.split()) == 6: + # croniter supports more fields, but vixie cron does not. + if len(schedule.split()) != 5: return Schedule( value=schedule, valid=False, - err="This cron does not support seconds in schedule (6 fields). " + err="This cron only support Vixie cron in schedule (5 fields). " "Exactly 5 columns must be specified for iterator expression.", ) @@ -70,9 +70,9 @@ def validated_schedule(schedule: str) -> Schedule: cron = croniter(schedule, base, max_years_between_matches=1) except ValueError as e: msg = str(e) - # croniter supports second repeats, but vixie cron does not, + # croniter supports more fields, but vixie cron does not, # so update the error message here to suit. - if "Exactly 5 or 6 columns" in msg: + if croniter.bad_length in msg: msg = ( "Exactly 5 columns must be specified for iterator expression." ) diff --git a/charms/tempest-k8s/tests/unit/test_validators.py b/charms/tempest-k8s/tests/unit/test_validators.py index 3026a170..15efd8ea 100644 --- a/charms/tempest-k8s/tests/unit/test_validators.py +++ b/charms/tempest-k8s/tests/unit/test_validators.py @@ -70,7 +70,15 @@ class TempestCharmValidatorTests(unittest.TestCase): exp = "*/30 * * * * 6" schedule = validated_schedule(exp) self.assertFalse(schedule.valid) - self.assertIn("not support seconds", schedule.err) + self.assertIn("Exactly 5 columns", schedule.err) + self.assertEqual(schedule.value, exp) + + def test_expression_seven_fields(self): + """Verify an expression with seven fields is caught.""" + exp = "*/30 * * * * 6 2024/2" + schedule = validated_schedule(exp) + self.assertFalse(schedule.valid) + self.assertIn("Exactly 5 columns", schedule.err) self.assertEqual(schedule.value, exp) def test_expression_missing_column(self):