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

chore(cfn-include): migrate all tests to Assertions #18537

Merged
merged 10 commits into from
Jan 20, 2022
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cloudformation-include/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@
"constructs": "^3.3.69"
},
"devDependencies": {
"@aws-cdk/assert-internal": "0.0.0",
"@aws-cdk/assertions": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/cdk-integ-tools": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as path from 'path';
import { SynthUtils } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import * as core from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as inc from '../lib';
Expand All @@ -22,23 +20,23 @@ describe('CDK Include', () => {
includeTestTemplate(stack, 'bucket-with-cors-rules-not-an-array.json');

expect(() => {
SynthUtils.synthesize(stack);
core.App.of(stack)?.synth();
}).toThrow(/corsRules: "CorsRules!" should be a list/);
});

test('throws a validation exception for a template with a null array element of a complex type with required fields', () => {
includeTestTemplate(stack, 'bucket-with-cors-rules-null-element.json');

expect(() => {
SynthUtils.synthesize(stack);
core.App.of(stack)?.synth();
}).toThrow(/allowedMethods: required but missing/);
});

test('throws a validation exception for a template with a missing nested resource property', () => {
includeTestTemplate(stack, 'bucket-with-invalid-cors-rule.json');

expect(() => {
SynthUtils.synthesize(stack);
core.App.of(stack)?.synth();
}).toThrow(/allowedOrigins: required but missing/);
});

Expand Down Expand Up @@ -130,7 +128,7 @@ describe('CDK Include', () => {
includeTestTemplate(stack, 'alphabetical-string-passed-to-number.json');

expect(() => {
SynthUtils.synthesize(stack);
core.App.of(stack)?.synth();
}).toThrow(/"abc" should be a number/);
});

comcalvi marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
47 changes: 23 additions & 24 deletions packages/@aws-cdk/cloudformation-include/test/nested-stacks.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from 'path';
import { ABSENT, ResourcePart } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { Match, Template } from '@aws-cdk/assertions';
import * as s3 from '@aws-cdk/aws-s3';
import * as core from '@aws-cdk/core';
import * as inc from '../lib';
Expand All @@ -27,7 +26,7 @@ describe('CDK Include for nested stacks', () => {
});

const childStack = parentTemplate.getNestedStack('ChildStack');
expect(childStack.stack).toMatchTemplate(
Template.fromStack(childStack.stack).templateMatches(
loadTestFileToJsObject('grandchild-import-stack.json'),
);
});
Expand All @@ -47,11 +46,11 @@ describe('CDK Include for nested stacks', () => {

const childStack = parentTemplate.getNestedStack('ChildStack');
const anotherChildStack = parentTemplate.getNestedStack('AnotherChildStack');
expect(childStack.stack).toMatchTemplate(
Template.fromStack(childStack.stack).templateMatches(
loadTestFileToJsObject('grandchild-import-stack.json'),
);

expect(anotherChildStack.stack).toMatchTemplate(
Template.fromStack(anotherChildStack.stack).templateMatches(
loadTestFileToJsObject('grandchild-import-stack.json'),
);
});
Expand All @@ -73,11 +72,11 @@ describe('CDK Include for nested stacks', () => {

const childStack = parentTemplate.getNestedStack('ChildStack');
const grandChildStack = childStack.includedTemplate.getNestedStack('GrandChildStack');
expect(childStack.stack).toMatchTemplate(
Template.fromStack(childStack.stack).templateMatches(
loadTestFileToJsObject('child-import-stack.expected.json'),
);

expect(grandChildStack.stack).toMatchTemplate(
Template.fromStack(grandChildStack.stack).templateMatches(
loadTestFileToJsObject('grandchild-import-stack.json'),
);
});
Expand Down Expand Up @@ -188,7 +187,7 @@ describe('CDK Include for nested stacks', () => {

bucket.bucketName = 'modified-bucket-name';

expect(childTemplate.stack).toHaveResource('AWS::S3::Bucket', { BucketName: 'modified-bucket-name' });
Template.fromStack(childTemplate.stack).hasResourceProperties('AWS::S3::Bucket', { BucketName: 'modified-bucket-name' });
});

test('can use a condition', () => {
Expand Down Expand Up @@ -218,7 +217,7 @@ describe('CDK Include for nested stacks', () => {

const assetParam = 'AssetParameters5dc7d4a99cfe2979687dc74f2db9fd75f253b5505a1912b5ceecf70c9aefba50S3BucketEAA24F0C';
const assetParamKey = 'AssetParameters5dc7d4a99cfe2979687dc74f2db9fd75f253b5505a1912b5ceecf70c9aefba50S3VersionKey1194CAB2';
expect(stack).toMatchTemplate({
Template.fromStack(stack).templateMatches({
"Parameters": {
[assetParam]: {
"Type": "String",
Expand Down Expand Up @@ -284,7 +283,7 @@ describe('CDK Include for nested stacks', () => {
templateFile: testTemplateFilePath('parent-two-children.json'),
});

expect(stack).toMatchTemplate(loadTestFileToJsObject('parent-two-children.json'));
Template.fromStack(stack).templateMatches(loadTestFileToJsObject('parent-two-children.json'));
});

test('getNestedStack() throws an exception when getting a resource that does not exist in the template', () => {
Expand Down Expand Up @@ -344,7 +343,7 @@ describe('CDK Include for nested stacks', () => {
},
});

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudFormation::Stack', {
"Parameters": {
"Param1": {
"Ref": "Param",
Expand All @@ -370,7 +369,7 @@ describe('CDK Include for nested stacks', () => {
const parameter = parentTemplate.getParameter('Param');
parameter.overrideLogicalId('DifferentParameter');

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudFormation::Stack', {
"Parameters": {
"Param1": {
"Ref": "DifferentParameter",
Expand Down Expand Up @@ -417,7 +416,7 @@ describe('CDK Include for nested stacks', () => {
},
});

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(stack).hasResource('AWS::CloudFormation::Stack', {
"Metadata": {
"Property1": "Value1",
},
Expand All @@ -426,7 +425,7 @@ describe('CDK Include for nested stacks', () => {
"AnotherChildStack",
],
"UpdateReplacePolicy": "Retain",
}, ResourcePart.CompleteDefinition);
});
});

test('correctly parses NotificationsARNs, Timeout', () => {
Expand All @@ -442,11 +441,11 @@ describe('CDK Include for nested stacks', () => {
},
});

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudFormation::Stack', {
"NotificationARNs": ["arn1"],
"TimeoutInMinutes": 5,
});
expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudFormation::Stack', {
"NotificationARNs": { "Ref": "ArrayParam" },
"TimeoutInMinutes": {
"Fn::Select": [0, {
Expand All @@ -466,7 +465,7 @@ describe('CDK Include for nested stacks', () => {
},
});

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudFormation::Stack', {
"Parameters": {
"Number": "60",
},
Expand All @@ -481,7 +480,7 @@ describe('CDK Include for nested stacks', () => {
templateFile: testTemplateFilePath('child-no-bucket.json'),
});

expect(includedChild.stack).toMatchTemplate(
Template.fromStack(includedChild.stack).templateMatches(
loadTestFileToJsObject('child-no-bucket.json'),
);
expect(includedChild.includedTemplate.getResource('GrandChildStack')).toBeDefined();
Expand Down Expand Up @@ -536,7 +535,7 @@ describe('CDK Include for nested stacks', () => {
});

test('correctly creates parameters in the parent stack, and passes them to the child stack', () => {
expect(assetStack).toMatchTemplate({
Template.fromStack(assetStack).templateMatches({
"Parameters": {
[parentBucketParam]: {
"Type": "String",
Expand Down Expand Up @@ -616,7 +615,7 @@ describe('CDK Include for nested stacks', () => {
});

test('correctly creates parameters in the child stack, and passes them to the grandchild stack', () => {
expect(child.stack).toMatchTemplate({
Template.fromStack(child.stack).templateMatches({
"Parameters": {
"MyBucketParameter": {
"Type": "String",
Expand Down Expand Up @@ -676,7 +675,7 @@ describe('CDK Include for nested stacks', () => {
});

test('leaves grandchild stack unmodified', () => {
expect(grandChild.stack).toMatchTemplate(
Template.fromStack(grandChild.stack).templateMatches(
loadTestFileToJsObject('grandchild-import-stack.json'),
);
});
Expand All @@ -703,7 +702,7 @@ describe('CDK Include for nested stacks', () => {
});

test('correctly removes the parameter from the child stack', () => {
expect(childStack).toMatchTemplate({
Template.fromStack(childStack).templateMatches({
"Parameters": {
"SecondParameter": {
"Type": "String",
Expand Down Expand Up @@ -733,9 +732,9 @@ describe('CDK Include for nested stacks', () => {
});

test('correctly removes the parameter from the parent stack', () => {
expect(parentStack).toHaveResourceLike('AWS::CloudFormation::Stack', {
Template.fromStack(parentStack).hasResourceProperties('AWS::CloudFormation::Stack', {
"Parameters": {
"FirstParameter": ABSENT,
"FirstParameter": Match.absent(),
"SecondParameter": "second-value",
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import '@aws-cdk/assert-internal/jest';
import { Template } from '@aws-cdk/assertions';
import * as core from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as inc from '../lib';
Expand All @@ -18,63 +18,63 @@ describe('CDK Include for templates with SAM transform', () => {
test('can ingest a template with only a minimal SAM function using S3Location for CodeUri, and output it unchanged', () => {
includeTestTemplate(stack, 'only-minimal-sam-function-codeuri-as-s3location.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('only-minimal-sam-function-codeuri-as-s3location.yaml'),
);
});

test('can ingest a template with only a SAM function using an array with DDB CRUD for Policies, and output it unchanged', () => {
includeTestTemplate(stack, 'only-sam-function-policies-array-ddb-crud.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('only-sam-function-policies-array-ddb-crud.yaml'),
);
});

test('can ingest a template with only a minimal SAM function using a parameter for CodeUri, and output it unchanged', () => {
includeTestTemplate(stack, 'only-minimal-sam-function-codeuri-as-param.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('only-minimal-sam-function-codeuri-as-param.yaml'),
);
});

test('can ingest a template with only a minimal SAM function using a parameter for CodeUri Bucket property, and output it unchanged', () => {
includeTestTemplate(stack, 'only-minimal-sam-function-codeuri-bucket-as-param.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('only-minimal-sam-function-codeuri-bucket-as-param.yaml'),
);
});

test('can ingest a template with only a SAM function using an array with DDB CRUD for Policies with an Fn::If expression, and output it unchanged', () => {
includeTestTemplate(stack, 'only-sam-function-policies-array-ddb-crud-if.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('only-sam-function-policies-array-ddb-crud-if.yaml'),
);
});

test('can ingest a template with a a union-type property provided as an object, and output it unchanged', () => {
includeTestTemplate(stack, 'api-endpoint-config-object.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('api-endpoint-config-object.yaml'),
);
});

test('can ingest a template with a a union-type property provided as a string, and output it unchanged', () => {
includeTestTemplate(stack, 'api-endpoint-config-string.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('api-endpoint-config-string.yaml'),
);
});

test('can ingest a template with a a union-type property provided as an empty string, and output it unchanged', () => {
includeTestTemplate(stack, 'api-endpoint-config-string-empty.yaml');

expect(stack).toMatchTemplate(
Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('api-endpoint-config-string-empty.yaml'),
);
});
Expand Down
Loading