Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
feat: add integration tests for lambda_http
  • Loading branch information
nmoutschen committed Dec 17, 2021
commit 79fce9dc927cd12b661dd5ff781aa5e721e41ac1
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
INTEG_STACK_NAME ?= rust-lambda-integration-tests
INTEG_FUNCTIONS_BUILD := runtime-fn runtime-trait
INTEG_FUNCTIONS_BUILD := runtime-fn runtime-trait http-fn
INTEG_FUNCTIONS_INVOKE := RuntimeFn RuntimeFnAl2 RuntimeTrait RuntimeTraitAl2 Python PythonAl2
INTEG_API_INVOKE := RestApiUrl HttpApiUrl
INTEG_EXTENSIONS := extension-fn extension-trait
# Using musl to run extensions on both AL1 and AL2
INTEG_ARCH := x86_64-unknown-linux-musl
Expand All @@ -21,6 +22,7 @@ integration-tests:
--no-fail-on-empty-changeset
# Invoke functions
${MAKE} ${MAKEOPTS} $(foreach function,${INTEG_FUNCTIONS_INVOKE}, invoke-integration-function-${function})
${MAKE} ${MAKEOPTS} $(foreach api,${INTEG_API_INVOKE}, invoke-integration-api-${api})

build-integration-function-%:
mkdir -p ./build/$*
Expand All @@ -33,4 +35,14 @@ build-integration-extension-%:
invoke-integration-function-%:
aws lambda invoke --function-name $$(aws cloudformation describe-stacks --stack-name $(INTEG_STACK_NAME) \
--query 'Stacks[0].Outputs[?OutputKey==`$*`].OutputValue' \
--output text) --payload '{"command": "hello"}' --cli-binary-format raw-in-base64-out /dev/stdout
--output text) --payload '{"command": "hello"}' --cli-binary-format raw-in-base64-out /dev/stdout

invoke-integration-api-%:
$(eval API_URL := $(shell aws cloudformation describe-stacks --stack-name $(INTEG_STACK_NAME) \
--query 'Stacks[0].Outputs[?OutputKey==`$*`].OutputValue' \
--output text))
curl $(API_URL)/get
curl $(API_URL)/al2/get
curl -X POST -d '{"command": "hello"}' $(API_URL)/post
curl -X POST -d '{"command": "hello"}' $(API_URL)/al2/post

7 changes: 1 addition & 6 deletions lambda-integration-tests/src/bin/extension-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ use std::{
pin::Pin,
};

#[derive(Default)]
struct MyExtension {
invoke_count: usize,
}

impl Default for MyExtension {
fn default() -> Self {
Self { invoke_count: 0 }
}
}

impl Extension for MyExtension {
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>;

Expand Down
19 changes: 19 additions & 0 deletions lambda-integration-tests/src/bin/http-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use lambda_http::{
lambda_runtime::{self, Context, Error},
IntoResponse, Request, Response,
};
use log::{info, LevelFilter};
use simple_logger::SimpleLogger;

async fn handler(event: Request, _context: Context) -> Result<impl IntoResponse, Error> {
info!("[http-fn] Received event {} {}", event.method(), event.uri().path());

Ok(Response::builder().status(200).body("Hello, world!").unwrap())
}

#[tokio::main]
async fn main() -> Result<(), Error> {
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();

lambda_runtime::run(lambda_http::handler(handler)).await
}
7 changes: 1 addition & 6 deletions lambda-integration-tests/src/bin/runtime-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ struct Response {
message: String,
}

#[derive(Default)]
struct MyHandler {
invoke_count: usize,
}

impl Default for MyHandler {
fn default() -> Self {
Self { invoke_count: 0 }
}
}

impl Handler<Request, Response> for MyHandler {
type Error = Error;
type Fut = Pin<Box<dyn Future<Output = Result<Response, Error>>>>;
Expand Down
69 changes: 68 additions & 1 deletion lambda-integration-tests/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,68 @@ Resources:
- !Ref ExtensionFn
- !Ref ExtensionTrait

# Rust function using lambda_http::runtime running on AL2
HttpFnAl2:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../build/http-fn/
Runtime: provided.al2
Events:
ApiGet:
Type: Api
Properties:
Method: GET
Path: /al2/get
ApiPost:
Type: Api
Properties:
Method: POST
Path: /al2/post
ApiV2Get:
Type: HttpApi
Properties:
Method: GET
Path: /al2/get
ApiV2Post:
Type: HttpApi
Properties:
Method: POST
Path: /al2/post
Layers:
- !Ref ExtensionFn
- !Ref ExtensionTrait

# Rust function using lambda_http::runtime running on AL1
HttpFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../build/http-fn/
Runtime: provided
Events:
ApiGet:
Type: Api
Properties:
Method: GET
Path: /get
ApiPost:
Type: Api
Properties:
Method: POST
Path: /post
ApiV2Get:
Type: HttpApi
Properties:
Method: GET
Path: /get
ApiV2Post:
Type: HttpApi
Properties:
Method: POST
Path: /post
Layers:
- !Ref ExtensionFn
- !Ref ExtensionTrait

# Python function running on AL2
PythonAl2:
Type: AWS::Serverless::Function
Expand Down Expand Up @@ -92,4 +154,9 @@ Outputs:
PythonAl2:
Value: !GetAtt PythonAl2.Arn
Python:
Value: !GetAtt Python.Arn
Value: !GetAtt Python.Arn

RestApiUrl:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
HttpApiUrl:
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"