Skip to content

Commit 0ea1743

Browse files
authoredMay 11, 2022
fix(Test Coverage): Improve test coverage of 2 core files (#691)
* Added additional tests * More coverage based on review
1 parent e34e726 commit 0ea1743

File tree

2 files changed

+278
-20
lines changed

2 files changed

+278
-20
lines changed
 

‎source/patterns/@aws-solutions-constructs/core/test/kinesis-analytics.test.ts

+157-20
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,38 @@
1111
* and limitations under the License.
1212
*/
1313

14-
import { Stack } from '@aws-cdk/core';
15-
import * as kinesisanalytics from '@aws-cdk/aws-kinesisanalytics';
16-
import * as defaults from '../index';
17-
import { overrideProps } from '../lib/utils';
18-
import '@aws-cdk/assert/jest';
14+
import { Stack, RemovalPolicy } from "@aws-cdk/core";
15+
import * as cdk from "@aws-cdk/core";
16+
import * as kinesisanalytics from "@aws-cdk/aws-kinesisanalytics";
17+
import * as kinesisFirehose from "@aws-cdk/aws-kinesisfirehose";
18+
import * as iam from "@aws-cdk/aws-iam";
19+
import * as kms from "@aws-cdk/aws-kms";
20+
import * as logs from "@aws-cdk/aws-logs";
21+
import * as defaults from "../index";
22+
import { overrideProps } from "../lib/utils";
23+
import "@aws-cdk/assert/jest";
1924

20-
test('test kinesisanalytics override inputProperty', () => {
25+
test("test kinesisanalytics override inputProperty", () => {
2126
const stack = new Stack();
2227

2328
const inputProperty: kinesisanalytics.CfnApplication.InputProperty = {
2429
inputSchema: {
25-
recordColumns: [{name: 'x', sqlType: 'y'}],
26-
recordFormat: { recordFormatType: 'csv' }
30+
recordColumns: [{ name: "x", sqlType: "y" }],
31+
recordFormat: { recordFormatType: "csv" },
2732
},
28-
namePrefix: 'zzz'
33+
namePrefix: "zzz",
2934
};
3035

31-
const defaultProps: kinesisanalytics.CfnApplicationProps = defaults.DefaultCfnApplicationProps;
36+
const defaultProps: kinesisanalytics.CfnApplicationProps =
37+
defaults.DefaultCfnApplicationProps;
3238

3339
const inProps: kinesisanalytics.CfnApplicationProps = {
34-
inputs: [inputProperty]
40+
inputs: [inputProperty],
3541
};
3642

3743
const outProps = overrideProps(defaultProps, inProps);
3844

39-
new kinesisanalytics.CfnApplication(stack, 'KinesisAnalytics', outProps);
45+
new kinesisanalytics.CfnApplication(stack, "KinesisAnalytics", outProps);
4046

4147
expect(stack).toHaveResource("AWS::KinesisAnalytics::Application", {
4248
Inputs: [
@@ -45,15 +51,146 @@ test('test kinesisanalytics override inputProperty', () => {
4551
RecordColumns: [
4652
{
4753
Name: "x",
48-
SqlType: "y"
49-
}
54+
SqlType: "y",
55+
},
5056
],
5157
RecordFormat: {
52-
RecordFormatType: "csv"
53-
}
58+
RecordFormatType: "csv",
59+
},
5460
},
55-
NamePrefix: "zzz"
56-
}
57-
]
61+
NamePrefix: "zzz",
62+
},
63+
],
5864
});
59-
});
65+
});
66+
67+
test("Test default implementation", () => {
68+
const stack = new Stack();
69+
70+
const newFirehose = CreateFirehose(stack);
71+
const kinesisProps: defaults.BuildKinesisAnalyticsAppProps = {
72+
kinesisFirehose: newFirehose,
73+
kinesisAnalyticsProps: {
74+
inputs: [{
75+
inputSchema: {
76+
recordColumns: [{
77+
name: 'ts',
78+
sqlType: 'TIMESTAMP',
79+
mapping: '$.timestamp'
80+
}, {
81+
name: 'trip_id',
82+
sqlType: 'VARCHAR(64)',
83+
mapping: '$.trip_id'
84+
}],
85+
recordFormat: {
86+
recordFormatType: 'JSON'
87+
},
88+
recordEncoding: 'UTF-8'
89+
},
90+
namePrefix: 'SOURCE_SQL_STREAM'
91+
}]
92+
},
93+
};
94+
95+
defaults.buildKinesisAnalyticsApp(stack, kinesisProps);
96+
97+
expect(stack).toHaveResourceLike("AWS::KinesisAnalytics::Application", {
98+
Inputs: [{
99+
InputSchema: {
100+
RecordColumns: [{
101+
Name: 'ts',
102+
SqlType: 'TIMESTAMP',
103+
Mapping: '$.timestamp'
104+
}, {
105+
Name: 'trip_id',
106+
SqlType: 'VARCHAR(64)',
107+
Mapping: '$.trip_id'
108+
}],
109+
RecordFormat: {
110+
RecordFormatType: 'JSON'
111+
},
112+
RecordEncoding: 'UTF-8'
113+
},
114+
NamePrefix: 'SOURCE_SQL_STREAM'
115+
}]
116+
});
117+
});
118+
119+
// test('Test for customer overrides', {
120+
// test('Check policy created', {
121+
122+
function CreateFirehose(stack: Stack): kinesisFirehose.CfnDeliveryStream {
123+
// Creating the Firehose is kind of a big deal. FirehoseToS3 is not readily available here in core,
124+
// so this routine pretty much replicates it. If this function ceases to work correctly, look at
125+
// FirehoseToS3 and see if that changed.
126+
const destinationBucket = defaults.CreateScrapBucket(stack, {
127+
removalPolicy: RemovalPolicy.DESTROY,
128+
autoDeleteObjects: true,
129+
});
130+
131+
const kinesisFirehoseLogGroup = defaults.buildLogGroup(
132+
stack,
133+
"firehose-log-group",
134+
{}
135+
);
136+
137+
const cwLogStream: logs.LogStream = kinesisFirehoseLogGroup.addStream(
138+
"firehose-log-stream"
139+
);
140+
141+
const firehoseRole = new iam.Role(stack, "test-role", {
142+
assumedBy: new iam.ServicePrincipal("firehose.amazonaws.com"),
143+
});
144+
145+
// Setup the IAM policy for Kinesis Firehose
146+
const firehosePolicy = new iam.Policy(stack, "KinesisFirehosePolicy", {
147+
statements: [
148+
new iam.PolicyStatement({
149+
actions: [
150+
"s3:AbortMultipartUpload",
151+
"s3:GetBucketLocation",
152+
"s3:GetObject",
153+
"s3:ListBucket",
154+
"s3:ListBucketMultipartUploads",
155+
"s3:PutObject",
156+
],
157+
resources: [
158+
`${destinationBucket.bucketArn}`,
159+
`${destinationBucket.bucketArn}/*`,
160+
],
161+
}),
162+
new iam.PolicyStatement({
163+
actions: ["logs:PutLogEvents"],
164+
resources: [
165+
`arn:${cdk.Aws.PARTITION}:logs:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:log-group:${kinesisFirehoseLogGroup.logGroupName}:log-stream:${cwLogStream.logStreamName}`,
166+
],
167+
}),
168+
],
169+
});
170+
171+
// Attach policy to role
172+
firehosePolicy.attachToRole(firehoseRole);
173+
174+
const awsManagedKey: kms.IKey = kms.Alias.fromAliasName(
175+
stack,
176+
"aws-managed-key",
177+
"alias/aws/s3"
178+
);
179+
180+
const defaultKinesisFirehoseProps: kinesisFirehose.CfnDeliveryStreamProps = defaults.DefaultCfnDeliveryStreamProps(
181+
destinationBucket.bucketArn,
182+
firehoseRole.roleArn,
183+
kinesisFirehoseLogGroup.logGroupName,
184+
cwLogStream.logStreamName,
185+
awsManagedKey
186+
);
187+
188+
destinationBucket.grantPut(firehoseRole);
189+
190+
const firehose = new kinesisFirehose.CfnDeliveryStream(
191+
stack,
192+
"KinesisFirehose",
193+
defaultKinesisFirehoseProps
194+
);
195+
return firehose;
196+
}

