Skip to content
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: all aws-cdk-lib custom resources now use Node18 #26212

Merged
merged 26 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
633a498
aws-cdk-lib/triggers
MrArnoldPalmer Jun 7, 2023
e1cee3d
edge function
MrArnoldPalmer Jun 9, 2023
ac487ca
ecr auto-delete
MrArnoldPalmer Jun 9, 2023
6ff96d8
aws-s3 auto-delete-objects
MrArnoldPalmer Jun 9, 2023
369505a
Merge branch 'main' of github.com:aws/aws-cdk into feat/aws-cdk-lib-s…
MrArnoldPalmer Jun 29, 2023
44520cc
change remaining CR runtimes and SDK usage
MrArnoldPalmer Jul 3, 2023
8be6406
Merge branch 'main' of github.com:aws/aws-cdk into feat/aws-cdk-lib-s…
MrArnoldPalmer Jul 3, 2023
f467ca9
Merge branch 'main' of github.com:aws/aws-cdk into feat/aws-cdk-lib-s…
MrArnoldPalmer Jul 5, 2023
620d2b4
fix custom-resource-handlers tests
MrArnoldPalmer Jul 5, 2023
74540f3
fix some tests
MrArnoldPalmer Jul 5, 2023
b3d6654
fix custom-resources tests
MrArnoldPalmer Jul 5, 2023
f2b78cc
fix more tests and unify versions
MrArnoldPalmer Jul 5, 2023
5072965
fix more tests
MrArnoldPalmer Jul 6, 2023
5c09989
fix deps
MrArnoldPalmer Jul 6, 2023
302259c
fix dependencies
MrArnoldPalmer Jul 6, 2023
1fb4d8e
Revert "fix dependencies"
MrArnoldPalmer Jul 6, 2023
153f5a9
unify dependencies for type errors
MrArnoldPalmer Jul 6, 2023
e7020a0
update integ tests to diff assets and snapshots
MrArnoldPalmer Jul 6, 2023
0f9c867
Merge branch 'main' of github.com:aws/aws-cdk into feat/aws-cdk-lib-s…
MrArnoldPalmer Jul 6, 2023
f75a150
update glue test snapshot
MrArnoldPalmer Jul 6, 2023
5541894
update alpha module snapshots
MrArnoldPalmer Jul 6, 2023
cb4aa4d
chore: download Noctilucent instead fo building it
rix0rrr Jul 6, 2023
dec7e85
Merge remote-tracking branch 'origin/huijbers/download-nocti' into fe…
rix0rrr Jul 6, 2023
9d4bafd
Snappy
rix0rrr Jul 6, 2023
ae6a2b0
Snapshots
rix0rrr Jul 6, 2023
d3b0b32
Merge branch 'main' into feat/aws-cdk-lib-sdk-v3
mergify[bot] Jul 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix more tests
  • Loading branch information
MrArnoldPalmer committed Jul 6, 2023
commit 50729653b734356979aa9e3b41a9e779b20b457b
35 changes: 18 additions & 17 deletions packages/aws-cdk-lib/aws-dynamodb/test/replica-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as AWS from 'aws-sdk-mock';
import { DescribeTableCommand, DynamoDBClient, UpdateTableCommand } from '@aws-sdk/client-dynamodb';
import { mockClient } from 'aws-sdk-client-mock';
import * as sinon from 'sinon';
import { OnEventRequest } from '../../custom-resources/lib/provider-framework/types';
import { isCompleteHandler, onEventHandler } from '../lib/replica-handler';
Expand All @@ -14,8 +15,6 @@ afterAll(() => {
global.console.log = oldConsoleLog;
});

AWS.setSDK(require.resolve('aws-sdk'));

const REGION = 'eu-west-2';

const createEvent: OnEventRequest = {
Expand All @@ -33,14 +32,16 @@ const createEvent: OnEventRequest = {
ResourceType: 'resource-type',
};

const ddbMock = mockClient(DynamoDBClient);

afterEach(() => {
AWS.restore();
ddbMock.reset();
});

test('on event', async () => {
const updateTableMock = sinon.fake.resolves({});

AWS.mock('DynamoDB', 'updateTable', updateTableMock);
ddbMock.on(UpdateTableCommand).callsFake(updateTableMock);

const data = await onEventHandler(createEvent);

Expand All @@ -61,16 +62,16 @@ test('on event', async () => {
});

test("on Update event from CFN calls updateTable with Create if a replica in the region doesn't exist", async () => {
AWS.mock('DynamoDB', 'describeTable', sinon.fake.resolves({
ddbMock.on(DescribeTableCommand).resolves({
Table: {
Replicas: [
// no replicas exist yet
],
},
}));
});

const updateTableMock = sinon.fake.resolves({});
AWS.mock('DynamoDB', 'updateTable', updateTableMock);
ddbMock.on(UpdateTableCommand).callsFake(updateTableMock);

const data = await onEventHandler({
...createEvent,
Expand All @@ -97,16 +98,16 @@ test("on Update event from CFN calls updateTable with Create if a replica in the
});

test("on Update event from CFN calls doesn't call updateTable if a replica in the region does exist", async () => {
AWS.mock('DynamoDB', 'describeTable', sinon.fake.resolves({
ddbMock.on(DescribeTableCommand).resolves({
Table: {
Replicas: [
{ RegionName: REGION },
],
},
}));
});

const updateTableMock = sinon.fake.resolves({});
AWS.mock('DynamoDB', 'updateTable', updateTableMock);
ddbMock.on(UpdateTableCommand).callsFake(updateTableMock);

await onEventHandler({
...createEvent,
Expand All @@ -122,7 +123,7 @@ test("on Update event from CFN calls doesn't call updateTable if a replica in th
test('on event calls updateTable with Delete', async () => {
const updateTableMock = sinon.fake.resolves({});

AWS.mock('DynamoDB', 'updateTable', updateTableMock);
ddbMock.on(UpdateTableCommand).callsFake(updateTableMock);

const data = await onEventHandler({
...createEvent,
Expand All @@ -149,7 +150,7 @@ test('is complete for create returns false without replicas', async () => {
Table: {},
});

AWS.mock('DynamoDB', 'describeTable', describeTableMock);
ddbMock.on(DescribeTableCommand).callsFake(describeTableMock);

const data = await isCompleteHandler(createEvent);

Expand All @@ -168,7 +169,7 @@ test('is complete for create returns false when replica is not active', async ()
},
});

AWS.mock('DynamoDB', 'describeTable', describeTableMock);
ddbMock.on(DescribeTableCommand).callsFake(describeTableMock);

const data = await isCompleteHandler(createEvent);

Expand All @@ -188,7 +189,7 @@ test('is complete for create returns false when table is not active', async () =
},
});

AWS.mock('DynamoDB', 'describeTable', describeTableMock);
ddbMock.on(DescribeTableCommand).callsFake(describeTableMock);

const data = await isCompleteHandler(createEvent);

Expand All @@ -208,7 +209,7 @@ test('is complete for create returns true when replica is active', async () => {
},
});

AWS.mock('DynamoDB', 'describeTable', describeTableMock);
ddbMock.on(DescribeTableCommand).callsFake(describeTableMock);

const data = await isCompleteHandler(createEvent);

Expand All @@ -228,7 +229,7 @@ test('is complete for delete returns true when replica is gone', async () => {
},
});

AWS.mock('DynamoDB', 'describeTable', describeTableMock);
ddbMock.on(DescribeTableCommand).callsFake(describeTableMock);

const data = await isCompleteHandler({
...createEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ jest.mock('@aws-sdk/client-sts', () => {
return {
STS: jest.fn().mockImplementation(() => {
return mockStsClient;;
})
}),
};
});

jest.mock('@aws-sdk/client-route-53', () => {
return {
Route53: jest.fn().mockImplementation(() => {
return mockRoute53Client;
})
}),
};
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { handler } from '../lib/delete-existing-record-set-handler';

const mockListResourceRecordSetsResponse = jest.fn();
const mockChangeResourceRecordSetsResponse = jest.fn();
const mockWaitUntilResourceRecordSetsChanged = jest.fn().mockResolvedValue({});

const mockRoute53 = {
listResourceRecordSets: jest.fn().mockReturnValue({
promise: mockListResourceRecordSetsResponse,
}),
changeResourceRecordSets: jest.fn().mockReturnValue({
promise: mockChangeResourceRecordSetsResponse,
}),
waitFor: jest.fn().mockReturnValue({
promise: jest.fn().mockResolvedValue({}),
}),
listResourceRecordSets: mockListResourceRecordSetsResponse,
changeResourceRecordSets: mockChangeResourceRecordSetsResponse,
};

jest.mock('aws-sdk', () => {
jest.mock('@aws-sdk/client-route-53', () => {
return {
Route53: jest.fn(() => mockRoute53),
Route53: jest.fn().mockImplementation(() => mockRoute53),
waitUntilResourceRecordSetsChanged: (...args: any[]) => mockWaitUntilResourceRecordSetsChanged(...args),
};
});

import { handler } from '../lib/delete-existing-record-set-handler';

const event: AWSLambda.CloudFormationCustomResourceEvent & { PhysicalResourceId?: string } = {
RequestType: 'Create',
ServiceToken: 'service-token',
Expand Down Expand Up @@ -87,7 +82,8 @@ test('create request with existing record', async () => {
},
});

expect(mockRoute53.waitFor).toHaveBeenCalledWith('resourceRecordSetsChanged', {
expect(mockWaitUntilResourceRecordSetsChanged).toHaveBeenCalledTimes(1);
expect(mockWaitUntilResourceRecordSetsChanged.mock.calls[0][1]).toEqual({
Id: 'change-id',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,17 @@ let mockDeleteParameters: jest.Mock ;
let mockAddTagsToResource: jest.Mock;
let mockGetParametersByPath: jest.Mock;
let mockRemoveTagsFromResource: jest.Mock;
jest.mock('aws-sdk', () => {

jest.mock('@aws-sdk/client-ssm', () => {
return {
SSM: jest.fn(() => {
return {
addTagsToResource: jest.fn((params) => {
return {
promise: () => mockAddTagsToResource(params),
};
}),
removeTagsFromResource: jest.fn((params) => {
return {
promise: () => mockRemoveTagsFromResource(params),
};
}),
getParametersByPath: jest.fn((params) => {
return {
promise: () => mockGetParametersByPath(params),
};
}),
};
}),
SSM: jest.fn().mockImplementation(() => ({
addTagsToResource: mockAddTagsToResource,
removeTagsFromResource: mockRemoveTagsFromResource,
getParametersByPath: mockGetParametersByPath,
})),
};
});

beforeEach(() => {
jest.spyOn(console, 'info').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { GetFunctionResponse, InvokeCommandInput } from '@aws-sdk/client-lambda';
import * as aws from 'aws-sdk';
import { InvocationResponse, InvokeCommandInput } from '@aws-sdk/client-lambda';
import { invokeFunction } from '../../lib/provider-framework/runtime/outbound';

jest.mock('aws-sdk', () => {
return {
Lambda: class {
public invoke() {
return { promise: () => mockInvoke() };
}
const mockInvoke = jest.fn();
const mockWaitUntilFunctionActive = jest.fn();
const mockLambda = {
invoke: mockInvoke,
};

public waitFor() {
return { promise: () => mockWaitFor() };
}
},
jest.mock('@aws-sdk/client-lambda', () => {
return {
Lambda: jest.fn().mockImplementation(() => {
return mockLambda;
}),
waitUntilFunctionActive: (...args: any[]) => mockWaitUntilFunctionActive(...args),
};
});

let mockInvoke: () => Promise<aws.Lambda.InvocationResponse>;

const req: InvokeCommandInput = {
FunctionName: 'Whatever',
Payload: JSON.stringify({
Expand All @@ -30,7 +28,7 @@ let invokeCount: number = 0;
let expectedFunctionStates: string[] = [];
let receivedFunctionStates: string[] = [];

const mockWaitFor = async (): Promise<GetFunctionResponse> => {
mockWaitUntilFunctionActive.mockImplementation(async () => {
let state = expectedFunctionStates.pop();
while (state !== 'Active') {
receivedFunctionStates.push(state!);
Expand All @@ -48,7 +46,7 @@ const mockWaitFor = async (): Promise<GetFunctionResponse> => {
State: 'Active',
},
};
};
});

describe('invokeFunction tests', () => {
afterEach(() => {
Expand All @@ -59,14 +57,14 @@ describe('invokeFunction tests', () => {

// Success cases
test('Inactive function that reactivates does not throw error', async () => {
mockInvoke = async () => {
mockInvoke.mockImplementation(async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
return { Payload: req.Payload } as InvocationResponse;
});

expectedFunctionStates.push('Active');
expectedFunctionStates.push('Pending');
Expand All @@ -77,14 +75,14 @@ describe('invokeFunction tests', () => {
});

test('Active function does not run waitFor or retry invoke', async () => {
mockInvoke = async () => {
mockInvoke.mockImplementation(async () => {
if (invokeCount == 1) {
invokeCount++;
throw new Error('This should not happen in this test');
}
invokeCount++;
return { Payload: req.Payload };
};
return { Payload: req.Payload } as InvocationResponse;
});

expectedFunctionStates.push('Active');

Expand All @@ -95,14 +93,14 @@ describe('invokeFunction tests', () => {

// Failure cases
test('Inactive function that goes back to inactive throws error', async () => {
mockInvoke = async () => {
mockInvoke.mockImplementation(async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
return { Payload: req.Payload } as InvocationResponse;
});

expectedFunctionStates.push('Inactive');
expectedFunctionStates.push('Pending');
Expand All @@ -114,14 +112,14 @@ describe('invokeFunction tests', () => {
});

test('Inactive function that goes to failed throws error', async () => {
mockInvoke = async () => {
mockInvoke.mockImplementation(async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
return { Payload: req.Payload } as InvocationResponse;
});

expectedFunctionStates.push('Failed');
expectedFunctionStates.push('Pending');
Expand All @@ -133,14 +131,14 @@ describe('invokeFunction tests', () => {
});

test('Inactive function that returns other value throws error', async () => {
mockInvoke = async () => {
mockInvoke.mockImplementation(async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
return { Payload: req.Payload } as InvocationResponse;
});

expectedFunctionStates.push('NewFunctionWhoDis');
expectedFunctionStates.push('Pending');
Expand All @@ -152,14 +150,14 @@ describe('invokeFunction tests', () => {
});

test('Wait for stops on terminal responses', async () => {
mockInvoke = async () => {
mockInvoke.mockImplementation(async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
return { Payload: req.Payload } as InvocationResponse;
});

expectedFunctionStates.push('SomethingElse');
expectedFunctionStates.push('Pending');
Expand Down
Loading