-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-cdk-lib/assertions): Simplify assertions on stack tags #27620
(aws-cdk-lib/assertions): Simplify assertions on stack tags #27620
Comments
My intent with this is to go ahead with a PR that introduces the It makes some sense to have it on the template, as that may align with the mental model of the tags applying directly to the stack as the e do to other resources, but CloudFormation treats them differently so it's probably correct for the API to follow the actual model here. WRT to the special cases listed above, I'm going to ignore both of them. I don't know that the nested stack issue affects tagging (happy to be corrected) and forcing re-synthesis like |
I've raised a PR with a possible implementation. It's likely to need cleaning up, but I'll wait on feedback that it's in the ballpark before refining it carefully. |
Adds a `Tag` class to the assertions library that permits assertions against tags on synthesized CDK stacks. Tags on AWS resources can be checked via assertions with the Template class, but since stack tags only appear on the cloud assembly manifest as a sibling of the template, a separate assertion mechanism is required. > [!NOTE] > Previously submitted as #27633, which was closed automatically while waiting for review. > > This PR is complete to the best of my knowledge, needing only an exemption from integration tests. As far as I can tell, this area of the library has no integration tests directly associated. ### API ```ts class Tags { public static fromStack(stack: Stack) : Tags; public hasValues(props: any): void; public all(): { [key: string]: string }; } ``` ### Usage This new class permits tests of the form: ```ts import { App, Stack } from 'aws-cdk-lib'; import { Tags } from 'aws-cdk-lib/assertions'; const app = new App(); const stack = new Stack(app, 'MyStack', { tags: { 'tag-name': 'tag-value' } }); const tags = Tags.fromStack(stack); // using a default 'objectLike' Matcher tags.hasValues({ 'tag-name': 'tag-value' }); // or another Matcher tags.hasValues({ 'tag-name': Match.anyValue() }); ``` You can also get the set of tags to test them in other ways: ```ts tags.all() ``` ## Issues ### No tags case One might expect that the case where no tags are present would match `undefined` or `null`, but since the Cloud Assembly API defaults tags to `{}` when none are present, this isn't possible. It's also not practical to directly test the `artifact.manifest.properties.tags` value directly, as there is a legacy case that the API handles. This means that the `artifact.tags` property is the best value to check against. The tests for this PR show that matching with `Match.absent()` will fail when there are no tags, but testing against the empty object will succeed. I think that this behaviour (defaulting to empty) will be OK, but potentially require a callout on the assertion method. ### API method naming The current suggested API went through some evolution, starting with: ```ts class Tags { public static fromStack(stack: Stack) : Tags; public hasTags(props: any): void; public getTags(): { [key: string]: string }; } ``` But this stuttered, and `getTags()` wasn't compatible with Java. I considered: ```ts class Tags { public static fromStack(stack: Stack) : Tags; public hasValues(props: any): void; public values(): { [key: string]: string }; } ``` and ```ts class Tags { public static fromStack(stack: Stack) : Tags; public has(props: any): void; public all(): { [key: string]: string }; } ``` ... before settling on a mix of the two. I think the current iteration fits with the rest of the `assertions` API and makes sense by itself, but very open to changes. Closes #27620. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
Describe the feature
Tags associated with a Cloudformation stack are recorded in the cloud assembly manifest. The existing
Template
andAnnotations
classes inaws-cdk-lib/assertions
allow for simplified testing of some parts of the cloud assembly, but not thetags
member.It is possible to get the data from the manifest by interrogating the cloud assembly, but this is far less straightforward than the existing mechanisms.
Use Case
We have constructs libraries that ensure our stacks are tagged appropriately for a project. Currently tests for this behaviour are possible but not straightforward.
Proposed Solution
I suggest additions the API such that it would be possible to write assertions similar to:
Other Information
It is possible to write these tests outside of the CDK assertions library by interrogating the Cloud Assembly API. 4
Something like the following works:
There are some special cases that occur when looking up details in the assembly in both
Template
andAnnotations
classes. Respectively, handling nested stacks and forcing synthesis. I'm not sure which of these special cases should be handled in the tag case.Template
handling of nested stacks:aws-cdk/packages/aws-cdk-lib/assertions/lib/template.ts
Lines 305 to 308 in 382a0ed
Annotations
forcing synthesis:aws-cdk/packages/aws-cdk-lib/assertions/lib/annotations.ts
Lines 162 to 165 in 382a0ed
I've started on a branch to feel out what an implementation would look like. If the
Tags
API is selected, the PR would be quite small by the looks.Acknowledgements
CDK version used
2.102.0
Environment details (OS name and version, etc.)
N/A
The text was updated successfully, but these errors were encountered: