-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathlambdaInvoke-test.ts
160 lines (136 loc) · 5.5 KB
/
lambdaInvoke-test.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*!
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT
*/
import { Lambda } from 'aws-sdk'
import { SdkUtils } from 'lib/sdkutils'
import { TaskOperations } from 'tasks/LambdaInvokeFunction/TaskOperations'
import { TaskParameters } from 'tasks/LambdaInvokeFunction/TaskParameters'
import { emptyConnectionParameters } from '../testCommon'
jest.mock('aws-sdk')
const baseTaskParameters: TaskParameters = {
awsConnectionParameters: emptyConnectionParameters,
functionName: 'coolfunction',
payload: '',
invocationType: '',
logType: '',
outputVariable: '',
outputLogsVariable: ''
}
const awsResponseThrows = {
promise: function() {
throw new Error('function nonexistent')
}
}
const getFunctionSucceeds = {
promise: function() {}
}
const invokeLambdaSucceeds = {
promise: function(): Lambda.InvocationResponse {
return {
Payload: 'payload'
}
}
}
const invokeLambdaSucceedsNoResponse = {
promise: function() {
return {
Payload: undefined
}
}
}
describe('Lambda Invoke', () => {
// TODO https://github.com/aws/aws-vsts-tools/issues/167
beforeAll(() => {
SdkUtils.readResourcesFromRelativePath('../../build/src/tasks/LambdaInvokeFunction/task.json')
})
test('Creates a TaskOperation', () => {
expect(new TaskOperations(new Lambda(), baseTaskParameters)).not.toBeNull()
})
test('Handles a null service response', async () => {
expect.assertions(3)
const taskParameters = { ...baseTaskParameters }
taskParameters.outputVariable = 'something'
taskParameters.outputLogsVariable = 'something'
await assertPayloadCorrect(
taskParameters,
jest.fn(() => {
// expectation to make sure the callback is called
expect(1).toBe(1)
return invokeLambdaSucceedsNoResponse
})
)
})
test("Fails when lambda doesn't exist", async () => {
expect.assertions(1)
const lambda = new Lambda() as any
lambda.getFunctionConfiguration = jest.fn(() => awsResponseThrows)
const taskOperations = new TaskOperations(lambda, baseTaskParameters)
await taskOperations.execute().catch(e => expect(`${e}`).toContain('coolfunction does not exist'))
})
test('Fails when lambda invoke fails', async () => {
expect.assertions(1)
const lambda = new Lambda() as any
lambda.getFunctionConfiguration = jest.fn(() => getFunctionSucceeds)
lambda.invoke = jest.fn(() => awsResponseThrows)
const taskOperations = new TaskOperations(lambda, baseTaskParameters)
// it re-throws the exception, so we check for that
await taskOperations.execute().catch(e => expect(`${e}`).toContain('function nonexistent'))
})
test('Happy path, reads function invoke output', async () => {
expect.assertions(2)
const taskParameters = { ...baseTaskParameters }
taskParameters.outputVariable = 'LambdaInvokeResult'
const lambda = new Lambda() as any
lambda.getFunctionConfiguration = jest.fn(() => getFunctionSucceeds)
lambda.invoke = jest.fn(() => invokeLambdaSucceeds)
const taskOperations = new TaskOperations(lambda, taskParameters)
await taskOperations.execute()
const taskOperationsWithBase = new TaskOperations(lambda, baseTaskParameters)
await taskOperationsWithBase.execute()
expect(lambda.invoke).toBeCalledTimes(2)
expect(lambda.getFunctionConfiguration).toBeCalledTimes(2)
})
test('Handles JSON Objects Property', async () => {
await testPayloadsAndOutputs('{"key": "value"}', '{"key": "value"}')
})
test('Handles JSON Arrays Property', async () => {
await testPayloadsAndOutputs(' ["key", "value"] ', ' ["key", "value"] ')
})
test('Handles JSON Strings Property', async () => {
await testPayloadsAndOutputs('"key"', '"key"')
})
test('Sringifies non-json property', async () => {
await testPayloadsAndOutputs('This is a "string', '"This is a \\"string"')
})
test('Sringifies tricky non-json property', async () => {
await testPayloadsAndOutputs('"This [is a strin{}"g', '"\\"This [is a strin{}\\"g"')
})
test('Quotes numbers for backwards compatability', async () => {
await testPayloadsAndOutputs('3', '"3"')
})
test('Does not stringify possibly valid JSON', async () => {
await testPayloadsAndOutputs('[{"abc": "def"}', '[{"abc": "def"}')
})
async function testPayloadsAndOutputs(input: string, expectedOutput: string) {
expect.assertions(3)
const taskParameters = { ...baseTaskParameters }
taskParameters.payload = input
await assertPayloadCorrect(
taskParameters,
jest.fn(params => {
expect(params.Payload.toString('utf8')).toBe(expectedOutput)
return invokeLambdaSucceeds
})
)
}
async function assertPayloadCorrect(taskParameters: any, callback: (params: any) => any) {
const lambda = new Lambda() as any
lambda.getFunctionConfiguration = jest.fn(() => getFunctionSucceeds)
lambda.invoke = callback
const taskOperations = new TaskOperations(lambda, taskParameters)
await taskOperations.execute()
expect(lambda.invoke).toBeCalledTimes(1)
expect(lambda.getFunctionConfiguration).toBeCalledTimes(1)
}
})