Skip to content

Commit

Permalink
Improve testing on read only rule (#3275)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Jun 6, 2024
1 parent 099df82 commit bcf5813
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/cfnlint/rules/resources/properties/ReadOnly.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from cfnlint.jsonschema import ValidationError, Validator
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
from cfnlint.schema import ResourceNotFoundError
from cfnlint.schema.resolver import RefResolutionError


class ReadOnly(CfnLintKeyword):
Expand Down Expand Up @@ -43,5 +43,5 @@ def validate(self, validator: Validator, _: Any, instance: Any, schema: Any):
f"({validator.context.path.cfn_path[-1]!r})"
)

except ResourceNotFoundError:
except RefResolutionError:
pass
21 changes: 19 additions & 2 deletions test/unit/rules/resources/properties/test_read_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"""

from collections import deque
from unittest import mock

import pytest

from cfnlint.context import Path
from cfnlint.jsonschema import CfnTemplateValidator, ValidationError
from cfnlint.rules.resources.properties.ReadOnly import ReadOnly
from cfnlint.schema.resolver import RefResolutionError


@pytest.fixture(scope="module")
Expand All @@ -30,36 +32,47 @@ def validator():


@pytest.mark.parametrize(
"name,path,expected",
"name,path,side_effect,expected",
[
(
"Not using read only property",
deque(["Resources", "Test", "Properties", "Name"]),
None,
[],
),
(
"Not in outputs",
deque(["Outputs"]),
None,
[],
),
(
"Not in resource properties",
deque(["Resources"]),
None,
[],
),
(
"Not in resource properties",
deque(["Resources", "*", "Metadata"]),
None,
[],
),
(
"Setting a read only property",
deque(["Resources", "Test", "Properties", "Id"]),
None,
[ValidationError("Read only properties are not allowed ('Id')")],
),
(
"No readonlyProperties in schema",
deque(["Resources", "Test", "Properties", "Id"]),
RefResolutionError("not found"),
[],
),
],
)
def test_validate(name, path, expected, rule, validator):
def test_validate(name, path, side_effect, expected, rule, validator):
validator = validator.evolve(
context=validator.context.evolve(
path=Path(
Expand All @@ -68,6 +81,10 @@ def test_validate(name, path, expected, rule, validator):
),
)

if side_effect:
validator.resolver = mock.MagicMock()
validator.resolver.resolve_from_url.side_effect = side_effect

errs = list(rule.validate(validator, "", "", {}))

assert errs == expected, f"{name} got errors {errs!r}"

0 comments on commit bcf5813

Please sign in to comment.