aws / aws/aws-cdk

integ-tests: better assertion data handling

Open
#23,231 1 comment 0 reactions 1 assignee Assigned to @colifran View on GitHub
@aws-cdk/integ-tests effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

Currently assertions can do retrieve data with two different purposes.

1. Retrieve data in order to assert against all or part of the data
2. Retrieve data in order to return part of it to be used by other resources in the stack.

For example, If I want to retrieve a message from a SQS queue

```json
{
"Messages": [
{
"Body":
"{\"LifecycleTransition\":\"autoscaling:EC2_INSTANCE_LAUNCHING\",\"LifecycleActionToken\":\"some_token\"}"
}
]
}
```

I would first retrieve the data

```ts
const message = integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: queueUrl,
});
```

And then I might want to assert some value

```ts
message.assertAtPath(
'Messages.0.Body.LifecycleTransition',
ExpectedResult.stringLikeRegexp('autoscaling:EC2_INSTANCE_LAUNCHING'),
).waitForAssertions();
```

Or I might want to retrieve a value to use later

```ts
const token = message.getAttString('Messsages.0.Body.LifecycleActionToken');
```

In this case the custom resource should only return the token (currently it returns the entire object and you just get a reference to the token attribute).

### Use Case

See above

### Proposed Solution

Currently we retrieve the data and (optionally) flatten it in the [sdk](https://github.com/aws/aws-cdk/blob/cde8bb91a303a17e2820596265ab085707c2a06e/packages/@aws-cdk/integ-tests/lib/assertions/providers/lambda-handler/sdk.ts) handler. We are also automatically attempting to parse any JSON strings in the response. Instead we should just be returning the entire response from this handler and allow the other handlers to process the data.

Using the above example, the sdk handler would return the entire message response as is. Then the [assertion](https://github.com/aws/aws-cdk/blob/cde8bb91a303a17e2820596265ab085707c2a06e/packages/@aws-cdk/integ-tests/lib/assertions/providers/lambda-handler/assertion.ts) handler would perform any assertions and return the result of the assertion. To do this we need to introduce some functionality to parse any JSON strings that are part of the response.

One method could be to introduce some special string like `` which tells the provider to parse the next item.

```ts
message.assertAtPath('Messages.0..Body.LifecycleTransition', ...);
```

This same method could be used in the handler that will return any values

```ts
message.getAttString('Messages.0..Body.LifecycleActionToken');
```

### Other Information

_No response_

### Acknowledgements

- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

any

### Environment details (OS name and version, etc.)

any

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.