-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integ-tests): chain assertion api calls (#22196)
This PR does two things. 1. Adds a helper method `next()` that makes it easier to chain assertion api calls together. Yes, it is possible to grab the underlying `node` and call `addDependency`, but I think `then` is a more intuitive experience. Look at `integ.log-group.ts` to see where I updated a test from `addDependency` -> `next` 2. Added an `ApiCallBase` class and renamed the api call interface. This will make it easier to add more types of Api Calls in the future (`HttpApiCall` coming soon*) ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
11 changed files
with
215 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/@aws-cdk/integ-tests/lib/assertions/api-call-base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { CustomResource, Reference } from '@aws-cdk/core'; | ||
import { Construct, IConstruct } from 'constructs'; | ||
import { ExpectedResult } from './common'; | ||
import { AssertionsProvider } from './providers'; | ||
|
||
/** | ||
* Represents an ApiCall | ||
*/ | ||
export interface IApiCall extends IConstruct { | ||
/** | ||
* access the AssertionsProvider. This can be used to add additional IAM policies | ||
* the the provider role policy | ||
* | ||
* @example | ||
* declare const apiCall: AwsApiCall; | ||
* apiCall.provider.addToRolePolicy({ | ||
* Effect: 'Allow', | ||
* Action: ['s3:GetObject'], | ||
* Resource: ['*'], | ||
* }); | ||
*/ | ||
readonly provider: AssertionsProvider; | ||
|
||
/** | ||
* Returns the value of an attribute of the custom resource of an arbitrary | ||
* type. Attributes are returned from the custom resource provider through the | ||
* `Data` map where the key is the attribute name. | ||
* | ||
* @param attributeName the name of the attribute | ||
* @returns a token for `Fn::GetAtt`. Use `Token.asXxx` to encode the returned `Reference` as a specific type or | ||
* use the convenience `getAttString` for string attributes. | ||
*/ | ||
getAtt(attributeName: string): Reference; | ||
|
||
/** | ||
* Returns the value of an attribute of the custom resource of type string. | ||
* Attributes are returned from the custom resource provider through the | ||
* `Data` map where the key is the attribute name. | ||
* | ||
* @param attributeName the name of the attribute | ||
* @returns a token for `Fn::GetAtt` encoded as a string. | ||
*/ | ||
getAttString(attributeName: string): string; | ||
|
||
/** | ||
* Assert that the ExpectedResult is equal | ||
* to the result of the AwsApiCall | ||
* | ||
* @example | ||
* declare const integ: IntegTest; | ||
* const invoke = integ.assertions.invokeFunction({ | ||
* functionName: 'my-func', | ||
* }); | ||
* invoke.expect(ExpectedResult.objectLike({ Payload: 'OK' })); | ||
*/ | ||
expect(expected: ExpectedResult): void; | ||
|
||
/** | ||
* Assert that the ExpectedResult is equal | ||
* to the result of the AwsApiCall at the given path. | ||
* | ||
* For example the SQS.receiveMessage api response would look | ||
* like: | ||
* | ||
* If you wanted to assert the value of `Body` you could do | ||
* | ||
* @example | ||
* const actual = { | ||
* Messages: [{ | ||
* MessageId: '', | ||
* ReceiptHandle: '', | ||
* MD5OfBody: '', | ||
* Body: 'hello', | ||
* Attributes: {}, | ||
* MD5OfMessageAttributes: {}, | ||
* MessageAttributes: {} | ||
* }] | ||
* }; | ||
* | ||
* | ||
* declare const integ: IntegTest; | ||
* const message = integ.assertions.awsApiCall('SQS', 'receiveMessage'); | ||
* | ||
* message.assertAtPath('Messages.0.Body', ExpectedResult.stringLikeRegexp('hello')); | ||
*/ | ||
assertAtPath(path: string, expected: ExpectedResult): void; | ||
|
||
/** | ||
* Allows you to chain IApiCalls. This adds an explicit dependency | ||
* betweent the two resources. | ||
* | ||
* Returns the IApiCall provided as `next` | ||
* | ||
* @example | ||
* declare const first: IApiCall; | ||
* declare const second: IApiCall; | ||
* | ||
* first.next(second); | ||
*/ | ||
next(next: IApiCall): IApiCall; | ||
} | ||
|
||
/** | ||
* Base class for an ApiCall | ||
*/ | ||
export abstract class ApiCallBase extends Construct implements IApiCall { | ||
protected abstract readonly apiCallResource: CustomResource; | ||
protected expectedResult?: string; | ||
protected flattenResponse: string = 'false'; | ||
protected stateMachineArn?: string; | ||
|
||
public abstract readonly provider: AssertionsProvider; | ||
|
||
constructor(scope: Construct, id: string) { | ||
super(scope, id); | ||
|
||
} | ||
|
||
public getAtt(attributeName: string): Reference { | ||
this.flattenResponse = 'true'; | ||
return this.apiCallResource.getAtt(`apiCallResponse.${attributeName}`); | ||
} | ||
|
||
public getAttString(attributeName: string): string { | ||
this.flattenResponse = 'true'; | ||
return this.apiCallResource.getAttString(`apiCallResponse.${attributeName}`); | ||
} | ||
|
||
public expect(expected: ExpectedResult): void { | ||
this.expectedResult = expected.result; | ||
} | ||
|
||
public abstract assertAtPath(path: string, expected: ExpectedResult): void; | ||
|
||
public next(next: IApiCall): IApiCall { | ||
next.node.addDependency(this); | ||
return next; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.