Skip to content
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

test: Terraform local start-api integration tests base #5240

Merged
Merged
Show file tree
Hide file tree
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] May 29, 2023
34d5874
Enable hook-name flag for sam local start-api
mildaniel May 29, 2023
7edfd61
Merge branch 'develop' into hook-name-flag-start-api
mildaniel May 29, 2023
5338ec4
Format files
mildaniel May 29, 2023
6d5bb14
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into hoo…
mildaniel May 29, 2023
8ee6c61
Merge branch 'hook-name-flag-start-api' of github.com:mildaniel/aws-s…
mildaniel May 29, 2023
dc5562a
fix: fix failing Terraform integration test cases (#5218)
moelasmar May 30, 2023
888cb45
chore: bump version to 1.85.0 (#5226)
moelasmar May 30, 2023
acc0acd
chore: use the SAR Application created in testing accounts (#5221)
moelasmar May 30, 2023
f23dc23
chore: update aws_lambda_builders to 1.32.0 (#5215)
github-actions[bot] May 30, 2023
2e79b50
feat: Added linking Gateway Method to Lambda Authorizer (#5228)
lucashuy May 31, 2023
ec942e9
feat: Return early during linking if no destination resources are fou…
lucashuy May 31, 2023
81b3a71
chore: Strengthen wording on "no Auth" during deploy (#5231)
jfuss May 31, 2023
d517ece
feat: Link Lambda Authorizer to Rest API (#5219)
lucashuy May 31, 2023
c2994bf
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into hoo…
mildaniel Jun 1, 2023
81c9c41
Terraform start-api integration tests
mildaniel Jun 1, 2023
400d4e6
Add test files
mildaniel Jun 1, 2023
c2217fd
Merge remote-tracking branch 'upstream/feat-terraform-start-api' into…
mildaniel Jun 1, 2023
6bcee24
Merge remote-tracking branch 'upstream/feat-terraform-start-api' into…
mildaniel Jun 1, 2023
52a6940
Uncomment skip
mildaniel Jun 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions tests/integration/local/start_api/start_api_integ_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class StartApiIntegBaseClass(TestCase):

do_collect_cmd_init_output: bool = False

command_list = None
project_directory = None

@classmethod
def setUpClass(cls):
# This is the directory for tests/integration which will be used to file the testdata
Expand Down Expand Up @@ -84,7 +87,8 @@ def start_api_with_retry(cls, retries=3):
def start_api(cls):
command = get_sam_command()

command_list = [command, "local", "start-api", "-t", cls.template, "-p", cls.port]
command_list = cls.command_list or [command, "local", "start-api", "-t", cls.template]
command_list.extend(["-p", cls.port])

if cls.container_mode:
command_list += ["--warm-containers", cls.container_mode]
Expand All @@ -99,7 +103,11 @@ def start_api(cls):
for image in cls.invoke_image:
command_list += ["--invoke-image", image]

cls.start_api_process = Popen(command_list, stderr=PIPE, stdout=PIPE)
cls.start_api_process = (
Popen(command_list, stderr=PIPE, stdout=PIPE)
if not cls.project_directory
else Popen(command_list, stderr=PIPE, stdout=PIPE, cwd=cls.project_directory)
)
cls.start_api_process_output = wait_for_local_process(
cls.start_api_process, cls.port, collect_output=cls.do_collect_cmd_init_output
)
Expand Down
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):
mildaniel marked this conversation as resolved.
Show resolved Hide resolved
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 not shown.
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"
}