展示HN:Tusk Drift – 将生产流量转化为API测试
Show HN: Tusk Drift – Turn production traffic into API tests

原始链接: https://github.com/Use-Tusk/tusk-drift-cli

## Tusk Drift:API 测试记录与回放 Tusk Drift 是一款 CLI 工具和云服务,用于使用记录的真实流量进行 API 测试。它允许您将这些“轨迹”回放至您的服务,从而实现逼真且可靠的测试。 **主要特性:** * **记录与回放:** 捕获实时流量并在本地或 CI/CD 管道中回放。 * **确定性测试:** 使用本地模拟服务器和动态字段规则(UUID、时间戳)以获得一致的结果。 * **云集成:** 将测试存储在 Tusk Drift Cloud 上,以进行智能回归分析、偏差分类和根本原因识别。 * **AI 驱动的设置:** 使用 AI 代理自动化 SDK 仪器化和配置(支持 Python 和 Node.js)。 * **灵活执行:** 在本地运行测试,并进行过滤、并发控制和结果保存,或通过云端远程运行。 **安装:** 通过 Linux/macOS 上的 `curl` 进行简单安装。建议 Windows 用户使用 WSL。 **配置:** 使用 `.tusk` 目录存储轨迹、结果和日志。配置可以自动化,也可以通过 `.tusk/config.yaml` 文件手动完成。 **了解更多:** [https://github.com/Use-Tusk/tusk-drift-cli](https://github.com/Use-Tusk/tusk-drift-cli)

## Tusk Drift:基于真实流量的真实API测试 Tusk Drift是一个新系统,旨在通过利用生产流量来简化和改进API测试。它不编写和维护脆弱的手工模拟,而是*记录*真实的API请求(包括数据库和HTTP调用),并将其作为确定性测试*回放*到正在运行的服务中。 该系统使用轻量级SDK(目前适用于Python和Node.js)来捕获流量,然后使用CLI工具 (`tusk run`) 对服务进行沙箱化,并通过Unix套接字提供模拟响应。这消除了测试代码或fixtures的需求,使测试始终与实际使用情况保持一致。 开发者们正在将其在CI/CD流水线中使用,甚至将其作为AI编码代理的测试 Harness,为代码变更提供快速反馈,而无需实时依赖项。该项目是开源的,并且可在GitHub上找到:[https://github.com/Use-Tusk/tusk-drift-cli](https://github.com/Use-Tusk/tusk-drift-cli)。
相关文章

原文

Tusk CLI Banner

Tusk Drift is an API test record/replay system that lets you run realistic tests generated from real traffic. This CLI orchestrates local and CI test runs, coordinating with a Tusk Drift SDK and Tusk Drift Cloud.

SDKs:

  • Replay recorded traces against your service under test
  • Deterministic outbound I/O via local mock server
  • JSON response comparison with dynamic field rules (UUIDs, timestamps, dates, etc.)
  • Tusk Drift Cloud: fetch and replay tests stored with Tusk, and upload test results for intelligent classification of regressions in CI/CD checks

Quick install (recommended)

Linux/macOS:

Install the latest version:

curl -fsSL https://cli.usetusk.ai/install.sh | sh

To install a specific version:

curl -fsSL https://cli.usetusk.ai/install.sh | sh -s -- v1.2.3

Linux additional dependencies (for replay sandboxing):

  • Debian/Ubuntu: sudo apt install bubblewrap socat
  • Fedora/RHEL: sudo dnf install bubblewrap socat
  • Arch: sudo pacman -S bubblewrap socat

Without these, sandboxing is disabled and replays run without network isolation. See Architecture - Sandboxing.

Homebrew:

Coming soon.

Windows:

We recommend using WSL for the best experience on Windows. With WSL, you can use the Linux/macOS installation steps above and avoid Windows-specific configuration. For native Windows installation without WSL, expand below to see the steps.

