-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into niroam-ec2-instance-type-p5
- Loading branch information
Showing
4 changed files
with
243 additions
and
14 deletions.
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
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
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
171 changes: 171 additions & 0 deletions
171
packages/aws-cdk-lib/pipelines/test/private/approve-lambda-handler.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,171 @@ | ||
const mockGetPipelineState = jest.fn(); | ||
const mockPutApprovalResult = jest.fn(); | ||
const mockCodePipeline = { | ||
getPipelineState: mockGetPipelineState, | ||
putApprovalResult: mockPutApprovalResult, | ||
}; | ||
|
||
jest.mock('@aws-sdk/client-codepipeline', () => { | ||
return { | ||
CodePipeline: jest.fn(() => mockCodePipeline), | ||
}; | ||
}); | ||
|
||
jest.setTimeout(10_000); | ||
|
||
import { handler } from '../../lib/private/approve-lambda/index'; | ||
|
||
describe('approve-lambda handler', () => { | ||
|
||
beforeEach(() => { | ||
jest.spyOn(global, 'setTimeout'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('automatic approval works', async () => { | ||
// GIVEN | ||
mockGetPipelineState.mockImplementation(() => { | ||
return { | ||
stageStates: [{ | ||
stageName: 'stage', | ||
actionStates: [{ | ||
actionName: 'action', | ||
latestExecution: { | ||
lastStatusChange: 1446137358.328, | ||
status: 'Succeeded', | ||
token: 'token', | ||
}, | ||
}], | ||
}], | ||
}; | ||
}); | ||
|
||
const event = { | ||
PipelineName: 'pipeline', | ||
StageName: 'stage', | ||
ActionName: 'action', | ||
}; | ||
|
||
// WHEN | ||
await handler(event, {}); | ||
|
||
// THEN | ||
expect(mockPutApprovalResult).toHaveBeenCalled(); | ||
}); | ||
|
||
test('does not approve if stageName not found', async () => { | ||
// GIVEN | ||
mockGetPipelineState.mockImplementation(async () => { | ||
return { | ||
stageStates: [{ | ||
stageName: 'unknown', | ||
actionStates: [{ | ||
actionName: 'action', | ||
latestExecution: { | ||
lastStatusChange: 1446137358.328, | ||
status: 'Succeeded', | ||
token: 'token', | ||
}, | ||
}], | ||
}], | ||
}; | ||
}); | ||
|
||
// expire deadline after first loop | ||
jest.spyOn(Date, 'now') | ||
.mockReturnValueOnce(0) | ||
.mockReturnValueOnce(0) | ||
.mockReturnValueOnce(10 * 60_000); | ||
|
||
const event = { | ||
PipelineName: 'pipeline', | ||
StageName: 'stage', | ||
ActionName: 'action', | ||
}; | ||
|
||
// WHEN | ||
await handler(event, {}); | ||
|
||
// THEN | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(mockPutApprovalResult).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('does not approve if actionName not found', async () => { | ||
// GIVEN | ||
mockGetPipelineState.mockImplementation(async () => { | ||
return { | ||
stageStates: [{ | ||
stageName: 'stage', | ||
actionStates: [{ | ||
actionName: 'unknown', | ||
latestExecution: { | ||
lastStatusChange: 1446137358.328, | ||
status: 'Succeeded', | ||
token: 'token', | ||
}, | ||
}], | ||
}], | ||
}; | ||
}); | ||
|
||
// expire deadline after first loop | ||
jest.spyOn(Date, 'now') | ||
.mockReturnValueOnce(0) | ||
.mockReturnValueOnce(0) | ||
.mockReturnValueOnce(10 * 60_000); | ||
|
||
const event = { | ||
PipelineName: 'pipeline', | ||
StageName: 'stage', | ||
ActionName: 'action', | ||
}; | ||
|
||
// WHEN | ||
await handler(event, {}); | ||
|
||
// THEN | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(mockPutApprovalResult).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('does not approve if token not present', async () => { | ||
// GIVEN | ||
mockGetPipelineState.mockImplementation(async () => { | ||
return { | ||
stageStates: [{ | ||
stageName: 'stage', | ||
actionStates: [{ | ||
actionName: 'unknown', | ||
latestExecution: { | ||
lastStatusChange: 1446137358.328, | ||
status: 'Succeeded', | ||
}, | ||
}], | ||
}], | ||
}; | ||
}); | ||
|
||
// expire deadline after first loop | ||
jest.spyOn(Date, 'now') | ||
.mockReturnValueOnce(0) | ||
.mockReturnValueOnce(0) | ||
.mockReturnValueOnce(10 * 60_000); | ||
|
||
const event = { | ||
PipelineName: 'pipeline', | ||
StageName: 'stage', | ||
ActionName: 'action', | ||
}; | ||
|
||
// WHEN | ||
await handler(event, {}); | ||
|
||
// THEN | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(mockPutApprovalResult).not.toHaveBeenCalled(); | ||
}); | ||
}); |