Skip to content
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

Closed
longtv2222 opened this issue Jun 17, 2021 · 6 comments
Closed

(@aws-cdk/assert): Unable to pass test if a resource doesn't exist #15184

longtv2222 opened this issue Jun 17, 2021 · 6 comments
Assignees
Labels
@aws-cdk/assert Related to the @aws-cdk/assert package guidance Question that needs advice or information. investigating This issue is being investigated and/or work is in progress to resolve the issue.

Comments

@longtv2222
Copy link
Contributor

longtv2222 commented Jun 17, 2021

❓ 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:

import { haveResourceLike,expect } from "@aws-cdk/assert";
import { mystack } from "..."

test('Bucket versioning must be enabled', () => {
        expect(mystack).to(haveResourceLike("AWS::S3::Bucket", {
            VersioningConfiguration: {
                Status: "Enabled"
            }
        }));
})

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

import { haveResourceLike,expect } from "@aws-cdk/assert";
import { mystack } from "..."

test('Bucket versioning must be enabled', () => {
    if (haveResourceLike("AWS::S3::Bucket"))
        expect(mystack).to(haveResourceLike("AWS::S3::Bucket", {
            VersioningConfiguration: {
                Status: "Enabled"
            }
        }));
})

However, the test didn't pass.

Is there anyway to bypass this limitation?

Environment

  • CDK CLI Version: 1.109
  • Module Version: 1.109.0
  • Node.js Version: v12.18.3
  • OS: Windows 10
  • Language (Version): TypeScript (4.3.3)

Other information

@longtv2222 longtv2222 added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Jun 17, 2021
@github-actions github-actions bot added the @aws-cdk/assert Related to the @aws-cdk/assert package label Jun 17, 2021
@peterwoodworth
Copy link
Contributor

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?

@peterwoodworth peterwoodworth added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Jun 17, 2021
@longtv2222
Copy link
Contributor Author

I created a sample repo for this https://github.com/longtv2222/cdk-assert-error. (Created with cdk init sample-app --language typescript)

image

It seems to me that the if statement didn't verify the existence of Queue.

@longtv2222
Copy link
Contributor Author

longtv2222 commented Jun 17, 2021

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
    });
    
  }
}

However the test passed
image

@peterwoodworth peterwoodworth removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 17, 2021
@peterwoodworth
Copy link
Contributor

Hmm I'm finding the same thing as you. The if statement isn't working, I'll research this and get back to you

@peterwoodworth peterwoodworth added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Jun 17, 2021
@peterwoodworth peterwoodworth self-assigned this Jun 17, 2021
@longtv2222
Copy link
Contributor Author

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.
Sample code is as below:

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');
  }
});

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assert Related to the @aws-cdk/assert package guidance Question that needs advice or information. investigating This issue is being investigated and/or work is in progress to resolve the issue.
Projects
None yet
Development

No branches or pull requests

3 participants