Skip to content

Commit

Permalink
sdk-v2-to-v3 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Sep 5, 2023
1 parent b6b971e commit cf6e3e0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/@aws-cdk/sdk-v2-to-v3-adapter/lib/sdk-v3-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ interface SdkV3Package {
}

export function loadV3ClientPackage(service: string): SdkV3Package {
const packageName = getV3ClientPackageName(service);
const packageName = service.startsWith('@aws-sdk/') ? service : getV3ClientPackageName(service);

try {
/* eslint-disable-next-line @typescript-eslint/no-require-imports */
const pkg = require(packageName);
/* eslint-disable-next-line @typescript-eslint/no-require-imports */
const { version } = require(packageName + '/package.json');

return {
service,
service: packageName.replace('@aws-sdk/client-', ''),
pkg,
packageName,
packageVersion: version,
Expand All @@ -27,7 +28,7 @@ export function loadV3ClientPackage(service: string): SdkV3Package {
}
}

export function getV3Client(sdkPkg: SdkV3Package, clientOptions: any): any {
export function getV3Client(sdkPkg: SdkV3Package, clientOptions: any = {}): any {
try {
const ServiceClient = findV3ClientConstructor(sdkPkg.pkg);
return new ServiceClient(clientOptions);
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/sdk-v2-to-v3-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"devDependencies": {
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@aws-sdk/client-s3": "^3.400.0",
"@types/jest": "^29.5.4",
"jest": "^29.6.4"
},
Expand Down
56 changes: 56 additions & 0 deletions packages/@aws-cdk/sdk-v2-to-v3-adapter/test/sdk-v3-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { getV3Client, getV3Command, loadV3ClientPackage } from '../lib';

describe('helpers for SDKv3', () => {
test('can load a SDK package by service name', () => {
const sdk = loadV3ClientPackage('S3');

expect(sdk.packageName).toBe('@aws-sdk/client-s3');
expect(sdk.service).toBe('s3');
expect(sdk.packageVersion).toMatch(/^3\..*/);
});

test('can load a SDK package by package name', () => {
const sdk = loadV3ClientPackage('@aws-sdk/client-s3');

expect(sdk.packageName).toBe('@aws-sdk/client-s3');
expect(sdk.service).toBe('s3');
expect(sdk.packageVersion).toMatch(/^3\..*/);
});

test('will throw when attempting to load unknown SDK package', () => {
expect(() => {
loadV3ClientPackage('@aws-sdk/client-foobar');
}).toThrow("Service @aws-sdk/client-foobar client package with name '@aws-sdk/client-foobar' does not exist.");
});

test('will throw when attempting to load unknown SDK package using V2 style name', () => {
expect(() => {
loadV3ClientPackage('FooBar');
}).toThrow("Client 'FooBar' is either deprecated or newly added.");
});

describe('with a SDK package loaded', () => {
const sdk = loadV3ClientPackage('@aws-sdk/client-s3');

test('can get client', () => {
const client = getV3Client(sdk);
expect(client.config.serviceId).toBe('S3');
});

test('can get client with config', async () => {
const client = getV3Client(sdk, { region: 'eu-west-1' });
const region = await client.config.region();
expect(region).toBe('eu-west-1');
});

test('can get command', () => {
const command = getV3Command(sdk, 'ListBuckets');
expect(command).toBeDefined();
});
test('will throw when attempting to get unknown command', () => {
expect(() => {
getV3Command(sdk, 'FooBar');
}).toThrow('Unable to find command named: FooBarCommand');
});
});
});

0 comments on commit cf6e3e0

Please sign in to comment.