Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
657 changes: 193 additions & 464 deletions tests/aws/cdk_templates/Bookstore/BookstoreStack.json

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions tests/aws/scenario/bookstore/functions/getBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
"use strict";

const AWS = require("aws-sdk");
let dynamoDb;

var config = {};
if (process.env.AWS_ENDPOINT_URL) {
dynamoDb = new AWS.DynamoDB.DocumentClient({
endpoint: process.env.AWS_ENDPOINT_URL,
region: 'us-east-1', // Change the region as per your setup
}
);
} else {
dynamoDb = new AWS.DynamoDB.DocumentClient();
config.endpoint = process.env.AWS_ENDPOINT_URL;
}

let dynamoDb = new AWS.DynamoDB.DocumentClient(config);

// GetBook - Get book informaton for a given book id
exports.handler = (event, context, callback) => {

Expand Down
14 changes: 6 additions & 8 deletions tests/aws/scenario/bookstore/functions/listBooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
"use strict";

const AWS = require("aws-sdk");
let dynamoDb;

var config = {};
if (process.env.AWS_ENDPOINT_URL) {
dynamoDb = new AWS.DynamoDB.DocumentClient({
endpoint: process.env.AWS_ENDPOINT_URL,
region: 'us-east-1', // Change the region as per your setup
}
);
} else {
dynamoDb = new AWS.DynamoDB.DocumentClient();
config.endpoint = process.env.AWS_ENDPOINT_URL;
}

let dynamoDb = new AWS.DynamoDB.DocumentClient(config);

// ListBooks - List all books or list all books in a particular category
exports.handler = (event, context, callback) => {

Expand Down
30 changes: 9 additions & 21 deletions tests/aws/scenario/bookstore/functions/loadBooksHelper.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
// source: https://github.com/aws-samples/aws-bookstore-demo-app/blob/master/functions/setup/uploadBooks.js

"use strict";

const https = require("https");
const url = require("url");
const AWS = require("aws-sdk");

var AWS = require("aws-sdk");
let documentClient;
let s3Client;
var config = {
's3ForcePathStyle': true,
};
if (process.env.AWS_ENDPOINT_URL) {
const localStackS3Config = {
endpoint: process.env.AWS_ENDPOINT_URL,
s3ForcePathStyle: true,
accessKeyId: 'test',
secretAccessKey: 'test',
region: 'us-east-1',
};
s3Client = new AWS.S3(localStackS3Config);

documentClient = new AWS.DynamoDB.DocumentClient({
endpoint: process.env.AWS_ENDPOINT_URL,
region: 'us-east-1', // Change the region as per your setup
}
);
} else {
// Use the default AWS configuration
s3Client = new AWS.S3();
documentClient = new AWS.DynamoDB.DocumentClient();
config.endpoint = process.env.AWS_ENDPOINT_URL;
}

let documentClient = new AWS.DynamoDB.DocumentClient(config);
let s3Client = new AWS.S3(config);

// UploadBooks - Upload sample set of books to DynamoDB
exports.handler = function(event, context, callback) {
getBooksData().then(function(data) {
Expand Down
2 changes: 1 addition & 1 deletion tests/aws/scenario/bookstore/functions/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests
from requests_aws4auth import AWS4Auth

region = os.environ["REGION"]
region = os.environ["AWS_REGION"]
service = "es"
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests
from requests_aws4auth import AWS4Auth

region = os.environ["REGION"]
region = os.environ["AWS_REGION"]
service = "es"
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(
Expand Down
43 changes: 29 additions & 14 deletions tests/aws/scenario/bookstore/test_bookstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import aws_cdk.aws_opensearchservice as opensearch
import pytest
from aws_cdk.aws_lambda_event_sources import DynamoEventSource
from botocore.exceptions import ClientError
from constructs import Construct

from localstack.testing.pytest import markers
from localstack.testing.scenario.cdk_lambda_helper import load_python_lambda_to_s3
from localstack.testing.scenario.provisioning import InfraProvisioner, cleanup_s3_bucket
from localstack.testing.snapshots.transformer import GenericTransformer, KeyValueBasedTransformer
from localstack.utils.aws.resources import create_s3_bucket
from localstack.utils.files import load_file
from localstack.utils.strings import to_bytes, to_str
from localstack.utils.sync import retry
Expand Down Expand Up @@ -115,7 +117,11 @@ def test_setup(self, aws_client, infrastructure, snapshot, cleanups):

# pre-fill dynamodb
# json-data is from https://aws-bookstore-demo.s3.amazonaws.com/data/books.json
aws_client.s3.create_bucket(Bucket=S3_BUCKET_BOOKS_INIT)
try:
create_s3_bucket(bucket_name=S3_BUCKET_BOOKS_INIT, s3_client=aws_client.s3)
except ClientError as exc:
if exc.response["Error"]["Code"] != "BucketAlreadyOwnedByYou":
raise exc
cleanups.append(
lambda: cleanup_s3_bucket(
aws_client.s3, bucket_name=S3_BUCKET_BOOKS_INIT, delete_bucket=True
Expand Down Expand Up @@ -410,6 +416,16 @@ def __init__(
projection_type=dynamodb.ProjectionType.ALL,
)

self.lambda_role = iam.Role(
self, "LambdaRole", assumed_by=iam.ServicePrincipal("lambda.amazonaws.com")
)
self.lambda_role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonS3FullAccess")
)
self.lambda_role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonDynamoDBFullAccess")
)

# lambda for pre-filling the dynamodb
self.load_books_helper_fn = awslambda.Function(
stack,
Expand All @@ -422,24 +438,20 @@ def __init__(
"S3_BUCKET": S3_BUCKET_BOOKS_INIT,
"FILE_NAME": S3_KEY_BOOKS_INIT,
},
role=self.lambda_role,
)
self.load_books_helper_fn.role.attach_inline_policy(
iam.Policy(
stack,
"BooksS3Policy",
statements=[
iam.PolicyStatement(resources=["arn:aws:s3:::*/*"], actions=["s3:GetObject"])
],
)
)

# lambdas to get and list books
self.get_book_fn = awslambda.Function(
stack,
"GetBookLambda",
handler="index.handler",
code=awslambda.InlineCode(code=load_file(self.GET_BOOK_PATH)),
runtime=awslambda.Runtime.NODEJS_16_X,
environment={"TABLE_NAME": self.books_table.table_name},
environment={
"TABLE_NAME": self.books_table.table_name,
},
role=self.lambda_role,
)

self.list_books_fn = awslambda.Function(
Expand All @@ -448,7 +460,10 @@ def __init__(
handler="index.handler",
code=awslambda.InlineCode(code=load_file(self.LIST_BOOKS_PATH)),
runtime=awslambda.Runtime.NODEJS_16_X,
environment={"TABLE_NAME": self.books_table.table_name},
environment={
"TABLE_NAME": self.books_table.table_name,
},
role=self.lambda_role,
)

# lambda to search for book
Expand All @@ -465,8 +480,8 @@ def __init__(
runtime=awslambda.Runtime.PYTHON_3_10,
environment={
"ESENDPOINT": self.opensearch_domain.domain_endpoint,
"REGION": stack.region,
},
role=self.lambda_role,
)

# lambda to update search cluster
Expand All @@ -478,8 +493,8 @@ def __init__(
runtime=awslambda.Runtime.PYTHON_3_10,
environment={
"ESENDPOINT": self.opensearch_domain.domain_endpoint,
"REGION": stack.region,
},
role=self.lambda_role,
)

event_source = DynamoEventSource(
Expand Down
6 changes: 3 additions & 3 deletions tests/aws/scenario/bookstore/test_bookstore.validation.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_lambda_dynamodb": {
"last_validated_date": "2023-08-30T11:23:16+00:00"
"last_validated_date": "2024-01-31T13:47:03+00:00"
},
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_opensearch_crud": {
"last_validated_date": "2023-09-07T10:02:59+00:00"
},
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_search_books": {
"last_validated_date": "2023-08-31T11:39:41+00:00"
"last_validated_date": "2024-01-31T13:47:48+00:00"
},
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_setup": {
"last_validated_date": "2023-08-29T15:20:44+00:00"
"last_validated_date": "2024-01-31T13:46:54+00:00"
}
}