New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(custom-resources): inactive lambda functions fail on invoke #22612
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a0a8e9
fix(custom-resources): inactive lambda funtions fail on invoke
TheRealAmazonKendra ce3fa3a
Merge branch 'main' into TheRealAmazonKendra/inactive-custom-resource
mergify[bot] e0e1468
update awscli-layer integ test
TheRealAmazonKendra 8226b6d
update lambda-layer-kubectl integ test
TheRealAmazonKendra 6081005
update lambda-layer-node-proxy-agent integ test
TheRealAmazonKendra b211a04
Merge branch 'main' into TheRealAmazonKendra/inactive-custom-resource
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
packages/@aws-cdk/custom-resources/test/provider-framework/outbound.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import * as aws from 'aws-sdk'; | ||
import { invokeFunction } from '../../lib/provider-framework/runtime/outbound'; | ||
|
||
jest.mock('aws-sdk', () => { | ||
return { | ||
Lambda: class { | ||
public invoke() { | ||
return { promise: () => mockInvoke() }; | ||
} | ||
|
||
public waitFor() { | ||
return { promise: () => mockWaitFor() }; | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
let mockInvoke: () => Promise<aws.Lambda.InvocationResponse>; | ||
|
||
const req: aws.Lambda.InvocationRequest = { | ||
FunctionName: 'Whatever', | ||
Payload: { | ||
IsThisATest: 'Yes, this is a test', | ||
AreYouSure: 'Yes, I am sure', | ||
}, | ||
}; | ||
|
||
let invokeCount: number = 0; | ||
let expectedFunctionStates: string[] = []; | ||
let receivedFunctionStates: string[] = []; | ||
|
||
const mockWaitFor = async (): Promise<aws.Lambda.GetFunctionResponse> => { | ||
let state = expectedFunctionStates.pop(); | ||
while (state !== 'Active') { | ||
receivedFunctionStates.push(state!); | ||
// If it goes back to inactive it's failed | ||
if (state === 'Inactive') throw new Error('Not today'); | ||
// If failed... it's failed | ||
if (state === 'Failed') throw new Error('Broken'); | ||
// If pending, continue the loop, no other valid options | ||
if (state !== 'Pending') throw new Error('State is confused'); | ||
state = expectedFunctionStates.pop(); | ||
} | ||
receivedFunctionStates.push(state); | ||
return { | ||
Configuration: { | ||
State: 'Active', | ||
}, | ||
}; | ||
}; | ||
|
||
describe('invokeFunction tests', () => { | ||
afterEach(() => { | ||
invokeCount = 0; | ||
expectedFunctionStates = []; | ||
receivedFunctionStates = []; | ||
}); | ||
|
||
// Success cases | ||
test('Inactive function that reactivates does not throw error', async () => { | ||
mockInvoke = async () => { | ||
if (invokeCount == 0) { | ||
invokeCount++; | ||
throw new Error('Better luck next time'); | ||
} | ||
invokeCount++; | ||
return { Payload: req.Payload }; | ||
}; | ||
|
||
expectedFunctionStates.push('Active'); | ||
expectedFunctionStates.push('Pending'); | ||
|
||
expect(await invokeFunction(req)).toEqual({ Payload: req.Payload }); | ||
expect(invokeCount).toEqual(2); | ||
expect(receivedFunctionStates).toEqual(['Pending', 'Active']); | ||
}); | ||
|
||
test('Active function does not run waitFor or retry invoke', async () => { | ||
mockInvoke = async () => { | ||
if (invokeCount == 1) { | ||
invokeCount++; | ||
throw new Error('This should not happen in this test'); | ||
} | ||
invokeCount++; | ||
return { Payload: req.Payload }; | ||
}; | ||
|
||
expectedFunctionStates.push('Active'); | ||
|
||
expect(await invokeFunction(req)).toEqual({ Payload: req.Payload }); | ||
expect(invokeCount).toEqual(1); | ||
expect(receivedFunctionStates).toEqual([]); | ||
}); | ||
|
||
// Failure cases | ||
test('Inactive function that goes back to inactive throws error', async () => { | ||
mockInvoke = async () => { | ||
if (invokeCount == 0) { | ||
invokeCount++; | ||
throw new Error('Better luck next time'); | ||
} | ||
invokeCount++; | ||
return { Payload: req.Payload }; | ||
}; | ||
|
||
expectedFunctionStates.push('Inactive'); | ||
expectedFunctionStates.push('Pending'); | ||
expectedFunctionStates.push('Pending'); | ||
|
||
await expect(invokeFunction(req)).rejects.toThrowError(new Error('Not today')); | ||
expect(invokeCount).toEqual(1); | ||
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Inactive']); | ||
}); | ||
|
||
test('Inactive function that goes to failed throws error', async () => { | ||
mockInvoke = async () => { | ||
if (invokeCount == 0) { | ||
invokeCount++; | ||
throw new Error('Better luck next time'); | ||
} | ||
invokeCount++; | ||
return { Payload: req.Payload }; | ||
}; | ||
|
||
expectedFunctionStates.push('Failed'); | ||
expectedFunctionStates.push('Pending'); | ||
expectedFunctionStates.push('Pending'); | ||
|
||
await expect(invokeFunction(req)).rejects.toThrowError(new Error('Broken')); | ||
expect(invokeCount).toEqual(1); | ||
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Failed']); | ||
}); | ||
|
||
test('Inactive function that returns other value throws error', async () => { | ||
mockInvoke = async () => { | ||
if (invokeCount == 0) { | ||
invokeCount++; | ||
throw new Error('Better luck next time'); | ||
} | ||
invokeCount++; | ||
return { Payload: req.Payload }; | ||
}; | ||
|
||
expectedFunctionStates.push('NewFunctionWhoDis'); | ||
expectedFunctionStates.push('Pending'); | ||
expectedFunctionStates.push('Pending'); | ||
|
||
await expect(invokeFunction(req)).rejects.toThrowError(new Error('State is confused')); | ||
expect(invokeCount).toEqual(1); | ||
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'NewFunctionWhoDis']); | ||
}); | ||
|
||
test('Wait for stops on terminal responses', async () => { | ||
mockInvoke = async () => { | ||
if (invokeCount == 0) { | ||
invokeCount++; | ||
throw new Error('Better luck next time'); | ||
} | ||
invokeCount++; | ||
return { Payload: req.Payload }; | ||
}; | ||
|
||
expectedFunctionStates.push('SomethingElse'); | ||
expectedFunctionStates.push('Pending'); | ||
expectedFunctionStates.push('Inactive'); | ||
expectedFunctionStates.push('Pending'); | ||
expectedFunctionStates.push('Pending'); | ||
|
||
await expect(invokeFunction(req)).rejects.toThrowError(new Error('Not today')); | ||
expect(invokeCount).toEqual(1); | ||
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Inactive']); | ||
}); | ||
}); | ||
|
Binary file modified
BIN
-421 Bytes
(100%)
...er.js.snapshot/asset.5d8d1d0aacea23824c62f362e1e3c14b7dd14a31c71b53bfae4d14a6373c5510.zip
Binary file not shown.
45 changes: 0 additions & 45 deletions
45
...apshot/asset.7215c88dd3e638d28329d4538b36cdbfb54233a4d972181795814f8b904d1037/outbound.js
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever use of await inside & outside the expect()