-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
index.test.js
117 lines (111 loc) · 3.76 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
const path = require('path');
const fs = require('fs');
const chai = require('chai');
const AwsProvider = require('../provider/awsProvider');
const Serverless = require('../../../Serverless');
const { createTmpDir } = require('../../../../tests/utils/fs');
const { addCustomResourceToService } = require('./index.js');
const expect = chai.expect;
chai.use(require('chai-as-promised'));
describe('#addCustomResourceToService()', () => {
let tmpDirPath;
let serverless;
let provider;
let context;
const iamRoleStatements = [
{
Effect: 'Allow',
Resource: 'arn:aws:lambda:*:*:function:custom-resource-func',
Action: ['lambda:AddPermission', 'lambda:RemovePermission'],
},
];
beforeEach(() => {
const options = {
stage: 'dev',
region: 'us-east-1',
};
tmpDirPath = createTmpDir();
serverless = new Serverless();
provider = new AwsProvider(serverless, options);
serverless.setProvider('aws', provider);
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = {
Resources: {},
};
serverless.config.servicePath = tmpDirPath;
serverless.service.package.artifactDirectoryName = 'artifact-dir-name';
context = {
serverless,
provider,
options,
};
});
describe('when using the custom S3 resouce', () => {
it('should add the custom resource to the service', () => {
return expect(
addCustomResourceToService.call(context, 's3', iamRoleStatements)
).to.be.fulfilled.then(() => {
const { Resources } = serverless.service.provider.compiledCloudFormationTemplate;
const customResourcesZipFilePath = path.join(
tmpDirPath,
'.serverless',
'custom-resources.zip'
);
expect(fs.existsSync(customResourcesZipFilePath)).to.equal(true);
expect(Resources).to.deep.equal({
CustomDashresourceDashexistingDashs3LambdaFunction: {
Type: 'AWS::Lambda::Function',
Properties: {
Code: {
S3Bucket: { Ref: 'ServerlessDeploymentBucket' },
S3Key: 'artifact-dir-name/custom-resources.zip',
},
FunctionName: 'some-service-dev-custom-resource-existing-s3',
Handler: 's3/handler.handler',
MemorySize: 1024,
Role: { 'Fn::GetAtt': ['IamRoleCustomResourcesLambdaExecution', 'Arn'] },
Runtime: 'nodejs10.x',
Timeout: 6,
},
DependsOn: ['IamRoleCustomResourcesLambdaExecution'],
},
IamRoleCustomResourcesLambdaExecution: {
Type: 'AWS::IAM::Role',
Properties: {
AssumeRolePolicyDocument: {
Statement: [
{
Action: ['sts:AssumeRole'],
Effect: 'Allow',
Principal: {
Service: ['lambda.amazonaws.com'],
},
},
],
Version: '2012-10-17',
},
Policies: [
{
PolicyDocument: {
Statement: [
{
Effect: 'Allow',
Resource: 'arn:aws:lambda:*:*:function:custom-resource-func',
Action: ['lambda:AddPermission', 'lambda:RemovePermission'],
},
],
Version: '2012-10-17',
},
PolicyName: {
'Fn::Join': ['-', ['dev', 'some-service', 'custom-resources-lambda']],
},
},
],
},
},
});
});
});
});
});