Skip to content

Commit

Permalink
test: Terraform local start-api integration tests base (aws#5240)
Browse files Browse the repository at this point in the history
* feat: update SAM CLI with latest App Templates commit hash (aws#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 (aws#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 (aws#5226)

* chore: use the SAR Application created in testing accounts (aws#5221)

* chore: update aws_lambda_builders to 1.32.0 (aws#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 (aws#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 (aws#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 (aws#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 (aws#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
8 people committed Jun 8, 2023
1 parent f779919 commit 4a8d798
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
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):
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"
}

0 comments on commit 4a8d798

Please sign in to comment.