AWSCDKUnittestunabletoMockNetworkLoadBalancerResource
创始人
2024-09-24 03:00:49
0

AWS CDK Unittest不能模拟NetworkLoadBalancer资源,并且此问题会导致难以进行单元测试。为了解决这个问题,可以使用AWS CDK提供的CDKIntegrationTest工具和MockIntegrationTest构建一组集成测试来测试网络负载平衡器资源。以下是一个示例代码,该代码使用了CDKIntegrationTest和MockIntegrationTest:

import boto3
from aws_cdk import (
    core as cdk,
    aws_ec2 as ec2,
    aws_elasticloadbalancingv2 as elbv2,
    testing as cdk_testing
)

class MyStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        vpc = ec2.Vpc(self, "MyVPC")

        alb = elbv2.NetworkLoadBalancer(
            self,
            "MyALB",
            vpc=vpc,
            internet_facing=False,
            load_balancer_name="MyLoadBalancer"
        )

        core.CfnOutput(self, "ALBARN", value=alb.load_balancer_arn)

class TestMyStack(unittest.TestCase):
    def test_stack(self):
        app = cdk.App()
        MyStack(app, "stack")

        # create a mock of the network load balancer
        mock_elbv2 = cdk_testing.MockAws(lambda: boto3.Session())
        mock_elbv2.add_mock("elbv2", "create_load_balancer", side_effect=Exception("cannot create network load balancer"))
        mock_elbv2.add_mock("elbv2", "delete_load_balancer", return_value={})

        # prepare the integration test
        integ_test = cdk_testing.CDKIntegrationTest(
            app,
            "test-stack",
            cloud_assembly_subdirectory="test-fixtures/test-stack.out"
        )
        # provide the mock to the integration test
        integ_test.set_aws(mock_elbv2.aws())

        # run the integration test and validate that the cloudformation stack was created successfully
        integ_test.synth()
        integ_test.deploy(expect_changes=True)

        stack = integ

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...