‎source/patterns/@aws-solutions-constructs/core/test/sqs-helper.test.ts

+121
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
// Imports
1515
import { Stack } from "@aws-cdk/core";
16+
import * as sqs from '@aws-cdk/aws-sqs';
1617
import * as defaults from '../';
1718
import '@aws-cdk/assert/jest';
19+
import { buildDeadLetterQueue, buildQueue } from "../lib/sqs-helper";
1820

1921
// --------------------------------------------------------------
2022
// Test deployment w/ imported encryption key
@@ -57,3 +59,122 @@ test('Test deployment without imported encryption key', () => {
5759
KmsMasterKeyId: "alias/aws/sqs"
5860
});
5961
});
62+
63+
// --------------------------------------------------------------
64+
// Test deployment w/ construct created encryption key
65+
// --------------------------------------------------------------
66+
test('Test deployment w/ construct created encryption key', () => {
67+
// Stack
68+
const stack = new Stack();
69+
// Helper declaration
70+
const [queue, key] = defaults.buildQueue(stack, 'existing-queue', {
71+
queueProps: {
72+
queueName: 'existing-queue'
73+
},
74+
enableEncryptionWithCustomerManagedKey: true,
75+
});
76+
77+
expect(stack).toHaveResource("AWS::SQS::Queue", {
78+
QueueName: "existing-queue"
79+
});
80+
expect(stack).toHaveResource("AWS::KMS::Key", {
81+
EnableKeyRotation: true
82+
});
83+
expect(queue).toBeDefined();
84+
expect(key).toBeDefined();
85+
});
86+
87+
test('Test DLQ when existing Queue Provided', () => {
88+
const stack = new Stack();
89+
90+
const existingQueue = new sqs.Queue(stack, 'test-queue');
91+
const buildDlqProps: defaults.BuildDeadLetterQueueProps = {
92+
existingQueueObj: existingQueue,
93+
};
94+
95+
const returnedQueueu = defaults.buildDeadLetterQueue(stack, buildDlqProps);
96+
97+
expect(returnedQueueu).toBeUndefined();
98+
expect(stack).toCountResources("AWS::SQS::Queue", 1);
99+
});
100+
101+
test('Test DLQ with all defaults', () => {
102+
const stack = new Stack();
103+
104+
buildDeadLetterQueue(stack, {});
105+
expect(stack).toHaveResourceLike("AWS::SQS::Queue", {
106+
KmsMasterKeyId: "alias/aws/sqs"
107+
});
108+
});
109+
110+
test("Test DLQ with a provided properties", () => {
111+
const stack = new Stack();
112+
const testQueueName = "test-unique252";
113+
114+
const returnedQueue = buildDeadLetterQueue(stack, {
115+
deadLetterQueueProps: {
116+
queueName: testQueueName,
117+
},
118+
});
119+
expect(stack).toHaveResourceLike("AWS::SQS::Queue", {
120+
QueueName: testQueueName,
121+
});
122+
expect(returnedQueue).toBeDefined();
123+
});
124+
125+
test('Test DLQ with a provided maxReceiveCount', () => {
126+
const stack = new Stack();
127+
const testMaxReceiveCount = 31;
128+
129+
const dlqInterface = buildDeadLetterQueue(stack, {
130+
maxReceiveCount: testMaxReceiveCount
131+
});
132+
expect(dlqInterface?.maxReceiveCount).toEqual(testMaxReceiveCount);
133+
});
134+
135+
test('Test returning an existing Queue', () => {
136+
const stack = new Stack();
137+
const testQueueName = 'existing-queue';
138+
139+
const existingQueue = new sqs.Queue(stack, 'test-queue', {
140+
queueName: testQueueName
141+
});
142+
143+
const [returnedQueue] = defaults.buildQueue(stack, 'newQueue', {
144+
existingQueueObj: existingQueue
145+
});
146+
147+
expect(stack).toHaveResourceLike("AWS::SQS::Queue", {
148+
QueueName: testQueueName,
149+
});
150+
expect(existingQueue.queueName).toEqual(returnedQueue.queueName);
151+
});
152+
153+
test('Test creating a queue with a DLQ', () => {
154+
const stack = new Stack();
155+
156+
const dlqInterface = buildDeadLetterQueue(stack, {});
157+
158+
const [newQueue] = buildQueue(stack, 'new-queue', {
159+
deadLetterQueue: dlqInterface
160+
});
161+
162+
expect(stack).toCountResources("AWS::SQS::Queue", 2);
163+
expect(newQueue).toBeDefined();
164+
expect(newQueue.deadLetterQueue).toBeDefined();
165+
});
166+
167+
test('Test creating a FIFO queue', () => {
168+
const stack = new Stack();
169+
170+
const [newFifoQueue] = buildQueue(stack, 'new-queue', {
171+
queueProps: {
172+
fifo: true
173+
}
174+
});
175+
176+
expect(stack).toHaveResourceLike("AWS::SQS::Queue", {
177+
FifoQueue: true
178+
});
179+
expect(newFifoQueue.fifo).toBe(true);
180+
});

0 commit comments

Comments
 (0)
Please sign in to comment.