-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch FN support inside Rules (#3712)
- Loading branch information
Showing
6 changed files
with
343 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from collections import deque | ||
|
||
import pytest | ||
|
||
from cfnlint.jsonschema import ValidationError | ||
from cfnlint.rules.conditions.And import And | ||
|
||
|
||
@pytest.fixture | ||
def rule(): | ||
rule = And() | ||
yield rule | ||
|
||
|
||
@pytest.fixture | ||
def template(): | ||
return { | ||
"Parameters": { | ||
"Environment": { | ||
"Type": "String", | ||
} | ||
}, | ||
"Conditions": { | ||
"IsUsEast1": {"Fn::Equals": [{"Ref": "AWS::Region"}, "us-east-1"]}, | ||
"IsProduction": {"Fn::Equals": [{"Ref": "Environment"}, "Production"]}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,instance,path,errors", | ||
[ | ||
( | ||
"Valid and with other conditions", | ||
{"Fn::And": [{"Condition": "IsUsEast1"}, {"Condition": "IsProduction"}]}, | ||
{}, | ||
[], | ||
), | ||
( | ||
"Valid and with boolean types", | ||
{"Fn::And": [True, False]}, | ||
{}, | ||
[], | ||
), | ||
( | ||
"Invalid Type", | ||
{"Fn::And": {}}, | ||
{}, | ||
[ | ||
ValidationError( | ||
"{} is not of type 'array'", | ||
validator="fn_and", | ||
schema_path=deque(["type"]), | ||
path=deque(["Fn::And"]), | ||
), | ||
], | ||
), | ||
( | ||
"Integer type", | ||
{"Fn::And": ["a", True]}, | ||
{}, | ||
[ | ||
ValidationError( | ||
"'a' is not of type 'boolean'", | ||
validator="fn_and", | ||
schema_path=deque(["fn_items", "type"]), | ||
path=deque(["Fn::And", 0]), | ||
) | ||
], | ||
), | ||
( | ||
"Invalid functions in Conditions", | ||
{"Fn::And": [True, {"Fn::Contains": []}]}, | ||
{"path": deque(["Conditions", "Condition1"])}, | ||
[ | ||
ValidationError( | ||
"{'Fn::Contains': []} is not of type 'boolean'", | ||
validator="fn_and", | ||
schema_path=deque(["fn_items", "type"]), | ||
path=deque(["Fn::And", 1]), | ||
) | ||
], | ||
), | ||
( | ||
"Valid functions in Rules", | ||
{"Fn::And": [True, {"Fn::Contains": []}]}, | ||
{"path": deque(["Rules", "Rule1"])}, | ||
[], | ||
), | ||
], | ||
indirect=["path"], | ||
) | ||
def test_condition(name, instance, errors, rule, validator): | ||
errs = list(rule.validate(validator, {}, instance, {})) | ||
|
||
assert errs == errors, name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from collections import deque | ||
|
||
import pytest | ||
|
||
from cfnlint.jsonschema import ValidationError | ||
from cfnlint.rules.conditions.Not import Not | ||
|
||
|
||
@pytest.fixture | ||
def rule(): | ||
rule = Not() | ||
yield rule | ||
|
||
|
||
@pytest.fixture | ||
def template(): | ||
return { | ||
"Parameters": { | ||
"Environment": { | ||
"Type": "String", | ||
} | ||
}, | ||
"Conditions": { | ||
"IsUsEast1": {"Fn::Equals": [{"Ref": "AWS::Region"}, "us-east-1"]}, | ||
"IsProduction": {"Fn::Equals": [{"Ref": "Environment"}, "Production"]}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,instance,path,errors", | ||
[ | ||
( | ||
"Valid or with other conditions", | ||
{"Fn::Not": [{"Condition": "IsUsEast1"}]}, | ||
{}, | ||
[], | ||
), | ||
( | ||
"Valid or with boolean types", | ||
{"Fn::Not": [True]}, | ||
{}, | ||
[], | ||
), | ||
( | ||
"Invalid Type", | ||
{"Fn::Not": {}}, | ||
{}, | ||
[ | ||
ValidationError( | ||
"{} is not of type 'array'", | ||
validator="fn_not", | ||
schema_path=deque(["type"]), | ||
path=deque(["Fn::Not"]), | ||
), | ||
], | ||
), | ||
( | ||
"Integer type", | ||
{"Fn::Not": ["a"]}, | ||
{}, | ||
[ | ||
ValidationError( | ||
"'a' is not of type 'boolean'", | ||
validator="fn_not", | ||
schema_path=deque(["fn_items", "type"]), | ||
path=deque(["Fn::Not", 0]), | ||
) | ||
], | ||
), | ||
( | ||
"Invalid functions in Conditions", | ||
{"Fn::Not": [{"Fn::Contains": []}]}, | ||
{"path": deque(["Conditions", "Condition1"])}, | ||
[ | ||
ValidationError( | ||
"{'Fn::Contains': []} is not of type 'boolean'", | ||
validator="fn_not", | ||
schema_path=deque(["fn_items", "type"]), | ||
path=deque(["Fn::Not", 0]), | ||
) | ||
], | ||
), | ||
( | ||
"Valid functions in Rules", | ||
{"Fn::Not": [{"Fn::Contains": []}]}, | ||
{"path": deque(["Rules", "Rule1"])}, | ||
[], | ||
), | ||
], | ||
indirect=["path"], | ||
) | ||
def test_condition(name, instance, errors, rule, validator): | ||
errs = list(rule.validate(validator, {}, instance, {})) | ||
|
||
assert errs == errors, name |
Oops, something went wrong.