# auto_test **Repository Path**: shaoninghouse/auto_test ## Basic Information - **Project Name**: auto_test - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-08 - **Last Updated**: 2026-05-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Resource Tester Framework (RTF) 一个支持容器和虚拟机的统一资源测试框架。 ## 1. 项目概述 ### 1.1 核心目标 - **统一抽象**:通过适配器模式,抽象容器和虚拟机的差异,提供统一的操作接口 - **配置驱动**:通过 YAML/JSON 配置声明式地创建资源(容器/VM) - **命令执行**:在容器或虚拟机中执行命令,支持脚本、探测、检查等操作 - **资源回收**:自动清理创建的资源,确保环境干净 - **分布式执行**:支持多 Agent 并行执行,结果统一上报 ### 1.2 支持的资源类型 | 类型 | 后端 | 说明 | |------|------|------| | **Container** | Podman | 无守护进程容器(推荐) | | **Container** | Docker | Docker 容器 | | **VM** | Libvirt/KVM | 虚拟机 | --- ## 2. 架构设计 ### 2.1 整体架构 ``` ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ RTF 架构 │ └─────────────────────────────────────────────────────────────────────────────────────┘ ┌─────────────────┐ │ Config │ │ (YAML/JSON) │ └────────┬────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ RTF Core (核心引擎) │ │ ┌───────────────────────────────────────────────────────────────────────────────┐ │ │ │ │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ │ │ Parser │───▶│ Resource │───▶│ Executor │───▶│ Reporter │ │ │ │ │ │ (解析器) │ │ Manager │ │ (执行器) │ │ (报告器) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ - 解析配置 │ │ - 创建资源 │ │ - 执行命令 │ │ - 结果汇总 │ │ │ │ │ │ - 验证参数 │ │ - 销毁资源 │ │ - 超时控制 │ │ - 生成报告 │ │ │ │ │ │ - 变量替换 │ │ - 状态监控 │ │ - 日志收集 │ │ │ │ │ │ │ └─────────────┘ └──────┬──────┘ └──────┬──────┘ └─────────────┘ │ │ │ │ │ │ │ │ │ └───────────────────────────────┼───────────────────┼────────────────────────────┘ │ │ │ │ │ └──────────────────────────────────┼───────────────────┼───────────────────────────────┘ │ │ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ Adapter Layer (适配器层) │ │ │ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ │ │ PodmanAdapter │ │ DockerAdapter │ │ LibvirtAdapter │ │ │ │ │ │ │ │ │ │ │ │ podman-remote │ │ docker-go SDK │ │ libvirt-go │ │ │ │ │ │ │ │ │ │ │ └────────┬────────┘ └────────┬────────┘ └────────┬────────┘ │ │ │ │ │ │ └────────────┼──────────────────────┼──────────────────────┼──────────────────────────┘ │ │ │ ▼ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ Infrastructure (基础设施) │ │ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │ Podman │ │ Docker │ │ KVM │ │ │ │ Daemon │ │ Daemon │ │/libvirt │ │ │ │(optional)│ │ │ │ │ │ │ └─────────┘ └─────────┘ └─────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────────────┘ ``` ### 2.2 核心组件 #### 2.2.1 配置解析器 (Parser) ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ 配置解析器 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ 输入: test.yaml 配置 │ │ │ │ 解析后输出: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ TestSpec: │ │ │ │ ├── Resources: 资源规格列表 │ │ │ │ ├── Commands: 命令规格列表 │ │ │ │ └── Variables: 变量映射表 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ │ 核心职责: │ │ - 解析 YAML/JSON 配置文件 │ │ - 验证配置参数合法性 │ │ - 执行变量替换 ({{ENV.VAR_NAME}} 语法) │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` #### 2.2.2 资源管理器 (Resource Manager) ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ 资源管理器 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ 职责: │ │ 1. 根据配置创建资源(容器/VM) │ │ 2. 管理资源生命周期 │ │ 3. 提供资源状态查询 │ │ 4. 资源清理 │ │ │ │ 资源状态机: │ │ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │ Created │───▶│Starting │───▶│ Running │───▶│ Stopped │───▶│Deleted │ │ │ │ (已创建) │ │ (启动中) │ │ (运行中) │ │ (已停止) │ │ (已删除) │ │ │ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌─────────┐ ┌─────────┐ │ │ │ Error │ │Timeout │ │ │ │ (错误) │ │(超时) │ │ │ └─────────┘ └─────────┘ │ │ │ │ 资源操作接口: │ │ │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ interface ResourceAdapter { │ │ │ │ Create(ctx, Spec) (Handle, error) // 创建资源 │ │ │ │ Start(ctx, Handle) error // 启动资源 │ │ │ │ Stop(ctx, Handle) error // 停止资源 │ │ │ │ Delete(ctx, Handle) error // 删除资源 │ │ │ │ Exec(ctx, Handle, ExecSpec) Result // 执行命令 │ │ │ │ Status(ctx, Handle) State // 获取状态 │ │ │ │ WaitForReady(ctx, Handle, timeout) // 等待就绪 │ │ │ │ } │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` #### 2.2.3 命令执行器 (Executor) ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ 命令执行器 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ 执行流程: │ │ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │ Prepare │───▶│ Exec │───▶│ Collect │───▶│ Cleanup │ │ │ │ (准备) │ │ (执行) │ │ (收集) │ │ (清理) │ │ │ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │ │ │ ▲ │ │ │ │ │ │ └────────────────────────────────────────────────────┘ │ │ (always_run 命令) │ │ │ │ 执行规格 (ExecSpec): │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ command: string 要执行的命令/脚本 │ │ │ │ workdir: string 工作目录 │ │ │ │ env: map[string]string 环境变量 │ │ │ │ user: string 执行用户 │ │ │ │ timeout: duration 超时时间 │ │ │ │ tty: bool 是否分配 TTY │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ │ 执行结果 (ExecResult): │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ exit_code: int 退出码 │ │ │ │ stdout: string 标准输出 │ │ │ │ stderr: string 标准错误 │ │ │ │ duration: duration 执行时长 │ │ │ │ start_time: timestamp 开始时间 │ │ │ │ end_time: timestamp 结束时间 │ │ │ │ status: Success|Failed|Timeout|Error │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` ### 2.3 适配器实现 #### 2.3.1 Podman 适配器 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ Podman 适配器 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ 架构: │ │ │ │ RTF ──────────────────────────────────────────▶ Podman API │ │ HTTP (podman-remote) (Unix Socket/HTTP) │ │ │ │ 特点: │ │ - 无需 root 权限(rootless 模式) │ │ - 支持 podman-remote 远程连接 │ │ - 镜像管理:支持拉取、构建、删除 │ │ - 网络管理:支持创建自定义网络 │ │ - 卷管理:支持绑定挂载 │ │ │ │ Podman API 映射: │ │ │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ RTF 操作 │ Podman API │ │ │ │ ─────────────────────────────────────────────────────────────────── │ │ │ │ Create │ POST /containers/create │ │ │ │ Start │ POST /containers/{id}/start │ │ │ │ Stop │ POST /containers/{id}/stop │ │ │ │ Delete │ DELETE /containers/{id} │ │ │ │ Exec │ POST /containers/{id}/exec + POST /exec/{id}/start│ │ │ │ Status │ GET /containers/{id}/json │ │ │ │ Logs │ GET /containers/{id}/logs │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` #### 2.3.2 Libvirt/KVM 适配器 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ Libvirt/KVM 适配器 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ 架构: │ │ │ │ RTF ──────────────▶ Libvirt API ──────────────▶ KVM/QEMU │ │ (libvirt-go) (TCP/SSH/TLS) │ │ │ │ 特点: │ │ - 完全虚拟化,完整操作系统支持 │ │ - 支持快照和回滚 │ │ - 支持多种网络模式(bridge/nat/passthrough) │ │ - 支持云镜像(cloud-init) │ │ │ │ VM 命令执行方式: │ │ │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ 方式 1: SSH (推荐) │ │ │ │ - VM 需要预配置 SSH 服务 │ │ │ │ - VM 需要预配置 SSH 密钥或密码 │ │ │ │ - VM 需要已知 IP 地址 │ │ │ │ │ │ │ │ 方式 2: QEMU Guest Agent │ │ │ │ - VM 需要安装 qemu-guest-agent │ │ │ │ - 需要启用 SPICE + QGA channel │ │ │ │ │ │ │ │ 方式 3: Cloud-Init (仅初始化) │ │ │ │ - 用于首次启动时的初始化(安装软件、配置网络) │ │ │ │ - 不适合运行时命令执行 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` ### 2.4 工作流程 #### 2.4.1 完整执行流程 ``` ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ RTF 执行流程 │ └─────────────────────────────────────────────────────────────────────────────────────┘ User/CLI RTF Core Resource Manager Adapter │ │ │ │ │──[1] 加载配置────────▶│ │ │ │ │──[2] 解析配置──────────▶│ │ │ │ │ │ │ │──[3] 选择适配器────────▶│ │ │ │ │──[4] 创建资源──────────────▶│ │ │ │◀──[5] 资源句柄─────────────│ │ │ │ │ │ │──[6] 启动资源──────────▶│──[7] Start ─────────────▶│ │ │ │◀──[8] Running ───────────│ │ │ │ │ │ │──[9] 等待就绪──────────▶│──[10] 检查状态──────────▶│ │ │ │◀──[11] Ready? ───────────│ │ │ │ │ │ │──[12] 执行命令 ───────▶│──[13] Exec ─────────────▶│ │◀──[14] 命令输出──────│◀──[15] 结果────────────│◀──[16] ExecResult ───────│ │ │ │ │ │ │──[17] 清理命令─────────▶│──[18] Exec ─────────────▶│ │ │ (always_run) │◀──[19] Done ──────────────│ │ │ │ │ │ │──[20] 停止资源─────────▶│──[21] Stop ─────────────▶│ │ │ │◀──[22] Stopped ───────────│ │ │ │ │ │ │──[23] 删除资源─────────▶│──[24] Delete ───────────▶│ │ │ │◀──[25] Deleted ──────────│ │ │ │ │ │◀──[26] 测试报告──────│──[27] 生成报告─────────│ │ ``` #### 2.4.2 命令执行流程 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ 命令执行详细流程 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ Container 执行: │ │ ┌─────────────────────────────────────────────────────────────────────┐ │ │ │ │ │ │ │ RTF Exec ──▶ PodmanAdapter ──▶ Podman API │ │ │ │ │ │ │ │ │ ├── POST /containers/{id}/exec │ │ │ │ ├── POST /exec/{exec_id}/start │ │ │ │ └── 返回 ExecResult (exit_code, stdout, stderr) │ │ │ │ │ │ │ └─────────────────────────────────────────────────────────────────────┘ │ │ │ │ VM 执行: │ │ ┌─────────────────────────────────────────────────────────────────────┐ │ │ │ │ │ │ │ RTF Exec ──▶ LibvirtAdapter ──▶ SSH │ │ │ │ │ │ │ │ │ ├── SSH user@vm_ip "command" │ │ │ │ └── 返回 ExecResult │ │ │ │ │ │ │ └─────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` --- ## 3. 命令结果收集设计 ### 3.1 核心设计理念 每个命令执行后,**独立存储**其完整结果,包括: - 命令名称和状态 - 标准输出 (stdout) - 标准错误 (stderr) - 退出码和执行时长 - 执行时间戳 ``` ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ 命令结果收集架构 │ └─────────────────────────────────────────────────────────────────────────────────────┘ 配置文件中的命令序列: ┌─────────────────────────────────────────────────────────────────────┐ │ commands: │ │ - name: install # ← 命令 1 │ │ - name: upgrade # ← 命令 2 │ │ - name: uninstall # ← 命令 3 │ │ - name: verify # ← 命令 4 │ │ - name: cleanup # ← 命令 5 (always_run) │ └─────────────────────────────────────────────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────┐ │ 每个命令产生独立的结果对象 │ └──────────────────────────────────────────────────┘ │ ┌───────────────────┼───────────────────┬───────────────────┐ ▼ ▼ ▼ ▼ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ Result #1 │ │ Result #2 │ │ Result #3 │ │ Result #4 │ │ install │ │ upgrade │ │ uninstall │ │ verify │ │ ✓ SUCCESS │ │ ✓ SUCCESS │ │ ✗ FAILED │ │ skipped │ └───────────────┘ └───────────────┘ └───────────────┘ └───────────────┘ ``` ### 3.2 数据结构定义 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ 数据结构 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ CommandResult 单个命令的执行结果: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ id: int 命令序号 (1, 2, 3...) │ │ │ │ name: string 命令名称 (来自配置) │ │ │ │ status: CommandStatus 执行状态 │ │ │ │ exit_code: int 退出码 │ │ │ │ start_time: timestamp 开始时间 │ │ │ │ end_time: timestamp 结束时间 │ │ │ │ duration_ms: int64 执行时长 (毫秒) │ │ │ │ stdout: string 标准输出 │ │ │ │ stderr: string 标准错误 │ │ │ │ resource: string 执行所在的资源名称 │ │ │ │ error: CommandError 错误信息 (可选) │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ │ CommandStatus 命令执行状态: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ SUCCESS - 执行成功 │ │ │ │ FAILED - 执行失败 │ │ │ │ SKIPPED - 跳过 (因前置命令失败) │ │ │ │ TIMEOUT - 执行超时 │ │ │ │ ERROR - 执行错误 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ │ TestResult 整个测试任务的聚合结果: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ test_name: string 测试名称 │ │ │ │ test_id: string 测试 ID │ │ │ │ status: TestStatus 测试状态 │ │ │ │ pass_count: int 成功数量 │ │ │ │ fail_count: int 失败数量 │ │ │ │ skip_count: int 跳过数量 │ │ │ │ commands: []CommandResult 命令结果列表 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` ### 3.3 存储结构 每个命令的结果**独立存储**,可以单独访问: ``` /var/lib/rtf/results/ └── test-{test_id}/ ├── metadata.json # 测试元信息 ├── summary.json # 汇总结果 │ ├── commands/ # 每个命令的独立结果 │ ├── 01-install.json # 命令 1 结果 │ ├── 02-upgrade.json # 命令 2 结果 │ ├── 03-uninstall.json # 命令 3 结果 │ ├── 04-verify.json # 命令 4 (SKIPPED) │ └── 05-cleanup.json # 命令 5 (always_run) │ └── artifacts/ # 收集的产物 ``` ### 3.4 执行流程与结果收集 ``` RTF Core Result Collector Storage │ │ │ │──[1] 执行命令 1 ──────────▶│ │ │◀──[2] ExecResult ───────────│ │ │ │──[3] 封装 CommandResult ───▶│ │ │ │ │──[4] 执行命令 2 ──────────▶│ │ │ (如果命令 1 成功) │ │ │◀──[5] ExecResult ───────────│ │ │ │──[6] 封装 CommandResult ───▶│ │ │ │ │──[7] 执行命令 3 ──────────▶│ │ │ (如果命令 2 失败) │ │ │ │──[8] CommandResult ───────▶│ │ │ (Status: SKIPPED) │ │ │ │ │──[9] 执行 cleanup ─────────▶│ │ │ (always_run) │ │ │◀──[10] ExecResult ──────────│ │ │ │──[11] 汇总 TestResult ─────▶│ │ │ │ │◀──[12] 最终报告 ────────────│ │ ``` --- ## 4. 分布式执行与结果上报 ### 4.1 整体架构 支持多 Agent 并行执行场景,各 Agent 完成后通过 HTTP 上报结果到 Coordinator,**不依赖任何中间件**(如消息队列、数据库等)。 ``` ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ 分布式执行架构 │ └─────────────────────────────────────────────────────────────────────────────────────┘ ┌─────────────────────────────────────────────────────────────┐ │ Coordinator │ │ ┌─────────────────────────────────────────────────────┐ │ │ │ HTTP Server (Go 内置) │ │ │ │ │ │ │ │ POST /api/results - 接收 Agent 上报 │ │ │ │ GET /api/results/:id - 查询单个结果 │ │ │ │ GET /api/summary - 获取汇总 │ │ │ └─────────────────────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ results/test-xxx/ │ │ ├── summary.json # 汇总 │ │ └── agents/ │ │ ├── agent-001.json │ │ ├── agent-002.json │ │ └── agent-003.json │ └─────────────────────────────────────────────────────────────┘ ▲ ▲ │ │ Agent 上报 Agent 执行 (HTTP POST) (本地执行) ``` ### 4.2 数据结构定义 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ 分布式数据结构 │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ AgentResult Agent 上报的完整结果: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ agent_id: string Agent 标识 │ │ │ │ test_id: string 测试任务 ID │ │ │ │ commands: []CommandResult 命令执行结果列表 │ │ │ │ timestamp: timestamp 上报时间 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ │ AgentSummary Agent 汇总信息: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ agent_id: string Agent 标识 │ │ │ │ commands: int 命令数量 │ │ │ │ status: TestStatus Agent 执行状态 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ │ TestSummary 汇总结果: │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ test_id: string 测试 ID │ │ │ │ total_agents: int Agent 数量 │ │ │ │ total_commands: int 命令总数 │ │ │ │ success: int 成功数量 │ │ │ │ failed: int 失败数量 │ │ │ │ skipped: int 跳过数量 │ │ │ │ total_duration_ms: int64 总执行时长 │ │ │ │ agents: []AgentSummary Agent 汇总列表 │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` ### 4.3 存储结构 ``` results/ └── test-xxx/ ├── summary.json # 汇总信息 └── agents/ ├── agent-001.json # Agent 1 完整结果 ├── agent-002.json # Agent 2 完整结果 └── agent-003.json # Agent 3 完整结果 ``` ### 4.4 数据流 ``` Agent 执行命令 │ ▼ 本地生成 commands/01.json, 02.json... │ ▼ Agent 调用 POST /api/results 上报 │ ▼ Coordinator 写入 results/test-xxx/agents/{agent-id}.json │ ▼ 更新 results/test-xxx/summary.json ``` ### 4.5 API 设计 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ Coordinator API │ ├─────────────────────────────────────────────────────────────────────────────┤ │ │ │ POST /api/results │ │ ───────────────────────────────────────────────────────────────────────── │ │ 接收 Agent 上报的执行结果 │ │ 请求体: AgentResult │ │ 响应: { "status": "ok" } │ │ │ │ GET /api/results/:test_id │ │ ───────────────────────────────────────────────────────────────────────── │ │ 获取测试的所有 Agent 结果 │ │ 响应: []AgentResult │ │ │ │ GET /api/summary?test_id=xxx │ │ ───────────────────────────────────────────────────────────────────────── │ │ 获取测试汇总信息 │ │ 响应: TestSummary │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` --- ## 5. 目录结构 ``` rtf/ ├── cmd/ │ └── rtf/ # CLI 主程序 │ └── commands/ # 命令子模块 │ ├── internal/ │ ├── config/ # 配置解析 │ │ ├── parser.go # 配置文件解析 │ │ ├── validator.go # 配置验证 │ │ └── variables.go # 变量替换 │ │ │ ├── resource/ # 资源管理 │ │ ├── manager.go # 资源管理器 │ │ ├── handle.go # 资源句柄 │ │ └── status.go # 资源状态 │ │ │ ├── executor/ # 命令执行 │ │ ├── runner.go # 执行器 │ │ └── collector.go # 结果收集 │ │ │ ├── reporter/ # 报告生成 │ │ └── generator.go # 报告生成器 │ │ │ └── coordinator/ # 分布式协调 (可选) │ └── server.go # Coordinator 服务 │ ├── pkg/ │ ├── adapter/ # 适配器接口和工厂 │ │ ├── adapter.go # 适配器接口定义 │ │ ├── factory.go # 适配器工厂 │ │ └── types.go # 通用类型定义 │ │ │ ├── adapter/podman/ # Podman 适配器 │ │ ├── adapter.go │ │ ├── client.go # Podman API 客户端 │ │ ├── container.go # 容器操作 │ │ ├── network.go # 网络操作 │ │ └── image.go # 镜像操作 │ │ │ ├── adapter/docker/ # Docker 适配器 │ │ └── adapter.go │ │ │ ├── adapter/libvirt/ # Libvirt 适配器 │ │ ├── adapter.go │ │ ├── domain.go # 域操作 │ │ ├── network.go # 网络操作 │ │ ├── storage.go # 存储操作 │ │ └── ssh.go # SSH 执行器 │ │ │ ├── model/ # 数据模型 │ │ ├── resource.go # 资源模型 │ │ ├── command.go # 命令模型 │ │ └── result.go # 结果模型 │ │ │ └── reporter/ # 报告器 │ └── reporter.go # 报告接口 │ ├── configs/ # 配置文件 │ ├── podman.yaml # Podman 默认配置 │ └── libvirt.yaml # Libvirt 默认配置 │ ├── testdata/ # 测试数据 │ └── examples/ # 示例配置 │ ├── container-test.yaml │ └── vm-test.yaml │ ├── go.mod ├── go.sum ├── Makefile └── README.md ``` --- ## 6. 使用示例 ### 6.1 配置文件示例 #### 容器测试 (Podman) ```yaml name: nginx-container-test description: 使用 Podman 容器测试 Nginx RPM environment: RPM_URL: "http://repo.local/nginx-1.20.1.rpm" TEST_PORT: 8080 resources: - name: nginx-container type: container backend: podman image: centos:7 hostname: nginx-test networks: - name: rtf-bridge ports: - host: 8080 guest: 80 environment: CONTAINER_TYPE: podman commands: - name: install-nginx script: | yum install -y {{ENV.RPM_URL}} systemctl enable nginx timeout: 300s - name: start-nginx script: | systemctl start nginx sleep 2 timeout: 30s - name: health-check script: | curl -f http://localhost:80 || exit 1 timeout: 30s - name: cleanup script: | systemctl stop nginx || true yum remove -y nginx || true always_run: true timeout: 60s ``` #### 虚拟机测试 (KVM) ```yaml name: nginx-vm-test description: 使用 KVM 虚拟机测试 Nginx RPM environment: VM_IP: "192.168.100.10" SSH_USER: root SSH_KEY: "/path/to/ssh-key" RPM_URL: "http://repo.local/nginx-1.20.1.rpm" resources: - name: nginx-vm type: vm backend: libvirt vcpu: 2 memory: 2048 disk: source: /var/lib/rtf/images/centos7-base.qcow2 bus: virtio networks: - name: rtf-bridge mac: 52:54:00:00:00:01 ssh: user: "{{ENV.SSH_USER}}" key: "{{ENV.SSH_KEY}}" commands: - name: install-nginx script: | yum install -y {{ENV.RPM_URL}} timeout: 300s - name: health-check script: | curl -f http://localhost:80 || exit 1 timeout: 30s - name: cleanup script: | systemctl stop nginx || true always_run: true timeout: 60s ``` ### 6.2 CLI 使用 ``` # 执行容器测试 rtf run -f examples/container-test.yaml # 执行虚拟机测试 rtf run -f examples/vm-test.yaml # 指定变量覆盖 rtf run -f examples/container-test.yaml \ -var RPM_URL=http://repo2.local/nginx.rpm # 资源管理 rtf resource list rtf resource status nginx-container rtf resource delete nginx-container # 结果查询 rtf result summary test-20260508-200000 rtf result get test-20260508-200000 --command install # 分布式执行 rtf run -f test.yaml --report-to http://coordinator:8080 ``` --- ## 7. 技术选型 | 组件 | 技术 | 说明 | |------|------|------| | **主语言** | Go 1.21+ | 高并发、跨平台 | | **容器 SDK** | podman-remote API | Podman HTTP API | | **容器 SDK** | docker-go | Docker SDK for Go | | **虚拟机 SDK** | libvirt-go | Libvirt Go 绑定 | | **配置格式** | YAML | 声明式配置 | | **HTTP 服务** | Go net/http | 内置,无需中间件 | | **结果存储** | 本地文件系统 | JSON 文件存储 | --- ## 8. API 设计(可选:HTTP Server 模式) ``` RTF Server API (端口 8080) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Resource APIs: POST /api/v1/resources # 创建资源 GET /api/v1/resources # 列表资源 GET /api/v1/resources/{id} # 资源详情 DELETE /api/v1/resources/{id} # 删除资源 POST /api/v1/resources/{id}/start # 启动资源 POST /api/v1/resources/{id}/stop # 停止资源 Exec APIs: POST /api/v1/resources/{id}/exec # 执行命令 GET /api/v1/resources/{id}/exec/{exec_id} # 获取执行结果 Test APIs: POST /api/v1/tests # 创建测试任务 GET /api/v1/tests/{id} # 测试详情 POST /api/v1/tests/{id}/stop # 停止测试 System APIs: GET /api/v1/health # 健康检查 GET /api/v1/stats # 统计信息 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` --- *文档版本: v2.0* *创建日期: 2026-05-08* *更新日期: 2026-05-08*