|
8 | 8 | from unittest import TestCase
|
9 | 9 | from jsonschema import validate
|
10 | 10 | from jsonschema.exceptions import ValidationError
|
| 11 | +from jsonschema.validators import Draft4Validator |
11 | 12 | from parameterized import parameterized
|
12 | 13 |
|
13 | 14 | from samtranslator.yaml_helper import yaml_parse
|
@@ -117,10 +118,133 @@ def test_validate_unified_schema(self, testcase):
|
117 | 118 |
|
118 | 119 | @parameterized.expand(
|
119 | 120 | [
|
| 121 | + (PROJECT_ROOT.joinpath("tests/translator/input/error_api_invalid_auth.yaml"),), |
| 122 | + (PROJECT_ROOT.joinpath("tests/translator/input/error_function_invalid_event_type.yaml"),), |
120 | 123 | (PROJECT_ROOT.joinpath("tests/translator/input/schema_validation_ec2_not_valid.yaml"),),
|
121 | 124 | ]
|
122 | 125 | )
|
123 | 126 | def test_validate_unified_schema_error(self, testcase):
|
124 | 127 | obj = yaml_parse(Path(testcase).read_bytes())
|
125 | 128 | with pytest.raises(ValidationError):
|
126 | 129 | validate(obj, schema=UNIFIED_SCHEMA)
|
| 130 | + |
| 131 | + @parameterized.expand( |
| 132 | + [ |
| 133 | + [ |
| 134 | + # Valid (SAM) |
| 135 | + { |
| 136 | + "Transform": "AWS::Serverless-2016-10-31", |
| 137 | + "Resources": { |
| 138 | + "Foo": { |
| 139 | + "Type": "AWS::Serverless::SimpleTable", |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + ], |
| 144 | + [ |
| 145 | + # Valid (CFN) |
| 146 | + { |
| 147 | + "Resources": { |
| 148 | + "Foo": { |
| 149 | + "Type": "AWS::S3::Bucket", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + ], |
| 155 | + ) |
| 156 | + def test_sanity_valid(self, template): |
| 157 | + assert UNIFIED_SCHEMA["$schema"] == "http://json-schema.org/draft-04/schema#" |
| 158 | + Draft4Validator(UNIFIED_SCHEMA).validate(template) |
| 159 | + validate(template, schema=UNIFIED_SCHEMA) |
| 160 | + |
| 161 | + @parameterized.expand( |
| 162 | + [ |
| 163 | + [ |
| 164 | + # Unknown property (SAM) |
| 165 | + { |
| 166 | + "Transform": "AWS::Serverless-2016-10-31", |
| 167 | + "Resources": { |
| 168 | + "Foo": { |
| 169 | + "Type": "AWS::Serverless::SimpleTable", |
| 170 | + "Properties": { |
| 171 | + "UnknownProperty": True, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + ], |
| 177 | + [ |
| 178 | + # Missing property (SAM) |
| 179 | + { |
| 180 | + "Transform": "AWS::Serverless-2016-10-31", |
| 181 | + "Resources": { |
| 182 | + "Foo": { |
| 183 | + "Type": "AWS::Serverless::SimpleTable", |
| 184 | + "Properties": { |
| 185 | + "PrimaryKey": { |
| 186 | + "Name": "Foo", |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + ], |
| 193 | + [ |
| 194 | + # Wrong value type (SAM) |
| 195 | + { |
| 196 | + "Transform": "AWS::Serverless-2016-10-31", |
| 197 | + "Resources": { |
| 198 | + "Foo": { |
| 199 | + "Type": "AWS::Serverless::Function", |
| 200 | + "Properties": { |
| 201 | + "InlineCode": "foo", |
| 202 | + "Handler": "bar", |
| 203 | + "Runtime": "node16.x", |
| 204 | + "Events": 1337, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + ], |
| 210 | + [ |
| 211 | + # Unknown property (CFN) |
| 212 | + { |
| 213 | + "Resources": { |
| 214 | + "Foo": { |
| 215 | + "Type": "AWS::S3::Bucket", |
| 216 | + "Properties": { |
| 217 | + "UnknownProperty": True, |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + ], |
| 223 | + [ |
| 224 | + # Missing property (CFN) |
| 225 | + { |
| 226 | + "Resources": { |
| 227 | + "Foo": { |
| 228 | + "Type": "AWS::ResourceGroups::Group", |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + ], |
| 233 | + [ |
| 234 | + # Wrong value type (CFN) |
| 235 | + { |
| 236 | + "Resources": { |
| 237 | + "Foo": { |
| 238 | + "Type": "AWS::ResourceGroups::Group", |
| 239 | + "Properties": { |
| 240 | + "Name": 1337, |
| 241 | + }, |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + ], |
| 246 | + ], |
| 247 | + ) |
| 248 | + def test_sanity_invalid(self, template): |
| 249 | + with pytest.raises(ValidationError): |
| 250 | + validate(template, schema=UNIFIED_SCHEMA) |
0 commit comments