Skip to content

Commit

Permalink
fix: modify function names and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanay Yogesh Shah committed Oct 1, 2024
1 parent 2aa0a1c commit fbc7f7a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 56 deletions.
3 changes: 2 additions & 1 deletion packages/amplify-migration-codegen-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@aws-amplify/amplify-e2e-core": "5.5.11-gen2-migration-test-alpha.0",
"@aws-amplify/amplify-gen2-codegen": "0.1.0-gen2-migration-test-alpha.0",
"@aws-sdk/client-cloudcontrol": "^3.658.1",
"fs-extra": "^8.1.0"
"fs-extra": "^8.1.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"jest": "^29.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import path from 'node:path';
import assert from 'node:assert';
import { createNewProjectDir } from '@aws-amplify/amplify-e2e-core';
import { cleanupProjects, setupGen1Project, assertGen1Setup, migrateCodegen, deployGen2Sandbox, assertUserPoolResource } from '../helpers';
import {
cleanupProjects,
setupAndPushGen1Project,
assertGen1Setup,
runCodegenCommand,
runGen2SandboxCommand,
assertUserPoolResource,
} from '../helpers';

void describe('Migration Codegen E2E tests', () => {
let projRoot: string;
Expand All @@ -14,10 +22,10 @@ void describe('Migration Codegen E2E tests', () => {
});

void it('performs full migration codegen flow with Auth backend', async () => {
await setupGen1Project(projRoot, 'CodegenTest');
await setupAndPushGen1Project(projRoot, 'CodegenTest');
const { gen1UserPoolId, gen1Region } = await assertGen1Setup(projRoot);
await migrateCodegen(projRoot);
await deployGen2Sandbox(projRoot);
await assert.doesNotReject(runCodegenCommand(projRoot), 'Codegen failed');
await assert.doesNotReject(runGen2SandboxCommand(projRoot), 'Gen2 CDK deployment failed');
await assertUserPoolResource(projRoot, gen1UserPoolId, gen1Region);
});
});
69 changes: 18 additions & 51 deletions packages/amplify-migration-codegen-e2e/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
amplifyPushAuth,
getProjectMeta,
getUserPool,
npmInstall,
getNpxPath,
nspawn as spawn,
} from '@aws-amplify/amplify-e2e-core';
import { npmInstall, getNpxPath, nspawn as spawn } from '@aws-amplify/amplify-e2e-core';
import * as fs from 'fs-extra';
import { $TSAny } from '@aws-amplify/amplify-cli-core';
import path from 'node:path';
import assert from 'node:assert';
import { CloudControlClient, GetResourceCommand } from '@aws-sdk/client-cloudcontrol';
import { unset } from 'lodash';

const pushTimeoutMS = 1000 * 60 * 20; // 20 minutes;

Expand All @@ -22,23 +24,12 @@ async function getResourceDetails(typeName: string, identifier: string, region:
TypeName: typeName,
Identifier: identifier,
});

try {
const response = await client.send(command);
return JSON.parse(response.ResourceDescription.Properties);
} catch (error) {
console.error('Error fetching resource details:', error);
throw error;
}
const response = await client.send(command);
return JSON.parse(response.ResourceDescription.Properties);
}