Installation steps Download the latest release from [GitHub Releases](https://github.com/Use-Tusk/tusk-drift-cli/releases/latest):
  1. Download tusk-drift-cli_*_Windows_x86_64.zip (or arm64 for ARM-based Windows)

  2. Extract the ZIP file

  3. Move tusk.exe to a directory in your PATH (e.g., C:\tools\), or add the extracted directory to your PATH:

    Option A: Add to PATH via PowerShell (run as Administrator):

    [Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\path\to\tusk", "User")

    Option B: Add to PATH via System Settings:

    • Press Win + R, type sysdm.cpl, press Enter
    • Click "Environment Variables"
    • Under "User variables", select Path and click "Edit"
    • Click "New" and add the folder containing tusk.exe
    • Click OK to save
  4. Restart your terminal and verify:

Note: Windows requires additional configuration for running tests. See Windows Support for details on TCP communication mode setup.

Download pre-built binaries from GitHub Releases.

# Go 1.25+
git clone https://github.com/Use-Tusk/tusk-drift-cli.git
cd tusk-drift-cli
make deps
make build

tusk --help

AI-powered setup (recommended)

Use our AI agent to automatically set up Tusk Drift for your service:

cd path/to/your/service
export ANTHROPIC_API_KEY=your-api-key
tusk setup

The agent will analyze your codebase, instrument the SDK, create configuration files, and test the setup with recording and replay. Supported languages: Python and Node.js.

Alternatively, use the interactive wizard:

cd path/to/your/service
tusk init

This will guide you to create your .tusk/config.yaml config file. You can also create the .tusk directory and config file manually. See configuration docs.

You will need to record traces for your service. See your respective SDK's guide for more details. Once you have traces recorded, you can replay them with the tusk run command.

Local traces (default):

# Run all tests from local traces
tusk run

# Or specify source
tusk run --trace-dir .tusk/traces
tusk run --trace-file path/to/trace.jsonl
tusk run --trace-id <traceId>

# Common flags
tusk run --filter '^/api/users' --concurrency 10 --enable-service-logs
tusk run --save-results --results-dir .tusk/results

You can use Tusk Drift as API tests in your CI/CD pipeline by running your test suite against commits in your pull requests. Tusk Drift Cloud offers storage of these tests alongside an additional layer of intelligence on deviations detected:

  • Automatic recording of traces based on live traffic in your environment of choice
  • Securely store these traces as test suites
  • Analyze deviations (classification of intended vs unintended deviations), root cause of deviations against your code changes, and suggested fixes.

If you used tusk setup, cloud configuration is included. Otherwise, run the cloud onboarding wizard:

For more details, dive into Tusk Drift Cloud docs.

List traces:

# Local traces
tusk list
tusk list --trace-dir .tusk/traces

# With Tusk Drift Cloud
tusk list --cloud

Interactive TUI (default when attached to a terminal):

tusk run

# Run against Tusk Drift Cloud
tusk run --cloud
tusk run --cloud --trace-test-id <id>           # single test from backend
tusk run --cloud --all-cloud-trace-tests        # run all tests for service

The TUI is best viewed in a window size of at least 150 x 40.

Run headless mode with JSON output for a single test:

tusk run --trace-id <id> --print --output-format=json

How this program uses your .tusk directory:

  • Recordings of your app's traffic will be stored in .tusk/traces by default. Specify traces.dir in your .tusk/config.yaml to override.
  • If --save-results is provided, results will be stored in .tusk/results by default. Specify results.dir in your .tusk/config.yaml to override.
  • If --enable-service-logs or --debug is used, trace replay service logs will be stored in .tusk/logs.

We recommend adding to your .gitignore:

  • .tusk/results
  • .tusk/logs
  • .tusk/traces (if you primarily intend to use Tusk Drift Cloud)

Join our open source community on Slack.

See CONTRIBUTING.md.

See LICENSE.

联系我们 contact @ memedata.com