aws / aws/aws-cdk

(assertions): serializedJson Matcher should resolve Fn::Join and Fn::GetAtt

Open
#17,938 16 comments 7 reactions 0 assignees View on GitHub
@aws-cdk/assertions @aws-cdk/core effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### Description

I would like to unit test helm chart values and kubernetes manifests. The hint in #17854 was really good and it looked like ```Match.serializedJson()``` could handle it. But as the helm charts get more complex, I see values that are a lot of ```Fn::Join``` with some stringified JSON that is joined together along with kubernetes resources, so that
```Match.serializedJson()``` won't work.

Here's an example:

```ts
{
"Type": "Custom::AWSCDK-EKS-HelmChart",
"Properties": {
...
"Release": "kube-prometheus-stack",
"Chart": "kube-prometheus-stack",
...
"Values": {
"Fn::Join": [
"",
[
"{\"defaultRules\":{\"create\":false},... a lot stringified JSON ... \"GF_DATABASE_HOST\":\"",
{
"Fn::GetAtt": [
"KubePrometheusStackGrafanaDatabase4D622AAE",
"Endpoint.Address"
]
},
":",
{
"Fn::GetAtt": [
"KubePrometheusStackGrafanaDatabase4D622AAE",
"Endpoint.Port"
]
}, "\",\"GF_DATABASE_USER\":.. a novel of stringified JSON ... \"sha\":\"\"}}}}"
]
]
},
"Namespace": "monitoring",
"Repository": "https://prometheus-community.github.io/helm-charts",
"CreateNamespace": true
},
"DependsOn": [
"AddonNamespaceNamespaceManifest00F0B981",
"KubePrometheusStackGrafanaDatabase4D622AAE"
],
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
}
```

I can fully understand, that I get this error message:

```bash
with the following mismatches:
Expected JSON as a string but found object at /Properties/Values (using serializedJson matcher)
```

### Use Case

I would like to have an easy way to test that the logic, creating kubernetes manifests or helm chart values works

### Proposed Solution

After looking into the code, I think we could fix this by implementing some Fn::Join and Fn::GetAtt serialization logic into the serializedJSON matcher (and it's used class):

https://github.com/aws/aws-cdk/blob/eca1e748913979646e59f4d740cf26e1c0e0f9ef/packages/%40aws-cdk/assertions/lib/match.ts#L309

Here we could, instead of failing if the serializedJSON isn't a string, resolve all Joins and GettAtts, so we have valid stringified json.

### Other information

no

### Acknowledge

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

Contributor guide

Open the contributing guide

Research direction

Start in packages/@aws-cdk/assertions/lib/match.ts around line 309, the serializedJson matcher referenced by the issue. Trace the matcher’s handling of Fn::Join and Fn::GetAtt and add focused assertion coverage for the shown manifest shape. Done means joined JSON values can be compared without the current object-versus-string mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, helm, kubernetes, typescript
Domain
cloud, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.