Skip to content

Commit 565c26b

Browse files
authored
chore(app-delivery): migrate unit tests to Assertions (#18574)
---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7c1795e commit 565c26b

File tree

2 files changed

+66
-86
lines changed

2 files changed

+66
-86
lines changed

packages/@aws-cdk/app-delivery/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"constructs": "^3.3.69"
6161
},
6262
"devDependencies": {
63-
"@aws-cdk/assert-internal": "0.0.0",
63+
"@aws-cdk/assertions": "0.0.0",
6464
"@aws-cdk/aws-s3": "0.0.0",
6565
"@aws-cdk/cdk-build-tools": "0.0.0",
6666
"@aws-cdk/cdk-integ-tools": "0.0.0",

packages/@aws-cdk/app-delivery/test/pipeline-deploy-stack-action.test.ts

+65-85
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
2-
import { isSuperObject } from '@aws-cdk/assert-internal';
1+
import { Match, Matcher, Template } from '@aws-cdk/assertions';
32
import * as cfn from '@aws-cdk/aws-cloudformation';
43
import * as codebuild from '@aws-cdk/aws-codebuild';
54
import * as codepipeline from '@aws-cdk/aws-codepipeline';
@@ -135,56 +134,43 @@ describeDeprecated('pipeline deploy stack action', () => {
135134
capabilities: [cfn.CloudFormationCapabilities.ANONYMOUS_IAM, cfn.CloudFormationCapabilities.AUTO_EXPAND],
136135
adminPermissions: false,
137136
}));
138-
expect(pipelineStack).toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
139-
Configuration: {
140-
StackName: 'TestStack',
141-
ActionMode: 'CHANGE_SET_REPLACE',
142-
Capabilities: 'CAPABILITY_NAMED_IAM',
143-
},
137+
138+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', hasPipelineActionConfiguration({
139+
StackName: 'TestStack',
140+
ActionMode: 'CHANGE_SET_REPLACE',
141+
Capabilities: 'CAPABILITY_NAMED_IAM',
144142
}));
145-
expect(pipelineStack).toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
146-
Configuration: {
147-
StackName: 'AnonymousIAM',
148-
ActionMode: 'CHANGE_SET_REPLACE',
149-
Capabilities: 'CAPABILITY_IAM',
150-
},
143+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', hasPipelineActionConfiguration({
144+
StackName: 'AnonymousIAM',
145+
ActionMode: 'CHANGE_SET_REPLACE',
146+
Capabilities: 'CAPABILITY_IAM',
151147
}));
152-
expect(pipelineStack).not.toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
153-
Configuration: {
154-
StackName: 'NoCapStack',
155-
ActionMode: 'CHANGE_SET_REPLACE',
156-
Capabilities: 'CAPABILITY_NAMED_IAM',
157-
},
148+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', Match.not(hasPipelineActionConfiguration({
149+
StackName: 'NoCapStack',
150+
ActionMode: 'CHANGE_SET_REPLACE',
151+
Capabilities: 'CAPABILITY_NAMED_IAM',
152+
})));
153+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', Match.not(hasPipelineActionConfiguration({
154+
StackName: 'NoCapStack',
155+
ActionMode: 'CHANGE_SET_REPLACE',
156+
Capabilities: 'CAPABILITY_IAM',
157+
})));
158+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', hasPipelineActionConfiguration({
159+
StackName: 'NoCapStack',
160+
ActionMode: 'CHANGE_SET_REPLACE',
158161
}));
159-
expect(pipelineStack).not.toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
160-
Configuration: {
161-
StackName: 'NoCapStack',
162-
ActionMode: 'CHANGE_SET_REPLACE',
163-
Capabilities: 'CAPABILITY_IAM',
164-
},
162+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', hasPipelineActionConfiguration({
163+
StackName: 'AutoExpand',
164+
ActionMode: 'CHANGE_SET_REPLACE',
165+
Capabilities: 'CAPABILITY_AUTO_EXPAND',
165166
}));
166-
expect(pipelineStack).toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
167-
Configuration: {
168-
StackName: 'NoCapStack',
169-
ActionMode: 'CHANGE_SET_REPLACE',
170-
},
167+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', hasPipelineActionConfiguration({
168+
StackName: 'AnonymousIAMAndAutoExpand',
169+
ActionMode: 'CHANGE_SET_REPLACE',
170+
Capabilities: 'CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND',
171171
}));
172-
expect(pipelineStack).toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
173-
Configuration: {
174-
StackName: 'AutoExpand',
175-
ActionMode: 'CHANGE_SET_REPLACE',
176-
Capabilities: 'CAPABILITY_AUTO_EXPAND',
177-
},
178-
}));
179-
expect(pipelineStack).toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
180-
Configuration: {
181-
StackName: 'AnonymousIAMAndAutoExpand',
182-
ActionMode: 'CHANGE_SET_REPLACE',
183-
Capabilities: 'CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND',
184-
},
185-
}));
186-
187172
});
173+
188174
test('users can use admin permissions', () => {
189175
const pipelineStack = getTestStack();
190176
const selfUpdatingStack = createSelfUpdatingStack(pipelineStack);
@@ -196,7 +182,7 @@ describeDeprecated('pipeline deploy stack action', () => {
196182
input: selfUpdatingStack.synthesizedApp,
197183
adminPermissions: true,
198184
}));
199-
expect(pipelineStack).toHaveResource('AWS::IAM::Policy', {
185+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::IAM::Policy', {
200186
PolicyDocument: {
201187
Version: '2012-10-17',
202188
Statement: [
@@ -251,15 +237,13 @@ describeDeprecated('pipeline deploy stack action', () => {
251237
],
252238
},
253239
});
254-
expect(pipelineStack).toHaveResource('AWS::CodePipeline::Pipeline', hasPipelineAction({
255-
Configuration: {
256-
StackName: 'TestStack',
257-
ActionMode: 'CHANGE_SET_REPLACE',
258-
Capabilities: 'CAPABILITY_NAMED_IAM,CAPABILITY_AUTO_EXPAND',
259-
},
240+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', hasPipelineActionConfiguration({
241+
StackName: 'TestStack',
242+
ActionMode: 'CHANGE_SET_REPLACE',
243+
Capabilities: 'CAPABILITY_NAMED_IAM,CAPABILITY_AUTO_EXPAND',
260244
}));
261-
262245
});
246+
263247
test('users can supply a role for deploy action', () => {
264248
const pipelineStack = getTestStack();
265249
const selfUpdatingStack = createSelfUpdatingStack(pipelineStack);
@@ -313,7 +297,7 @@ describeDeprecated('pipeline deploy stack action', () => {
313297
}));
314298

315299
// THEN //
316-
expect(pipelineStack).toHaveResource('AWS::IAM::Policy', {
300+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::IAM::Policy', {
317301
PolicyDocument: {
318302
Version: '2012-10-17',
319303
Statement: [
@@ -391,7 +375,7 @@ describeDeprecated('pipeline deploy stack action', () => {
391375
const app = new cdk.App();
392376

393377
const deployedStack = new cdk.Stack(app, 'DeployedStack');
394-
for (let i = 0 ; i < assetCount ; i++) {
378+
for (let i = 0; i < assetCount; i++) {
395379
deployedStack.node.addMetadata(cxschema.ArtifactMetadataEntryType.ASSET, {});
396380
}
397381

@@ -406,7 +390,6 @@ describeDeprecated('pipeline deploy stack action', () => {
406390
},
407391
),
408392
);
409-
410393
});
411394

412395
test('allows overriding the ChangeSet and Execute action names', () => {
@@ -425,25 +408,21 @@ describeDeprecated('pipeline deploy stack action', () => {
425408
],
426409
});
427410

428-
expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
429-
Stages: [
430-
{},
431-
{},
432-
{
411+
Template.fromStack(stack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
412+
Stages: Match.arrayWith([
413+
Match.objectLike({
433414
Name: 'Deploy',
434-
Actions: [
435-
{
415+
Actions: Match.arrayWith([
416+
Match.objectLike({
436417
Name: 'Prepare',
437-
},
438-
{
418+
}),
419+
Match.objectLike({
439420
Name: 'Deploy',
440-
},
441-
],
442-
},
443-
],
421+
}),
422+
]),
423+
}),
424+
]),
444425
});
445-
446-
447426
});
448427
});
449428

@@ -481,7 +460,7 @@ function createSelfUpdatingStack(pipelineStack: cdk.Stack): SelfUpdatingPipeline
481460
});
482461

