Skip to content

Commit

Permalink
chore(iot-actions): simplify cloudwatch-logs-action test (aws#17385)
Browse files Browse the repository at this point in the history
This PR refactor the test that I committed earlier based on the above comment.

- aws#17307 (comment)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
yamatatsu authored and TikiTDO committed Feb 21, 2022
1 parent edd7aa6 commit 59f365b
Showing 1 changed file with 12 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('Default cloudwatch logs action', () => {
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
});
const logGroup = new logs.LogGroup(stack, 'MyLogGroup');
const logGroup = logs.LogGroup.fromLogGroupArn(stack, 'my-log-group', 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group');

// WHEN
topicRule.addAction(
Expand All @@ -24,7 +24,7 @@ test('Default cloudwatch logs action', () => {
Actions: [
{
CloudwatchLogs: {
LogGroupName: { Ref: 'MyLogGroup5C0DAD85' },
LogGroupName: 'my-log-group',
RoleArn: {
'Fn::GetAtt': [
'MyTopicRuleTopicRuleActionRoleCE2D05DA',
Expand Down Expand Up @@ -58,16 +58,12 @@ test('Default cloudwatch logs action', () => {
{
Action: ['logs:CreateLogStream', 'logs:PutLogEvents'],
Effect: 'Allow',
Resource: {
'Fn::GetAtt': ['MyLogGroup5C0DAD85', 'Arn'],
},
Resource: 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group:*',
},
{
Action: 'logs:DescribeLogStreams',
Effect: 'Allow',
Resource: {
'Fn::GetAtt': ['MyLogGroup5C0DAD85', 'Arn'],
},
Resource: 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group:*',
},
],
Version: '2012-10-17',
Expand All @@ -85,7 +81,7 @@ test('can set role', () => {
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
});
const logGroup = new logs.LogGroup(stack, 'MyLogGroup');
const logGroup = logs.LogGroup.fromLogGroupArn(stack, 'my-log-group', 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group');
const role = iam.Role.fromRoleArn(stack, 'MyRole', 'arn:aws:iam::123456789012:role/ForTest');

// WHEN
Expand All @@ -101,48 +97,26 @@ test('can set role', () => {
Actions: [
{
CloudwatchLogs: {
LogGroupName: { Ref: 'MyLogGroup5C0DAD85' },
LogGroupName: 'my-log-group',
RoleArn: 'arn:aws:iam::123456789012:role/ForTest',
},
},
],
},
});
});

test('The specified role is added a policy needed for sending data to logs', () => {
// GIVEN
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
});
const logGroup = new logs.LogGroup(stack, 'MyLogGroup');
const role = iam.Role.fromRoleArn(stack, 'MyRole', 'arn:aws:iam::123456789012:role/ForTest');

// WHEN
topicRule.addAction(
new actions.CloudWatchLogsAction(logGroup, {
role,
}),
);

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: ['logs:CreateLogStream', 'logs:PutLogEvents'],
Effect: 'Allow',
Resource: {
'Fn::GetAtt': ['MyLogGroup5C0DAD85', 'Arn'],
},
Resource: 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group:*',
},
{
Action: 'logs:DescribeLogStreams',
Effect: 'Allow',
Resource: {
'Fn::GetAtt': ['MyLogGroup5C0DAD85', 'Arn'],
},
Resource: 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group:*',
},
],
Version: '2012-10-17',
Expand All @@ -152,16 +126,14 @@ test('The specified role is added a policy needed for sending data to logs', ()
});
});


test('When multiple actions are omitted role property, the actions use same one role', () => {
// GIVEN
// GIVEN
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
});
const logGroup1 = new logs.LogGroup(stack, 'MyLogGroup1');
const logGroup2 = new logs.LogGroup(stack, 'MyLogGroup2');
const logGroup1 = logs.LogGroup.fromLogGroupArn(stack, 'my-log-group1', 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group1');
const logGroup2 = logs.LogGroup.fromLogGroupArn(stack, 'my-log-group2', 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group2');

// WHEN
topicRule.addAction(new actions.CloudWatchLogsAction(logGroup1));
Expand All @@ -173,7 +145,7 @@ test('When multiple actions are omitted role property, the actions use same one
Actions: [
{
CloudwatchLogs: {
LogGroupName: { Ref: 'MyLogGroup14A6E382A' },
LogGroupName: 'my-log-group1',
RoleArn: {
'Fn::GetAtt': [
'MyTopicRuleTopicRuleActionRoleCE2D05DA',
Expand All @@ -184,7 +156,7 @@ test('When multiple actions are omitted role property, the actions use same one
},
{
CloudwatchLogs: {
LogGroupName: { Ref: 'MyLogGroup279D6359D' },
LogGroupName: 'my-log-group2',
RoleArn: {
'Fn::GetAtt': [
'MyTopicRuleTopicRuleActionRoleCE2D05DA',
Expand Down

0 comments on commit 59f365b

Please sign in to comment.