关于python:Daggerio尝鲜20行代码实现基于多操作系统多python版本兼容性测试并发

5次阅读

共计 2360 个字符,预计需要花费 6 分钟才能阅读完成。

背景

  • Dagger.io 介绍能够参考这一篇 新兴却大牛星散的 Dagger.io
  • 最近在用目前热度很高的提醒工程框架 langchain,遇到一个 bug,打算提一个 Pull Request

内容

  • 提交代码会波及大量本地测试工作:波及 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 版本和操作系统版本的兼容测试,只是做测试环境的筹备,就十分麻烦。
    • 测试效率问题

      • 环境构建以及测试工作最好并发执行,减速测试过程。
  • 有没有一种可能,面对以上繁琐的测试,用一个脚本、大量代码,疾速、优雅地实现?Dagger.io 能够做到
  • 上面介绍 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。
  • 当初国内用 Dagger.io 的人还比拟少,还没有什么中文文档,但我感觉这是一个挺酷的我的项目,只有不浪好好发育,前期必定会倒退的很不错,更多 Dagger.io 的玩法请见官网博客

拓展浏览

  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
正文完
 0