v0-json-schema
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
# Python 代码规则详解
|
||||
|
||||
## 概述
|
||||
|
||||
Python 代码规则是 DDMS 合规性验证软件中最灵活的规则类型,允许使用 Python 编写复杂的验证逻辑。与其他基于JSON结构的规则不同,Python 代码规则能够实现任何自定义逻辑,如复杂的数学计算、字符串处理、条件判断等。
|
||||
|
||||
## 规则结构
|
||||
|
||||
Python 代码规则由两部分组成:
|
||||
|
||||
1. **元数据文件** (JSON格式):包含规则的基本信息和配置参数
|
||||
2. **代码文件** (Python格式):包含实际的验证逻辑代码
|
||||
|
||||
### 元数据文件格式
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "rule-id",
|
||||
"name": "规则名称",
|
||||
"description": "规则描述",
|
||||
"category": "PythonCode",
|
||||
"version": "1.0.0",
|
||||
"severity": "error",
|
||||
"source": "Standard-2023",
|
||||
"is_enabled": true,
|
||||
"tags": ["tag1", "tag2"],
|
||||
"target_type": "DataObject",
|
||||
"target_identifier": "TargetName",
|
||||
"allow_imports": true,
|
||||
"allowed_modules": ["math", "re", "json", "datetime"],
|
||||
"entry_function": "validate",
|
||||
"expected_parameters": ["param1", "param2"],
|
||||
"timeout": 5,
|
||||
"code_file": "path/to/code/file.py"
|
||||
}
|
||||
```
|
||||
|
||||
### 主要字段说明
|
||||
|
||||
- **id**: 规则的唯一标识符
|
||||
- **name**: 规则名称
|
||||
- **description**: 规则功能描述
|
||||
- **category**: 固定为 "PythonCode"
|
||||
- **version**: 规则版本号
|
||||
- **severity**: 规则严重级别,如 "error", "warning", "info"
|
||||
- **is_enabled**: 规则是否启用
|
||||
- **tags**: 用于分类和筛选的标签列表
|
||||
- **target_type**: 规则适用的目标类型,如 "DataObject", "API", "Process"
|
||||
- **target_identifier**: 具体目标的标识符,如 "Well", "Seismic"
|
||||
- **allow_imports**: 是否允许导入外部模块
|
||||
- **allowed_modules**: 允许导入的模块列表
|
||||
- **entry_function**: 入口函数名(默认为 "validate")
|
||||
- **expected_parameters**: 规则执行所需的参数列表
|
||||
- **timeout**: 代码执行超时时间(秒)
|
||||
- **code_file**: 外部Python代码文件的路径(相对于rules目录)
|
||||
|
||||
## 代码文件
|
||||
|
||||
代码文件应包含与 `entry_function` 字段指定的同名函数(默认是 "validate"),该函数作为验证逻辑的入口点。
|
||||
|
||||
### 代码文件示例
|
||||
|
||||
```python
|
||||
"""
|
||||
井坐标验证规则
|
||||
|
||||
此规则验证井的坐标是否在有效范围内,并检查与参考井的距离。
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
def is_valid_coordinate(lat, lon):
|
||||
# 验证逻辑
|
||||
return True
|
||||
|
||||
def calculate_distance(lat1, lon1, lat2, lon2):
|
||||
# 计算距离的逻辑
|
||||
return 0.0
|
||||
|
||||
def validate():
|
||||
"""
|
||||
验证入口函数
|
||||
|
||||
在这里实现完整的验证逻辑
|
||||
"""
|
||||
# 从全局命名空间获取参数
|
||||
param1 = globals().get('param1')
|
||||
|
||||
# 执行验证逻辑
|
||||
# ...
|
||||
|
||||
# 返回验证结果
|
||||
return {
|
||||
'is_valid': True,
|
||||
'message': '验证通过',
|
||||
'details': {
|
||||
'additional_info': 'some value'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 验证结果
|
||||
|
||||
验证函数应返回以下格式的结果:
|
||||
|
||||
1. **布尔值**:简单地表示验证成功或失败
|
||||
2. **字典**:包含详细的验证结果,推荐格式如下:
|
||||
```python
|
||||
{
|
||||
'is_valid': True/False, # 必需,表示验证结果
|
||||
'message': '验证结果消息', # 可选,描述验证结果
|
||||
'details': { ... } # 可选,包含详细信息的字典
|
||||
}
|
||||
```
|
||||
|
||||
## 安全限制
|
||||
|
||||
为了确保系统安全,Python 代码规则在执行时受到以下限制:
|
||||
|
||||
1. 只能导入明确允许的模块
|
||||
2. 代码执行有超时限制
|
||||
3. 无法访问文件系统、网络或系统命令
|
||||
4. 在隔离的执行环境中运行
|
||||
|
||||
## 代码规则存储结构
|
||||
|
||||
推荐的存储结构如下:
|
||||
|
||||
```
|
||||
rules/
|
||||
python_code/
|
||||
<rule-id>/
|
||||
<version>.json # 元数据文件
|
||||
<version>.py # Python代码文件
|
||||
```
|
||||
|
||||
例如:
|
||||
|
||||
```
|
||||
rules/
|
||||
python_code/
|
||||
well-coordinates-validation/
|
||||
1.0.0.json
|
||||
1.0.0.py
|
||||
```
|
||||
|
||||
## 使用场景
|
||||
|
||||
Python 代码规则适用于以下场景:
|
||||
|
||||
1. **复杂的数值计算**:如坐标转换、距离计算、范围检查等
|
||||
2. **字符串处理**:解析和验证具有特定格式的字符串
|
||||
3. **条件逻辑**:需要多个条件组合的复杂判断
|
||||
4. **时间处理**:日期和时间的验证和计算
|
||||
5. **数据转换**:在验证前需要对数据进行转换或规范化
|
||||
|
||||
## 测试代码规则
|
||||
|
||||
可以使用提供的测试工具来验证规则的正确性:
|
||||
|
||||
```bash
|
||||
python -m ddms_compliance_suite.test_executor.test_python_external_code
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **代码注释**:添加详细的注释,特别是对于复杂的逻辑
|
||||
2. **模块化**:将复杂逻辑拆分为多个小函数
|
||||
3. **错误处理**:使用异常处理捕获可能的错误
|
||||
4. **参数验证**:在函数开始时验证参数的有效性
|
||||
5. **详细结果**:返回详细的验证结果,便于理解验证失败的原因
|
||||
6. **版本管理**:为每个版本的规则创建单独的代码文件
|
||||
@@ -0,0 +1,387 @@
|
||||
# 规则库增强设计与用法指南
|
||||
|
||||
## 概述
|
||||
|
||||
DDMS合规性验证软件的规则库是整个系统的核心组件,用于存储、管理和执行各种验证规则。本文档介绍了规则库的增强设计,包括新增的规则类型、生命周期和作用域支持、YAML规则格式等特性,以及如何使用这些新功能。
|
||||
|
||||
## 增强特性
|
||||
|
||||
### 1. 规则生命周期
|
||||
|
||||
规则生命周期定义了规则在API测试流程中的适用阶段,使规则执行更加精确和高效。规则生命周期包括以下几个阶段:
|
||||
|
||||
- **请求准备阶段 (RequestPreparation)**: 在构建和发送API请求之前执行的规则,用于验证请求URL、请求头、请求参数等是否符合要求。
|
||||
- **请求执行阶段 (RequestExecution)**: 在发送API请求过程中执行的规则,用于监控请求的执行过程。
|
||||
- **响应验证阶段 (ResponseValidation)**: 在接收到API响应后执行的规则,用于验证响应状态码、响应头、响应体等是否符合要求。
|
||||
- **后处理阶段 (PostValidation)**: 在完成响应验证后执行的规则,用于执行一些清理或记录工作。
|
||||
- **任意阶段 (AnyStage)**: 不关注具体执行阶段的通用规则。
|
||||
|
||||
### 2. 规则作用域
|
||||
|
||||
规则作用域定义了规则针对的具体对象,使规则的应用更加精确。规则作用域包括以下几个类型:
|
||||
|
||||
- **请求URL (RequestURL)**: 规则验证请求的URL是否符合要求,如是否符合RESTful设计规范等。
|
||||
- **请求头 (RequestHeaders)**: 规则验证请求头是否符合要求,如是否包含必要的认证信息等。
|
||||
- **请求参数 (RequestParams)**: 规则验证请求参数是否符合要求,如参数格式、必填项等。
|
||||
- **请求体 (RequestBody)**: 规则验证请求体是否符合要求,如必要的字段、格式等。
|
||||
- **响应状态码 (ResponseStatus)**: 规则验证响应状态码是否符合要求,如是否为200、404等特定状态码。
|
||||
- **响应头 (ResponseHeaders)**: 规则验证响应头是否符合要求,如是否包含跨域头等。
|
||||
- **响应体 (ResponseBody)**: 规则验证响应体是否符合要求,如必要的字段、格式等。
|
||||
- **响应时间 (ResponseTime)**: 规则验证API响应时间是否在允许的范围内。
|
||||
- **安全性 (Security)**: 规则验证API安全相关的要求,如是否使用HTTPS、是否包含认证信息等。
|
||||
- **性能 (Performance)**: 规则验证API性能相关的要求,如响应时间、资源消耗等。
|
||||
- **任意作用域 (AnyScope)**: 不关注具体作用域的通用规则。
|
||||
|
||||
### 3. 新增规则类型
|
||||
|
||||
为了满足不同场景的验证需求,增强了以下规则类型:
|
||||
|
||||
- **性能规则 (PerformanceRule)**: 用于验证API性能相关的指标,如响应时间、吞吐量等。
|
||||
- **安全规则 (SecurityRule)**: 用于验证API安全相关的要求,如HTTPS强制、认证授权等。
|
||||
- **RESTful设计规则 (RESTfulDesignRule)**: 用于验证API URL设计是否符合RESTful规范。
|
||||
- **错误处理规则 (ErrorHandlingRule)**: 用于验证API错误响应是否符合标准格式和处理方式。
|
||||
|
||||
### 4. YAML规则格式
|
||||
|
||||
为了提高规则的可读性和可维护性,增加了对YAML格式规则的支持。YAML格式的规则可以直接嵌入Python代码,实现更灵活的验证逻辑。
|
||||
|
||||
## 规则示例
|
||||
|
||||
### 性能规则示例
|
||||
|
||||
```yaml
|
||||
id: response-time-threshold
|
||||
name: 响应时间阈值规则
|
||||
description: 验证API响应时间是否在允许的范围内
|
||||
category: Performance
|
||||
version: 1.0.0
|
||||
severity: warning
|
||||
is_enabled: true
|
||||
tags:
|
||||
- performance
|
||||
- response-time
|
||||
target_type: APIResponse
|
||||
lifecycle: ResponseValidation
|
||||
scope: ResponseTime
|
||||
threshold: 500 # 毫秒
|
||||
metric: response_time
|
||||
unit: ms
|
||||
code: |
|
||||
def validate(context):
|
||||
response = context.get('api_response')
|
||||
if not response:
|
||||
return {'is_valid': False, 'message': '缺少API响应对象'}
|
||||
|
||||
response_time = response.elapsed_time * 1000 # 转换为毫秒
|
||||
threshold = context.get('threshold', 500) # 默认500毫秒
|
||||
|
||||
if response_time > threshold:
|
||||
return {
|
||||
'is_valid': False,
|
||||
'message': f'响应时间 {response_time:.2f}ms 超过阈值 {threshold}ms',
|
||||
'details': {
|
||||
'actual_time': response_time,
|
||||
'threshold': threshold,
|
||||
'unit': 'ms'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
'is_valid': True,
|
||||
'message': f'响应时间 {response_time:.2f}ms 在阈值 {threshold}ms 内',
|
||||
'details': {
|
||||
'actual_time': response_time,
|
||||
'threshold': threshold,
|
||||
'unit': 'ms'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 安全规则示例
|
||||
|
||||
```yaml
|
||||
id: https-only-rule
|
||||
name: HTTPS强制使用规则
|
||||
description: 验证API是否只使用HTTPS协议,确保通信安全
|
||||
category: Security
|
||||
version: 1.0.0
|
||||
severity: error
|
||||
is_enabled: true
|
||||
tags:
|
||||
- security
|
||||
- https
|
||||
- encryption
|
||||
target_type: APIRequest
|
||||
lifecycle: RequestPreparation
|
||||
scope: Security
|
||||
check_type: transport_security
|
||||
expected_value: https
|
||||
code: |
|
||||
def validate(context):
|
||||
request = context.get('api_request')
|
||||
if not request:
|
||||
return {'is_valid': False, 'message': '缺少API请求对象'}
|
||||
|
||||
url = str(request.url)
|
||||
|
||||
if not url.startswith('https://'):
|
||||
return {
|
||||
'is_valid': False,
|
||||
'message': 'API请求必须使用HTTPS协议',
|
||||
'details': {
|
||||
'current_url': url,
|
||||
'expected_protocol': 'https'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
'is_valid': True,
|
||||
'message': 'API请求使用了HTTPS协议',
|
||||
'details': {
|
||||
'url': url
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### RESTful设计规则示例
|
||||
|
||||
```yaml
|
||||
id: restful-url-pattern
|
||||
name: RESTful URL设计规则
|
||||
description: 验证API URL是否符合RESTful设计规范
|
||||
category: APIDesign
|
||||
version: 1.0.0
|
||||
severity: warning
|
||||
is_enabled: true
|
||||
tags:
|
||||
- restful
|
||||
- api-design
|
||||
- url-pattern
|
||||
target_type: APIRequest
|
||||
lifecycle: RequestPreparation
|
||||
scope: RequestURL
|
||||
design_aspect: URL设计
|
||||
pattern: "^/api/v\\d+/[a-z0-9-]+(/[a-z0-9-]+)*$"
|
||||
code: |
|
||||
import re
|
||||
|
||||
def validate(context):
|
||||
request = context.get('api_request')
|
||||
if not request:
|
||||
return {'is_valid': False, 'message': '缺少API请求对象'}
|
||||
|
||||
url = str(request.url)
|
||||
|
||||
# 解析URL,获取路径部分
|
||||
from urllib.parse import urlparse
|
||||
parsed_url = urlparse(url)
|
||||
path = parsed_url.path
|
||||
|
||||
# 使用正则表达式验证路径
|
||||
pattern = context.get('pattern', "^/api/v\\d+/[a-z0-9-]+(/[a-z0-9-]+)*$")
|
||||
if not re.match(pattern, path):
|
||||
return {
|
||||
'is_valid': False,
|
||||
'message': 'API URL不符合RESTful设计规范',
|
||||
'details': {
|
||||
'current_path': path,
|
||||
'expected_pattern': pattern,
|
||||
'suggestion': '路径应该遵循 /api/v{version}/{资源}[/{id}] 格式'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
'is_valid': True,
|
||||
'message': 'API URL符合RESTful设计规范',
|
||||
'details': {
|
||||
'path': path
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 错误处理规则示例
|
||||
|
||||
```yaml
|
||||
id: standard-error-response
|
||||
name: 标准错误响应格式规则
|
||||
description: 验证API错误响应是否符合标准格式
|
||||
category: ErrorHandling
|
||||
version: 1.0.0
|
||||
severity: warning
|
||||
is_enabled: true
|
||||
tags:
|
||||
- error-handling
|
||||
- response-format
|
||||
target_type: APIResponse
|
||||
lifecycle: ResponseValidation
|
||||
scope: ResponseBody
|
||||
error_code: "*" # 匹配所有错误码
|
||||
expected_status: -1 # 不验证状态码
|
||||
code: |
|
||||
def validate(context):
|
||||
response = context.get('api_response')
|
||||
if not response:
|
||||
return {'is_valid': False, 'message': '缺少API响应对象'}
|
||||
|
||||
# 只检查4xx和5xx状态码的响应
|
||||
if response.status_code < 400:
|
||||
return {'is_valid': True, 'message': '非错误响应,跳过验证'}
|
||||
|
||||
# 确保响应包含JSON内容
|
||||
if not response.json_content:
|
||||
return {
|
||||
'is_valid': False,
|
||||
'message': '错误响应不是有效的JSON格式',
|
||||
'details': {
|
||||
'status_code': response.status_code,
|
||||
'content_type': response.headers.get('Content-Type', '未知')
|
||||
}
|
||||
}
|
||||
|
||||
# 检查错误响应的必要字段
|
||||
required_fields = ['code', 'message']
|
||||
missing_fields = [field for field in required_fields if field not in response.json_content]
|
||||
|
||||
if missing_fields:
|
||||
return {
|
||||
'is_valid': False,
|
||||
'message': '错误响应缺少必要字段',
|
||||
'details': {
|
||||
'missing_fields': missing_fields,
|
||||
'required_fields': required_fields,
|
||||
'response': response.json_content
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
'is_valid': True,
|
||||
'message': '错误响应符合标准格式',
|
||||
'details': {
|
||||
'status_code': response.status_code,
|
||||
'error_code': response.json_content.get('code'),
|
||||
'error_message': response.json_content.get('message')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 1. 创建规则
|
||||
|
||||
可以通过以下两种方式创建规则:
|
||||
|
||||
1. **编程方式创建**:通过实例化规则类来创建规则对象,然后使用规则库的`save_rule`方法保存。
|
||||
|
||||
```python
|
||||
from ddms_compliance_suite.models.rule_models import PerformanceRule, RuleCategory, TargetType, RuleLifecycle, RuleScope
|
||||
|
||||
# 创建性能规则
|
||||
performance_rule = PerformanceRule(
|
||||
id="response-time-max-500ms",
|
||||
name="响应时间不超过500毫秒",
|
||||
description="验证API响应时间不超过500毫秒",
|
||||
category=RuleCategory.PERFORMANCE,
|
||||
severity="warning",
|
||||
target_type=TargetType.API_RESPONSE,
|
||||
lifecycle=RuleLifecycle.RESPONSE_VALIDATION,
|
||||
scope=RuleScope.RESPONSE_TIME,
|
||||
threshold=500,
|
||||
metric="response_time",
|
||||
unit="ms"
|
||||
)
|
||||
|
||||
# 保存规则
|
||||
rule_repository.save_rule(performance_rule)
|
||||
```
|
||||
|
||||
2. **YAML文件创建**:将规则定义为YAML文件,存放在规则库的目录结构中。
|
||||
|
||||
YAML规则文件存放路径: `rules/yaml_rules/{category}/{rule_id}/{version}.yaml`
|
||||
|
||||
### 2. 查询规则
|
||||
|
||||
可以使用规则库的`query_rules`方法查询规则,支持按规则类别、目标类型、生命周期、作用域等条件筛选。
|
||||
|
||||
```python
|
||||
from ddms_compliance_suite.models.rule_models import RuleQuery, RuleCategory, TargetType, RuleLifecycle, RuleScope
|
||||
|
||||
# 查询所有API响应验证阶段的性能规则
|
||||
query = RuleQuery(
|
||||
category=RuleCategory.PERFORMANCE,
|
||||
target_type=TargetType.API_RESPONSE,
|
||||
lifecycle=RuleLifecycle.RESPONSE_VALIDATION,
|
||||
scope=RuleScope.RESPONSE_TIME,
|
||||
is_enabled=True
|
||||
)
|
||||
|
||||
rules = rule_repository.query_rules(query)
|
||||
print(f"找到 {len(rules)} 条规则")
|
||||
```
|
||||
|
||||
### 3. 执行规则
|
||||
|
||||
可以使用规则执行引擎的`execute_rule`方法执行单个规则,或使用`execute_rules_for_lifecycle`方法执行特定生命周期阶段的所有规则。
|
||||
|
||||
```python
|
||||
from ddms_compliance_suite.rule_executor.executor import RuleExecutor
|
||||
|
||||
# 创建规则执行引擎
|
||||
executor = RuleExecutor(rule_repository)
|
||||
|
||||
# 执行单个规则
|
||||
result = executor.execute_rule(rule, context)
|
||||
print(f"规则 '{result.rule_name}' 结果: {'通过' if result.is_valid else '失败'} - {result.message}")
|
||||
|
||||
# 执行特定生命周期阶段的所有规则
|
||||
results = executor.execute_rules_for_lifecycle(RuleLifecycle.RESPONSE_VALIDATION, context)
|
||||
for result in results:
|
||||
print(f"规则 '{result.rule_name}' 结果: {'通过' if result.is_valid else '失败'} - {result.message}")
|
||||
```
|
||||
|
||||
### 4. 在测试中使用规则
|
||||
|
||||
可以在API测试流程中集成规则验证,以确保API请求和响应符合规范要求。
|
||||
|
||||
```python
|
||||
from ddms_compliance_suite.api_caller.caller import APICaller, APIRequest
|
||||
from ddms_compliance_suite.rule_executor.executor import RuleExecutor
|
||||
|
||||
# 创建API调用器
|
||||
api_caller = APICaller()
|
||||
|
||||
# 创建API请求
|
||||
request = APIRequest(
|
||||
method="GET",
|
||||
url="https://api.example.com/api/v1/users/123",
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
|
||||
# 执行请求准备阶段的规则
|
||||
context = {"api_request": request}
|
||||
prep_results = executor.execute_rules_for_lifecycle(RuleLifecycle.REQUEST_PREPARATION, context)
|
||||
for result in prep_results:
|
||||
print(f"规则 '{result.rule_name}' 结果: {'通过' if result.is_valid else '失败'} - {result.message}")
|
||||
|
||||
# 发送API请求
|
||||
response = api_caller.call_api(request)
|
||||
|
||||
# 执行响应验证阶段的规则
|
||||
context["api_response"] = response
|
||||
resp_results = executor.execute_rules_for_lifecycle(RuleLifecycle.RESPONSE_VALIDATION, context)
|
||||
for result in resp_results:
|
||||
print(f"规则 '{result.rule_name}' 结果: {'通过' if result.is_valid else '失败'} - {result.message}")
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **合理组织规则**:按照规则类别、生命周期和作用域组织规则,使规则库结构清晰。
|
||||
2. **使用标签**:为规则添加标签,方便按照特定主题或功能筛选规则。
|
||||
3. **合理设置优先级**:为规则设置合理的严重性级别,以便于区分重要规则和次要规则。
|
||||
4. **编写清晰的规则描述**:为规则提供清晰的描述,使其他开发者能够理解规则的用途和行为。
|
||||
5. **使用YAML格式**:对于复杂的验证逻辑,优先使用YAML格式的规则,便于维护和调试。
|
||||
6. **避免硬编码**:在规则中避免硬编码具体的验证标准,而是通过规则属性来配置。
|
||||
7. **定期维护规则库**:随着API的演进,定期更新和维护规则库,确保规则始终有效。
|
||||
|
||||
## 结论
|
||||
|
||||
规则库的增强设计为API测试提供了更强大、更灵活的验证能力,使得DDMS合规性验证软件能够更精确地验证API接口的行为,确保其符合平台定义的数据共享标准和技术规范。通过合理组织和使用规则,可以显著提高API测试的效率和质量。
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
# DDMS合规性验证框架项目总结
|
||||
|
||||
## 项目概述
|
||||
|
||||
DDMS合规性验证框架(DDMS Compliance Framework)是一个专门为验证API接口合规性而设计的软件框架。该框架能够自动生成API测试用例,执行API调用,验证API响应,并根据预定义的规则检查API是否符合DDMS平台的技术规范和数据共享标准。
|
||||
|
||||
## 项目目标
|
||||
|
||||
1. **自动化API测试**:实现API测试用例的自动生成和执行,提高测试效率。
|
||||
2. **规范性验证**:确保API接口符合REST风格设计规范。
|
||||
3. **性能监控**:监控API响应时间和资源使用,确保API性能符合要求。
|
||||
4. **安全性检查**:验证API实现了必要的安全措施,如HTTPS传输和认证授权。
|
||||
5. **灵活的规则配置**:支持多种规则类型和规则定义方式,以适应不同的验证需求。
|
||||
6. **可扩展性**:框架设计具有良好的可扩展性,便于添加新的验证规则和功能。
|
||||
|
||||
## 系统架构
|
||||
|
||||
### 核心组件
|
||||
|
||||
1. **API测试生成器 (API Test Generator)**
|
||||
- 解析API定义:从YAPI和Swagger文件中解析API定义。
|
||||
- 生成测试用例:基于API定义自动生成测试用例。
|
||||
- 支持多种HTTP方法:GET、POST、PUT、DELETE等。
|
||||
- 处理各种参数类型:路径参数、查询参数、请求体等。
|
||||
|
||||
2. **API调用器 (API Caller)**
|
||||
- 构建API请求:根据测试用例构建HTTP请求。
|
||||
- 发送请求:向目标服务器发送HTTP请求。
|
||||
- 处理响应:接收并处理HTTP响应。
|
||||
- 支持各种请求类型:支持JSON、XML、表单等格式的请求。
|
||||
|
||||
3. **规则库 (Rule Repository)**
|
||||
- 存储规则:管理各种验证规则。
|
||||
- 规则分类:支持按类别、生命周期、作用域等分类规则。
|
||||
- 规则查询:提供灵活的规则查询接口。
|
||||
- YAML规则支持:支持使用YAML格式定义规则和嵌入Python验证逻辑。
|
||||
|
||||
4. **规则执行引擎 (Rule Executor)**
|
||||
- 执行规则:在API测试流程中执行相应的规则。
|
||||
- 结果收集:收集规则执行结果。
|
||||
- 结果分析:分析规则执行结果,生成验证报告。
|
||||
- 支持各种规则类型:性能规则、安全规则、设计规则等。
|
||||
|
||||
5. **测试协调器 (Test Orchestrator)**
|
||||
- 协调测试流程:管理整个API测试的流程。
|
||||
- 集成各个组件:将API测试生成器、API调用器、规则库和规则执行引擎集成起来。
|
||||
- 结果汇总:汇总各个阶段的测试结果。
|
||||
- 生成报告:生成详细的API测试报告。
|
||||
|
||||
### 流程说明
|
||||
|
||||
1. **测试准备阶段**:
|
||||
- 解析API定义,提取API信息。
|
||||
- 生成API测试用例。
|
||||
- 根据测试用例构建API请求。
|
||||
- 执行请求准备阶段的规则验证。
|
||||
|
||||
2. **测试执行阶段**:
|
||||
- 发送API请求到目标服务器。
|
||||
- 接收API响应。
|
||||
- 执行请求执行阶段的规则验证。
|
||||
|
||||
3. **结果验证阶段**:
|
||||
- 验证API响应是否符合预期。
|
||||
- 执行响应验证阶段的规则验证。
|
||||
- 收集验证结果。
|
||||
|
||||
4. **报告生成阶段**:
|
||||
- 汇总各个阶段的验证结果。
|
||||
- 生成详细的测试报告。
|
||||
- 执行后处理阶段的规则验证。
|
||||
|
||||
## 技术特点
|
||||
|
||||
### 1. 规则生命周期支持
|
||||
|
||||
框架引入了规则生命周期的概念,使规则执行更加精确和高效:
|
||||
|
||||
- **请求准备阶段 (RequestPreparation)**:在构建和发送API请求之前执行的规则,用于验证请求URL、请求头、请求参数等是否符合要求。
|
||||
- **请求执行阶段 (RequestExecution)**:在发送API请求过程中执行的规则,用于监控请求的执行过程。
|
||||
- **响应验证阶段 (ResponseValidation)**:在接收到API响应后执行的规则,用于验证响应状态码、响应头、响应体等是否符合要求。
|
||||
- **后处理阶段 (PostValidation)**:在完成响应验证后执行的规则,用于执行一些清理或记录工作。
|
||||
|
||||
### 2. 规则作用域支持
|
||||
|
||||
框架引入了规则作用域的概念,使规则的应用更加精确:
|
||||
|
||||
- **请求URL (RequestURL)**:规则验证请求的URL是否符合要求,如是否符合RESTful设计规范等。
|
||||
- **请求头 (RequestHeaders)**:规则验证请求头是否符合要求,如是否包含必要的认证信息等。
|
||||
- **响应状态码 (ResponseStatus)**:规则验证响应状态码是否符合要求,如是否为200、404等特定状态码。
|
||||
- **响应时间 (ResponseTime)**:规则验证API响应时间是否在允许的范围内。
|
||||
- **安全性 (Security)**:规则验证API安全相关的要求,如是否使用HTTPS、是否包含认证信息等。
|
||||
|
||||
### 3. 多种规则类型
|
||||
|
||||
框架支持多种规则类型,以满足不同场景的验证需求:
|
||||
|
||||
- **性能规则 (PerformanceRule)**:用于验证API性能相关的指标,如响应时间、吞吐量等。
|
||||
- **安全规则 (SecurityRule)**:用于验证API安全相关的要求,如HTTPS强制、认证授权等。
|
||||
- **RESTful设计规则 (RESTfulDesignRule)**:用于验证API URL设计是否符合RESTful规范。
|
||||
- **错误处理规则 (ErrorHandlingRule)**:用于验证API错误响应是否符合标准格式和处理方式。
|
||||
|
||||
### 4. YAML规则格式支持
|
||||
|
||||
框架支持使用YAML格式定义规则,并可以在YAML中嵌入Python验证逻辑,提高规则的可读性和可维护性。
|
||||
|
||||
### 5. 自动生成测试数据
|
||||
|
||||
框架能够基于API定义自动生成测试数据,包括路径参数、查询参数、请求体等,减少手动编写测试数据的工作量。
|
||||
|
||||
## 项目成果
|
||||
|
||||
1. **API测试生成器**:成功实现了从YAPI和Swagger文件中自动生成API测试用例的功能,支持各种HTTP方法和参数类型。
|
||||
|
||||
2. **规则库增强**:实现了规则生命周期、规则作用域、多种规则类型和YAML规则格式的支持,使规则库更加强大和灵活。
|
||||
|
||||
3. **规则执行引擎**:实现了规则执行引擎,能够在API测试流程中执行各种规则,并收集和分析规则执行结果。
|
||||
|
||||
4. **示例规则实现**:实现了多种类型的规则示例,包括性能规则、安全规则、RESTful设计规则和错误处理规则等。
|
||||
|
||||
5. **演示脚本**:实现了演示脚本,用于展示框架的功能和用法,包括规则执行演示和API测试演示等。
|
||||
|
||||
6. **完善的文档**:编写了详细的文档,包括项目总结、规则库增强设计与用法指南、API测试框架使用说明等。
|
||||
|
||||
## 未来展望
|
||||
|
||||
1. **规则库扩展**:继续扩展规则库,添加更多类型的规则和验证逻辑。
|
||||
|
||||
2. **界面开发**:开发Web界面,方便用户管理规则、执行测试和查看测试报告。
|
||||
|
||||
3. **报告优化**:优化测试报告的格式和内容,提供更丰富的图表和分析结果。
|
||||
|
||||
4. **集成CI/CD**:将框架集成到CI/CD流程中,实现API测试的自动化执行和报告生成。
|
||||
|
||||
5. **数据驱动**:支持数据驱动测试,使测试用例更加灵活和全面。
|
||||
|
||||
6. **负载测试**:增加负载测试功能,验证API在高并发情况下的性能和稳定性。
|
||||
|
||||
## 总结
|
||||
|
||||
DDMS合规性验证框架是一个强大而灵活的API测试和验证工具,它通过自动化测试和规则验证,确保API接口符合DDMS平台的技术规范和数据共享标准。该框架的设计和实现充分考虑了可扩展性和灵活性,能够满足各种API测试和验证需求。
|
||||
Reference in New Issue
Block a user