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

feat: basic e2e integration test flow for migration tool #13946

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'packages/amplify-e2e-core/',
'packages/amplify-e2e-tests/',
'packages/amplify-console-integration-tests/',
'packages/amplify-migration-codegen-e2e/src/__tests__',
'packages/graphql-transformers-e2e-tests/',
'packages/amplify-util-mock/src/__e2e__/',
'packages/amplify-ui-tests/',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"coverage": "codecov || exit 0",
"deep-clean-install": "yarn verdaccio-clean && yarn cache clean && yarn clean && git clean -fdx -e .vscode -e .env && yarn",
"e2e": "lerna run e2e",
"e2e-migration": "lerna run e2e-migration",
"extract-api": "lerna run extract-api --concurrency 4",
"extract-formatting-changes": "yarn ts-node ./scripts/extract-formatting-changes.ts",
"finish-release": "ts-node ./scripts/finish-release.ts",
Expand Down
44 changes: 44 additions & 0 deletions packages/amplify-migration-codegen-e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
preset: 'ts-jest',
verbose: false,
testRunner: '@aws-amplify/amplify-e2e-core/runner',
testEnvironment: '@aws-amplify/amplify-e2e-core/environment',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: false,
},
],
},
testEnvironmentOptions: {
url: 'http://localhost',
},
testRegex: '(src/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$',
collectCoverage: false,
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/__tests__/**', '!src/**/*.test.(ts|tsx|js|jsx)$', '!**/*.d.ts'],
reporters: [
'default',
'jest-junit',
[
'@aws-amplify/amplify-e2e-core/reporter',
{
publicPath: './amplify-migration-e2e-reports',
filename: 'index.html',
expand: true,
},
],
[
'@aws-amplify/amplify-e2e-core/failed-test-reporter',
{
reportPath: './amplify-migration-e2e-reports/amplify-migration-e2e-failed-test.txt',
},
],
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
setupFilesAfterEnv: ['<rootDir>/src/setup-tests.ts'],
moduleNameMapper: {
'^uuid$': require.resolve('uuid'),
'^yaml$': require.resolve('yaml'),
},
};
11 changes: 9 additions & 2 deletions packages/amplify-migration-codegen-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
"version": "0.1.0-gen2-migration-test-alpha.0",
"main": "index.js",
"dependencies": {
"@aws-amplify/amplify-gen2-codegen": "0.1.0-gen2-migration-test-alpha.0"
"@aws-amplify/amplify-cli-core": "4.4.0-gen2-migration-test-alpha.0",
"@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",
"lodash": "^4.17.21"
},
"devDependencies": {
"jest": "^29.5.0",
"ts-node": "^10.4.0",
"typescript": "^5.4.5"
},
"jest": {
Expand Down Expand Up @@ -39,7 +45,8 @@
"scripts": {
"build": "exit 0",
"pretest": "mkdir -p coverage",
"test": "jest --logHeapUsage"
"e2e-migration": "yarn setup-profile && jest --verbose --config=jest.config.js",
"setup-profile": "ts-node ./src/configure_tests.ts"
},
"author": "",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from 'node:path';
import assert from 'node:assert';
import { createNewProjectDir } from '@aws-amplify/amplify-e2e-core';
import {
cleanupProjects,
setupAndPushGen1Project,
assertGen1Setup,
runCodegenCommand,
runGen2SandboxCommand,
assertUserPoolResource,
} from '../helpers';

void describe('Migration Codegen E2E tests', () => {
let projRoot: string;
beforeEach(async () => {
const baseDir = process.env.INIT_CWD ?? process.cwd();
projRoot = await createNewProjectDir('codegen_e2e_flow_test', path.join(baseDir, '..', '..'));
});

afterEach(async () => {
await cleanupProjects(projRoot);
});

void it('performs full migration codegen flow with Auth backend', async () => {
await setupAndPushGen1Project(projRoot, 'CodegenTest');
const { gen1UserPoolId, gen1Region } = await assertGen1Setup(projRoot);
await assert.doesNotReject(runCodegenCommand(projRoot), 'Codegen failed');
await assert.doesNotReject(runGen2SandboxCommand(projRoot), 'Gen2 CDK deployment failed');
await assertUserPoolResource(projRoot, gen1UserPoolId, gen1Region);
});
});
33 changes: 33 additions & 0 deletions packages/amplify-migration-codegen-e2e/src/configure_tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { amplifyConfigure as configure, injectSessionToken, isCI } from '@aws-amplify/amplify-e2e-core';

async function setupAmplify() {
if (isCI()) {
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
const REGION = process.env.CLI_REGION;
if (!AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !REGION) {
throw new Error('Please set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and CLI_REGION in .env');
}
await configure(null, {
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
profileName: 'amplify-integ-test-user',
region: REGION,
});
if (process.env.AWS_SESSION_TOKEN) {
injectSessionToken('amplify-integ-test-user');
}
} else {
console.log('AWS Profile is already configured');
}
}

process.nextTick(async () => {
try {
await setupAmplify();
} catch (e) {
console.log(e.stack);
process.exit(1);
}
process.exit();
});
124 changes: 124 additions & 0 deletions packages/amplify-migration-codegen-e2e/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import {
deleteProject as deleteGen1Project,
deleteProjectDir,
initJSProjectWithProfile,
addAuthWithDefault,
amplifyPushAuth,
getProjectMeta,
getUserPool,
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 { CloudControlClient, GetResourceCommand } from '@aws-sdk/client-cloudcontrol';
import { unset } from 'lodash';

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

async function getResourceDetails(typeName: string, identifier: string, region: string) {
const client = new CloudControlClient({ region });
const command = new GetResourceCommand({
TypeName: typeName,
Identifier: identifier,
});
const response = await client.send(command);
return JSON.parse(response.ResourceDescription.Properties);
}

export function runGen2SandboxCommand(cwd: string) {
npmInstall(cwd);
return spawn(getNpxPath(), ['ampx', 'sandbox', '--once'], {
cwd,
stripColors: true,
noOutputTimeout: pushTimeoutMS,
env: { ...process.env, npm_config_user_agent: 'npm' },
}).runAsync();
}

export function runCodegenCommand(cwd: string) {
return spawn(getNpxPath(), ['@aws-amplify/migrate', 'to-gen-2', 'generate-code'], {
cwd,
stripColors: true,
noOutputTimeout: pushTimeoutMS,
env: { ...process.env, npm_config_user_agent: 'npm' },
}).runAsync();
}

function deleteGen2Sandbox(cwd: string) {
return spawn(getNpxPath(), ['ampx', 'sandbox', 'delete'], {
cwd,
stripColors: true,
noOutputTimeout: pushTimeoutMS,
env: { ...process.env, npm_config_user_agent: 'npm' },
})
.wait("Are you sure you want to delete all the resources in your sandbox environment (This can't be undone)?")
.sendConfirmYes()
.wait('Finished deleting.')
.runAsync();
}

export async function setupAndPushGen1Project(projRoot: string, projectName: string) {
await initJSProjectWithProfile(projRoot, { name: projectName, disableAmplifyAppCreation: false });
await addAuthWithDefault(projRoot);
await amplifyPushAuth(projRoot);
}

export async function assertGen1Setup(projRoot: string) {
const gen1Meta = getProjectMeta(projRoot);
const gen1UserPoolId = Object.keys(gen1Meta.auth).map((key) => gen1Meta.auth[key])[0].output.UserPoolId;
const gen1Region = gen1Meta.providers.awscloudformation.Region;
const userPool = await getUserPool(gen1UserPoolId, gen1Region);
expect(userPool.UserPool).toBeDefined();
return { gen1UserPoolId, gen1Region };
}

export async function assertUserPoolResource(projRoot: string, gen1UserPoolId: string, gen1Region: string) {
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
Sanayshah2 marked this conversation as resolved.
Show resolved Hide resolved
removeProperties(gen1Resource, [
'UserPoolTags',
'VerificationMessageTemplate.EmailMessage',
'VerificationMessageTemplate.EmailSubject',
'EmailVerificationSubject',
'AccountRecoverySetting',
'EmailVerificationMessage',
]);
const gen2Meta = getProjectOutputs(projRoot);
const gen2UserPoolId = gen2Meta.auth.user_pool_id;
const gen2Region = gen2Meta.auth.aws_region;
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
removeProperties(gen2Resource, [
'UserPoolTags',
'VerificationMessageTemplate.EmailMessage',
'VerificationMessageTemplate.SmsMessage',
'VerificationMessageTemplate.EmailSubject',
'SmsVerificationMessage',
'EmailVerificationSubject',
'AccountRecoverySetting',
'EmailVerificationMessage',
]);
expect(gen2Resource).toEqual(gen1Resource);
}

function removeProperties(obj: Record<string, unknown>, propertiesToRemove: string[]) {
propertiesToRemove.forEach((prop) => unset(obj, prop));
}

const getProjectOutputsPath = (projectRoot: string) => path.join(projectRoot, 'amplify_outputs.json');

const getProjectOutputs = (projectRoot: string): $TSAny => {
const metaFilePath: string = getProjectOutputsPath(projectRoot);
return JSON.parse(fs.readFileSync(metaFilePath, 'utf8'));
};

export async function cleanupProjects(cwd: string) {
await deleteGen1Project(path.join(cwd, '.amplify', 'migration'));
await deleteGen2Sandbox(cwd);
deleteProjectDir(cwd);
}
24 changes: 24 additions & 0 deletions packages/amplify-migration-codegen-e2e/src/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const removeYarnPaths = () => {
// Remove yarn's temporary PATH modifications as they affect the yarn version used by jest tests when building the lambda functions
process.env.PATH = process.env.PATH.split(':')
.filter((p) => !p.includes('/xfs-') && !p.includes('\\Temp\\xfs-'))
.join(':');
};

removeYarnPaths();

const JEST_TIMEOUT = 1000 * 60 * 60; // 1 hour
jest.setTimeout(JEST_TIMEOUT);
if (process.env.CIRCLECI) {
jest.retryTimes(1);
}

beforeEach(async () => {
if (process.env.CLI_REGION) {
console.log(`CLI_REGION set to: ${process.env.CLI_REGION}. Overwriting AWS_REGION and AWS_DEFAULT_REGION`);
process.env.AWS_REGION = process.env.CLI_REGION;
process.env.AWS_DEFAULT_REGION = process.env.CLI_REGION;
} else {
console.log('No CLI_REGION variable found');
}
});
Loading
Loading