Skip to content

Commit

Permalink
Return Symbol instead of None on Fn::Equals logic (#3663)
Browse files Browse the repository at this point in the history
* Return Symbol instead of None on Fn::Equals logic
* Try and except on building all assertions
  • Loading branch information
kddejong authored Sep 5, 2024
1 parent df3f505 commit 469fe11
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/cfnlint/conditions/_equals.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction:
return BooleanTrue()
return BooleanFalse()

return params.get(self.hash)
if self.hash in params:
return params.get(self.hash)

return Symbol(self.hash)

def test(self, scenarios: Mapping[str, str]) -> bool:
"""Do an equals based on the provided scenario"""
Expand Down
6 changes: 5 additions & 1 deletion src/cfnlint/conditions/_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | Non
for assertion in self._assertions:
assertions.append(assertion.build_cnf(params))

return And(*assertions)
try:
return And(*assertions)
except Exception as e:
LOGGER.debug(f"Error building conditions: {e}")
return BooleanTrue()

@property
def equals(self) -> list[Equal]:
Expand Down
64 changes: 64 additions & 0 deletions test/unit/module/conditions/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,70 @@ def test_fn_equals_assertions_one(self):
)
)

def test_fn_equals_assertions_ref_no_data(self):
template = decode_str(
"""
Parameters:
AccountId:
Type: String
Rules:
Rule1:
Assertions:
- Assert: !Equals [!Ref AccountId, !Ref AWS::AccountId]
"""
)[0]

cfn = Template("", template)
self.assertEqual(len(cfn.conditions._conditions), 0)
self.assertEqual(len(cfn.conditions._rules), 1)

self.assertListEqual(
[equal.hash for equal in cfn.conditions._rules[0].equals],
[
"f36e61f3d5bf6cdc6ea2e7f01487af728094a439",
],
)

self.assertTrue(
cfn.conditions.satisfiable(
{},
{},
)
)

def test_fn_equals_assertions_ref_never_satisfiable(self):
template = decode_str(
"""
Parameters:
AccountId:
Type: String
Rules:
Rule1:
Assertions:
- Assert: !Equals [!Ref AccountId, !Ref AWS::AccountId]
- Assert: !Not [!Equals [!Ref AccountId, !Ref AWS::AccountId]]
"""
)[0]

cfn = Template("", template)
self.assertEqual(len(cfn.conditions._conditions), 0)
self.assertEqual(len(cfn.conditions._rules), 1)

self.assertListEqual(
[equal.hash for equal in cfn.conditions._rules[0].equals],
[
"f36e61f3d5bf6cdc6ea2e7f01487af728094a439",
"f36e61f3d5bf6cdc6ea2e7f01487af728094a439",
],
)

self.assertFalse(
cfn.conditions.satisfiable(
{},
{},
)
)


class TestAssertion(TestCase):
def test_assertion_errors(self):
Expand Down

0 comments on commit 469fe11

Please sign in to comment.