Skip to content

Commit 9ca7ba8

Browse files
authored
chore(integ-tests): add waiterProvider to IApiCall (#27844)
This PR changes to add the `waiterProvider` property to an `IApiCall` for `awsApiCall` in integ-tests-alpha. By default `awsApiCall` in integ tests, 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 following example the service is SQS and the api is receiveMessage so it will create a policy with Action: 'sqs:ReceiveMessage'. ```ts const integ = new IntegTest(app, 'Integ', { testCases: [stack], }); integ.assertions.awsApiCall('SQS', 'receiveMessage', { QueueUrl: 'url', }); ``` 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 const apiCall = integ.assertions.awsApiCall('S3', 'listObjectsV2', { Bucket: 'mybucket', }); apiCall.provider.addToRolePolicy({ Effect: 'Allow', Action: ['s3:GetObject', 's3:ListBucket'], Resource: ['*'], }); ``` On the other hand, there is the case to use `waitForAssertions` when using `awsApiCall` in integ tests. This causes `apiCall` to have a `waiterProvider` property in addition to `provider`. ```ts const apiCall = integ.assertions.awsApiCall('S3', 'listObjectsV2', { Bucket: 'mybucket', }).expect(ExpectedResult.objectLike({ KeyCount: 1, })).waitForAssertions({ interval: cdk.Duration.seconds(30), totalTimeout: cdk.Duration.minutes(10), }); ``` In the case, `waiterProvider` actually calls to the service/api, so it should have the proper policies. However a type of a return value of `apiCall` is `IApiCall` interface so that the interface has a `provider` property, `waiterProvider` is not in `IApiCall` but in `AwsApiCall`. Then it cannot take the policies without casting the following. (`apiCall instanceof AwsApiCall`) ```ts if (apiCall instanceof AwsApiCall) { apiCall.waiterProvider?.addToRolePolicy({ Effect: 'Allow', Action: ['s3:GetObject', 's3:ListBucket'], Resource: ['*'], }); } ``` So I add `waiterProvider` to `IApiCall`, so that it can take the policies without casting: ```ts // if (apiCall instanceof AwsApiCall) { apiCall.waiterProvider?.addToRolePolicy({ Effect: 'Allow', Action: ['s3:GetObject', 's3:ListBucket'], Resource: ['*'], }); //} ``` In my opinion, I see no negative impact from this. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 588b106 commit 9ca7ba8

12 files changed

+1235
-2
lines changed

packages/@aws-cdk/integ-tests-alpha/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,32 @@ const describe = testCase.assertions.awsApiCall('StepFunctions', 'describeExecut
522522
});
523523
```
524524

525+
When `waitForAssertions()` is used for the `awsApiCall`, the actual API call is executed
526+
by the `waiterProvider` assertion provider.
527+
528+
By default, the `AwsApiCall` construct will automatically add the correct IAM policies
529+
to allow the Lambda function to make the API call. It does this based on the `service`
530+
and `api` that is provided. In the above example the service is `SQS` and the api is
531+
`receiveMessage` so it will create a policy with `Action: 'sqs:ReceiveMessage`.
532+
533+
There are some cases where the permissions do not exactly match the service/api call, for
534+
example the S3 `listObjectsV2` api. In these cases it is possible to add the correct policy
535+
by accessing the `waiterProvider` object.
536+
537+
```ts
538+
declare const integ: IntegTest;
539+
540+
const apiCall = integ.assertions.awsApiCall('S3', 'listObjectsV2', {
541+
Bucket: 'mybucket',
542+
}).waitForAssertions({
543+
totalTimeout: Duration.minutes(5),
544+
interval: Duration.seconds(15),
545+
backoffRate: 3,
546+
});
547+
548+
apiCall.waiterProvider?.addToRolePolicy({
549+
Effect: 'Allow',
550+
Action: ['s3:GetObject', 's3:ListBucket'],
551+
Resource: ['*'],
552+
});
553+
```

packages/@aws-cdk/integ-tests-alpha/lib/assertions/api-call-base.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { WaiterStateMachineOptions } from './waiter-state-machine';
99
*/
1010
export interface IApiCall extends IConstruct {
1111
/**
12-
* access the AssertionsProvider. This can be used to add additional IAM policies
13-
* the the provider role policy
12+
* Access the AssertionsProvider. This can be used to add additional IAM policies
13+
* to the provider role policy.
1414
*
1515
* @example
1616
* declare const apiCall: AwsApiCall;
@@ -22,6 +22,21 @@ export interface IApiCall extends IConstruct {
2222
*/
2323
readonly provider: AssertionsProvider;
2424

25+
/**
26+
* Access the AssertionsProvider for the waiter state machine.
27+
* This can be used to add additional IAM policies
28+
* to the provider role policy.
29+
*
30+
* @example
31+
* declare const apiCall: AwsApiCall;
32+
* apiCall.waiterProvider?.addToRolePolicy({
33+
* Effect: 'Allow',
34+
* Action: ['s3:GetObject'],
35+
* Resource: ['*'],
36+
* });
37+
*/
38+
readonly waiterProvider?: AssertionsProvider;
39+
2540
/**
2641
* Returns the value of an attribute of the custom resource of an arbitrary
2742
* type. Attributes are returned from the custom resource provider through the

packages/@aws-cdk/integ-tests-alpha/test/assertions/providers/lambda-handler/integ.waiter-provider.js.snapshot/WaiterProviderStack.assets.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"Resources": {
3+
"Bucket83908E77": {
4+
"Type": "AWS::S3::Bucket",
5+
"UpdateReplacePolicy": "Delete",
6+
"DeletionPolicy": "Delete"
7+
}
8+
},
9+
"Outputs": {
10+
"ExportsOutputRefBucket83908E7781C90AC0": {
11+
"Value": {
12+
"Ref": "Bucket83908E77"
13+
},
14+
"Export": {
15+
"Name": "WaiterProviderStack:ExportsOutputRefBucket83908E7781C90AC0"
16+
}
17+
},
18+
"ExportsOutputFnGetAttBucket83908E77Arn063C8555": {
19+
"Value": {
20+
"Fn::GetAtt": [
21+
"Bucket83908E77",
22+
"Arn"
23+
]
24+
},
25+
"Export": {
26+
"Name": "WaiterProviderStack:ExportsOutputFnGetAttBucket83908E77Arn063C8555"
27+
}
28+
}
29+
},
30+
"Parameters": {
31+
"BootstrapVersion": {
32+
"Type": "AWS::SSM::Parameter::Value<String>",
33+
"Default": "/cdk-bootstrap/hnb659fds/version",
34+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
35+
}
36+
},
37+
"Rules": {
38+
"CheckBootstrapVersion": {
39+
"Assertions": [
40+
{
41+
"Assert": {
42+
"Fn::Not": [
43+
{
44+
"Fn::Contains": [
45+
[
46+
"1",
47+
"2",
48+
"3",
49+
"4",
50+
"5"
51+
],
52+
{
53+
"Ref": "BootstrapVersion"
54+
}
55+
]
56+
}
57+
]
58+
},
59+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
60+
}
61+
]
62+
}
63+
}
64+
}

packages/@aws-cdk/integ-tests-alpha/test/assertions/providers/lambda-handler/integ.waiter-provider.js.snapshot/WaiterProviderTestDefaultTestDeployAssertDADDA65F.assets.json

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)