function runGen2SandboxCommand(cwd: string) {
try {
npmInstall(cwd);
} catch (error) {
console.error('Failed to install dependencies:', error);
throw error;
}
export function runGen2SandboxCommand(cwd: string) {
npmInstall(cwd);
return spawn(getNpxPath(), ['ampx', 'sandbox', '--once'], {
cwd,
stripColors: true,
Expand All @@ -47,7 +38,7 @@ function runGen2SandboxCommand(cwd: string) {
}).runAsync();
}

function runCodegenCommand(cwd: string) {
export function runCodegenCommand(cwd: string) {
return spawn(getNpxPath(), ['@aws-amplify/migrate', 'to-gen-2', 'generate-code'], {
cwd,
stripColors: true,
Expand All @@ -69,7 +60,7 @@ function deleteGen2Sandbox(cwd: string) {
.runAsync();
}

export async function setupGen1Project(projRoot: string, projectName: string) {
export async function setupAndPushGen1Project(projRoot: string, projectName: string) {
await initJSProjectWithProfile(projRoot, { name: projectName, disableAmplifyAppCreation: false });
await addAuthWithDefault(projRoot);
await amplifyPushAuth(projRoot);
Expand All @@ -85,10 +76,10 @@ export async function assertGen1Setup(projRoot: string) {
}

export async function assertUserPoolResource(projRoot: string, gen1UserPoolId: string, gen1Region: string) {
let gen1Resource = await getResourceDetails('AWS::Cognito::UserPool', gen1UserPoolId, gen1Region);
gen1Resource = removeNestedJsonKeys(gen1Resource, ['ProviderURL', 'ProviderName', 'UserPoolId', 'Arn']);
const gen1Resource = await getResourceDetails('AWS::Cognito::UserPool', gen1UserPoolId, gen1Region);
removeProperties(gen1Resource, ['ProviderURL', 'ProviderName', 'UserPoolId', 'Arn']);
// TODO: remove below line after EmailMessage, EmailSubject, SmsMessage, SmsVerificationMessage, EmailVerificationMessage, EmailVerificationSubject, AccountRecoverySetting inconsistency is fixed
gen1Resource = removeNestedJsonKeys(gen1Resource, [
removeProperties(gen1Resource, [
'UserPoolTags',
'VerificationMessageTemplate.EmailMessage',
'VerificationMessageTemplate.EmailSubject',
Expand All @@ -99,10 +90,10 @@ export async function assertUserPoolResource(projRoot: string, gen1UserPoolId: s
const gen2Meta = getProjectOutputs(projRoot);
const gen2UserPoolId = gen2Meta.auth.user_pool_id;
const gen2Region = gen2Meta.auth.aws_region;
let gen2Resource = await getResourceDetails('AWS::Cognito::UserPool', gen2UserPoolId, gen2Region);
gen2Resource = removeNestedJsonKeys(gen2Resource, ['ProviderURL', 'ProviderName', 'UserPoolId', 'Arn']);
const gen2Resource = await getResourceDetails('AWS::Cognito::UserPool', gen2UserPoolId, gen2Region);
removeProperties(gen2Resource, ['ProviderURL', 'ProviderName', 'UserPoolId', 'Arn']);
// TODO: remove below line after EmailMessage, EmailSubject, SmsMessage, SmsVerificationMessage, EmailVerificationMessage, EmailVerificationSubject, AccountRecoverySetting inconsistency is fixed
gen2Resource = removeNestedJsonKeys(gen2Resource, [
removeProperties(gen2Resource, [
'UserPoolTags',
'VerificationMessageTemplate.EmailMessage',
'VerificationMessageTemplate.SmsMessage',
Expand All @@ -115,24 +106,8 @@ export async function assertUserPoolResource(projRoot: string, gen1UserPoolId: s
expect(gen2Resource).toEqual(gen1Resource);
}

function removeNestedJsonKeys<T extends Record<string, unknown>>(obj: T, keysToRemove: string[]): T {
const result = { ...obj };
keysToRemove.forEach((path) => {
const parts = path.split('.');
let current: Record<string, unknown> = result;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (current[part] === undefined || current[part] === null) {
return; // Path doesn't exist, nothing to delete
}
current = current[part] as Record<string, unknown>;
}
const lastPart = parts[parts.length - 1];
if (current && lastPart in current) {
delete current[lastPart];
}
});
return result;
function removeProperties(obj: Record<string, unknown>, propertiesToRemove: string[]) {
propertiesToRemove.forEach((prop) => unset(obj, prop));
}

const getProjectOutputsPath = (projectRoot: string) => path.join(projectRoot, 'amplify_outputs.json');
Expand All @@ -142,14 +117,6 @@ const getProjectOutputs = (projectRoot: string): $TSAny => {
return JSON.parse(fs.readFileSync(metaFilePath, 'utf8'));
};

export async function migrateCodegen(projRoot: string) {
await assert.doesNotReject(runCodegenCommand(projRoot), 'Codegen failed');
}

export async function deployGen2Sandbox(projRoot: string) {
await assert.doesNotReject(runGen2SandboxCommand(projRoot), 'Gen2 CDK deployment failed');
}

export async function cleanupProjects(cwd: string) {
await deleteGen1Project(path.join(cwd, '.amplify', 'migration'));
await deleteGen2Sandbox(cwd);
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ __metadata:
"@aws-sdk/client-cloudcontrol": ^3.658.1
fs-extra: ^8.1.0
jest: ^29.5.0
lodash: ^4.17.21
ts-node: ^10.4.0
typescript: ^5.4.5
languageName: unknown
Expand Down

0 comments on commit fbc7f7a

Please sign in to comment.