New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate default template.json #2855
Changes from all commits
a36971f
2800184
710ff2f
ab7b968
4ceda1b
d3d8e10
f8bb608
324cf20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"AWSTemplateFormatVersion": "2010-09-09", | ||
"Transform": "AWS::Serverless-2016-10-31", | ||
|
||
"Resources": { | ||
"HelloWorldFunction": { | ||
"Type": "AWS::Serverless::Function", | ||
"Properties": { | ||
"CodeUri": "hello-world/", | ||
"Handler": "app.lambdaHandler", | ||
"Runtime": "nodejs14.x" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
|
||
Resources: | ||
HelloWorldFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: HelloWorldFunction | ||
Handler: app.lambdaHandler | ||
Runtime: nodejs14.x |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"AWSTemplateFormatVersion": "2010-09-09", | ||
"Transform": "AWS::Serverless-2016-10-31", | ||
|
||
"Resources": { | ||
"HelloWorldFunction": { | ||
"Type": "AWS::Serverless::Function", | ||
"Properties": { | ||
"CodeUri": "hello-world/", | ||
"Handler": "app.lambdaHandler", | ||
"Runtime": "nodejs14.x" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
|
||
Resources: | ||
HelloWorldFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: HelloWorldFunction | ||
Handler: app.lambdaHandler | ||
Runtime: nodejs14.x |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
|
||
Resources: | ||
HelloWorldFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: HelloWorldFunction | ||
Handler: app.lambdaHandler | ||
Runtime: nodejs14.x |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"AWSTemplateFormatVersion": "2010-09-09", | ||
"Transform": "AWS::Serverless-2016-10-31", | ||
|
||
"Resources": { | ||
"HelloWorldFunction": { | ||
"Type": "AWS::Serverless::Function", | ||
"Properties": { | ||
"CodeUri": "hello-world/", | ||
"Handler": "app.lambdaHandler", | ||
"Runtime": "nodejs14.x" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
Integration tests for sam validate | ||
""" | ||
|
||
import os | ||
import re | ||
from enum import Enum, auto | ||
from pathlib import Path | ||
from typing import List, Optional | ||
from unittest import TestCase | ||
from unittest.case import skipIf | ||
|
||
from parameterized import parameterized | ||
from tests.testing_utils import RUN_BY_CANARY, RUNNING_ON_CI, RUNNING_TEST_FOR_MASTER_ON_CI, run_command | ||
|
||
# Validate tests require credentials and CI/CD will only add credentials to the env if the PR is from the same repo. | ||
# This is to restrict package tests to run outside of CI/CD, when the branch is not master or tests are not run by Canary | ||
SKIP_VALIDATE_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY | ||
|
||
|
||
class TemplateFileTypes(Enum): | ||
JSON = auto() | ||
YAML = auto() | ||
|
||
|
||
@skipIf(SKIP_VALIDATE_TESTS, "Skip validate tests in CI/CD only") | ||
class TestValidate(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.patterns = { | ||
TemplateFileTypes.JSON: re.compile(r"^/.+/template[.]json is a valid SAM Template$"), | ||
TemplateFileTypes.YAML: re.compile(r"^/.+/template[.]yaml is a valid SAM Template$"), | ||
} | ||
|
||
@staticmethod | ||
def base_command() -> str: | ||
return "samdev" if os.getenv("SAM_CLI_DEV") else "sam" | ||
|
||
def command_list( | ||
self, | ||
template_file: Optional[Path] = None, | ||
profile: Optional[str] = None, | ||
region: Optional[str] = None, | ||
config_file: Optional[Path] = None, | ||
) -> List[str]: | ||
command_list = [self.base_command(), "validate"] | ||
if template_file: | ||
command_list += ["--template-file", str(template_file)] | ||
if profile: | ||
command_list += ["--profile", profile] | ||
if region: | ||
command_list += ["--region", region] | ||
if config_file: | ||
command_list += ["--config_file", str(config_file)] | ||
return command_list | ||
|
||
@parameterized.expand( | ||
[ | ||
("default_yaml", TemplateFileTypes.YAML), # project with template.yaml | ||
("default_json", TemplateFileTypes.JSON), # project with template.json | ||
("multiple_files", TemplateFileTypes.YAML), # project with both template.yaml and template.json | ||
( | ||
"with_build", | ||
TemplateFileTypes.JSON, | ||
), # project with template.json and standard build directory .aws-sam/build/template.yaml | ||
] | ||
) | ||
def test_default_template_file_choice(self, relative_folder: str, expected_file: TemplateFileTypes): | ||
test_data_path = Path(__file__).resolve().parents[2] / "integration" / "testdata" / "validate" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, it's nice to have operator overloading 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the "/" similar to using pathlib.join ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I tested, it is |
||
process_dir = test_data_path / relative_folder | ||
command_result = run_command(self.command_list(), cwd=str(process_dir)) | ||
pattern = self.patterns[expected_file] # type: ignore | ||
output = command_result.stdout.decode("utf-8") | ||
self.assertEqual(command_result.process.returncode, 0) | ||
self.assertRegex(output, pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something to check, if build is included, we always the convert the built template back to yaml (IIRC) and that is referenced template if one is not provided during the deploy command, can we check this behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
sam build
is executed,template.json
will be rewritten into.aws-sam/build/template.yaml
. And ifsam package/deploy
is later executed,.aws-sam/build/template.yaml
should be picked up. Nice to double check this behavior.@sriram-mv so we want the priority in
sam local
andsam package/deploy
to be:is that accurate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sriram-mv Yes, when we run build,
template.json
is converted totemplate.yaml
in.aws-sam/build
aftersam build
execution. Duringdeploy
command that.aws-sam/build/template.yaml
from the build directory is used by default. When we runvalidate
in a project with build directory,template.json
from the root directory is validated by default. I believe it works as expectedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sounds good, it (template.json) should be the last thing to look up in order to avoid any breaking changes or unwanted behavior from this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something I just realised. the parser is still a yaml_parser.
yaml_helper.py
if you look at the comment, it suggest use a json parser instead.