Skip to content

Commit

Permalink
chore(logs): migrate all tests to assertions (aws#18517)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
comcalvi authored and TikiTDO committed Feb 21, 2022
1 parent 5276676 commit adc0250
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 140 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
},
"license": "Apache-2.0",
"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/cfn2ts": "0.0.0",
Expand Down
30 changes: 20 additions & 10 deletions packages/@aws-cdk/aws-logs/test/destination.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '@aws-cdk/assert-internal/jest';
import { Template, Match } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { CrossAccountDestination } from '../lib';
Expand All @@ -19,13 +19,11 @@ describe('destination', () => {
});

// THEN
expect(stack).toHaveResource('AWS::Logs::Destination', {
Template.fromStack(stack).hasResourceProperties('AWS::Logs::Destination', {
DestinationName: 'MyDestination',
RoleArn: { 'Fn::GetAtt': ['Role1ABCC5F0', 'Arn'] },
TargetArn: 'arn:bogus',
});


});

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

// THEN
expect(stack).toHaveResource('AWS::Logs::Destination', (props: any) => {
const pol = JSON.parse(props.DestinationPolicy);

return pol.Statement[0].Action === 'logs:TalkToMe';
Template.fromStack(stack).hasResourceProperties('AWS::Logs::Destination', {
DestinationName: 'MyDestination',
DestinationPolicy: Match.serializedJson({
Statement: [
{
Action: 'logs:TalkToMe',
Effect: 'Allow',
},
],
Version: '2012-10-17',
}),
RoleArn: {
'Fn::GetAtt': [
'Role1ABCC5F0',
'Arn',
],
},
TargetArn: 'arn:bogus',
});


});
});
37 changes: 12 additions & 25 deletions packages/@aws-cdk/aws-logs/test/log-retention.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from 'path';
import '@aws-cdk/assert-internal/jest';
import { ABSENT, ResourcePart } from '@aws-cdk/assert-internal';
import { Match, Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
Expand All @@ -20,7 +19,7 @@ describe('log retention', () => {
});

// THEN
expect(stack).toHaveResource('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': [
{
Expand All @@ -42,12 +41,12 @@ describe('log retention', () => {
],
});

expect(stack).toHaveResource('AWS::Lambda::Function', {
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Handler: 'index.handler',
Runtime: 'nodejs14.x',
});

expect(stack).toHaveResource('Custom::LogRetention', {
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
'ServiceToken': {
'Fn::GetAtt': [
'LogRetentionaae0aa3c5b4d4f87b02d85b201efdd8aFD4BFC8A',
Expand All @@ -57,8 +56,6 @@ describe('log retention', () => {
'LogGroupName': 'group',
'RetentionInDays': 30,
});


});

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

// THEN
expect(stack).toHaveResource('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': [
{
Expand All @@ -94,9 +91,7 @@ describe('log retention', () => {
],
});

expect(stack).toCountResources('AWS::IAM::Role', 0);


Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 0);
});

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

expect(stack).toHaveResource('Custom::LogRetention', {
RetentionInDays: ABSENT,
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
RetentionInDays: Match.absent(),
});


});

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

expect(stack).toHaveResource('Custom::LogRetention', {
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
LogGroupRegion: 'us-east-1',
});


});

test('log group ARN is well formed and conforms', () => {
Expand All @@ -140,7 +131,6 @@ describe('log retention', () => {
expect(logGroupArn.indexOf('logs')).toBeGreaterThan(-1);
expect(logGroupArn.indexOf('log-group')).toBeGreaterThan(-1);
expect(logGroupArn.endsWith(':*')).toEqual(true);

});

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

});

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

expect(stack).toHaveResourceLike('AWS::Lambda::Function', {
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Tags: [
{
Key: 'test-key',
Value: 'test-value',
},
],
});

});

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

// Then
expect(stack).toHaveResource('AWS::Lambda::Function', {
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
Metadata: {
'aws:asset:path': assetLocation,
'aws:asset:is-bundled': false,
'aws:asset:property': 'Code',
},
}, ResourcePart.CompleteDefinition);

});
});
});
Loading

0 comments on commit adc0250

Please sign in to comment.