-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
README.md
524 lines (402 loc) · 15.3 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# integ-tests
<!--BEGIN STABILITY BANNER-->
---
![cdk-constructs: Experimental](https://img.shields.io/badge/cdk--constructs-experimental-important.svg?style=for-the-badge)
> The APIs of higher level constructs in this module are experimental and under active development.
> They are subject to non-backward compatible changes or removal in any future version. These are
> not subject to the [Semantic Versioning](https://semver.org/) model and breaking changes will be
> announced in the release notes. This means that while you may use them, you may need to update
> your source code when upgrading to a newer version of this package.
---
<!--END STABILITY BANNER-->
## Overview
This library is meant to be used in combination with the [integ-runner](https://github.com/aws/aws-cdk/tree/main/packages/%40aws-cdk/integ-runner) CLI
to enable users to write and execute integration tests for AWS CDK Constructs.
An integration test should be defined as a CDK application, and
there should be a 1:1 relationship between an integration test and a CDK application.
So for example, in order to create an integration test called `my-function`
we would need to create a file to contain our integration test application.
*test/integ.my-function.ts*
```ts
const app = new App();
const stack = new Stack();
new lambda.Function(stack, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});
```
This is a self contained CDK application which we could deploy by running
```bash
cdk deploy --app 'node test/integ.my-function.js'
```
In order to turn this into an integration test, all that is needed is to
use the `IntegTest` construct.
```ts
declare const app: App;
declare const stack: Stack;
new IntegTest(app, 'Integ', { testCases: [stack] });
```
You will notice that the `stack` is registered to the `IntegTest` as a test case.
Each integration test can contain multiple test cases, which are just instances
of a stack. See the [Usage](#usage) section for more details.
## Usage
### IntegTest
Suppose you have a simple stack, that only encapsulates a Lambda function with a
certain handler:
```ts
interface StackUnderTestProps extends StackProps {
architecture?: lambda.Architecture;
}
class StackUnderTest extends Stack {
constructor(scope: Construct, id: string, props: StackUnderTestProps) {
super(scope, id, props);
new lambda.Function(this, 'Handler', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
architecture: props.architecture,
});
}
}
```
You may want to test this stack under different conditions. For example, we want
this stack to be deployed correctly, regardless of the architecture we choose
for the Lambda function. In particular, it should work for both `ARM_64` and
`X86_64`. So you can create an `IntegTestCase` that exercises both scenarios:
```ts
interface StackUnderTestProps extends StackProps {
architecture?: lambda.Architecture;
}
class StackUnderTest extends Stack {
constructor(scope: Construct, id: string, props: StackUnderTestProps) {
super(scope, id, props);
new lambda.Function(this, 'Handler', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
architecture: props.architecture,
});
}
}
// Beginning of the test suite
const app = new App();
new IntegTest(app, 'DifferentArchitectures', {
testCases: [
new StackUnderTest(app, 'Stack1', {
architecture: lambda.Architecture.ARM_64,
}),
new StackUnderTest(app, 'Stack2', {
architecture: lambda.Architecture.X86_64,
}),
],
});
```
This is all the instruction you need for the integration test runner to know
which stacks to synthesize, deploy and destroy. But you may also need to
customize the behavior of the runner by changing its parameters. For example:
```ts
const app = new App();
const stackUnderTest = new Stack(app, 'StackUnderTest', /* ... */);
const stack = new Stack(app, 'stack');
const testCase = new IntegTest(app, 'CustomizedDeploymentWorkflow', {
testCases: [stackUnderTest],
diffAssets: true,
stackUpdateWorkflow: true,
cdkCommandOptions: {
deploy: {
args: {
requireApproval: RequireApproval.NEVER,
json: true,
},
},
destroy: {
args: {
force: true,
},
},
},
});
```
### IntegTestCaseStack
In the majority of cases an integration test will contain a single `IntegTestCase`.
By default when you create an `IntegTest` an `IntegTestCase` is created for you
and all of your test cases are registered to this `IntegTestCase`. The `IntegTestCase`
and `IntegTestCaseStack` constructs are only needed when it is necessary to
defined different options for individual test cases.
For example, you might want to have one test case where `diffAssets` is enabled.
```ts
declare const app: App;
declare const stackUnderTest: Stack;
const testCaseWithAssets = new IntegTestCaseStack(app, 'TestCaseAssets', {
diffAssets: true,
});
new IntegTest(app, 'Integ', { testCases: [stackUnderTest, testCaseWithAssets] });
```
## Assertions
This library also provides a utility to make assertions against the infrastructure that the integration test deploys.
There are two main scenarios in which assertions are created.
- Part of an integration test using `integ-runner`
In this case you would create an integration test using the `IntegTest` construct and then make assertions using the `assert` property.
You should **not** utilize the assertion constructs directly, but should instead use the `methods` on `IntegTest.assertions`.
```ts
declare const app: App;
declare const stack: Stack;
const integ = new IntegTest(app, 'Integ', { testCases: [stack] });
integ.assertions.awsApiCall('S3', 'getObject');
```
By default an assertions stack is automatically generated for you. You may however provide your own stack to use.
```ts
declare const app: App;
declare const stack: Stack;
declare const assertionStack: Stack;
const integ = new IntegTest(app, 'Integ', { testCases: [stack], assertionStack: assertionStack });
integ.assertions.awsApiCall('S3', 'getObject');
```
- Part of a normal CDK deployment
In this case you may be using assertions as part of a normal CDK deployment in order to make an assertion on the infrastructure
before the deployment is considered successful. In this case you can utilize the assertions constructs directly.
```ts
declare const myAppStack: Stack;
new AwsApiCall(myAppStack, 'GetObject', {
service: 'S3',
api: 'getObject',
});
```
### DeployAssert
Assertions are created by using the `DeployAssert` construct. This construct creates it's own `Stack` separate from
any stacks that you create as part of your integration tests. This `Stack` is treated differently from other stacks
by the `integ-runner` tool. For example, this stack will not be diffed by the `integ-runner`.
`DeployAssert` also provides utilities to register your own assertions.
```ts
declare const myCustomResource: CustomResource;
declare const stack: Stack;
declare const app: App;
const integ = new IntegTest(app, 'Integ', { testCases: [stack] });
integ.assertions.expect(
'CustomAssertion',
ExpectedResult.objectLike({ foo: 'bar' }),
ActualResult.fromCustomResource(myCustomResource, 'data'),
);
```
In the above example an assertion is created that will trigger a user defined `CustomResource`
and assert that the `data` attribute is equal to `{ foo: 'bar' }`.
### API Calls
A common method to retrieve the "actual" results to compare with what is expected is to make an
API call to receive some data. This library does this by utilizing CloudFormation custom resources
which means that CloudFormation will call out to a Lambda Function which will
make the API call.
#### HttpApiCall
Using the `HttpApiCall` will use the
[node-fetch](https://github.com/node-fetch/node-fetch) JavaScript library to
make the HTTP call.
This can be done by using the class directory (in the case of a normal deployment):
```ts
declare const stack: Stack;
new HttpApiCall(stack, 'MyAsssertion', {
url: 'https://example-api.com/abc',
});
```
Or by using the `httpApiCall` method on `DeployAssert` (when writing integration tests):
```ts
declare const app: App;
declare const stack: Stack;
const integ = new IntegTest(app, 'Integ', {
testCases: [stack],
});
integ.assertions.httpApiCall('https://example-api.com/abc');
```
#### AwsApiCall
Using the `AwsApiCall` construct will use the AWS JavaScript SDK to make the API call.
This can be done by using the class directory (in the case of a normal deployment):
```ts
declare const stack: Stack;
new AwsApiCall(stack, 'MyAssertion', {
service: 'SQS',
api: 'receiveMessage',
parameters: {
QueueUrl: 'url',
},
});
```
Or by using the `awsApiCall` method on `DeployAssert` (when writing integration tests):
```ts
declare const app: App;
declare const stack: Stack;
const integ = new IntegTest(app, 'Integ', {
testCases: [stack],
});
integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: 'url',
});
```
By default, the `AwsApiCall` construct will automatically add the correct IAM policies
to allow the Lambda function to make the API call. It does this based on the `service`
and `api` that is provided. In the above example the service is `SQS` and the api is
`receiveMessage` so it will create a policy with `Action: 'sqs:ReceiveMessage`.
There are some cases where the permissions do not exactly match the service/api call, for
example the S3 `listObjectsV2` api. In these cases it is possible to add the correct policy
by accessing the `provider` object.
```ts
declare const app: App;
declare const stack: Stack;
declare const integ: IntegTest;
const apiCall = integ.assertions.awsApiCall('S3', 'listObjectsV2', {
Bucket: 'mybucket',
});
apiCall.provider.addToRolePolicy({
Effect: 'Allow',
Action: ['s3:GetObject', 's3:ListBucket'],
Resource: ['*'],
});
```
Note that addToRolePolicy() uses direct IAM JSON policy blobs, not a iam.PolicyStatement
object like you will see in the rest of the CDK.
### EqualsAssertion
This library currently provides the ability to assert that two values are equal
to one another by utilizing the `EqualsAssertion` class. This utilizes a Lambda
backed `CustomResource` which in tern uses the [Match](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions.Match.html) utility from the
[@aws-cdk/assertions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions-readme.html) library.
```ts
declare const app: App;
declare const stack: Stack;
declare const queue: sqs.Queue;
declare const fn: lambda.IFunction;
const integ = new IntegTest(app, 'Integ', {
testCases: [stack],
});
integ.assertions.invokeFunction({
functionName: fn.functionName,
invocationType: InvocationType.EVENT,
payload: JSON.stringify({ status: 'OK' }),
});
const message = integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: queue.queueUrl,
WaitTimeSeconds: 20,
});
message.assertAtPath('Messages.0.Body', ExpectedResult.objectLike({
requestContext: {
condition: 'Success',
},
requestPayload: {
status: 'OK',
},
responseContext: {
statusCode: 200,
},
responsePayload: 'success',
}));
```
#### Match
`integ-tests` also provides a `Match` utility similar to the `@aws-cdk/assertions` module. `Match`
can be used to construct the `ExpectedResult`. While the utility is similar, only a subset of methods are currently available on the `Match` utility of this module: `arrayWith`, `objectLike`, `stringLikeRegexp` and `serializedJson`.
```ts
declare const message: AwsApiCall;
message.expect(ExpectedResult.objectLike({
Messages: Match.arrayWith([
{
Payload: Match.serializedJson({ key: 'value' }),
},
{
Body: {
Values: Match.arrayWith([{ Asdf: 3 }]),
Message: Match.stringLikeRegexp('message'),
},
},
]),
}));
```
### Examples
#### Invoke a Lambda Function
In this example there is a Lambda Function that is invoked and
we assert that the payload that is returned is equal to '200'.
```ts
declare const lambdaFunction: lambda.IFunction;
declare const app: App;
const stack = new Stack(app, 'cdk-integ-lambda-bundling');
const integ = new IntegTest(app, 'IntegTest', {
testCases: [stack],
});
const invoke = integ.assertions.invokeFunction({
functionName: lambdaFunction.functionName,
});
invoke.expect(ExpectedResult.objectLike({
Payload: '200',
}));
```
#### Make an AWS API Call
In this example there is a StepFunctions state machine that is executed
and then we assert that the result of the execution is successful.
```ts
declare const app: App;
declare const stack: Stack;
declare const sm: IStateMachine;
const testCase = new IntegTest(app, 'IntegTest', {
testCases: [stack],
});
// Start an execution
const start = testCase.assertions.awsApiCall('StepFunctions', 'startExecution', {
stateMachineArn: sm.stateMachineArn,
});
// describe the results of the execution
const describe = testCase.assertions.awsApiCall('StepFunctions', 'describeExecution', {
executionArn: start.getAttString('executionArn'),
});
// assert the results
describe.expect(ExpectedResult.objectLike({
status: 'SUCCEEDED',
}));
```
#### Chain ApiCalls
Sometimes it may be necessary to chain API Calls. Since each API call is its own resource, all you
need to do is add a dependency between the calls. There is an helper method `next` that can be used.
```ts
declare const integ: IntegTest;
integ.assertions.awsApiCall('S3', 'putObject', {
Bucket: 'my-bucket',
Key: 'my-key',
Body: 'helloWorld',
}).next(integ.assertions.awsApiCall('S3', 'getObject', {
Bucket: 'my-bucket',
Key: 'my-key',
}));
```
#### Wait for results
A common use case when performing assertions is to wait for a condition to pass. Sometimes the thing
that you are asserting against is not done provisioning by the time the assertion runs. In these
cases it is possible to run the assertion asynchronously by calling the `waitForAssertions()` method.
Taking the example above of executing a StepFunctions state machine, depending on the complexity of
the state machine, it might take a while for it to complete.
```ts
declare const app: App;
declare const stack: Stack;
declare const sm: IStateMachine;
const testCase = new IntegTest(app, 'IntegTest', {
testCases: [stack],
});
// Start an execution
const start = testCase.assertions.awsApiCall('StepFunctions', 'startExecution', {
stateMachineArn: sm.stateMachineArn,
});
// describe the results of the execution
const describe = testCase.assertions.awsApiCall('StepFunctions', 'describeExecution', {
executionArn: start.getAttString('executionArn'),
}).expect(ExpectedResult.objectLike({
status: 'SUCCEEDED',
})).waitForAssertions();
```
When you call `waitForAssertions()` the assertion provider will continuously make the `awsApiCall` until the
`ExpectedResult` is met. You can also control the parameters for waiting, for example:
```ts
declare const testCase: IntegTest;
declare const start: IApiCall;
const describe = testCase.assertions.awsApiCall('StepFunctions', 'describeExecution', {
executionArn: start.getAttString('executionArn'),
}).expect(ExpectedResult.objectLike({
status: 'SUCCEEDED',
})).waitForAssertions({
totalTimeout: Duration.minutes(5),
interval: Duration.seconds(15),
backoffRate: 3,
});
```