Skip to content

Commit 465d44b

Browse files
committed
Integ test
1 parent 5c07d8c commit 465d44b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/app/app.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,17 @@ class DriftableStack extends cdk.Stack {
502502
}
503503
}
504504

505+
class EarlyValidationStack extends cdk.Stack {
506+
constructor(parent, id, props) {
507+
super(parent, id, props);
508+
509+
new s3.Bucket(this, 'MyBucket', {
510+
bucketName: process.env.BUCKET_NAME,
511+
removalPolicy: cdk.RemovalPolicy.DESTROY,
512+
});
513+
}
514+
}
515+
505516
class IamRolesStack extends cdk.Stack {
506517
constructor(parent, id, props) {
507518
super(parent, id, props);
@@ -971,6 +982,9 @@ switch (stackSet) {
971982
new MetadataStack(app, `${stackPrefix}-metadata`);
972983

973984
new DriftableStack(app, `${stackPrefix}-driftable`);
985+
986+
new EarlyValidationStack(app, `${stackPrefix}-early-validation-stack1`);
987+
new EarlyValidationStack(app, `${stackPrefix}-early-validation-stack2`);
974988
break;
975989

976990
case 'stage-using-context':
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { integTest, withDefaultFixture } from '../../../lib';
3+
4+
jest.setTimeout(2 * 60 * 60_000); // Includes the time to acquire locks, worst-case single-threaded runtime
5+
6+
integTest(
7+
'deploy - early validation error',
8+
withDefaultFixture(async (fixture) => {
9+
const bucketName = randomUUID();
10+
11+
// First, deploy a stack that creates a bucket with a custom name,
12+
// which we expect to succeed
13+
await fixture.cdkDeploy('early-validation-stack1', {
14+
modEnv: {
15+
BUCKET_NAME: bucketName,
16+
},
17+
});
18+
19+
// Then deploy a different instance of the stack, that creates another
20+
// bucket with the same name, to induce an early validation error
21+
const stdErr = await fixture.cdkDeploy('early-validation-stack2', {
22+
modEnv: {
23+
BUCKET_NAME: bucketName,
24+
},
25+
allowErrExit: true,
26+
});
27+
28+
expect(stdErr).toContain(`Resource of type 'AWS::S3::Bucket' with identifier '${bucketName}' already exists`,
29+
);
30+
}),
31+
);
32+

packages/@aws-cdk/toolkit-lib/lib/api/deployments/early-validation.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@ import { ToolkitError } from '../../toolkit/toolkit-error';
55
import type { SDK } from '../aws-auth/sdk';
66
import type { EnvironmentResources } from '../environment/index';
77

8+
/**
9+
* Reports early validation failures from CloudFormation ChangeSets. To determine what the actual error is
10+
* it needs to call the `DescribeEvents` API, and therefore the deployment role in the bootstrap bucket
11+
* must have permissions to call that API. This permission was introduced in version 30 of the bootstrap template.
12+
* If the bootstrap stack is older than that, a special error message is thrown to instruct the user to
13+
* re-bootstrap.
14+
*/
815
export class EarlyValidationReporter implements ValidationReporter {
916
constructor(private readonly sdk: SDK, private readonly environmentResources: EnvironmentResources) {
1017
}
1118

19+
/**
20+
* Checks whether the ChangeSet failed early validation, and if so, throw an error. Otherwise, do nothing.
21+
* @param description - the ChangeSet description, resulting from the call to CloudFormation's DescribeChangeSet API
22+
* @param changeSetName - the name of the ChangeSet
23+
* @param stackName - the name of the stack
24+
*/
1225
public async check(description: DescribeChangeSetCommandOutput, changeSetName: string, stackName: string) {
1326
if (description.Status === ChangeSetStatus.FAILED && description.StatusReason?.includes('AWS::EarlyValidation')) {
1427
await this.checkBootstrapVersion();
@@ -40,7 +53,7 @@ export class EarlyValidationReporter implements ValidationReporter {
4053
const env = `aws://${environment.account}/${environment.region}`;
4154
throw new ToolkitError(
4255
'While creating the change set, CloudFormation detected errors in the generated templates.\n' +
43-
`To see details about these errors, re-bootstrap your environment with 'cdk bootstrap ${env}', and run 'cdk deploy' again.`,
56+
`To see details about these errors, re-bootstrap your environment with 'cdk bootstrap ${env}', and run 'cdk deploy' again.`,
4457
);
4558
}
4659
}

0 commit comments

Comments
 (0)