-
Notifications
You must be signed in to change notification settings - Fork 1.3k
test: Terraform local start-api integration tests base #5240
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
Merged
mildaniel
merged 20 commits into
aws:feat/terraform-start-api
from
mildaniel:tf-start-api-integ-base
Jun 2, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6f137dc
feat: update SAM CLI with latest App Templates commit hash (#5211)
github-actions[bot] 34d5874
Enable hook-name flag for sam local start-api
mildaniel 7edfd61
Merge branch 'develop' into hook-name-flag-start-api
mildaniel 5338ec4
Format files
mildaniel 6d5bb14
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into hoo…
mildaniel 8ee6c61
Merge branch 'hook-name-flag-start-api' of github.com:mildaniel/aws-s…
mildaniel dc5562a
fix: fix failing Terraform integration test cases (#5218)
moelasmar 888cb45
chore: bump version to 1.85.0 (#5226)
moelasmar acc0acd
chore: use the SAR Application created in testing accounts (#5221)
moelasmar f23dc23
chore: update aws_lambda_builders to 1.32.0 (#5215)
github-actions[bot] 2e79b50
feat: Added linking Gateway Method to Lambda Authorizer (#5228)
lucashuy ec942e9
feat: Return early during linking if no destination resources are fou…
lucashuy 81b3a71
chore: Strengthen wording on "no Auth" during deploy (#5231)
jfuss d517ece
feat: Link Lambda Authorizer to Rest API (#5219)
lucashuy c2994bf
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into hoo…
mildaniel 81c9c41
Terraform start-api integration tests
mildaniel 400d4e6
Add test files
mildaniel c2217fd
Merge remote-tracking branch 'upstream/feat-terraform-start-api' into…
mildaniel 6bcee24
Merge remote-tracking branch 'upstream/feat-terraform-start-api' into…
mildaniel 52a6940
Uncomment skip
mildaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
tests/integration/local/start_api/test_start_api_with_terraform_application.py
This file contains hidden or 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,56 @@ | ||
| import shutil | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Optional | ||
| from unittest import skipIf | ||
| from http.client import HTTPConnection | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
| from tests.integration.local.start_api.start_api_integ_base import StartApiIntegBaseClass | ||
| from tests.testing_utils import get_sam_command, CI_OVERRIDE | ||
|
|
||
|
|
||
| class TerraformStartApiIntegrationBase(StartApiIntegBaseClass): | ||
| terraform_application: Optional[str] = None | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| command = get_sam_command() | ||
| cls.template_path = "" | ||
| cls.build_before_invoke = False | ||
| cls.command_list = [command, "local", "start-api", "--hook-name", "terraform", "--beta-features"] | ||
| cls.test_data_path = Path(cls.get_integ_dir()) / "testdata" / "start_api" | ||
| cls.project_directory = cls.test_data_path / "terraform" / cls.terraform_application | ||
| super(TerraformStartApiIntegrationBase, cls).setUpClass() | ||
|
|
||
| @staticmethod | ||
| def get_integ_dir(): | ||
| return Path(__file__).resolve().parents[2] | ||
|
|
||
| def tearDown(self) -> None: | ||
| shutil.rmtree(str(Path(self.project_directory / ".aws-sam-iacs")), ignore_errors=True) # type: ignore | ||
| shutil.rmtree(str(Path(self.project_directory / ".terraform")), ignore_errors=True) # type: ignore | ||
| try: | ||
| os.remove(str(Path(self.project_directory / ".terraform.lock.hcl"))) # type: ignore | ||
| except (FileNotFoundError, PermissionError): | ||
| pass | ||
|
|
||
|
|
||
| @skipIf( | ||
| not CI_OVERRIDE, | ||
| "Skip Terraform test cases unless running in CI", | ||
| ) | ||
| @pytest.mark.flaky(reruns=3) | ||
| class TestStartApiTerraformApplication(TerraformStartApiIntegrationBase): | ||
| terraform_application = "terraform-v1-api-simple" | ||
|
|
||
| def setUp(self): | ||
| self.url = "http://127.0.0.1:{}".format(self.port) | ||
|
|
||
| def test_successful_request(self): | ||
| response = requests.get(self.url + "/hello", timeout=300) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual(response.json(), {"message": "hello world"}) | ||
Binary file added
BIN
+1.05 KB
...s/integration/testdata/start_api/terraform/terraform-v1-api-simple/HelloWorldFunction.zip
Binary file not shown.
112 changes: 112 additions & 0 deletions
112
tests/integration/testdata/start_api/terraform/terraform-v1-api-simple/main.tf
This file contains hidden or 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,112 @@ | ||
| provider "aws" { | ||
| } | ||
|
|
||
| resource "aws_iam_role" "iam_for_lambda" { | ||
| name = "iam_for_lambda" | ||
|
|
||
| assume_role_policy = <<EOF | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Action": "sts:AssumeRole", | ||
| "Principal": { | ||
| "Service": "lambda.amazonaws.com" | ||
| }, | ||
| "Effect": "Allow", | ||
| "Sid": "" | ||
| } | ||
| ] | ||
| } | ||
| EOF | ||
| } | ||
|
|
||
| resource "aws_s3_bucket" "lambda_code_bucket" { | ||
| bucket = "lambda_code_bucket" | ||
| } | ||
|
|
||
| resource "aws_s3_object" "s3_lambda_code" { | ||
| bucket = "lambda_code_bucket" | ||
| key = "s3_lambda_code_key" | ||
| source = "HelloWorldFunction.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_layer_version" "MyAwesomeLayer" { | ||
| filename = "HelloWorldFunction.zip" | ||
| layer_name = "MyAwesomeLayer" | ||
| compatible_runtimes = ["python3.8"] | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "HelloWorldFunction" { | ||
| s3_bucket = "lambda_code_bucket" | ||
| s3_key = "s3_lambda_code_key" | ||
| handler = "app.lambda_handler" | ||
| runtime = "python3.8" | ||
| function_name = "HelloWorldFunction" | ||
| timeout = 500 | ||
| role = aws_iam_role.iam_for_lambda.arn | ||
| layers = [aws_lambda_layer_version.MyAwesomeLayer.arn] | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "HelloWorldFunction2" { | ||
| s3_bucket = "lambda_code_bucket" | ||
| s3_key = "s3_lambda_code_key" | ||
| handler = "app.lambda_handler" | ||
| runtime = "python3.8" | ||
| function_name = "HelloWorldFunction2" | ||
| timeout = 500 | ||
| role = aws_iam_role.iam_for_lambda.arn | ||
| layers = ["arn:aws:lambda:us-east-1:178733185316:layer:01383708b0:1"] | ||
| } | ||
|
|
||
| resource "aws_api_gateway_rest_api" "MyDemoAPI" { | ||
| name = "MyDemoAPI" | ||
| binary_media_types = [ "utf-8" ] | ||
| } | ||
|
|
||
| resource "aws_api_gateway_resource" "MyDemoResource" { | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| parent_id = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id | ||
| path_part = "hello" | ||
| } | ||
|
|
||
| resource "aws_api_gateway_method" "GetMethod" { | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| resource_id = aws_api_gateway_resource.MyDemoResource.id | ||
| http_method = "GET" | ||
| authorization = "NONE" | ||
| } | ||
|
|
||
| resource "aws_api_gateway_method" "PostMethod" { | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| resource_id = aws_api_gateway_resource.MyDemoResource.id | ||
| http_method = "POST" | ||
| authorization = "NONE" | ||
| } | ||
|
|
||
| resource "aws_api_gateway_stage" "MyDemoStage" { | ||
| stage_name = "prod" | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| deployment_id = aws_api_gateway_deployment.MyDemoDeployment.id | ||
| } | ||
|
|
||
| resource "aws_api_gateway_deployment" "MyDemoDeployment" { | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| stage_name = "prod" | ||
| } | ||
|
|
||
| resource "aws_api_gateway_integration" "MyDemoIntegration" { | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| resource_id = aws_api_gateway_resource.MyDemoResource.id | ||
| http_method = aws_api_gateway_method.GetMethod.http_method | ||
| type = "AWS_PROXY" | ||
| content_handling = "CONVERT_TO_TEXT" | ||
| uri = aws_lambda_function.HelloWorldFunction.invoke_arn | ||
| } | ||
|
|
||
| resource "aws_api_gateway_integration" "MyDemoIntegrationMock" { | ||
| rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id | ||
| resource_id = aws_api_gateway_resource.MyDemoResource.id | ||
| http_method = aws_api_gateway_method.PostMethod.http_method | ||
| type = "MOCK" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.