aws / aws/aws-cdk

aws-cdk-lib/assertions: Cannot run testing against ProductStack objects

Open
#24,990 7 comments 1 reaction 1 assignee Assigned to @pahud View on GitHub
@aws-cdk/aws-servicecatalog bug effort/medium p2 package/tools
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

Reopening this issue for https://github.com/aws/aws-cdk/issues/24988 from [thebillest](https://github.com/thebillest)

### Discussed in https://github.com/aws/aws-cdk/discussions/24989

Originally posted by **thebillest** April 7, 2023
### Describe the bug

When trying to perform testing with cdk-nag, according to the methodology listed [on this AWS blog](https://aws.amazon.com/blogs/devops/manage-application-security-and-compliance-with-the-aws-cloud-development-kit-and-cdk-nag/), it fails if the stack passed into `Annotations.fromStack()` is a `ProductStack`.

### Expected Behavior

The expectation would be that the test would pass/fail based on the CDK-Nag output/findings.

### Current Behavior

The mentioned code will fail with an error similar to: `Unable to find artifact with id "TestStackTestProductStack`

### Reproduction Steps

```typescript
import { Annotations, Match } from 'aws-cdk-lib/assertions';
import { App, Aspects, Stack } from 'aws-cdk-lib';
import { AwsSolutionsChecks } from 'cdk-nag';
import { CdkTestProductStack } from '../lib/cdk_test-product-stack';

describe('cdk-nag AwsSolutions Pack', () => {
let productStack: ProductStack;
let stack: Stack;
let app: App;
// In this case we can use beforeAll() over beforeEach() since our tests
// do not modify the state of the application
beforeAll(() => {
// GIVEN
app = new App();
stack = new Stack(app, 'test-stack');
productStack = new CdkTestProductStack(stack, 'test-productStack');

// WHEN
Aspects.of(app).add(new AwsSolutionsChecks());
});

// THEN
test('No unsuppressed Warnings', () => {
const warnings = Annotations.fromStack(productStack).findWarning(
'*',
Match.stringLikeRegexp('AwsSolutions-.*')
);
expect(warnings).toHaveLength(0);
});

test('No unsuppressed Errors', () => {
const errors = Annotations.fromStack(productStack).findError(
'*',
Match.stringLikeRegexp('AwsSolutions-.*')
);
expect(errors).toHaveLength(0);
});
});
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.69.0

### Framework Version

_No response_

### Node.js Version

19.8.1

### OS

macOS Ventura 13.2.1

### Language

Typescript

### Language Version

_No response_

### Other information

_No response_

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.