Skip to content

Commit

Permalink
Added stateful test to verify default values (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammokhov committed May 9, 2024
1 parent 4fa8617 commit 4be1074
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/BREAKING_CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@
||`MI010`|`"cannot remove maximum from properties"`|
||`MI011`|`"only NEWLY ADDED properties can have additional maximum constraint"`|
||`MI012`|`"new maximum value cannot be less than the old value"`|


***
#### Non CFN Enforced Rules
| Rule Name | Check Id | Message |
|----------|-------------|------------------------------------------------------------|
|`ensure_default_values_have_not_changed`|`TFDF001`| `"cannot remove default values from properties"` |
||`TFDF002`|`"cannot change default values"`|
1 change: 1 addition & 0 deletions src/rpdk/guard_rail/core/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class DIFFKEYS:
"additionalProperties",
"uniqueItems",
"dependencies",
"default",
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
rule ensure_default_values_have_not_changed when default exists
{
default.removed !exists
<<
{
"result": "NON_COMPLIANT",
"check_id": "TFDF001",
"message": "cannot remove default values from properties"
}
>>

default.changed !exists
<<
{
"result": "NON_COMPLIANT",
"check_id": "TFDF002",
"message": "cannot change default values"
}
>>
}
96 changes: 96 additions & 0 deletions tests/integ/runner/test_integ_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=C0302
"""
Integ test for runner.py
"""
Expand Down Expand Up @@ -914,3 +915,98 @@ def test_exec_compliance_stateful_create_only_breaking_change_with_no_properties
assert (
non_compliant_result == compliance_result.non_compliant[non_compliant_rule]
)


@pytest.mark.parametrize(
"previous_schema, current_schema, collected_rules,non_compliant_rules,warning_rules",
[
(
{
"properties": {
"LastName": {
"type": "string",
"default": "Snow",
},
"Name": {
"type": "string",
"default": "Jon",
},
},
},
{
"properties": {
"LastName": {
"type": "string",
"default": "Targaryen",
},
"Name": {
"type": "string",
"default": "Jon",
},
},
},
[],
{
"ensure_default_values_have_not_changed": {
GuardRuleResult(
check_id="TFDF002",
message="cannot change default values",
path="/default/changed",
)
},
},
[],
),
(
{
"properties": {
"LastName": {
"type": "string",
"default": "Snow",
},
"Name": {
"type": "string",
"default": "Jon",
},
},
},
{
"properties": {
"LastName": {
"type": "string",
},
"Name": {
"type": "string",
"default": "Jon",
},
},
},
[],
{
"ensure_default_values_have_not_changed": {
GuardRuleResult(
check_id="TFDF001",
message="cannot remove default values from properties",
path="/default/removed",
)
},
},
[],
),
],
)
def test_exec_compliance_stateful_default_value_breaking_change(
previous_schema, current_schema, collected_rules, non_compliant_rules, warning_rules
):
"""Test exec_compliance for stateful"""
payload: Stateful = Stateful(
previous_schema=previous_schema,
current_schema=current_schema,
rules=collected_rules,
)
compliance_result = exec_compliance(payload)[0]
for non_compliant_rule, non_compliant_result in non_compliant_rules.items():
assert non_compliant_rule in compliance_result.non_compliant
assert (
non_compliant_result == compliance_result.non_compliant[non_compliant_rule]
)

0 comments on commit 4be1074

Please sign in to comment.