Skip to content

Commit 1fc5dda

Browse files
chore(migrate): add integ tests with app deployment (#27509)
> REPLACE THIS TEXT BLOCK > > Describe the reason for this change, what the solution is, and any > important design decisions you made. > > Remember to follow the [CONTRIBUTING GUIDE] and [DESIGN GUIDELINES] for any > code you submit. > > [CONTRIBUTING GUIDE]: https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md > [DESIGN GUIDELINES]: https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md Closes #<issue number here>. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 71b9737 commit 1fc5dda

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts

+63-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,50 @@ export function withCdkApp(
8484
};
8585
}
8686

87+
export function withCdkMigrateApp<A extends TestContext>(language: string, block: (context: TestFixture) => Promise<void>) {
88+
return async (context: A) => {
89+
const stackName = `cdk-migrate-${language}-integ-${context.randomString}`;
90+
const integTestDir = path.join(os.tmpdir(), `cdk-migrate-${language}-integ-${context.randomString}`);
91+
92+
context.output.write(` Stack name: ${stackName}\n`);
93+
context.output.write(` Test directory: ${integTestDir}\n`);
94+
95+
const awsClients = await AwsClients.default(context.output);
96+
fs.mkdirSync(integTestDir);
97+
const fixture = new TestFixture(
98+
integTestDir,
99+
stackName,
100+
context.output,
101+
awsClients,
102+
context.randomString,
103+
);
104+
105+
await fixture.cdkMigrate(language, stackName);
106+
107+
const testFixture = new TestFixture(
108+
path.join(integTestDir, stackName),
109+
stackName,
110+
context.output,
111+
awsClients,
112+
context.randomString,
113+
);
114+
115+
let success = true;
116+
try {
117+
await block(testFixture);
118+
} catch (e) {
119+
success = false;
120+
throw e;
121+
} finally {
122+
if (process.env.INTEG_NO_CLEAN) {
123+
context.log(`Left test directory in '${integTestDir}' ($INTEG_NO_CLEAN)`);
124+
} else {
125+
await fixture.dispose(success);
126+
}
127+
}
128+
};
129+
}
130+
87131
export function withMonolithicCfnIncludeCdkApp<A extends TestContext>(block: (context: TestFixture) => Promise<void>) {
88132
return async (context: A) => {
89133
const uberPackage = process.env.UBERPACKAGE;
@@ -141,6 +185,10 @@ export function withDefaultFixture(block: (context: TestFixture) => Promise<void
141185
return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withCdkApp(block)));
142186
}
143187

188+
export function withCDKMigrateFixture(language: string, block: (content: TestFixture) => Promise<void>) {
189+
return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withCdkMigrateApp(language, block)));
190+
}
191+
144192
export interface DisableBootstrapContext {
145193
/**
146194
* Whether to disable creating the default bootstrap
@@ -270,7 +318,7 @@ export class TestFixture extends ShellHelper {
270318
this.output.write(`${s}\n`);
271319
}
272320

273-
public async cdkDeploy(stackNames: string | string[], options: CdkCliOptions = {}) {
321+
public async cdkDeploy(stackNames: string | string[], options: CdkCliOptions = {}, skipStackRename?: boolean) {
274322
stackNames = typeof stackNames === 'string' ? [stackNames] : stackNames;
275323

276324
const neverRequireApproval = options.neverRequireApproval ?? true;
@@ -280,7 +328,7 @@ export class TestFixture extends ShellHelper {
280328
...(options.options ?? []),
281329
// use events because bar renders bad in tests
282330
'--progress', 'events',
283-
...this.fullStackName(stackNames)], options);
331+
...(skipStackRename ? stackNames : this.fullStackName(stackNames))], options);
284332
}
285333

286334
public async cdkSynth(options: CdkCliOptions = {}) {
@@ -379,6 +427,19 @@ export class TestFixture extends ShellHelper {
379427
});
380428
}
381429

430+
public async cdkMigrate(language: string, stackName: string, inputPath?: string, options?: CdkCliOptions) {
431+
return this.cdk([
432+
'migrate',
433+
'--language',
434+
language,
435+
'--stack-name',
436+
stackName,
437+
'--from-path',
438+
inputPath ?? path.join(__dirname, '..', 'resources', 'templates', 'sqs-template.json').toString(),
439+
...(options?.options ?? []),
440+
], options);
441+
}
442+
382443
public async cdk(args: string[], options: CdkCliOptions = {}) {
383444
const verbose = options.verbose ?? true;
384445

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Description": "AWS CloudFormation Sample Template SQS_With_CloudWatch_Alarms: Sample template showing how to create an SQS queue with AWS CloudWatch alarms on queue depth.",
4+
"Resources": {
5+
"MyQueue": {
6+
"Type": "AWS::SQS::Queue",
7+
"Properties": {}
8+
}
9+
},
10+
"Outputs": {
11+
"QueueURL": {
12+
"Description": "URL of newly created SQS Queue",
13+
"Value": {
14+
"Ref": "MyQueue"
15+
}
16+
},
17+
"QueueARN": {
18+
"Description": "ARN of newly created SQS Queue",
19+
"Value": {
20+
"Fn::GetAtt": [
21+
"MyQueue",
22+
"Arn"
23+
]
24+
}
25+
},
26+
"QueueName": {
27+
"Description": "Name newly created SQS Queue",
28+
"Value": {
29+
"Fn::GetAtt": [
30+
"MyQueue",
31+
"QueueName"
32+
]
33+
}
34+
}
35+
}
36+
}

packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cli.integtest.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs, existsSync } from 'fs';
22
import * as os from 'os';
33
import * as path from 'path';
4-
import { integTest, cloneDirectory, shell, withDefaultFixture, retry, sleep, randomInteger, withSamIntegrationFixture, RESOURCES_DIR } from '../../lib';
4+
import { integTest, cloneDirectory, shell, withDefaultFixture, retry, sleep, randomInteger, withSamIntegrationFixture, RESOURCES_DIR, withCDKMigrateFixture } from '../../lib';
55

66
jest.setTimeout(2 * 60 * 60_000); // Includes the time to acquire locks, worst-case single-threaded runtime
77

@@ -571,6 +571,23 @@ integTest('deploy with role', withDefaultFixture(async (fixture) => {
571571
}
572572
}));
573573

574+
// TODO add go back in when template synths properly
575+
['typescript', 'python', 'csharp', 'java'].forEach(language => {
576+
integTest(`cdk migrate ${language}`, withCDKMigrateFixture(language, async (fixture) => {
577+
if (language === 'python') {
578+
await fixture.shell(['pip', 'install', '-r', 'requirements.txt']);
579+
}
580+
581+
const stackArn = await fixture.cdkDeploy(fixture.stackNamePrefix, { neverRequireApproval: true, verbose: true, captureStderr: false }, true);
582+
const response = await fixture.aws.cloudFormation('describeStacks', {
583+
StackName: stackArn,
584+
});
585+
586+
expect(response.Stacks?.[0].StackStatus).toEqual('CREATE_COMPLETE');
587+
await fixture.cdkDestroy(fixture.stackNamePrefix);
588+
}));
589+
});
590+
574591
integTest('cdk diff', withDefaultFixture(async (fixture) => {
575592
const diff1 = await fixture.cdk(['diff', fixture.fullStackName('test-1')]);
576593
expect(diff1).toContain('AWS::SNS::Topic');

0 commit comments

Comments
 (0)