483462
// simple source
484-
const bucket = s3.Bucket.fromBucketArn( pipeline, 'PatternBucket', 'arn:aws:s3:::totally-fake-bucket');
463+
const bucket = s3.Bucket.fromBucketArn(pipeline, 'PatternBucket', 'arn:aws:s3:::totally-fake-bucket');
485464
const sourceOutput = new codepipeline.Artifact('SourceOutput');
486465
const sourceAction = new cpactions.S3SourceAction({
487466
actionName: 'S3Source',
@@ -509,15 +488,16 @@ function createSelfUpdatingStack(pipelineStack: cdk.Stack): SelfUpdatingPipeline
509488
return { synthesizedApp: buildOutput, pipeline };
510489
}
511490

512-
function hasPipelineAction(expectedAction: any): (props: any) => boolean {
513-
return (props: any) => {
514-
for (const stage of props.Stages) {
515-
for (const action of stage.Actions) {
516-
if (isSuperObject(action, expectedAction, [], true)) {
517-
return true;
518-
}
519-
}
520-
}
521-
return false;
522-
};
523-
}
491+
function hasPipelineActionConfiguration(expectedActionConfiguration: any): Matcher {
492+
return Match.objectLike({
493+
Stages: Match.arrayWith([
494+
Match.objectLike({
495+
Actions: Match.arrayWith([
496+
Match.objectLike({
497+
Configuration: expectedActionConfiguration,
498+
}),
499+
]),
500+
}),
501+
]),
502+
});
503+
}

0 commit comments

Comments
 (0)