首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们能否在运行时合成器中使用AWS断言Jest测试Lambda是否与正确的子网连接?

我们能否在运行时合成器中使用AWS断言Jest测试Lambda是否与正确的子网连接?
EN

Stack Overflow用户
提问于 2022-02-07 09:31:23
回答 1查看 314关注 0票数 3

我有一个全局堆栈,其中有预定义的VPCs和子网。

代码语言:javascript
复制
  export class GlobalStack extends InternalStack {
    /**
     * Shared VPC Instance
     */
    public readonly vpc = Vpc.fromLookup(this, 'vpc', {
      vpcId: process.env.VPC_ID!,
    })
  
    /**
     * Private Subnet 1 
     */
    public privateSubnet1 = new Subnet(this, 'subnet-1', {
      vpcId: this.vpc.vpcId,
      cidrBlock: 'xx.x.x.x/xx',
      availabilityZone: `${this.region}-1`,
    })
  
    /**
     * Private Subnet 2 
     */
    public privateSubnet2 = new Subnet(this, 'subnet-2', {
      vpcId: this.vpc.vpcId,
      cidrBlock: 'xx.x.x.x/xx',
      availabilityZone: `${this.region}-2`,
    })
  
    /**
     * Private Subnet 3 
     */
    public privateSubnet3 = new Subnet(this, 'subnet-3', {
      vpcId: this.vpc.vpcId,
      cidrBlock: 'xx.x.x.x/xx',
      availabilityZone: `${this.region}-3`,
    })

    public readonly apiGatewayVpcEndpoint = this.vpc.addInterfaceEndpoint(
        'ApiGateway',
        {
          service: InterfaceVpcEndpointAwsService.APIGATEWAY,
          subnets: {
            subnets: [
              this.privateSubnet1,
              this.privateSubnet2,
              this.privateSubnet3,
            ],
          },
        }
      )
}

一旦我得到这些vpc和子网,我就把它们传递给我们的lambda,它有一个增强的节点js lambda (一个围绕节点js lambda函数的包装器),在这里我检查vpc支持程序是否可用,我将添加定义的三个子网。

我的lambda函数的构造函数:

代码语言:javascript
复制
constructor(scope: Construct, id: string, props: EnhancedNodeJsLambdaProps) {
    super(scope, id, {
      ...props,
      ...(props.vpc && {
        vpcSubnets: props.vpc.selectSubnets({
          subnetFilters: [
            SubnetFilter.containsIpAddresses(['xx.x.x.x/xx', 'xx.x.x.x/xx', 'xx.x.x.x/xx']
            ),
          ],
        }),
      }),
      runtime: props.runtime || Runtime.NODEJS_12_X,
      tracing: Tracing.ACTIVE,
    })
}

所以现在,当我尝试测试我的lambda时,无论子网是否附加到它,我要么得到一些虚拟的az值,要么它返回lambda与子网没有连接,我如何测试相同的子网?

FYR,我会在下面附上一些测试用例

代码语言:javascript
复制
it('testing vpc subnets ', async () => {
    const app = new cdk.App()
    const topicsStack = new cdk.Stack(app, 'TopicsStack')
    const globalStack = await new GlobalStack(app, 'global-stack', {
      stackName: 'global-stack',
      description: 'Global Resources (Shared at the account level)',
      env: {
        account: '11111111',
        region: 'us-east-1',
      },
      envName: 'test',
    })
    let newLambda = new EnhancedNodeJsLambda(topicsStack, 'test-lambda', {
      entry,
      connectionReuseEnabled: true,
      vpc: globalStack.vpc,
    })

    console.log(
        globalStack.vpc.selectSubnets({
          subnetFilters: [
            SubnetFilter.containsIpAddresses(['xx.x.x.x/xx', 'xx.x.x.x/xx', 'xx.x.x.x/xx']),
          ],
        }).availabilityZones
      )
    //how to test subnets are properly linked?
  })

同样关于虚拟值,上面测试用例中的控制台日志将返回一些虚拟的az值,而不是我的代码值:

控制台日志返回

代码语言:javascript
复制
[ 'dummy1a', 'dummy1b' ]

我试图通过将下面的代码添加到上面的测试用例来测试连接

代码语言:javascript
复制
    const othertemp = Template.fromStack(topicsStack)
    othertemp.hasResourceProperties('AWS::Lambda::Function', {
      VpcConfig: {
        SubnetIds: globalStack.vpc.selectSubnets({
          subnetFilters: [
            SubnetFilter.containsIpAddresses(['xx.x.x.x/xx', 'xx.x.x.x/xx', 'xx.x.x.x/xx']
            ),
          ],
        }),
      },
    })

但它没能说

代码语言:javascript
复制
    with the following mismatches:
        Expected type object but received array at /Properties/VpcConfig/SubnetIds (using objectLike matcher)

另外,子网id和az的是虚拟值,而不是我想要的值。

我不知道它为什么返回虚拟值而不是预定义的值,而且我也不知道如何测试lambda是否与正确的子网相关联。

EN

回答 1

Stack Overflow用户

发布于 2022-07-22 15:10:46

嗯,我不知道如何用CDK断言来完成它,但是我能够用开玩笑的断言来完成它:

代码语言:javascript
复制
expect(myFunction.Properties.VpcConfig.SubnetIds).toEqual('my-subnet-ids');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71016240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档