背景
- 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 sysimport anyioimport daggerasync 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的玩法请见官网博客
拓展浏览
- Introducing Dagger: a new way to create CI/CD pipelines
- Build, Test and Publish a Spring Application with Dagger
- Dagger in Action: Cutting Deployment Times from 3 Hours to 3 Minutes
- Reproducible Builds with Dagger
- Dagger in Action: Going from Pull Request to Production Faster at Discern
- Dagger in Action: How Flipt Improved Coverage and Build Times with Dagger
- Using Dockerfiles with Dagger
- Dagger in Action: Building Grafana for Multiple Architectures in 8 Minutes or Less
- Use Dagger with Multi-stage Container Builds
- Understand Multi-Platform Support
- Use Dagger with Private Git Repositories