Skip to content

Commit

Permalink
chore(s3-deployment): restore lambda tests (aws#12220)
Browse files Browse the repository at this point in the history
The change in aws#12129 accidentally deleted the unit tests for the s3-deployment lambda function. This change restores them.

Since tests are written in python, run them inside a docker image derived from public.ecr.aws/lambda/python.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Elad Ben-Israel authored Dec 24, 2020
1 parent 63bc98f commit a4934be
Show file tree
Hide file tree
Showing 6 changed files with 552 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/test/lambda.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { spawnSync } from 'child_process';
import * as path from 'path';

test('lambda python pytest', () => {
const result = spawnSync(path.join(__dirname, 'lambda', 'test.sh'), { stdio: 'inherit' });
expect(result.status).toBe(0);
});
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/test/lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:latest

# add everything to /opt/awscli (this is where `aws` is executed from)
ADD . /opt/awscli

# install boto3, which is available on Lambda
RUN pip3 install boto3

# run tests
WORKDIR /opt/awscli
RUN ["python3", "./test.py"]

ENTRYPOINT [ "/bin/bash" ]
27 changes: 27 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/test/lambda/aws
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
#===================================================================================================
# mock for the "aws" cli
#
# the mock behaves as follows:
# - argvs are going to be written to "aws.out" (one command in each line)
# - if "aws s3 cp" is invoked, the destination will be populated with a test zip file.
# - for "cp" and "sync", "aws.out" argv[4] is replaced by "archive.zip" and "contents.zip"
# becuase the actual value is a full path of a temporary directory
#
import sys
import json
import os
import shutil

scriptdir=os.path.dirname(os.path.realpath(__file__))

# if "cp" is called with a local destination, copy a test zip file to the destination or
if sys.argv[2] == "cp" and not sys.argv[4].startswith("s3://"):
shutil.copyfile(os.path.join(scriptdir, 'test.zip'), sys.argv[4])
sys.argv[4] = "archive.zip"

if sys.argv[2] == "sync":
sys.argv[4 if '--delete' in sys.argv else 3] = "contents.zip"

with open("./aws.out", "a") as myfile:
myfile.write(json.dumps(sys.argv[1:]) + "\n")
Loading

0 comments on commit a4934be

Please sign in to comment.