qe-test-environment-management
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest Environment Management
测试环境管理
<default_to_action>
When managing test environments:
- DEFINE environment types (local, CI, staging, prod)
- CONTAINERIZE with Docker for consistency
- ENSURE parity with production (same versions, configs)
- MOCK external services (service virtualization)
- OPTIMIZE costs (auto-shutdown, spot instances)
Quick Environment Checklist:
- Same OS/versions as production
- Same database type and version
- Same configuration structure
- Containers for reproducibility
- Auto-shutdown after hours
Critical Success Factors:
- "Works on my machine" = environment inconsistency
- Infrastructure as Code = repeatable environments
- Service virtualization = test without external dependencies </default_to_action>
<default_to_action>
管理测试环境时:
- 定义环境类型(本地、CI、预发布、生产)
- 使用Docker容器化以保证一致性
- 确保与生产环境一致(相同版本、配置)
- 模拟外部服务(服务虚拟化)
- 优化成本(自动关机、竞价实例)
快速环境检查清单:
- 与生产环境使用相同的操作系统/版本
- 相同的数据库类型和版本
- 相同的配置结构
- 使用容器保证可复现性
- 非工作时间自动关机
关键成功要素:
- “在我机器上能运行”=环境不一致
- 基础设施即代码=可重复创建的环境
- 服务虚拟化=无需依赖外部服务即可测试 </default_to_action>
Quick Reference Card
快速参考卡片
When to Use
适用场景
- Setting up test infrastructure
- Debugging environment-specific failures
- Reducing test infrastructure costs
- Ensuring dev/prod parity
- 搭建测试基础设施
- 调试特定环境下的故障
- 降低测试基础设施成本
- 确保开发/生产环境一致性
Environment Types
环境类型
| Type | Purpose | Lifetime |
|---|---|---|
| Local | Fast feedback | Developer session |
| CI | Automated tests | Per build (ephemeral) |
| Staging | Pre-prod validation | Persistent |
| Production | Canary/synthetic | Continuous |
| 类型 | 用途 | 生命周期 |
|---|---|---|
| 本地 | 快速反馈 | 开发者会话期间 |
| CI | 自动化测试 | 每个构建周期(临时) |
| 预发布 | 生产前验证 | 持久化 |
| 生产 | 金丝雀测试/合成监控 | 持续运行 |
Dev/Prod Parity Checklist
开发/生产环境一致性检查清单
| Item | Must Match |
|---|---|
| OS | Same version |
| Database | Same type + version |
| Dependencies | Same versions |
| Config | Same structure |
| Env vars | Same names |
| 项目 | 必须匹配 |
|---|---|
| 操作系统 | 相同版本 |
| 数据库 | 相同类型+版本 |
| 依赖项 | 相同版本 |
| 配置 | 相同结构 |
| 环境变量 | 相同名称 |
Docker for Test Environments
用于测试环境的Docker
yaml
undefinedyaml
undefineddocker-compose.test.yml
docker-compose.test.yml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
NODE_ENV: test
DATABASE_URL: postgres://postgres:password@db:5432/test
depends_on:
- db
- redis
db:
image: postgres:15
environment:
POSTGRES_DB: test
POSTGRES_PASSWORD: password
redis:
image: redis:7
**Run tests in container:**
```bash
docker-compose -f docker-compose.test.yml up -d
docker-compose -f docker-compose.test.yml exec app npm test
docker-compose -f docker-compose.test.yml downversion: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
NODE_ENV: test
DATABASE_URL: postgres://postgres:password@db:5432/test
depends_on:
- db
- redis
db:
image: postgres:15
environment:
POSTGRES_DB: test
POSTGRES_PASSWORD: password
redis:
image: redis:7
**在容器中运行测试:**
```bash
docker-compose -f docker-compose.test.yml up -d
docker-compose -f docker-compose.test.yml exec app npm test
docker-compose -f docker-compose.test.yml downInfrastructure as Code
基础设施即代码
hcl
undefinedhcl
undefinedtest-environment.tf
test-environment.tf
resource "aws_instance" "test_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.medium"
tags = {
Name = "test-environment"
Environment = "test"
AutoShutdown = "20:00" # Cost optimization
}
}
resource "aws_rds_instance" "test_db" {
engine = "postgres"
engine_version = "15"
instance_class = "db.t3.micro"
backup_retention_period = 0 # No backups needed for test
skip_final_snapshot = true
}
---resource "aws_instance" "test_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.medium"
tags = {
Name = "test-environment"
Environment = "test"
AutoShutdown = "20:00" # Cost optimization
}
}
resource "aws_rds_instance" "test_db" {
engine = "postgres"
engine_version = "15"
instance_class = "db.t3.micro"
backup_retention_period = 0 # No backups needed for test
skip_final_snapshot = true
}
---Service Virtualization
服务虚拟化
javascript
// Mock external services with WireMock
import { WireMock } from 'wiremock-captain';
const wiremock = new WireMock('http://localhost:8080');
// Mock payment gateway
await wiremock.register({
request: {
method: 'POST',
url: '/charge'
},
response: {
status: 200,
jsonBody: { transactionId: '12345', status: 'approved' }
}
});
// Tests use mock instead of real gatewayjavascript
// Mock external services with WireMock
import { WireMock } from 'wiremock-captain';
const wiremock = new WireMock('http://localhost:8080');
// Mock payment gateway
await wiremock.register({
request: {
method: 'POST',
url: '/charge'
},
response: {
status: 200,
jsonBody: { transactionId: '12345', status: 'approved' }
}
});
// Tests use mock instead of real gatewayCost Optimization
成本优化
bash
undefinedbash
undefinedAuto-shutdown test environments after hours
Auto-shutdown test environments after hours
0 20 * * * aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
0 20 * * * aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
Start before work hours
Start before work hours
0 7 * * 1-5 aws ec2 start-instances --instance-ids $(aws ec2 describe-instances
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
**Use spot instances (70% savings):**
```hcl
resource "aws_instance" "test_runner" {
instance_type = "c5.2xlarge"
instance_market_options {
market_type = "spot"
spot_options {
max_price = "0.10"
}
}
}0 7 * * 1-5 aws ec2 start-instances --instance-ids $(aws ec2 describe-instances
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
--filters "Name=tag:Environment,Values=test"
--query "Reservations[].Instances[].InstanceId" --output text)
**使用竞价实例(节省70%成本):**
```hcl
resource "aws_instance" "test_runner" {
instance_type = "c5.2xlarge"
instance_market_options {
market_type = "spot"
spot_options {
max_price = "0.10"
}
}
}Agent-Driven Environment Management
由Agent驱动的环境管理
typescript
// Provision test environment
await Task("Environment Provisioning", {
type: 'integration-testing',
services: ['app', 'db', 'redis', 'mocks'],
parity: 'production',
lifetime: '2h'
}, "qe-test-executor");
// Chaos testing in isolated environment
await Task("Chaos Test Environment", {
baseline: 'staging',
isolate: true,
injectFaults: ['network-delay', 'pod-failure']
}, "qe-chaos-engineer");typescript
// Provision test environment
await Task("Environment Provisioning", {
type: 'integration-testing',
services: ['app', 'db', 'redis', 'mocks'],
parity: 'production',
lifetime: '2h'
}, "qe-test-executor");
// Chaos testing in isolated environment
await Task("Chaos Test Environment", {
baseline: 'staging',
isolate: true,
injectFaults: ['network-delay', 'pod-failure']
}, "qe-chaos-engineer");Agent Coordination Hints
Agent协调提示
Memory Namespace
内存命名空间
aqe/environment-management/
├── configs/* - Environment configurations
├── parity-checks/* - Dev/prod parity results
├── cost-reports/* - Infrastructure costs
└── service-mocks/* - Service virtualization configsaqe/environment-management/
├── configs/* - Environment configurations
├── parity-checks/* - Dev/prod parity results
├── cost-reports/* - Infrastructure costs
└── service-mocks/* - Service virtualization configsFleet Coordination
集群协调
typescript
const envFleet = await FleetManager.coordinate({
strategy: 'environment-management',
agents: [
'qe-test-executor', // Provision environments
'qe-performance-tester', // Environment performance
'qe-chaos-engineer' // Resilience testing
],
topology: 'sequential'
});typescript
const envFleet = await FleetManager.coordinate({
strategy: 'environment-management',
agents: [
'qe-test-executor', // Provision environments
'qe-performance-tester', // Environment performance
'qe-chaos-engineer' // Resilience testing
],
topology: 'sequential'
});Related Skills
相关技能
- test-data-management - Data for environments
- continuous-testing-shift-left - CI/CD environments
- chaos-engineering-resilience - Environment resilience
- test-data-management - 环境所需数据
- continuous-testing-shift-left - CI/CD环境
- chaos-engineering-resilience - 环境韧性
Remember
注意事项
Environment inconsistency = flaky tests. "Works on my machine" problems come from: different OS/versions, missing dependencies, configuration differences, data differences.
Infrastructure as Code ensures repeatability. Version control your environment configurations. Spin up identical environments on demand.
With Agents: Agents automatically provision test environments matching production, ensure parity, mock external services, and optimize costs with auto-scaling and auto-shutdown.
环境不一致=不稳定的测试。 “在我机器上能运行”的问题源于:不同的操作系统/版本、缺失的依赖项、配置差异、数据差异。
基础设施即代码确保可重复性。 对环境配置进行版本控制。按需创建完全一致的环境。
借助Agent: Agent可自动搭建与生产环境匹配的测试环境、确保一致性、模拟外部服务,并通过自动扩缩容和自动关机优化成本。