Skip to content

Commit

Permalink
Validate CfnLintKeyword functions have validate function (#3266)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed May 26, 2024
1 parent 6af0e42 commit ce62ac4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/cfnlint/rules/resources/backup/BackupPlanLifecycleRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
11 changes: 3 additions & 8 deletions src/cfnlint/rules/resources/elasticache/CacheClusterFailover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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):
Expand Down
38 changes: 38 additions & 0 deletions test/integration/jsonschema/test_cfn_lint_rules.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

0 comments on commit ce62ac4

Please sign in to comment.