简体   繁体   English

AWS CDK 断言 - 查看堆栈中的单个资源

[英]AWS CDK Assertions - Looking at Individual Resources In A Stack

Current Situation:现在的情况:

  1. I have a file that has 11 different NodejsFunction s.我有一个包含 11 个不同NodejsFunction的文件。 I am looking to write assertions with CDK Template .我正在寻找使用CDK Template编写断言。
  2. I have code that checks the whole stack, and says "Is there a Lambda handler?": template.hasResourceProperties("AWS::Lambda::Function", { Handler: "index.handler" }))我有检查整个堆栈的代码,并说“是否有 Lambda 处理程序?”: template.hasResourceProperties("AWS::Lambda::Function", { Handler: "index.handler" }))

Requirement:要求:

How do I ensure each NodejsFunction function has Handler: "index.handler" ?如何确保每个NodejsFunction function 都有Handler: "index.handler" Can I narrow down to specific public readonly lambdaExample: NodejsFunction from the stack, or map over the services in the stack?我可以缩小到特定的public readonly lambdaExample: NodejsFunction从堆栈,或 map 在堆栈中的服务?

Current Test:当前测试:

import { Template } from "aws-cdk-lib/assertions";
import { createStacks } from "../../bin/template";

    describe.only("lambdaStack", () => {
      let allStacks, template: Template;
      beforeAll(async () => {
        allStacks= await createStacks(true);
        template = Template.fromStack(allStacks.lambdaStack);
      });
    
      it("should have Handler = 'handler'", () => template.hasResourceProperties("AWS::Lambda::Function", { Handler: "index.handler" }));
    });

To assert every resource has the desired property, use allResourceProperties :要断言每个资源都具有所需的属性,请使用allResourceProperties

template.allResourcesProperties("AWS::Lambda::Function", {
  Handler: "index.handler",
});

To assert a given resource (by Logical ID) has the desired property, filter and assert:要断言给定资源(通过逻辑 ID)具有所需的属性,请过滤并断言:

expect(
  Object.entries(template.findResources("AWS::Lambda::Function")).filter(
    ([k, v]) =>
      k.match(/^LambdaExample[A-F0-9]{8}$/) &&
      v["Properties"]?.["Handler"] === "index.handler"
  )
).toHaveLength(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM