-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Terraform local start-api integration tests base (#5240)
* feat: update SAM CLI with latest App Templates commit hash (#5211) * feat: updating app templates repo hash with (a34f563f067e13df3eb350d36461b99397b6cda6) * dummy change to trigger checks * revert dummy commit --------- Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> * Enable hook-name flag for sam local start-api * Format files * fix: fix failing Terraform integration test cases (#5218) * fix: fix the failing terraform integration test cases * fix: fix the resource address while accessing the module config resources * fix: fix checking the experimental log integration test cases * chore: bump version to 1.85.0 (#5226) * chore: use the SAR Application created in testing accounts (#5221) * chore: update aws_lambda_builders to 1.32.0 (#5215) Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> * feat: Added linking Gateway Method to Lambda Authorizer (#5228) * Added linking method to authorizer * Fixed docstring spelling mistake --------- Co-authored-by: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> * feat: Return early during linking if no destination resources are found (#5220) * Returns during linking if no destination resources are found * Updated comment to correctly reflect state * Cleaned extra word --------- Co-authored-by: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> * chore: Strengthen wording on "no Auth" during deploy (#5231) Co-authored-by: Jacob Fuss <jfuss@users.noreply.github.com> Co-authored-by: Sriram Madapusi Vasudevan <3770774+sriram-mv@users.noreply.github.com> * feat: Link Lambda Authorizer to Rest API (#5219) * Link RestApiId property for Lambda Authorizers * Updated docstring * Format files --------- Co-authored-by: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> * Terraform start-api integration tests * Add test files * Uncomment skip --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> Co-authored-by: Lucas <12496191+lucashuy@users.noreply.github.com> Co-authored-by: Jacob Fuss <32497805+jfuss@users.noreply.github.com> Co-authored-by: Jacob Fuss <jfuss@users.noreply.github.com> Co-authored-by: Sriram Madapusi Vasudevan <3770774+sriram-mv@users.noreply.github.com>
- Loading branch information
1 parent
7be9427
commit 93682cb
Showing
4 changed files
with
178 additions
and
2 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
56 changes: 56 additions & 0 deletions
56
tests/integration/local/start_api/test_start_api_with_terraform_application.py
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,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 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" | ||
} |