forked from davidgf/serverless-plugin-canary-deployments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lambda.test.js
103 lines (96 loc) · 3.69 KB
/
Lambda.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
const { expect } = require('chai');
const _ = require('lodash/fp');
const Lambda = require('./Lambda');
describe('Lambda', () => {
describe('.buildAlias', () => {
const functionName = 'MyFunctionName';
const functionVersion = 'MyFunctionVersion';
const alias = 'live';
const baseAlias = {
Type: 'AWS::Lambda::Alias',
Properties: {
FunctionVersion: { 'Fn::GetAtt': [ functionVersion, 'Version' ] },
FunctionName: { Ref: functionName },
Name: alias
}
};
it('should generate a AWS::Lambda::Alias resouce', () => {
const expected = baseAlias;
const actual = Lambda.buildAlias({ alias, functionName, functionVersion });
expect(actual).to.deep.equal(expected);
});
context('when traffic shifting settings were provided', () => {
it('should include the UpdatePolicy', () => {
const trafficShiftingSettings = {
codeDeployApp: 'CodeDeployAppName',
deploymentGroup: 'DeploymentGroup',
beforeHook: 'BeforeHookLambdaFn',
afterHook: 'AfterHookLambdaFn'
};
const expected = {
UpdatePolicy: {
CodeDeployLambdaAliasUpdate: {
ApplicationName: { Ref: trafficShiftingSettings.codeDeployApp },
AfterAllowTrafficHook: { Ref: trafficShiftingSettings.afterHook },
BeforeAllowTrafficHook: { Ref: trafficShiftingSettings.beforeHook },
DeploymentGroupName: { Ref: trafficShiftingSettings.deploymentGroup }
}
}
};
const actual = Lambda.buildAlias({ alias, functionName, functionVersion, trafficShiftingSettings });
expect(actual).to.deep.include(expected);
});
});
});
describe('.replacePermissionFunctionWithAlias', () => {
const lambdaPermission = {
Type: 'AWS::Lambda::Permission',
Properties: {
FunctionName: { 'Fn::GetAtt': ['HelloLambdaFunctionAliasLive', 'Arn'] },
Action: 'lambda:InvokeFunction',
Principal: 'apigateway.amazonaws.com',
SourceArn: {
'Fn::Join': [
'',
[
'arn:aws:execute-api:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':',
{ Ref: 'ApiGatewayRestApi' },
'/*/*'
]
]
}
}
};
it('replaces the permission\'s function for an alias', () => {
const functionAlias = 'TheFunctionAlias';
const permissionFunctionWithAlias = { Ref: functionAlias };
const expected = _.set('Properties.FunctionName', permissionFunctionWithAlias, lambdaPermission);
const actual = Lambda.replacePermissionFunctionWithAlias(lambdaPermission, functionAlias);
expect(actual).to.deep.equal(expected);
});
});
describe('.replaceEventMappingFunctionWithAlias', () => {
const eventSourceMapping = {
Type: 'AWS::Lambda::EventSourceMapping',
DependsOn: 'IamRoleLambdaExecution',
Properties: {
BatchSize: 10,
EventSourceArn: { 'Fn::GetAtt': ['StreamsTestTable', 'StreamArn'] },
FunctionName: { 'Fn::GetAtt': ['HelloLambdaFunction', 'Arn'] },
StartingPosition: 'TRIM_HORIZON',
Enabled: 'True'
}
};
it('replaces the event source mapping\'s function for an alias', () => {
const functionAlias = 'TheFunctionAlias';
const eventMappingFunctionWithAlias = { Ref: functionAlias };
const expected = _.set('Properties.FunctionName', eventMappingFunctionWithAlias, eventSourceMapping);
const actual = Lambda.replaceEventMappingFunctionWithAlias(eventSourceMapping, functionAlias);
expect(actual).to.deep.equal(expected);
});
});
});