-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: templategen command e2e integration tests
- Loading branch information
Sanay Yogesh Shah
committed
Oct 23, 2024
1 parent
ec9a9f6
commit 8f2ede1
Showing
13 changed files
with
1,005 additions
and
39 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
50 changes: 50 additions & 0 deletions
50
packages/amplify-migration-e2e/src/__tests__/migration_templategen_e2e.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,50 @@ | ||
import path from 'node:path'; | ||
import assert from 'node:assert'; | ||
import { createNewProjectDir, npmInstall, deleteS3Bucket } from '@aws-amplify/amplify-e2e-core'; | ||
import { assertDefaultGen1Setup } from '../assertions'; | ||
import { setupAndPushDefaultGen1Project, runCodegenCommand, runGen2SandboxCommand, cleanupProjects } from '..'; | ||
import { copyFunctionFile } from '../function_utils'; | ||
import { copyGen1Schema } from '../api_utils'; | ||
import { updatePackageDependency } from '../updatePackageJson'; | ||
import { createS3Bucket } from '../sdk_calls'; | ||
import { runTemplategenCommand, stackRefactor } from '../templategen'; | ||
|
||
void describe('Templategen E2E tests', () => { | ||
void describe('Full Migration Templategen Flow', () => { | ||
let projRoot: string; | ||
let projName: string; | ||
let bucketName: string; | ||
|
||
beforeEach(async () => { | ||
const baseDir = process.env.INIT_CWD ?? process.cwd(); | ||
projRoot = await createNewProjectDir('templategen_e2e_flow_test', path.join(baseDir, '..', '..')); | ||
projName = `test${Math.floor(Math.random() * 1000000)}`; | ||
bucketName = `testbucket${Math.floor(Math.random() * 1000000)}`; | ||
}); | ||
|
||
afterEach(async () => { | ||
await cleanupProjects(projRoot); | ||
await deleteS3Bucket(bucketName); | ||
}); | ||
|
||
void it('should init a project & add auth, function, storage, api with defaults & perform full migration templategen flow', async () => { | ||
await setupAndPushDefaultGen1Project(projRoot, projName); | ||
const { gen1StackName, gen1FunctionName, gen1Region } = await assertDefaultGen1Setup(projRoot); | ||
await createS3Bucket(bucketName, gen1Region); | ||
assert(gen1StackName); | ||
await runCodegenCommand(projRoot); | ||
await copyFunctionFile(projRoot, 'function', gen1FunctionName); | ||
await copyGen1Schema(projRoot, projName); | ||
|
||
// TODO: replace below line with correct package version | ||
await updatePackageDependency(projRoot, '@aws-amplify/backend'); | ||
|
||
await npmInstall(projRoot); | ||
const gen2StackName = await runGen2SandboxCommand(projRoot); | ||
assert(gen2StackName); | ||
await runTemplategenCommand(projRoot, gen1StackName, gen2StackName); | ||
// await stackRefactor(projRoot, 'auth', bucketName); | ||
await stackRefactor(projRoot, 'storage', bucketName); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type EnvVariableAction = 'SET' | 'DELETE'; | ||
|
||
export function toggleEnvVariable(name: string, option: EnvVariableAction, value?: string) { | ||
if (option === 'SET') { | ||
process.env[name] = value; | ||
} else if (option === 'DELETE') { | ||
delete process.env[name]; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/amplify-migration-e2e/src/gen1ResourceDetailsFetcher.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,42 @@ | ||
import path from 'path'; | ||
import { RefactorCategory } from './templategen'; | ||
import { getProjectMeta } from '@aws-amplify/amplify-e2e-core'; | ||
import { assertIdentityPool, assertStorage, assertUserPool, assertUserPoolClients } from './assertions'; | ||
import { getResourceDetails } from './sdk_calls'; | ||
|
||
async function getGen1AuthResourceDetails(projRoot: string) { | ||
const gen1ProjRoot = path.join(projRoot, '.amplify', 'migration'); | ||
const gen1Meta = getProjectMeta(gen1ProjRoot); | ||
const gen1Region = gen1Meta.providers.awscloudformation.Region; | ||
const { gen1UserPoolId } = await assertUserPool(gen1Meta, gen1Region); | ||
const { gen1IdentityPoolId } = await assertIdentityPool(gen1Meta, gen1Region); | ||
const { gen1ClientIds } = await assertUserPoolClients(gen1Meta, gen1Region); | ||
const [gen1ClientIdWeb, gen1ClientId] = gen1ClientIds; | ||
Check notice Code scanning / CodeQL Unused variable, import, function or class Note
Unused variable gen1ClientId.
|
||
const gen1ResourceIds = [gen1UserPoolId, gen1IdentityPoolId, gen1ClientIdWeb]; | ||
|
||
const gen1ResourceDetails = await Promise.all([ | ||
getResourceDetails('AWS::Cognito::UserPool', gen1UserPoolId, gen1Region), | ||
getResourceDetails('AWS::Cognito::IdentityPool', gen1IdentityPoolId, gen1Region), | ||
getResourceDetails('AWS::Cognito::UserPoolClient', `${gen1UserPoolId}|${gen1ClientIdWeb}`, gen1Region), | ||
]); | ||
|
||
return { gen1ResourceIds, gen1ResourceDetails }; | ||
} | ||
|
||
async function getGen1StorageResourceDetails(projRoot: string) { | ||
const gen1ProjRoot = path.join(projRoot, '.amplify', 'migration'); | ||
const gen1Meta = getProjectMeta(gen1ProjRoot); | ||
const gen1Region = gen1Meta.providers.awscloudformation.Region; | ||
const { gen1BucketName } = await assertStorage(gen1Meta, gen1Region); | ||
const gen1ResourceIds = [gen1BucketName]; | ||
const gen1ResourceDetails = await getResourceDetails('AWS::S3::Bucket', gen1BucketName, gen1Region); | ||
return { gen1ResourceIds, gen1ResourceDetails }; | ||
} | ||
|
||
export async function getGen1ResourceDetails(projRoot: string, category: RefactorCategory) { | ||
if (category === 'auth') { | ||
return await getGen1AuthResourceDetails(projRoot); | ||
} else { | ||
return await getGen1StorageResourceDetails(projRoot); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/amplify-migration-e2e/src/gen2ResourceDetailsFetcher.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,37 @@ | ||
import { getProjectOutputs } from './projectOutputs'; | ||
import { getResourceDetails } from './sdk_calls'; | ||
import { RefactorCategory } from './templategen'; | ||
|
||
async function getGen2AuthResourceDetails(projRoot: string) { | ||
const gen2Meta = getProjectOutputs(projRoot); | ||
const gen2Region = gen2Meta.auth.aws_region; | ||
const gen2UserPoolId = gen2Meta.auth.user_pool_id; | ||
const gen2ClientIdWeb = gen2Meta.auth.user_pool_client_id; | ||
const gen2IdentityPoolId = gen2Meta.auth.identity_pool_id; | ||
const gen2ResourceIds = [gen2UserPoolId, gen2IdentityPoolId, gen2ClientIdWeb]; | ||
|
||
const gen2ResourceDetails = await Promise.all([ | ||
getResourceDetails('AWS::Cognito::UserPool', gen2UserPoolId, gen2Region), | ||
getResourceDetails('AWS::Cognito::IdentityPool', gen2IdentityPoolId, gen2Region), | ||
getResourceDetails('AWS::Cognito::UserPoolClient', `${gen2UserPoolId}|${gen2ClientIdWeb}`, gen2Region), | ||
]); | ||
|
||
return { gen2ResourceIds, gen2ResourceDetails }; | ||
} | ||
|
||
async function getGen2StorageResourceDetails(projRoot: string) { | ||
const gen2Meta = getProjectOutputs(projRoot); | ||
const gen2Region = gen2Meta.auth.aws_region; | ||
const gen2BucketName = gen2Meta.storage.bucket_name; | ||
const gen2ResourceIds = [gen2BucketName]; | ||
const gen2ResourceDetails = await getResourceDetails('AWS::S3::Bucket', gen2BucketName, gen2Region); | ||
return { gen2ResourceIds, gen2ResourceDetails }; | ||
} | ||
|
||
export async function getGen2ResourceDetails(projRoot: string, category: RefactorCategory) { | ||
if (category === 'auth') { | ||
return await getGen2AuthResourceDetails(projRoot); | ||
} else { | ||
return await getGen2StorageResourceDetails(projRoot); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
packages/amplify-migration-e2e/src/migrationReadmeParser.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,35 @@ | ||
function extractContent(readmeContent: string, startRegex: string, endRegex: string) { | ||
const pattern = new RegExp(`${startRegex}([\\s\\S]*?)${endRegex}`, 'i'); | ||
const match = readmeContent.match(pattern); | ||
|
||
if (match && match[1]) { | ||
return match[1].trim(); | ||
} | ||
throw new Error('README file parsing failed to get the stack refactor commands'); | ||
} | ||
|
||
function extractCommands(readmeContent: string) { | ||
const pattern = /```([\s\S]*?)```/g; | ||
const matches = readmeContent.matchAll(pattern); | ||
const commands = []; | ||
|
||
for (const match of matches) { | ||
if (match[1]) { | ||
commands.push(match[1].trim()); | ||
} | ||
} | ||
if (commands.length === 0) { | ||
throw new Error('README file parsing failed to get the stack refactor commands'); | ||
} | ||
return commands; | ||
} | ||
|
||
export function getCommandsFromReadme(readmeContent: string) { | ||
const step1Content = extractContent(readmeContent, '### STEP 1', '#### Rollback step'); | ||
const step2Content = extractContent(readmeContent, '### STEP 2', '#### Rollback step'); | ||
const step3Content = extractContent(readmeContent, '### STEP 3', '#### Rollback step'); | ||
const step1Commands = extractCommands(step1Content); | ||
const step2commands = extractCommands(step2Content); | ||
const step3Commands = extractCommands(step3Content); | ||
return { step1Commands, step2commands, step3Commands }; | ||
} |
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,34 @@ | ||
import { getNpxPath, nspawn as spawn } from '@aws-amplify/amplify-e2e-core'; | ||
import { pushTimeoutMS } from '.'; | ||
import execa from 'execa'; | ||
|
||
export async function runGen2SandboxCommand(cwd: string) { | ||
const processResult = execa.sync(getNpxPath(), ['ampx', 'sandbox', '--once'], { | ||
cwd, | ||
env: { ...process.env, npm_config_user_agent: 'npm' }, | ||
encoding: 'utf-8', | ||
}); | ||
if (processResult.exitCode === 0) { | ||
const match = processResult.stdout.match(/arn:aws:cloudformation:.*:stack\/([^/]+)\//); | ||
if (match) { | ||
return match[1]; | ||
} else { | ||
throw new Error('Stack name not found in the command output'); | ||
} | ||
} else { | ||
throw new Error(`Sandbox command exit code: ${processResult.exitCode}, message: ${processResult.stderr}`); | ||
} | ||
} | ||
|
||
export 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(); | ||
} |
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
Oops, something went wrong.