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/assert): Unable to pass test if a resource doesn't exist #15184
Comments
Hey @longtv2222, I think in theory what you posted should work, can you tell me what specifically is going wrong when the test doesn't pass? |
I created a sample repo for this https://github.com/longtv2222/cdk-assert-error. (Created with It seems to me that the if statement didn't verify the existence of Queue. |
For sanity check, I tried import { expect as expectCDK, haveResourceLike } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import * as CdkAssertError from '../lib/cdk-assert-error-stack';
test('SQS Queue Exist', () => {
const app = new cdk.App();
// WHEN
const stack = new CdkAssertError.CdkAssertErrorStack(app, 'MyTestStack');
// THEN
if (!haveResourceLike("AWS::SQS::Queue")) //Test if the if statement does anything
expectCDK(stack).to(haveResourceLike("AWS::SQS::Queue", {
VisibilityTimeout: 300
}));
}); with import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
export class CdkAssertErrorStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const queue = new sqs.Queue(this, 'CdkAssertErrorQueue', {
visibilityTimeout: cdk.Duration.seconds(200) //Different timeout value
});
}
} |
Hmm I'm finding the same thing as you. The if statement isn't working, I'll research this and get back to you |
For anyone who encounters this problem, my work around is to wrap haveResourceLike inside a try catch to check if a resource exists or not. import { expect as expectCDK, haveResourceLike } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import * as CdkAssertError from '../lib/cdk-assert-error-stack';
test('SQS Queue Exist', () => {
const app = new cdk.App();
// WHEN
const stack = new CdkAssertError.CdkAssertErrorStack(app, 'MyTestStack');
// THEN
let sqsExist = true;
try {
expectCDK(stack).to(haveResourceLike("AWS::SQS::Queue"));
} catch (error) {
sqsExist = false; //SQS doesn't exist if haveResourceLike throws an error
}
if (sqsExist) {
expectCDK(stack).to(haveResourceLike("AWS::SQS::Queue", {
VisibilityTimeout: 300
}));
} else {
console.warn('Resource does not exist but we pass them anyway');
}
});
|
|
❓ General Issue
The Question
I want to write validation tests and make it a library so that every stack can follow best security practices, naming convention, ... for my cdk stacks.
I've been looking at aws-assert for it and it seems not possible to run test only if a resource exists in the stack.
For example, if I were to write:
without any bucket in the stack then that would fail as expected.
The issue #12452 had similar problem as mine and I tried the proposed solution which was
However, the test didn't pass.
Is there anyway to bypass this limitation?
Environment
Other information
The text was updated successfully, but these errors were encountered: