Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Commit 55c6c59

Browse files
authored
App Integ tests (#8)
* Implement app integ tests * Remove app parameter ArtifactStoreBucket in both app template and integ tests
1 parent 20781a9 commit 55c6c59

File tree

14 files changed

+1213
-7
lines changed

14 files changed

+1213
-7
lines changed

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ SRC_DIR := src
77
TEST_DIR := test
88
SAM_DIR := .aws-sam
99
TEMPLATE_DIR := sam
10+
TESTAPP_DIR := test/integration/testdata/
1011

1112
# Required environment variables (user must override)
1213

1314
# S3 bucket used for packaging SAM templates
1415
PACKAGE_BUCKET ?= aws-sar-publishing
16+
INTEG_TEST_BUCKET ?= codepipeline-sar-publish-integ-tests
17+
1518

1619
# user can optionally override the following by setting environment variables with the same names before running make
1720

@@ -37,6 +40,7 @@ PYTHON := $(shell /usr/bin/which python$(PY_VERSION))
3740
clean:
3841
rm -f $(SRC_DIR)/requirements.txt
3942
rm -rf $(SAM_DIR)
43+
rm -f test/integration/testdata/testapp.zip
4044

4145
# used by CI build to install dependencies
4246
init:
@@ -54,8 +58,12 @@ compile:
5458
pipenv lock --requirements > $(SRC_DIR)/requirements.txt
5559
pipenv run sam build -t $(TEMPLATE_DIR)/app.yml -m $(SRC_DIR)/requirements.txt --debug
5660

57-
integ-test:
58-
pipenv run py.test --cov=$(SRC_DIR) --cov-fail-under=85 -vv test/integration
61+
integ-test: compile
62+
pipenv run sam package --template-file $(SAM_DIR)/build/template.yaml --s3-bucket $(INTEG_TEST_BUCKET) --output-template-file $(SAM_DIR)/packaged-app.yml
63+
pipenv run aws s3api put-object --bucket $(INTEG_TEST_BUCKET) --key template.yml --body $(SAM_DIR)/packaged-app.yml
64+
cd $(TESTAPP_DIR); \
65+
zip -r testapp.zip *; cd -
66+
pipenv run py.test --cov=$(SRC_DIR) -s -vv test/integration
5967

6068
build: compile
6169

sam/app.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ Parameters:
2121
Type: String
2222
Description: Log level for Lambda function logging, e.g., ERROR, INFO, DEBUG, etc
2323
Default: INFO
24-
ArtifactStoreBucket:
25-
Type: String
26-
Description: The S3 bucket name that CodePipeline uses to store artifacts
2724

2825
Resources:
2926
ServerlessRepoPublish:
@@ -39,8 +36,6 @@ Resources:
3936
Variables:
4037
LOG_LEVEL: !Ref LogLevel
4138
Policies:
42-
- S3ReadPolicy:
43-
BucketName: !Ref ArtifactStoreBucket
4439
- Version: '2012-10-17'
4540
Statement:
4641
- Effect: 'Allow'
@@ -57,3 +52,11 @@ Resources:
5752
- serverlessrepo:UpdateApplication
5853
Resource:
5954
!Sub 'arn:${AWS::Partition}:serverlessrepo:${AWS::Region}:${AWS::AccountId}:applications/*'
55+
56+
Outputs:
57+
ServerlessRepoPublishFunctionName:
58+
Description: Name of ServerlessRepoPublish lambda function
59+
Value: !Ref ServerlessRepoPublish
60+
ServerlessRepoPublishFunctionArn:
61+
Description: ARN of ServerlessRepoPublish lambda function
62+
Value: !GetAtt ServerlessRepoPublish.Arn
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: 'AWS::Serverless-2016-10-31'
3+
4+
Resources:
5+
CodePipelineServerlessRepoPublishApp:
6+
Type: 'AWS::Serverless::Application'
7+
Properties:
8+
Location:
9+
ApplicationId: ${PUBLISH_APP_ARN}
10+
SemanticVersion: 0.0.1
11+
12+
Pipeline:
13+
Type: AWS::CodePipeline::Pipeline
14+
Properties:
15+
ArtifactStore:
16+
Type: S3
17+
Location:
18+
Ref: ArtifactStoreBucket
19+
RoleArn: !GetAtt PipelineRole.Arn
20+
Stages:
21+
- Name: Source
22+
Actions:
23+
- Name: Source
24+
ActionTypeId:
25+
Category: Source
26+
Owner: AWS
27+
Provider: S3
28+
Version: '1'
29+
Configuration:
30+
S3Bucket: !Ref SourceBucket
31+
S3ObjectKey: testapp.zip
32+
OutputArtifacts:
33+
- Name: SourceArtifact
34+
RunOrder: '1'
35+
- Name: Build
36+
Actions:
37+
- Name: Build
38+
ActionTypeId:
39+
Category: Build
40+
Owner: AWS
41+
Provider: CodeBuild
42+
Version: '1'
43+
Configuration:
44+
ProjectName: !Ref CICodeBuildProject
45+
InputArtifacts:
46+
- Name: SourceArtifact
47+
OutputArtifacts:
48+
- Name: BuildArtifact
49+
RunOrder: '1'
50+
- Name: Deploy
51+
Actions:
52+
- Name: DeployToServerlessRepo
53+
ActionTypeId:
54+
Category: Invoke
55+
Owner: AWS
56+
Provider: Lambda
57+
Version: '1'
58+
Configuration:
59+
FunctionName: !GetAtt CodePipelineServerlessRepoPublishApp.Outputs.ServerlessRepoPublishFunctionName
60+
InputArtifacts:
61+
- Name: BuildArtifact
62+
RunOrder: '1'
63+
64+
ArtifactStoreBucket:
65+
Type: AWS::S3::Bucket
66+
Properties:
67+
VersioningConfiguration:
68+
Status: Enabled
69+
70+
ArtifactStoreBucketPolicy:
71+
Type: AWS::S3::BucketPolicy
72+
Properties:
73+
Bucket:
74+
Ref: ArtifactStoreBucket
75+
PolicyDocument:
76+
Version: '2012-10-17'
77+
Statement:
78+
- Action: ['s3:GetObject']
79+
Effect: Allow
80+
Principal:
81+
Service: 'serverlessrepo.amazonaws.com'
82+
Resource:
83+
- !Sub ${ArtifactStoreBucket.Arn}/*
84+
85+
SourceBucket:
86+
Type: AWS::S3::Bucket
87+
Properties:
88+
VersioningConfiguration:
89+
Status: Enabled
90+
91+
PipelineRole:
92+
Type: AWS::IAM::Role
93+
Properties:
94+
AssumeRolePolicyDocument:
95+
Statement:
96+
- Action: ['sts:AssumeRole']
97+
Effect: Allow
98+
Principal:
99+
Service: [codepipeline.amazonaws.com]
100+
Version: '2012-10-17'
101+
Path: /
102+
Policies:
103+
- PolicyName: CodePipelineAccess
104+
PolicyDocument:
105+
Version: '2012-10-17'
106+
Statement:
107+
- Action:
108+
- 'iam:PassRole'
109+
Effect: Allow
110+
Resource: '*'
111+
- Effect: Allow
112+
Action:
113+
- "codebuild:BatchGetBuilds"
114+
- "codebuild:StartBuild"
115+
Resource:
116+
- !GetAtt CICodeBuildProject.Arn
117+
- Effect: Allow
118+
Action:
119+
- "lambda:InvokeFunction"
120+
Resource:
121+
- !GetAtt CodePipelineServerlessRepoPublishApp.Outputs.ServerlessRepoPublishFunctionArn
122+
- Action:
123+
- 's3:ListBucket'
124+
- 's3:GetBucketVersioning'
125+
Effect: Allow
126+
Resource:
127+
- !Sub ${ArtifactStoreBucket.Arn}
128+
- !Sub ${SourceBucket.Arn}
129+
- Action:
130+
- 's3:PutObject'
131+
- 's3:GetObject'
132+
- 's3:GetObjectVersion'
133+
Effect: Allow
134+
Resource:
135+
- !Sub ${ArtifactStoreBucket.Arn}/*
136+
- !Sub ${SourceBucket.Arn}/*
137+
138+
CICodeBuildProject:
139+
Type: AWS::CodeBuild::Project
140+
Properties:
141+
ServiceRole: !GetAtt CICodeBuildRole.Arn
142+
Source:
143+
Type: CODEPIPELINE
144+
Artifacts:
145+
Type: CODEPIPELINE
146+
Environment:
147+
ComputeType: BUILD_GENERAL1_SMALL
148+
Image: aws/codebuild/python:3.7.1
149+
Type: LINUX_CONTAINER
150+
EnvironmentVariables:
151+
- Name: PACKAGE_BUCKET
152+
Value: !Ref ArtifactStoreBucket
153+
154+
CICodeBuildRole:
155+
Type: AWS::IAM::Role
156+
Properties:
157+
AssumeRolePolicyDocument:
158+
Version: "2012-10-17"
159+
Statement:
160+
- Effect: Allow
161+
Principal:
162+
Service:
163+
- "codebuild.amazonaws.com"
164+
Action:
165+
- "sts:AssumeRole"
166+
Path: /service-role/
167+
Policies:
168+
- PolicyName: CICodeBuildRolePolicy
169+
PolicyDocument:
170+
Version: "2012-10-17"
171+
Statement:
172+
- Effect: Allow
173+
Action:
174+
- "logs:CreateLogGroup"
175+
- "logs:CreateLogStream"
176+
- "logs:PutLogEvents"
177+
Resource:
178+
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
179+
- Effect: Allow
180+
Action:
181+
- "s3:PutObject"
182+
- "s3:GetObject"
183+
- "s3:GetObjectVersion"
184+
Resource:
185+
- !Sub ${ArtifactStoreBucket.Arn}/*
186+
- Effect: Allow
187+
Action:
188+
- "s3:ListBucket"
189+
Resource:
190+
- !Sub ${ArtifactStoreBucket.Arn}

0 commit comments

Comments
 (0)