Dagger尝鲜-20行代码实现基于多操作系统、多python版本兼容性测试

背景

内容

  • 提交代码会涉及大量本地测试工作:涉及python版本向下兼容,多操作系统兼容,langchain项目依赖繁多、环境复杂,使用传统的测试会有如下麻烦
    • 待测试环境多
      • 多python版本:python>=3.8.1,<4.0,小版本加起来有接近60个,不过可以简化一下,每个大版本中只挑选一个小版本,3.8.1、3.9.1、3.10.1、3.11.1,一共4个。
      • 多操作系统:linux/amd64、darwin/amd64一共2个。
      • 根据上面python版本和操作系统版本的组合(笛卡尔乘积),就有种8种测试环境的可能,如果涉及更多python版本和操作系统版本的兼容测试,只是做测试环境的准备,就非常麻烦。
    • 测试效率问题
      • 环境构建以及测试工作最好并发执行,加速测试过程。
  • 有没有一种可能,面对以上繁琐的测试,用一个脚本、少量代码,快速、优雅地完成?DaggerIO可以做到
  • 下面介绍Dagger.io的具体操作
    • 在项目根目录创建文件dagger_test_multi_versions.py,内容如下
      • """
        文件名:dagger_test_multi_versions.py
        描述:完成跨多操作系统、多python版本的测试
        """
        import sys
        import anyio
        import dagger

        async def test():
        platforms = ["linux/amd64", "darwin/amd64"]
        versions = ["3.8.1", "3.9.1", "3.10.1", "3.11.1"]

        async with dagger.Connection(dagger.Config(log_output=sys.stderr)) as client:
        # 在本地项目根目录执行
        src = client.host().directory(".")

        async def test_platform_version(platform:str, version: str):
        python = (
        client.container(platform=dagger.Platform(platform))
        .from_(f"python:{version}-slim")
        # 挂载项目代码
        .with_directory("/src", src)
        # CWD
        .with_workdir("/src")
        # 安装依赖
        .with_exec(["pip", "install", "-r", "requirements.txt"])
        # 执行测试
        .with_exec(["pytest", "tests"])
        )

        print(f"Starting tests for Python {version}")

        # 异步并发执行
        await python.sync()

        print(f"Tests for Python {version} succeeded!")

        # 相当于join操作
        async with anyio.create_task_group() as tg:
        for platform in platforms:
        for version in versions:
        tg.start_soon(test_platform_version, platform, version)

        print("All tasks have finished")

        anyio.run(test)
    • 执行 python dagger_test_multi_versions.py 即可开始测试

思考

  • Dagger.io的意义在于
    • 让各种构建、测试、发布的pipeline接入python生态(不仅是python SDK,官方已支持TypeScript、JavaScript、Go、GraphQL的SDK),让整个过程有了更多想象空间。
    • 适用于全语言、全平台,兼容现有主流 CI/CD工具,如Jenkins等。
    • 摆脱传统YAML文件的限制,让pipeline各个组件可重用、可缓存、从而极大地加速构建、测试、发布的流程,节约生命,比如Dagger in Action: Cutting Deployment Times from 3 Hours to 3 Minutes
  • 现在国内DaggerIO的人还比较少,还没有什么中文文档,但我觉得这是一个挺酷的项目,只要不浪好好发育,后期肯定会发展的很不错,更多DaggerIO的玩法请见官方博客

拓展阅读

  1. Introducing Dagger: a new way to create CI/CD pipelines
  2. Build, Test and Publish a Spring Application with Dagger
  3. Dagger in Action: Cutting Deployment Times from 3 Hours to 3 Minutes
  4. Reproducible Builds with Dagger
  5. Dagger in Action: Going from Pull Request to Production Faster at Discern
  6. Dagger in Action: How Flipt Improved Coverage and Build Times with Dagger
  7. Using Dockerfiles with Dagger
  8. Dagger in Action: Building Grafana for Multiple Architectures in 8 Minutes or Less
  9. Use Dagger with Multi-stage Container Builds
  10. Understand Multi-Platform Support
  11. Use Dagger with Private Git Repositories

柚子

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Index