Skip to content

Commit d9d0688

Browse files
authored
chore(logs): migrate all tests to assertions (#18517)
---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1480213 commit d9d0688

9 files changed

+66
-140
lines changed

packages/@aws-cdk/aws-logs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
},
8080
"license": "Apache-2.0",
8181
"devDependencies": {
82-
"@aws-cdk/assert-internal": "0.0.0",
82+
"@aws-cdk/assertions": "0.0.0",
8383
"@aws-cdk/cdk-build-tools": "0.0.0",
8484
"@aws-cdk/cdk-integ-tools": "0.0.0",
8585
"@aws-cdk/cfn2ts": "0.0.0",

packages/@aws-cdk/aws-logs/test/destination.test.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
1+
import { Template, Match } from '@aws-cdk/assertions';
22
import * as iam from '@aws-cdk/aws-iam';
33
import * as cdk from '@aws-cdk/core';
44
import { CrossAccountDestination } from '../lib';
@@ -19,13 +19,11 @@ describe('destination', () => {
1919
});
2020

2121
// THEN
22-
expect(stack).toHaveResource('AWS::Logs::Destination', {
22+
Template.fromStack(stack).hasResourceProperties('AWS::Logs::Destination', {
2323
DestinationName: 'MyDestination',
2424
RoleArn: { 'Fn::GetAtt': ['Role1ABCC5F0', 'Arn'] },
2525
TargetArn: 'arn:bogus',
2626
});
27-
28-
2927
});
3028

3129
test('add policy to destination', () => {
@@ -47,12 +45,24 @@ describe('destination', () => {
4745
}));
4846

4947
// THEN
50-
expect(stack).toHaveResource('AWS::Logs::Destination', (props: any) => {
51-
const pol = JSON.parse(props.DestinationPolicy);
52-
53-
return pol.Statement[0].Action === 'logs:TalkToMe';
48+
Template.fromStack(stack).hasResourceProperties('AWS::Logs::Destination', {
49+
DestinationName: 'MyDestination',
50+
DestinationPolicy: Match.serializedJson({
51+
Statement: [
52+
{
53+
Action: 'logs:TalkToMe',
54+
Effect: 'Allow',
55+
},
56+
],
57+
Version: '2012-10-17',
58+
}),
59+
RoleArn: {
60+
'Fn::GetAtt': [
61+
'Role1ABCC5F0',
62+
'Arn',
63+
],
64+
},
65+
TargetArn: 'arn:bogus',
5466
});
55-
56-
5767
});
5868
});

packages/@aws-cdk/aws-logs/test/log-retention.test.ts

+12-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from 'path';
2-
import '@aws-cdk/assert-internal/jest';
3-
import { ABSENT, ResourcePart } from '@aws-cdk/assert-internal';
2+
import { Match, Template } from '@aws-cdk/assertions';
43
import * as iam from '@aws-cdk/aws-iam';
54
import * as cdk from '@aws-cdk/core';
65
import * as cxapi from '@aws-cdk/cx-api';
@@ -20,7 +19,7 @@ describe('log retention', () => {
2019
});
2120

2221
// THEN
23-
expect(stack).toHaveResource('AWS::IAM::Policy', {
22+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
2423
'PolicyDocument': {
2524
'Statement': [
2625
{
@@ -42,12 +41,12 @@ describe('log retention', () => {
4241
],
4342
});
4443

45-
expect(stack).toHaveResource('AWS::Lambda::Function', {
44+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
4645
Handler: 'index.handler',
4746
Runtime: 'nodejs14.x',
4847
});
4948

50-
expect(stack).toHaveResource('Custom::LogRetention', {
49+
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
5150
'ServiceToken': {
5251
'Fn::GetAtt': [
5352
'LogRetentionaae0aa3c5b4d4f87b02d85b201efdd8aFD4BFC8A',
@@ -57,8 +56,6 @@ describe('log retention', () => {
5756
'LogGroupName': 'group',
5857
'RetentionInDays': 30,
5958
});
60-
61-
6259
});
6360

6461
test('with imported role', () => {
@@ -74,7 +71,7 @@ describe('log retention', () => {
7471
});
7572

7673
// THEN
77-
expect(stack).toHaveResource('AWS::IAM::Policy', {
74+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
7875
'PolicyDocument': {
7976
'Statement': [
8077
{
@@ -94,9 +91,7 @@ describe('log retention', () => {
9491
],
9592
});
9693

97-
expect(stack).toCountResources('AWS::IAM::Role', 0);
98-
99-
94+
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 0);
10095
});
10196

10297
test('with RetentionPeriod set to Infinity', () => {
@@ -107,11 +102,9 @@ describe('log retention', () => {
107102
retention: RetentionDays.INFINITE,
108103
});
109104

110-
expect(stack).toHaveResource('Custom::LogRetention', {
111-
RetentionInDays: ABSENT,
105+
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
106+
RetentionInDays: Match.absent(),
112107
});
113-
114-
115108
});
116109

117110
test('with LogGroupRegion specified', () => {
@@ -122,11 +115,9 @@ describe('log retention', () => {
122115
retention: RetentionDays.INFINITE,
123116
});
124117

125-
expect(stack).toHaveResource('Custom::LogRetention', {
118+
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
126119
LogGroupRegion: 'us-east-1',
127120
});
128-
129-
130121
});
131122

132123
test('log group ARN is well formed and conforms', () => {
@@ -140,7 +131,6 @@ describe('log retention', () => {
140131
expect(logGroupArn.indexOf('logs')).toBeGreaterThan(-1);
141132
expect(logGroupArn.indexOf('log-group')).toBeGreaterThan(-1);
142133
expect(logGroupArn.endsWith(':*')).toEqual(true);
143-
144134
});
145135

146136
test('log group ARN is well formed and conforms when region is specified', () => {
@@ -156,7 +146,6 @@ describe('log retention', () => {
156146
expect(logGroupArn.indexOf('logs')).toBeGreaterThan(-1);
157147
expect(logGroupArn.indexOf('log-group')).toBeGreaterThan(-1);
158148
expect(logGroupArn.endsWith(':*')).toEqual(true);
159-
160149
});
161150

162151
test('retention Lambda CfnResource receives propagated tags', () => {
@@ -167,15 +156,14 @@ describe('log retention', () => {
167156
retention: RetentionDays.ONE_MONTH,
168157
});
169158

170-
expect(stack).toHaveResourceLike('AWS::Lambda::Function', {
159+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
171160
Tags: [
172161
{
173162
Key: 'test-key',
174163
Value: 'test-value',
175164
},
176165
],
177166
});
178-
179167
});
180168

181169
test('asset metadata added to log retention construct lambda function', () => {
@@ -193,13 +181,12 @@ describe('log retention', () => {
193181
});
194182

195183
// Then
196-
expect(stack).toHaveResource('AWS::Lambda::Function', {
184+
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
197185
Metadata: {
198186
'aws:asset:path': assetLocation,
199187
'aws:asset:is-bundled': false,
200188
'aws:asset:property': 'Code',
201189
},
202-
}, ResourcePart.CompleteDefinition);
203-
190+
});
204191
});
205192
});

0 commit comments

Comments
 (0)