From ce62ac42a4e020ba6be67a471cf2ad1b62b93f57 Mon Sep 17 00:00:00 2001 From: Kevin DeJong Date: Sun, 26 May 2024 07:56:45 -0700 Subject: [PATCH] Validate CfnLintKeyword functions have validate function (#3266) --- .../backup/BackupPlanLifecycleRule.py | 5 ++- .../elasticache/CacheClusterFailover.py | 11 ++---- .../jsonschema/test_cfn_lint_rules.py | 38 +++++++++++++++++++ .../backup/test_backup_plan_lifecycle_rule.py | 4 +- 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 test/integration/jsonschema/test_cfn_lint_rules.py diff --git a/src/cfnlint/rules/resources/backup/BackupPlanLifecycleRule.py b/src/cfnlint/rules/resources/backup/BackupPlanLifecycleRule.py index 0a439b19ab..3d05fdc9b5 100644 --- a/src/cfnlint/rules/resources/backup/BackupPlanLifecycleRule.py +++ b/src/cfnlint/rules/resources/backup/BackupPlanLifecycleRule.py @@ -27,11 +27,12 @@ class BackupPlanLifecycleRule(CfnLintKeyword): def __init__(self) -> None: super().__init__( [ - "Resources/AWS::Backup::BackupPlan/Properties/BackupPlan/BackupPlanRule/*/Lifecycle" + "Resources/AWS::Backup::BackupPlan/Properties/BackupPlan/BackupPlanRule/*/Lifecycle", + "Resources/AWS::Backup::BackupPlan/Properties/BackupPlan/BackupPlanRule/*/CopyActions/*/Lifecycle", ] ) - def backupbackupplanlifecycle(self, validator, uI, instance, schema): + def validate(self, validator, uI, instance, schema): delete_after_days = instance.get("DeleteAfterDays") move_to_cold_storage_after_days = instance.get("MoveToColdStorageAfterDays") diff --git a/src/cfnlint/rules/resources/elasticache/CacheClusterFailover.py b/src/cfnlint/rules/resources/elasticache/CacheClusterFailover.py index 0fc0d6e3ec..caadc2cca7 100644 --- a/src/cfnlint/rules/resources/elasticache/CacheClusterFailover.py +++ b/src/cfnlint/rules/resources/elasticache/CacheClusterFailover.py @@ -4,11 +4,10 @@ """ from cfnlint.helpers import bool_compare -from cfnlint.rules import RuleMatch -from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword +from cfnlint.rules import CloudFormationLintRule, RuleMatch -class CacheClusterFailover(CfnLintKeyword): +class CacheClusterFailover(CloudFormationLintRule): """Check automatic failover on a cache cluster""" id = "E3026" @@ -22,11 +21,7 @@ class CacheClusterFailover(CfnLintKeyword): def __init__(self): """Init""" - super().__init__( - keywords=[ - "Resources/AWS::ElastiCache::ReplicationGroup/Properties/CacheParameterGroupName" - ] - ) + super().__init__() self.resource_property_types.append("AWS::ElastiCache::ReplicationGroup") def is_cluster_enabled(self, properties): diff --git a/test/integration/jsonschema/test_cfn_lint_rules.py b/test/integration/jsonschema/test_cfn_lint_rules.py new file mode 100644 index 0000000000..cdc959da9c --- /dev/null +++ b/test/integration/jsonschema/test_cfn_lint_rules.py @@ -0,0 +1,38 @@ +""" +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +SPDX-License-Identifier: MIT-0 +""" + +from __future__ import annotations + +import pathlib + +from cfnlint.helpers import load_plugins + + +def test_cfn_lint_rules_have_validate_function(): + root_dir = pathlib.Path(__file__).parent.parent.parent.parent / "src/cfnlint/rules" + rules = load_plugins( + str(root_dir), + "CfnLintKeyword", + "cfnlint.rules.jsonschema.CfnLintKeyword", + ) + rules.extend( + load_plugins( + str(root_dir), + "CfnLintJsonSchema", + "cfnlint.rules.jsonschema.CfnLintJsonSchema", + ) + ) + rules.extend( + load_plugins( + str(root_dir), + "CfnLintJsonSchemaRegional", + "cfnlint.rules.jsonschema.CfnLintJsonSchemaRegional", + ) + ) + + for rule in rules: + assert hasattr(rule, "validate") + assert callable(rule.validate) + assert "E1101" in rule._parent_rules diff --git a/test/unit/rules/resources/backup/test_backup_plan_lifecycle_rule.py b/test/unit/rules/resources/backup/test_backup_plan_lifecycle_rule.py index c08736b8b5..f3d31cf689 100644 --- a/test/unit/rules/resources/backup/test_backup_plan_lifecycle_rule.py +++ b/test/unit/rules/resources/backup/test_backup_plan_lifecycle_rule.py @@ -71,7 +71,5 @@ def validator(): ], ) def test_backup_lifecycle(instance, expected, rule, validator): - errs = list( - rule.backupbackupplanlifecycle(validator, "LambdaRuntime", instance, {}) - ) + errs = list(rule.validate(validator, "LambdaRuntime", instance, {})) assert errs == expected, f"Expected {expected} got {errs}"