forked from aws/aws-sam-cli
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate default template.json (aws#2855)
Issue: aws#2355 Added integration tests for `validate` command Co-authored-by: Slava Senchenko <sencslav@amazon.com> Co-authored-by: _sam <3804518+aahung@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
165 additions
and
4 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
15 changes: 15 additions & 0 deletions
15
tests/integration/testdata/validate/default_json/template.json
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,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" | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/integration/testdata/validate/default_yaml/template.yaml
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,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 |
15 changes: 15 additions & 0 deletions
15
tests/integration/testdata/validate/multiple_files/template.json
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,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" | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/integration/testdata/validate/multiple_files/template.yaml
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,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 |
10 changes: 10 additions & 0 deletions
10
tests/integration/testdata/validate/with_build/.aws-sam/build/template.yaml
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,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 |
15 changes: 15 additions & 0 deletions
15
tests/integration/testdata/validate/with_build/template.json
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,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" | ||
} | ||
} | ||
} | ||
} |
Empty file.
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,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" | ||
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) |
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