Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1290a8bc54 |
@@ -1,66 +0,0 @@
|
|||||||
# 一键部署编排 + 回滚引擎(Issue #60 / PRD 5.6)
|
|
||||||
|
|
||||||
> 父 Issue「⑥ 一键部署脚本与回滚机制 · 0.5d」
|
|
||||||
|
|
||||||
把一键部署与回滚落为**可测试的纯标准库编排引擎**——不真执行 k8s/helm,但模拟
|
|
||||||
完整编排序列(pre_check → deploy → health_check → post_check)+ 部署前快照 +
|
|
||||||
失败回滚。对齐 `deploy/k8s/helm/iaop`(Chart)与 `deploy/k8s/healthz`(探针)。
|
|
||||||
|
|
||||||
## 1. 部署编排(`deploy_plan.py`)
|
|
||||||
|
|
||||||
- `DeployStep` —— 声明式步骤:`kind`(pre_check/deploy/health_check/post_check)
|
|
||||||
+ `name` + `action` 回调(返回 `(ok, detail)`,默认模拟成功)。
|
|
||||||
- `DeployPlan` —— 步骤序列 + 发布元信息(release/namespace/chart/version);
|
|
||||||
`default_helm_release()` 生成默认 4 步 Helm 发布计划;步骤顺序约束
|
|
||||||
(pre_check < deploy < health_check < post_check)自动校验。
|
|
||||||
- `DeployOrchestrator` —— 按序执行,任何必需步骤失败即中止并标记回滚
|
|
||||||
(`DeployOutcome.needs_rollback`),产出 `DeployReport`(逐步结果 + 日志 +
|
|
||||||
可解释 reason)。deploy/health_check 是回滚检查点;pre_check 失败无需回滚。
|
|
||||||
- `HealthCheckContract` —— 健康检查契约(端点/超时/重试/可用率目标,对齐
|
|
||||||
`deploy/k8s/healthz` 的 99.8%)。
|
|
||||||
|
|
||||||
```python
|
|
||||||
from deploy_plan import DeployPlan, DeployOrchestrator
|
|
||||||
|
|
||||||
plan = DeployPlan.default_helm_release(release="iaop", backend="gpu")
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
if not report.succeeded and report.outcome.needs_rollback:
|
|
||||||
print("需回滚:", report.outcome.reason)
|
|
||||||
```
|
|
||||||
|
|
||||||
## 2. 回滚引擎(`rollback.py`)
|
|
||||||
|
|
||||||
- `RollbackPoint` —— 部署前快照(release/namespace/version/chart/values_hash/
|
|
||||||
created_at,不可变)。`values_hash`(SHA1)检测配置漂移。
|
|
||||||
- `RollbackManager` —— 快照栈 + 回滚执行:`snapshot(plan)` 部署前压栈;
|
|
||||||
`rollback_to_latest()` / `rollback(n)` 失败时回滚到稳定版本。回滚失败把目标压回
|
|
||||||
栈顶(保持栈一致性);支持回滚后健康检查。FIFO 淘汰防内存膨胀。
|
|
||||||
- `RollbackResult` —— 回滚结果(SUCCESS/FAILED/NO_TARGET/SKIPPED + 可解释 reason)。
|
|
||||||
|
|
||||||
```python
|
|
||||||
from deploy_plan import DeployPlan, DeployOrchestrator
|
|
||||||
from rollback import RollbackManager
|
|
||||||
|
|
||||||
mgr = RollbackManager()
|
|
||||||
mgr.snapshot(plan) # 部署前快照
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
if not report.succeeded:
|
|
||||||
result = mgr.rollback_to_latest(reason=report.outcome.reason)
|
|
||||||
print(result.status.value, result.detail)
|
|
||||||
```
|
|
||||||
|
|
||||||
## 与现有部署资产的关系
|
|
||||||
|
|
||||||
- 真实环境把 `action` / `restore_action` 注入为 `helm upgrade` / `helm rollback`
|
|
||||||
等真回调即可;本引擎只做编排与状态管理,不耦合 k8s 客户端。
|
|
||||||
- 健康检查契约对齐 `deploy/k8s/healthz/probe_availability.py`(99.8% 可用率目标)。
|
|
||||||
|
|
||||||
## 测试
|
|
||||||
|
|
||||||
```bash
|
|
||||||
python -m unittest discover -s deploy/orchestration/tests -p "test_*.py" -v
|
|
||||||
```
|
|
||||||
|
|
||||||
覆盖正常 + 边界 + 错误(30 用例):4 步计划构造、顺序约束、全成功/各类失败、
|
|
||||||
回滚检查点判定、action 异常、dry_run、空计划、快照压栈/FIFO、回滚成功/失败/
|
|
||||||
多版本/空栈/异常、健康检查契约校验、values 漂移、报告序列化。
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""一键部署编排 + 回滚引擎包(Issue #60 / PRD 5.6「⑥ 部署底座」)。
|
|
||||||
|
|
||||||
把部署与回滚落为**可测试的纯标准库编排引擎**——不真执行 k8s/helm,但模拟
|
|
||||||
完整编排序列(pre_check → deploy → health_check → post_check)+ 部署前快照
|
|
||||||
+ 失败回滚。零运行时依赖,与 iAOP 部署底座一致。
|
|
||||||
"""
|
|
||||||
from .deploy_plan import (
|
|
||||||
DeployOrchestrator,
|
|
||||||
DeployOutcome,
|
|
||||||
DeployPlan,
|
|
||||||
DeployReport,
|
|
||||||
DeployStep,
|
|
||||||
DeployStepKind,
|
|
||||||
DeployStepResult,
|
|
||||||
DeployStatus,
|
|
||||||
HealthCheckContract,
|
|
||||||
)
|
|
||||||
from .rollback import (
|
|
||||||
RollbackManager,
|
|
||||||
RollbackPoint,
|
|
||||||
RollbackResult,
|
|
||||||
RollbackStatus,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
# deploy_plan
|
|
||||||
"DeployPlan",
|
|
||||||
"DeployStep",
|
|
||||||
"DeployStepKind",
|
|
||||||
"DeployStepResult",
|
|
||||||
"DeployStatus",
|
|
||||||
"DeployOutcome",
|
|
||||||
"DeployReport",
|
|
||||||
"DeployOrchestrator",
|
|
||||||
"HealthCheckContract",
|
|
||||||
# rollback
|
|
||||||
"RollbackPoint",
|
|
||||||
"RollbackManager",
|
|
||||||
"RollbackResult",
|
|
||||||
"RollbackStatus",
|
|
||||||
]
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""部署编排 + 回滚冒烟脚本(Issue #60)。
|
|
||||||
|
|
||||||
直接运行 ``python _sanity_check.py`` 验证:默认 Helm 计划全成功部署、
|
|
||||||
快照压栈、模拟部署失败触发回滚到上一稳定版本。零第三方依赖。
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
sys.path.insert(0, HERE)
|
|
||||||
|
|
||||||
from deploy_plan import ( # noqa: E402
|
|
||||||
DeployOrchestrator, DeployPlan, DeployStepKind)
|
|
||||||
from rollback import RollbackManager # noqa: E402
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
|
||||||
# 1) 部署前快照(当前稳定版本 v1.0.0)
|
|
||||||
mgr = RollbackManager()
|
|
||||||
stable = DeployPlan.default_helm_release(version="v1.0.0")
|
|
||||||
mgr.snapshot(stable, reason="部署 v1.1.0 前的稳定版本")
|
|
||||||
print(f"[OK] 快照压栈:稳定版本 {mgr.latest().version}")
|
|
||||||
|
|
||||||
# 2) 部署 v1.1.0 全成功
|
|
||||||
new_plan = DeployPlan.default_helm_release(version="v1.1.0")
|
|
||||||
report = DeployOrchestrator(new_plan).execute()
|
|
||||||
assert report.succeeded, report.outcome.reason
|
|
||||||
mgr.snapshot(new_plan, reason="v1.1.0 部署成功")
|
|
||||||
print(f"[OK] 部署 v1.1.0 成功:{len(report.results)} 步,无需回滚")
|
|
||||||
|
|
||||||
# 3) 模拟 v1.2.0 部署失败(health_check 失败)→ 触发回滚
|
|
||||||
def _bad_health(ctx):
|
|
||||||
return False, "/health 503(服务未就绪)"
|
|
||||||
bad_plan = DeployPlan.default_helm_release(
|
|
||||||
version="v1.2.0",
|
|
||||||
step_actions={DeployStepKind.HEALTH_CHECK: _bad_health})
|
|
||||||
bad_report = DeployOrchestrator(bad_plan).execute()
|
|
||||||
assert not bad_report.succeeded
|
|
||||||
assert bad_report.outcome.needs_rollback
|
|
||||||
print(f"[OK] v1.2.0 部署失败:{bad_report.outcome.reason}")
|
|
||||||
|
|
||||||
result = mgr.rollback_to_latest(reason="v1.2.0 健康检查失败")
|
|
||||||
assert result.succeeded, result.detail
|
|
||||||
print(f"[OK] 回滚到 {result.target.version}:{result.detail}")
|
|
||||||
print("部署编排 + 回滚冒烟通过 ✅")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
raise SystemExit(main())
|
|
||||||
@@ -1,428 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""一键部署编排引擎(Issue #60 / PRD 5.6「⑥ 部署底座」)。
|
|
||||||
|
|
||||||
PRD 5.6 验收:一套 Helm Chart 部署内核 + 模板,GPU/NPU 推理后端可插拔。
|
|
||||||
本模块把部署落为**可测试的编排引擎**——把部署拆为有序步骤序列
|
|
||||||
(pre_check → deploy → health_check → post_check),编排器按序执行(模拟,
|
|
||||||
不真调 k8s/helm),逐步产出步骤日志与状态,失败即触发回滚。
|
|
||||||
|
|
||||||
设计要点
|
|
||||||
--------
|
|
||||||
1. **部署即编排序列**(``DeployStep``):每步声明 ``kind``(四类)+ ``name``
|
|
||||||
+ 可执行动作(``action`` 回调,返回 ok/detail);步骤序列即 :class:`DeployPlan`。
|
|
||||||
- ``pre_check`` 部署前检查(集群可达 / 镜像存在 / values 合法 / 资源配额);
|
|
||||||
- ``deploy`` 部署动作(helm install/upgrade,模拟);
|
|
||||||
- ``health_check``健康检查(探活 /health,对齐 ``deploy/k8s/healthz``);
|
|
||||||
- ``post_check`` 部署后验证(业务接口回归 / 监控接入)。
|
|
||||||
2. **编排器**(``DeployOrchestrator``):按序执行步骤,任何一步失败即中止并
|
|
||||||
标记需回滚(``DeployOutcome.needs_rollback``),产出 :class:`DeployReport`
|
|
||||||
(逐步结果 + 总状态 + 可解释 reason)。
|
|
||||||
3. **健康检查契约**(``HealthCheckContract``):定义健康检查的判定口径
|
|
||||||
(超时/重试/目标可用率,对齐 ``deploy/k8s/healthz`` 的 99.8% 目标)。
|
|
||||||
4. **纯标准库**:action 为回调,默认实现模拟成功;真实环境注入真 action 即可。
|
|
||||||
5. **不真执行 k8s/helm**:编排只产日志与状态,便于离线测试与 CI 集成。
|
|
||||||
|
|
||||||
用法::
|
|
||||||
|
|
||||||
plan = DeployPlan.default_helm_release(release="iaop", namespace="iaop")
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
if not report.outcome.succeeded:
|
|
||||||
print("需回滚:", report.outcome.reason)
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import time
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from enum import Enum
|
|
||||||
from typing import Callable, Dict, List, Optional, Sequence, Tuple
|
|
||||||
|
|
||||||
#: 健康检查默认超时(秒,对齐 deploy/k8s/healthz 探针口径)。
|
|
||||||
DEFAULT_HEALTH_TIMEOUT_S = 30.0
|
|
||||||
#: 健康检查默认重试次数。
|
|
||||||
DEFAULT_HEALTH_RETRIES = 3
|
|
||||||
#: 可用率目标(对齐 deploy/k8s/healthz probe_availability 的 99.8%)。
|
|
||||||
DEFAULT_AVAILABILITY_TARGET = 0.998
|
|
||||||
|
|
||||||
|
|
||||||
class DeployError(ValueError):
|
|
||||||
"""部署计划声明/编排错误(步骤序列非法、kind 未知、回调异常等)。"""
|
|
||||||
|
|
||||||
|
|
||||||
class DeployStepKind(str, Enum):
|
|
||||||
"""部署步骤类型(决定执行顺序与失败后果)。"""
|
|
||||||
|
|
||||||
PRE_CHECK = "pre_check" # 部署前检查
|
|
||||||
DEPLOY = "deploy" # 部署动作
|
|
||||||
HEALTH_CHECK = "health_check" # 健康检查
|
|
||||||
POST_CHECK = "post_check" # 部署后验证
|
|
||||||
|
|
||||||
@property
|
|
||||||
def label(self) -> str:
|
|
||||||
return {
|
|
||||||
DeployStepKind.PRE_CHECK: "部署前检查",
|
|
||||||
DeployStepKind.DEPLOY: "部署动作",
|
|
||||||
DeployStepKind.HEALTH_CHECK: "健康检查",
|
|
||||||
DeployStepKind.POST_CHECK: "部署后验证",
|
|
||||||
}[self]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_checkpoint(self) -> bool:
|
|
||||||
"""该步失败是否触发回滚(deploy/health_check 失败必须回滚)。"""
|
|
||||||
return self in (DeployStepKind.DEPLOY, DeployStepKind.HEALTH_CHECK)
|
|
||||||
|
|
||||||
|
|
||||||
#: 步骤动作回调:返回 (ok, detail)。
|
|
||||||
StepAction = Callable[[Dict[str, object]], Tuple[bool, str]]
|
|
||||||
|
|
||||||
|
|
||||||
def _default_action(ctx: Dict[str, object]) -> Tuple[bool, str]:
|
|
||||||
"""默认动作:模拟成功(真实环境注入真回调)。"""
|
|
||||||
return True, "模拟成功(无 k8s/helm 环境)"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class DeployStep:
|
|
||||||
"""单个部署步骤(声明式)。
|
|
||||||
|
|
||||||
Attributes:
|
|
||||||
kind: 步骤类型。
|
|
||||||
name: 步骤名(可读,如 "helm upgrade")。
|
|
||||||
action: 可执行动作(ctx → (ok, detail));默认模拟成功。
|
|
||||||
required: 是否必需(必需步骤失败即中止并标记回滚;非必需失败仅告警)。
|
|
||||||
detail: 步骤说明(引 PRD/SOP,可解释)。
|
|
||||||
"""
|
|
||||||
|
|
||||||
kind: DeployStepKind
|
|
||||||
name: str
|
|
||||||
action: StepAction = field(default=_default_action, repr=False)
|
|
||||||
required: bool = True
|
|
||||||
detail: str = ""
|
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
|
||||||
if not self.name:
|
|
||||||
raise DeployError("DeployStep.name 不能为空")
|
|
||||||
if not isinstance(self.kind, DeployStepKind):
|
|
||||||
raise DeployError(f"kind 必须是 DeployStepKind,实际 {type(self.kind)}")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class HealthCheckContract:
|
|
||||||
"""健康检查契约(对齐 deploy/k8s/healthz 的探针口径)。
|
|
||||||
|
|
||||||
Attributes:
|
|
||||||
endpoint: 健康端点(如 http://iaop:8000/health)。
|
|
||||||
timeout_s: 单次探测超时。
|
|
||||||
retries: 重试次数(窗口内达到目标可用率即通过)。
|
|
||||||
availability_target: 可用率目标(默认 0.998 = 99.8%)。
|
|
||||||
"""
|
|
||||||
|
|
||||||
endpoint: str = "http://iaop:8000/health"
|
|
||||||
timeout_s: float = DEFAULT_HEALTH_TIMEOUT_S
|
|
||||||
retries: int = DEFAULT_HEALTH_RETRIES
|
|
||||||
availability_target: float = DEFAULT_AVAILABILITY_TARGET
|
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
|
||||||
if self.timeout_s <= 0:
|
|
||||||
raise DeployError(f"timeout_s 必须 > 0,实际 {self.timeout_s}")
|
|
||||||
if self.retries < 1:
|
|
||||||
raise DeployError(f"retries 必须 ≥ 1,实际 {self.retries}")
|
|
||||||
if not (0.0 < self.availability_target <= 1.0):
|
|
||||||
raise DeployError(
|
|
||||||
f"availability_target 须在 (0,1],实际 {self.availability_target}")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class DeployPlan:
|
|
||||||
"""部署计划:步骤序列 + 发布元信息。
|
|
||||||
|
|
||||||
步骤顺序约束:pre_check 必须在 deploy 前;health_check 必须在 deploy 后、
|
|
||||||
post_check 前。``__post_init__`` 校验该顺序,违例即拒绝(防编排漂移)。
|
|
||||||
|
|
||||||
Attributes:
|
|
||||||
name: 计划名(如 "iaop-helm-upgrade")。
|
|
||||||
release: Helm release 名(如 "iaop")。
|
|
||||||
namespace: 命名空间。
|
|
||||||
chart: Chart 引用(如 "deploy/k8s/helm/iaop")。
|
|
||||||
version: 目标版本(appVersion,如 "v1.0.0")。
|
|
||||||
steps: 步骤序列(按执行顺序)。
|
|
||||||
health: 健康检查契约(health_check 步骤引用)。
|
|
||||||
"""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
release: str = "iaop"
|
|
||||||
namespace: str = "iaop"
|
|
||||||
chart: str = "deploy/k8s/helm/iaop"
|
|
||||||
version: str = "v1.0.0"
|
|
||||||
steps: List[DeployStep] = field(default_factory=list)
|
|
||||||
health: HealthCheckContract = field(default_factory=HealthCheckContract)
|
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
|
||||||
if not self.name:
|
|
||||||
raise DeployError("DeployPlan.name 不能为空")
|
|
||||||
self._validate_order()
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def _validate_order(self) -> None:
|
|
||||||
"""校验步骤顺序:pre_check < deploy < health_check < post_check。"""
|
|
||||||
if not self.steps:
|
|
||||||
return # 空计划在 execute() 时报错
|
|
||||||
order_rank = {
|
|
||||||
DeployStepKind.PRE_CHECK: 0,
|
|
||||||
DeployStepKind.DEPLOY: 1,
|
|
||||||
DeployStepKind.HEALTH_CHECK: 2,
|
|
||||||
DeployStepKind.POST_CHECK: 3,
|
|
||||||
}
|
|
||||||
prev_rank = -1
|
|
||||||
for s in self.steps:
|
|
||||||
r = order_rank[s.kind]
|
|
||||||
if r < prev_rank:
|
|
||||||
raise DeployError(
|
|
||||||
f"步骤顺序违例:{s.kind.value}({s.name}) 不能排在"
|
|
||||||
f" rank {prev_rank} 之后(pre_check < deploy < "
|
|
||||||
f"health_check < post_check)")
|
|
||||||
prev_rank = r
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
@classmethod
|
|
||||||
def default_helm_release(
|
|
||||||
cls,
|
|
||||||
release: str = "iaop",
|
|
||||||
namespace: str = "iaop",
|
|
||||||
version: str = "v1.0.0",
|
|
||||||
backend: str = "gpu",
|
|
||||||
health: Optional[HealthCheckContract] = None,
|
|
||||||
step_actions: Optional[Dict[DeployStepKind, StepAction]] = None,
|
|
||||||
) -> "DeployPlan":
|
|
||||||
"""默认 Helm 发布计划(pre_check → deploy → health_check → post_check)。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
release: release 名。
|
|
||||||
namespace: 命名空间。
|
|
||||||
version: 目标 appVersion。
|
|
||||||
backend: 推理后端(gpu|npu,对齐 PRD 5.6 配置点)。
|
|
||||||
health: 健康检查契约(默认 :class:`HealthCheckContract`)。
|
|
||||||
step_actions: 各类步骤的动作回调覆盖(默认模拟成功)。
|
|
||||||
"""
|
|
||||||
actions = step_actions or {}
|
|
||||||
plan = cls(
|
|
||||||
name=f"{release}-helm-upgrade",
|
|
||||||
release=release,
|
|
||||||
namespace=namespace,
|
|
||||||
version=version,
|
|
||||||
health=health or HealthCheckContract(),
|
|
||||||
steps=[
|
|
||||||
DeployStep(
|
|
||||||
kind=DeployStepKind.PRE_CHECK,
|
|
||||||
name="pre-check-cluster-values",
|
|
||||||
action=actions.get(DeployStepKind.PRE_CHECK, _default_action),
|
|
||||||
detail="部署前检查:集群可达 / 镜像存在 / values 合法 / 资源配额"),
|
|
||||||
DeployStep(
|
|
||||||
kind=DeployStepKind.DEPLOY,
|
|
||||||
name=f"helm-upgrade-{backend}",
|
|
||||||
action=actions.get(DeployStepKind.DEPLOY, _default_action),
|
|
||||||
detail=f"helm upgrade --install {release}(backend={backend},PRD 5.6)"),
|
|
||||||
DeployStep(
|
|
||||||
kind=DeployStepKind.HEALTH_CHECK,
|
|
||||||
name="health-check-probe",
|
|
||||||
action=actions.get(DeployStepKind.HEALTH_CHECK, _default_action),
|
|
||||||
detail="健康检查:探活 /health(目标可用率 99.8%,对齐 healthz)"),
|
|
||||||
DeployStep(
|
|
||||||
kind=DeployStepKind.POST_CHECK,
|
|
||||||
name="post-check-smoke",
|
|
||||||
action=actions.get(DeployStepKind.POST_CHECK, _default_action),
|
|
||||||
required=False, # 烟测非阻断
|
|
||||||
detail="部署后验证:业务接口烟测 / 监控接入(非阻断)"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
return plan
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 执行结果
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
class DeployStatus(str, Enum):
|
|
||||||
"""单步执行状态。"""
|
|
||||||
|
|
||||||
PENDING = "pending"
|
|
||||||
SUCCESS = "success"
|
|
||||||
FAILED = "failed"
|
|
||||||
SKIPPED = "skipped"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class DeployStepResult:
|
|
||||||
"""单步执行结果。"""
|
|
||||||
|
|
||||||
step: DeployStep
|
|
||||||
status: DeployStatus = DeployStatus.PENDING
|
|
||||||
detail: str = ""
|
|
||||||
duration_ms: float = 0.0
|
|
||||||
index: int = 0
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class DeployOutcome:
|
|
||||||
"""部署总结果(可解释:是否成功 + 是否需回滚 + reason)。"""
|
|
||||||
|
|
||||||
succeeded: bool
|
|
||||||
needs_rollback: bool
|
|
||||||
failed_step: Optional[str] = None
|
|
||||||
reason: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class DeployReport:
|
|
||||||
"""部署编排报告(逐步结果 + 总状态 + 日志行)。"""
|
|
||||||
|
|
||||||
plan_name: str
|
|
||||||
results: List[DeployStepResult] = field(default_factory=list)
|
|
||||||
outcome: Optional[DeployOutcome] = None
|
|
||||||
log_lines: List[str] = field(default_factory=list)
|
|
||||||
context: Dict[str, object] = field(default_factory=dict)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def succeeded(self) -> bool:
|
|
||||||
return self.outcome.succeeded if self.outcome else False
|
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
|
||||||
return {
|
|
||||||
"plan_name": self.plan_name,
|
|
||||||
"succeeded": self.succeeded,
|
|
||||||
"needs_rollback": self.outcome.needs_rollback if self.outcome else False,
|
|
||||||
"reason": self.outcome.reason if self.outcome else "",
|
|
||||||
"steps": [
|
|
||||||
{"index": r.index, "kind": r.step.kind.value, "name": r.step.name,
|
|
||||||
"status": r.status.value, "detail": r.detail,
|
|
||||||
"duration_ms": round(r.duration_ms, 2)}
|
|
||||||
for r in self.results
|
|
||||||
],
|
|
||||||
"log": list(self.log_lines),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 编排器
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
class DeployOrchestrator:
|
|
||||||
"""部署编排器:按序执行 DeployPlan 步骤,失败即中止并标记回滚。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
plan: 部署计划。
|
|
||||||
dry_run: 空跑模式(不执行 action,全部标记 SKIPPED;用于计划校验)。
|
|
||||||
logger: 日志回调(默认 print 到内部 log_lines 缓冲)。
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
plan: DeployPlan,
|
|
||||||
dry_run: bool = False,
|
|
||||||
logger: Optional[Callable[[str], None]] = None,
|
|
||||||
) -> None:
|
|
||||||
self.plan = plan
|
|
||||||
self.dry_run = bool(dry_run)
|
|
||||||
self._log: List[str] = []
|
|
||||||
self._logger = logger
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def _log_line(self, line: str) -> None:
|
|
||||||
self._log.append(line)
|
|
||||||
if self._logger is not None:
|
|
||||||
self._logger(line)
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def execute(self, context: Optional[Dict[str, object]] = None) -> DeployReport:
|
|
||||||
"""执行部署计划,返回报告。"""
|
|
||||||
report = DeployReport(plan_name=self.plan.name)
|
|
||||||
ctx: Dict[str, object] = {
|
|
||||||
"release": self.plan.release,
|
|
||||||
"namespace": self.plan.namespace,
|
|
||||||
"chart": self.plan.chart,
|
|
||||||
"version": self.plan.version,
|
|
||||||
"health": {
|
|
||||||
"endpoint": self.plan.health.endpoint,
|
|
||||||
"timeout_s": self.plan.health.timeout_s,
|
|
||||||
"retries": self.plan.health.retries,
|
|
||||||
"availability_target": self.plan.health.availability_target,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if context:
|
|
||||||
ctx.update(context)
|
|
||||||
report.context = dict(ctx)
|
|
||||||
|
|
||||||
if not self.plan.steps:
|
|
||||||
report.outcome = DeployOutcome(
|
|
||||||
succeeded=False, needs_rollback=False,
|
|
||||||
reason="部署计划无步骤(DeployPlan.steps 为空)")
|
|
||||||
self._log_line("[FAIL] 部署计划无步骤,未执行")
|
|
||||||
report.log_lines = list(self._log)
|
|
||||||
return report
|
|
||||||
|
|
||||||
self._log_line(
|
|
||||||
f"[START] 部署 {self.plan.name}:release={self.plan.release} "
|
|
||||||
f"namespace={self.plan.namespace} version={self.plan.version} "
|
|
||||||
f"steps={len(self.plan.steps)}")
|
|
||||||
|
|
||||||
failed_result: Optional[DeployStepResult] = None
|
|
||||||
for idx, step in enumerate(self.plan.steps):
|
|
||||||
result = DeployStepResult(step=step, index=idx)
|
|
||||||
|
|
||||||
if self.dry_run:
|
|
||||||
result.status = DeployStatus.SKIPPED
|
|
||||||
result.detail = "dry-run 跳过"
|
|
||||||
report.results.append(result)
|
|
||||||
self._log_line(
|
|
||||||
f"[SKIP] #{idx} {step.kind.label}/{step.name}(dry-run)")
|
|
||||||
continue
|
|
||||||
|
|
||||||
self._log_line(f"[RUN] #{idx} {step.kind.label}/{step.name}")
|
|
||||||
started = time.monotonic()
|
|
||||||
try:
|
|
||||||
ok, detail = step.action(ctx)
|
|
||||||
except Exception as exc: # noqa: BLE001 - 任意 action 异常视为失败
|
|
||||||
ok, detail = False, f"action 异常:{exc}"
|
|
||||||
result.duration_ms = (time.monotonic() - started) * 1000.0
|
|
||||||
result.detail = detail
|
|
||||||
result.status = DeployStatus.SUCCESS if ok else DeployStatus.FAILED
|
|
||||||
report.results.append(result)
|
|
||||||
|
|
||||||
tag = "OK" if ok else "FAIL"
|
|
||||||
self._log_line(
|
|
||||||
f"[{tag}] #{idx} {step.name}:{detail} "
|
|
||||||
f"({result.duration_ms:.0f}ms)")
|
|
||||||
|
|
||||||
if not ok:
|
|
||||||
if step.required:
|
|
||||||
failed_result = result
|
|
||||||
break
|
|
||||||
# 非必需步骤失败:告警但继续
|
|
||||||
self._log_line(
|
|
||||||
f"[WARN] #{idx} {step.name} 非必需步骤失败,继续编排")
|
|
||||||
|
|
||||||
report.log_lines = list(self._log)
|
|
||||||
|
|
||||||
if self.dry_run:
|
|
||||||
report.outcome = DeployOutcome(
|
|
||||||
succeeded=True, needs_rollback=False,
|
|
||||||
reason=f"dry-run:{len(self.plan.steps)} 步全部跳过(计划校验通过)")
|
|
||||||
return report
|
|
||||||
|
|
||||||
if failed_result is not None:
|
|
||||||
needs_rollback = failed_result.step.kind.is_checkpoint
|
|
||||||
report.outcome = DeployOutcome(
|
|
||||||
succeeded=False,
|
|
||||||
needs_rollback=needs_rollback,
|
|
||||||
failed_step=failed_result.step.name,
|
|
||||||
reason=(
|
|
||||||
f"步骤 #{failed_result.index} {failed_result.step.kind.label}/"
|
|
||||||
f"{failed_result.step.name} 失败:{failed_result.detail}"
|
|
||||||
+ (",需回滚到上一稳定版本" if needs_rollback
|
|
||||||
else "(非回滚检查点,无需回滚)")))
|
|
||||||
else:
|
|
||||||
report.outcome = DeployOutcome(
|
|
||||||
succeeded=True, needs_rollback=False,
|
|
||||||
reason=f"全部 {len(report.results)} 步执行成功,部署完成")
|
|
||||||
return report
|
|
||||||
@@ -1,265 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""部署回滚引擎(Issue #60 / PRD 5.6「⑥ 部署底座」)。
|
|
||||||
|
|
||||||
PRD 5.6 一键部署需配套**回滚机制**——部署失败时恢复到部署前的稳定配置版本,
|
|
||||||
保证可用性。本模块把回滚落为**可测试的纯标准库引擎**:部署前对当前配置版本
|
|
||||||
打快照(:class:`RollbackPoint`),失败时按快照恢复(:class:`RollbackManager`),
|
|
||||||
产出可解释的 :class:`RollbackResult`。
|
|
||||||
|
|
||||||
设计要点
|
|
||||||
--------
|
|
||||||
1. **快照即配置版本**(``RollbackPoint``):部署前记录当前稳定版本的元信息
|
|
||||||
(release/namespace/version/chart/values_hash/创建时间),存入快照栈。
|
|
||||||
快照不可变(只读),保证回滚目标确定。
|
|
||||||
2. **快照栈**(``RollbackManager``):每次成功部署压栈;失败时弹出最近快照执行
|
|
||||||
回滚。支持多版本回溯(rollback(n) 回滚 n 个版本)。
|
|
||||||
3. **回滚动作**(``restore_action`` 回调):默认模拟成功;真实环境注入
|
|
||||||
``helm rollback`` 等真回调。回滚后重新健康检查(契约对齐 deploy_plan)。
|
|
||||||
4. **纯标准库**:无 k8s/helm 依赖,便于离线测试与 CI 集成。
|
|
||||||
|
|
||||||
用法::
|
|
||||||
|
|
||||||
mgr = RollbackManager()
|
|
||||||
mgr.snapshot(plan) # 部署前快照
|
|
||||||
report = orchestrator.execute() # 部署
|
|
||||||
if not report.succeeded:
|
|
||||||
result = mgr.rollback_to_latest(reason=report.outcome.reason)
|
|
||||||
print(result.status, result.detail)
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from enum import Enum
|
|
||||||
from typing import Callable, Dict, List, Optional, Tuple
|
|
||||||
|
|
||||||
from deploy_plan import (
|
|
||||||
DEFAULT_AVAILABILITY_TARGET,
|
|
||||||
DEFAULT_HEALTH_RETRIES,
|
|
||||||
DEFAULT_HEALTH_TIMEOUT_S,
|
|
||||||
DeployError,
|
|
||||||
DeployPlan,
|
|
||||||
HealthCheckContract,
|
|
||||||
)
|
|
||||||
|
|
||||||
#: 回滚动作回调:(snapshot, ctx) → (ok, detail)。
|
|
||||||
RestoreAction = Callable[["RollbackPoint", Dict[str, object]], Tuple[bool, str]]
|
|
||||||
|
|
||||||
|
|
||||||
def _default_restore(snapshot: "RollbackPoint",
|
|
||||||
ctx: Dict[str, object]) -> Tuple[bool, str]:
|
|
||||||
"""默认回滚动作:模拟成功(真实环境注入 helm rollback 回调)。"""
|
|
||||||
return True, f"模拟回滚到 {snapshot.version}(无 k8s/helm 环境)"
|
|
||||||
|
|
||||||
|
|
||||||
class RollbackStatus(str, Enum):
|
|
||||||
"""回滚执行状态。"""
|
|
||||||
|
|
||||||
SUCCESS = "success" # 回滚成功(含回滚后健康检查通过)
|
|
||||||
FAILED = "failed" # 回滚失败(restore 动作失败或健康检查不过)
|
|
||||||
NO_TARGET = "no_target" # 无可回滚快照(栈空)
|
|
||||||
SKIPPED = "skipped" # 跳过(手动)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class RollbackPoint:
|
|
||||||
"""部署前快照(不可变配置版本)。
|
|
||||||
|
|
||||||
Attributes:
|
|
||||||
release: Helm release 名。
|
|
||||||
namespace: 命名空间。
|
|
||||||
version: 快照时的版本(appVersion,回滚目标)。
|
|
||||||
chart: Chart 引用。
|
|
||||||
values_hash: values 配置的哈希(检测配置漂移)。
|
|
||||||
created_at: 快照创建时间戳(epoch 秒)。
|
|
||||||
reason: 快照说明(如 "部署 v1.1.0 前的稳定版本 v1.0.0")。
|
|
||||||
"""
|
|
||||||
|
|
||||||
release: str
|
|
||||||
namespace: str
|
|
||||||
version: str
|
|
||||||
chart: str
|
|
||||||
values_hash: str
|
|
||||||
created_at: float = field(default_factory=time.time)
|
|
||||||
reason: str = ""
|
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
|
||||||
return {
|
|
||||||
"release": self.release,
|
|
||||||
"namespace": self.namespace,
|
|
||||||
"version": self.version,
|
|
||||||
"chart": self.chart,
|
|
||||||
"values_hash": self.values_hash,
|
|
||||||
"created_at": self.created_at,
|
|
||||||
"reason": self.reason,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class RollbackResult:
|
|
||||||
"""回滚执行结果(可解释:状态 + 目标版本 + reason)。"""
|
|
||||||
|
|
||||||
status: RollbackStatus
|
|
||||||
target: Optional[RollbackPoint] = None
|
|
||||||
detail: str = ""
|
|
||||||
reason: str = ""
|
|
||||||
|
|
||||||
@property
|
|
||||||
def succeeded(self) -> bool:
|
|
||||||
return self.status is RollbackStatus.SUCCESS
|
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
|
||||||
return {
|
|
||||||
"status": self.status.value,
|
|
||||||
"succeeded": self.succeeded,
|
|
||||||
"target": self.target.to_dict() if self.target else None,
|
|
||||||
"detail": self.detail,
|
|
||||||
"reason": self.reason,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class RollbackManager:
|
|
||||||
"""部署回滚管理器:快照栈 + 回滚执行。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
restore_action: 回滚动作回调(默认模拟成功)。
|
|
||||||
post_rollback_health: 回滚后是否健康检查(默认 False,由编排器单独跑)。
|
|
||||||
max_snapshots: 快照栈上限(FIFO 淘汰,防内存膨胀;默认 10)。
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
restore_action: RestoreAction = _default_restore,
|
|
||||||
post_rollback_health: bool = False,
|
|
||||||
max_snapshots: int = 10,
|
|
||||||
) -> None:
|
|
||||||
if max_snapshots < 1:
|
|
||||||
raise DeployError(f"max_snapshots 必须 ≥ 1,实际 {max_snapshots}")
|
|
||||||
self._stack: List[RollbackPoint] = []
|
|
||||||
self.restore_action = restore_action
|
|
||||||
self.post_rollback_health = bool(post_rollback_health)
|
|
||||||
self.max_snapshots = int(max_snapshots)
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# 快照栈
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
@property
|
|
||||||
def snapshots(self) -> List[RollbackPoint]:
|
|
||||||
"""当前快照栈(栈顶 = 最近快照 = 列表末尾)。只读视图。"""
|
|
||||||
return list(self._stack)
|
|
||||||
|
|
||||||
def latest(self) -> Optional[RollbackPoint]:
|
|
||||||
"""栈顶快照(最近一次成功版本);栈空返回 None。"""
|
|
||||||
return self._stack[-1] if self._stack else None
|
|
||||||
|
|
||||||
def snapshot(self, plan: DeployPlan, values: Optional[Dict[str, object]] = None,
|
|
||||||
reason: str = "") -> RollbackPoint:
|
|
||||||
"""部署前打快照(压栈)。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
plan: 部署计划(取 release/namespace/version/chart)。
|
|
||||||
values: 当前 values 配置(用于计算 values_hash,检测漂移)。
|
|
||||||
reason: 快照说明。
|
|
||||||
"""
|
|
||||||
values_hash = _hash_values(values or {})
|
|
||||||
point = RollbackPoint(
|
|
||||||
release=plan.release,
|
|
||||||
namespace=plan.namespace,
|
|
||||||
version=plan.version,
|
|
||||||
chart=plan.chart,
|
|
||||||
values_hash=values_hash,
|
|
||||||
reason=reason or f"部署 {plan.name} 前快照(version={plan.version})",
|
|
||||||
)
|
|
||||||
self._stack.append(point)
|
|
||||||
# FIFO 淘汰(保留最近 max_snapshots 个)
|
|
||||||
if len(self._stack) > self.max_snapshots:
|
|
||||||
self._stack = self._stack[-self.max_snapshots:]
|
|
||||||
return point
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# 回滚
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def rollback_to_latest(self, reason: str = "") -> RollbackResult:
|
|
||||||
"""回滚到最近快照(弹出栈顶)。"""
|
|
||||||
return self._rollback(n=1, reason=reason)
|
|
||||||
|
|
||||||
def rollback(self, n: int = 1, reason: str = "") -> RollbackResult:
|
|
||||||
"""回滚 n 个版本(弹出 n 个快照,回滚到第 n 个)。"""
|
|
||||||
return self._rollback(n=n, reason=reason)
|
|
||||||
|
|
||||||
def _rollback(self, n: int, reason: str) -> RollbackResult:
|
|
||||||
if n < 1:
|
|
||||||
return RollbackResult(
|
|
||||||
status=RollbackStatus.SKIPPED,
|
|
||||||
detail=f"n={n},跳过回滚",
|
|
||||||
reason="回滚步数 n 必须 ≥ 1")
|
|
||||||
if len(self._stack) < n:
|
|
||||||
return RollbackResult(
|
|
||||||
status=RollbackStatus.NO_TARGET,
|
|
||||||
detail=f"快照栈仅 {len(self._stack)} 个,无法回滚 {n} 个版本",
|
|
||||||
reason="无可回滚的稳定版本快照")
|
|
||||||
|
|
||||||
# 弹出 n 个快照,回滚目标 = 第 n 个(最后弹出的)
|
|
||||||
popped: List[RollbackPoint] = []
|
|
||||||
for _ in range(n):
|
|
||||||
popped.append(self._stack.pop())
|
|
||||||
target = popped[-1]
|
|
||||||
|
|
||||||
ctx: Dict[str, object] = {
|
|
||||||
"release": target.release,
|
|
||||||
"namespace": target.namespace,
|
|
||||||
"version": target.version,
|
|
||||||
"reason": reason or f"部署失败,回滚到 {target.version}",
|
|
||||||
}
|
|
||||||
try:
|
|
||||||
ok, detail = self.restore_action(target, ctx)
|
|
||||||
except Exception as exc: # noqa: BLE001
|
|
||||||
ok, detail = False, f"restore action 异常:{exc}"
|
|
||||||
|
|
||||||
if not ok:
|
|
||||||
# 回滚失败:把目标压回栈顶(保持快照栈一致性)
|
|
||||||
self._stack.append(target)
|
|
||||||
return RollbackResult(
|
|
||||||
status=RollbackStatus.FAILED,
|
|
||||||
target=target,
|
|
||||||
detail=detail,
|
|
||||||
reason=f"回滚到 {target.version} 失败:{detail}")
|
|
||||||
|
|
||||||
full_detail = detail
|
|
||||||
# 回滚后健康检查(可选)
|
|
||||||
if self.post_rollback_health:
|
|
||||||
health_ok, health_detail = self._health_check(target)
|
|
||||||
if not health_ok:
|
|
||||||
self._stack.append(target)
|
|
||||||
return RollbackResult(
|
|
||||||
status=RollbackStatus.FAILED,
|
|
||||||
target=target,
|
|
||||||
detail=f"回滚动作成功但健康检查失败:{health_detail}",
|
|
||||||
reason=f"回滚到 {target.version} 后健康检查未通过")
|
|
||||||
full_detail = f"{detail};健康检查通过"
|
|
||||||
|
|
||||||
return RollbackResult(
|
|
||||||
status=RollbackStatus.SUCCESS,
|
|
||||||
target=target,
|
|
||||||
detail=full_detail,
|
|
||||||
reason=f"回滚到 {target.version} 成功"
|
|
||||||
+ (f"({reason})" if reason else ""))
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def _health_check(self, target: RollbackPoint) -> Tuple[bool, str]:
|
|
||||||
"""回滚后健康检查(模拟,默认通过;真实环境注入 restore_action 内)。"""
|
|
||||||
# 纯标准库模拟:回滚后假设服务恢复(真实健康检查由 deploy_plan 编排)
|
|
||||||
return True, f"回滚后 {target.version} 服务健康(模拟)"
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 辅助
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def _hash_values(values: Dict[str, object]) -> str:
|
|
||||||
"""计算 values 配置的短哈希(检测配置漂移,SHA1 前 12 位)。"""
|
|
||||||
payload = json.dumps(values, sort_keys=True, ensure_ascii=False,
|
|
||||||
default=str)
|
|
||||||
return hashlib.sha1(payload.encode("utf-8")).hexdigest()[:12]
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""测试引导:把 ``deploy/orchestration`` 挂到 sys.path,使 deploy_plan/rollback
|
|
||||||
可被 ``from deploy_plan import ...`` 导入(orchestration 包内相对导入需要)。
|
|
||||||
|
|
||||||
与 core 模块测试引导同款模式。
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import types
|
|
||||||
|
|
||||||
PKG_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
||||||
if PKG_DIR not in sys.path:
|
|
||||||
sys.path.insert(0, PKG_DIR)
|
|
||||||
@@ -1,200 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""部署编排引擎测试(Issue #60)。
|
|
||||||
|
|
||||||
覆盖:
|
|
||||||
1. 默认 Helm 计划构造(4 步:pre_check/deploy/health_check/post_check);
|
|
||||||
2. 步骤顺序约束校验(违例 → DeployError);
|
|
||||||
3. 全成功执行(succeeded + 无需回滚);
|
|
||||||
4. deploy 步骤失败(needs_rollback + failed_step);
|
|
||||||
5. pre_check 失败(needs_rollback=False,非回滚检查点);
|
|
||||||
6. 非必需 post_check 失败(继续编排,succeeded);
|
|
||||||
7. action 异常(视为失败);
|
|
||||||
8. dry_run(全部 SKIPPED);
|
|
||||||
9. 空计划(succeeded=False);
|
|
||||||
10. HealthCheckContract 校验;
|
|
||||||
11. 报告序列化。
|
|
||||||
"""
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
import _bootstrap # noqa: F401 (sys.path 挂载)
|
|
||||||
|
|
||||||
from deploy_plan import (
|
|
||||||
DEFAULT_AVAILABILITY_TARGET,
|
|
||||||
DeployError,
|
|
||||||
DeployOrchestrator,
|
|
||||||
DeployOutcome,
|
|
||||||
DeployPlan,
|
|
||||||
DeployStep,
|
|
||||||
DeployStepKind,
|
|
||||||
DeployStatus,
|
|
||||||
HealthCheckContract,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _action(ok: bool, detail: str = ""):
|
|
||||||
"""构造固定返回值的 action。"""
|
|
||||||
def _fn(ctx):
|
|
||||||
return ok, (detail or ("成功" if ok else "失败"))
|
|
||||||
return _fn
|
|
||||||
|
|
||||||
|
|
||||||
def _raising_action(exc_type=RuntimeError):
|
|
||||||
def _fn(ctx):
|
|
||||||
raise exc_type("boom")
|
|
||||||
return _fn
|
|
||||||
|
|
||||||
|
|
||||||
class TestDeployPlanModel(unittest.TestCase):
|
|
||||||
"""部署计划模型与顺序约束。"""
|
|
||||||
|
|
||||||
def test_default_helm_release_has_four_steps(self):
|
|
||||||
plan = DeployPlan.default_helm_release()
|
|
||||||
kinds = [s.kind for s in plan.steps]
|
|
||||||
self.assertEqual(kinds, [
|
|
||||||
DeployStepKind.PRE_CHECK,
|
|
||||||
DeployStepKind.DEPLOY,
|
|
||||||
DeployStepKind.HEALTH_CHECK,
|
|
||||||
DeployStepKind.POST_CHECK,
|
|
||||||
])
|
|
||||||
# post_check 默认非必需
|
|
||||||
self.assertFalse(plan.steps[-1].required)
|
|
||||||
|
|
||||||
def test_step_order_violation_rejected(self):
|
|
||||||
# health_check 排在 deploy 前 → 违例
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
DeployPlan(
|
|
||||||
name="bad",
|
|
||||||
steps=[
|
|
||||||
DeployStep(DeployStepKind.HEALTH_CHECK, "hc"),
|
|
||||||
DeployStep(DeployStepKind.DEPLOY, "d"),
|
|
||||||
])
|
|
||||||
|
|
||||||
def test_empty_step_name_rejected(self):
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
DeployStep(DeployStepKind.DEPLOY, "")
|
|
||||||
|
|
||||||
def test_plan_name_required(self):
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
DeployPlan(name="")
|
|
||||||
|
|
||||||
|
|
||||||
class TestOrchestratorSuccess(unittest.TestCase):
|
|
||||||
"""全成功编排。"""
|
|
||||||
|
|
||||||
def test_all_steps_succeed(self):
|
|
||||||
plan = DeployPlan.default_helm_release()
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertTrue(report.succeeded)
|
|
||||||
self.assertFalse(report.outcome.needs_rollback)
|
|
||||||
self.assertEqual(len(report.results), 4)
|
|
||||||
self.assertEqual(
|
|
||||||
[r.status for r in report.results],
|
|
||||||
[DeployStatus.SUCCESS] * 4)
|
|
||||||
# 日志含 START/RUN/OK
|
|
||||||
self.assertTrue(any("[START]" in ln for ln in report.log_lines))
|
|
||||||
self.assertTrue(any("[OK]" in ln for ln in report.log_lines))
|
|
||||||
|
|
||||||
def test_context_populated(self):
|
|
||||||
plan = DeployPlan.default_helm_release()
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertEqual(report.context["release"], "iaop")
|
|
||||||
self.assertEqual(report.context["namespace"], "iaop")
|
|
||||||
self.assertIn("health", report.context)
|
|
||||||
|
|
||||||
|
|
||||||
class TestOrchestratorFailures(unittest.TestCase):
|
|
||||||
"""失败场景 + 回滚标记。"""
|
|
||||||
|
|
||||||
def test_deploy_failure_triggers_rollback(self):
|
|
||||||
plan = DeployPlan.default_helm_release(
|
|
||||||
step_actions={DeployStepKind.DEPLOY: _action(False, "helm 失败")})
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertFalse(report.succeeded)
|
|
||||||
self.assertTrue(report.outcome.needs_rollback) # deploy 是回滚检查点
|
|
||||||
self.assertIsNotNone(report.outcome.failed_step)
|
|
||||||
# deploy 之后的 health_check/post_check 未执行
|
|
||||||
executed_kinds = [r.step.kind for r in report.results]
|
|
||||||
self.assertNotIn(DeployStepKind.HEALTH_CHECK, executed_kinds)
|
|
||||||
self.assertNotIn(DeployStepKind.POST_CHECK, executed_kinds)
|
|
||||||
|
|
||||||
def test_pre_check_failure_no_rollback(self):
|
|
||||||
plan = DeployPlan.default_helm_release(
|
|
||||||
step_actions={DeployStepKind.PRE_CHECK: _action(False, "集群不可达")})
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertFalse(report.succeeded)
|
|
||||||
# pre_check 失败:尚未部署,无需回滚
|
|
||||||
self.assertFalse(report.outcome.needs_rollback)
|
|
||||||
self.assertIn("集群不可达", report.outcome.reason)
|
|
||||||
|
|
||||||
def test_non_required_post_check_failure_continues(self):
|
|
||||||
plan = DeployPlan.default_helm_release(
|
|
||||||
step_actions={DeployStepKind.POST_CHECK: _action(False, "烟测失败")})
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
# post_check 非必需,失败仍判成功(核心步骤都过了)
|
|
||||||
self.assertTrue(report.succeeded)
|
|
||||||
self.assertFalse(report.outcome.needs_rollback)
|
|
||||||
# 但有 WARN 日志
|
|
||||||
self.assertTrue(any("[WARN]" in ln for ln in report.log_lines))
|
|
||||||
|
|
||||||
def test_action_exception_treated_as_failure(self):
|
|
||||||
plan = DeployPlan.default_helm_release(
|
|
||||||
step_actions={DeployStepKind.DEPLOY: _raising_action()})
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertFalse(report.succeeded)
|
|
||||||
self.assertTrue(report.outcome.needs_rollback)
|
|
||||||
self.assertIn("action 异常", report.outcome.reason)
|
|
||||||
|
|
||||||
def test_health_check_failure_triggers_rollback(self):
|
|
||||||
plan = DeployPlan.default_helm_release(
|
|
||||||
step_actions={DeployStepKind.HEALTH_CHECK: _action(False, "/health 不可用")})
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertFalse(report.succeeded)
|
|
||||||
self.assertTrue(report.outcome.needs_rollback)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDryRunAndEmpty(unittest.TestCase):
|
|
||||||
"""dry_run + 空计划。"""
|
|
||||||
|
|
||||||
def test_dry_run_skips_all(self):
|
|
||||||
plan = DeployPlan.default_helm_release()
|
|
||||||
report = DeployOrchestrator(plan, dry_run=True).execute()
|
|
||||||
self.assertTrue(report.succeeded)
|
|
||||||
self.assertEqual(
|
|
||||||
[r.status for r in report.results],
|
|
||||||
[DeployStatus.SKIPPED] * 4)
|
|
||||||
|
|
||||||
def test_empty_plan_fails(self):
|
|
||||||
plan = DeployPlan(name="empty", steps=[])
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
self.assertFalse(report.succeeded)
|
|
||||||
self.assertIn("无步骤", report.outcome.reason)
|
|
||||||
|
|
||||||
|
|
||||||
class TestHealthContractAndReport(unittest.TestCase):
|
|
||||||
"""HealthCheckContract 校验 + 报告序列化。"""
|
|
||||||
|
|
||||||
def test_health_contract_defaults(self):
|
|
||||||
h = HealthCheckContract()
|
|
||||||
self.assertEqual(h.endpoint, "http://iaop:8000/health")
|
|
||||||
self.assertEqual(h.retries, 3)
|
|
||||||
self.assertAlmostEqual(h.availability_target, DEFAULT_AVAILABILITY_TARGET)
|
|
||||||
|
|
||||||
def test_health_contract_validation(self):
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
HealthCheckContract(timeout_s=0)
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
HealthCheckContract(retries=0)
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
HealthCheckContract(availability_target=1.5)
|
|
||||||
|
|
||||||
def test_report_to_dict(self):
|
|
||||||
plan = DeployPlan.default_helm_release()
|
|
||||||
report = DeployOrchestrator(plan).execute()
|
|
||||||
d = report.to_dict()
|
|
||||||
self.assertTrue(d["succeeded"])
|
|
||||||
self.assertEqual(len(d["steps"]), 4)
|
|
||||||
self.assertEqual(d["steps"][0]["kind"], "pre_check")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""部署回滚引擎测试(Issue #60)。
|
|
||||||
|
|
||||||
覆盖:
|
|
||||||
1. 快照压栈 + latest 查询;
|
|
||||||
2. FIFO 淘汰(超 max_snapshots);
|
|
||||||
3. rollback_to_latest 成功(弹出栈顶 + 回滚目标版本);
|
|
||||||
4. rollback(n) 多版本回溯;
|
|
||||||
5. 空栈回滚(NO_TARGET);
|
|
||||||
6. restore 动作失败(FAILED + 压回栈顶);
|
|
||||||
7. restore 异常(FAILED);
|
|
||||||
8. post_rollback_health 失败(FAILED + 压回);
|
|
||||||
9. n<1 跳过;
|
|
||||||
10. values_hash 漂移检测;
|
|
||||||
11. 报告序列化。
|
|
||||||
"""
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
import _bootstrap # noqa: F401 (sys.path 挂载)
|
|
||||||
|
|
||||||
from deploy_plan import DeployPlan
|
|
||||||
from rollback import (
|
|
||||||
DeployError,
|
|
||||||
RollbackManager,
|
|
||||||
RollbackPoint,
|
|
||||||
RollbackStatus,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _restore(ok: bool, detail: str = ""):
|
|
||||||
def _fn(snapshot, ctx):
|
|
||||||
return ok, (detail or ("回滚成功" if ok else "回滚失败"))
|
|
||||||
return _fn
|
|
||||||
|
|
||||||
|
|
||||||
def _raising_restore():
|
|
||||||
def _fn(snapshot, ctx):
|
|
||||||
raise RuntimeError("restore boom")
|
|
||||||
return _fn
|
|
||||||
|
|
||||||
|
|
||||||
def _make_plan(version="v1.0.0", release="iaop"):
|
|
||||||
return DeployPlan.default_helm_release(release=release, version=version)
|
|
||||||
|
|
||||||
|
|
||||||
class TestSnapshotStack(unittest.TestCase):
|
|
||||||
"""快照压栈与查询。"""
|
|
||||||
|
|
||||||
def test_snapshot_push_and_latest(self):
|
|
||||||
mgr = RollbackManager()
|
|
||||||
# snapshot 即压栈(部署前快照),latest 即栈顶
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"), values={"k": 1}, reason="第一次")
|
|
||||||
self.assertEqual(mgr.latest().version, "v1.0.0")
|
|
||||||
self.assertEqual(len(mgr.snapshots), 1)
|
|
||||||
mgr.snapshot(_make_plan("v1.1.0"), values={"k": 2})
|
|
||||||
self.assertEqual(mgr.latest().version, "v1.1.0")
|
|
||||||
self.assertEqual(len(mgr.snapshots), 2)
|
|
||||||
# 空栈时 latest 返回 None
|
|
||||||
self.assertIsNotNone(mgr.latest())
|
|
||||||
mgr2 = RollbackManager()
|
|
||||||
self.assertIsNone(mgr2.latest())
|
|
||||||
|
|
||||||
def test_fifo_eviction(self):
|
|
||||||
mgr = RollbackManager(max_snapshots=2)
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
mgr.snapshot(_make_plan("v1.1.0"))
|
|
||||||
mgr.snapshot(_make_plan("v1.2.0"))
|
|
||||||
# 上限 2,淘汰最旧
|
|
||||||
self.assertEqual(len(mgr.snapshots), 2)
|
|
||||||
self.assertEqual([s.version for s in mgr.snapshots], ["v1.1.0", "v1.2.0"])
|
|
||||||
|
|
||||||
def test_max_snapshots_validation(self):
|
|
||||||
with self.assertRaises(DeployError):
|
|
||||||
RollbackManager(max_snapshots=0)
|
|
||||||
|
|
||||||
def test_values_hash_changes_with_config(self):
|
|
||||||
mgr = RollbackManager()
|
|
||||||
p1 = mgr.snapshot(_make_plan(), values={"backend": "gpu"})
|
|
||||||
p2 = mgr.snapshot(_make_plan(), values={"backend": "npu"})
|
|
||||||
self.assertNotEqual(p1.values_hash, p2.values_hash)
|
|
||||||
|
|
||||||
|
|
||||||
class TestRollbackSuccess(unittest.TestCase):
|
|
||||||
"""回滚成功场景。"""
|
|
||||||
|
|
||||||
def test_rollback_to_latest_success(self):
|
|
||||||
mgr = RollbackManager(restore_action=_restore(True))
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
mgr.snapshot(_make_plan("v1.1.0"))
|
|
||||||
result = mgr.rollback_to_latest(reason="部署 v1.2.0 失败")
|
|
||||||
self.assertTrue(result.succeeded)
|
|
||||||
self.assertEqual(result.status, RollbackStatus.SUCCESS)
|
|
||||||
self.assertEqual(result.target.version, "v1.1.0")
|
|
||||||
# 弹出栈顶后栈剩 1 个
|
|
||||||
self.assertEqual(len(mgr.snapshots), 1)
|
|
||||||
self.assertEqual(mgr.latest().version, "v1.0.0")
|
|
||||||
self.assertIn("v1.2.0 失败", result.reason)
|
|
||||||
|
|
||||||
def test_rollback_n_versions(self):
|
|
||||||
mgr = RollbackManager(restore_action=_restore(True))
|
|
||||||
for v in ["v1.0.0", "v1.1.0", "v1.2.0"]:
|
|
||||||
mgr.snapshot(_make_plan(v))
|
|
||||||
# 回滚 2 个版本 → 目标 v1.1.0(弹出 v1.2.0 和 v1.1.0)
|
|
||||||
result = mgr.rollback(n=2)
|
|
||||||
self.assertTrue(result.succeeded)
|
|
||||||
self.assertEqual(result.target.version, "v1.1.0")
|
|
||||||
self.assertEqual(len(mgr.snapshots), 1)
|
|
||||||
|
|
||||||
|
|
||||||
class TestRollbackFailures(unittest.TestCase):
|
|
||||||
"""回滚失败与边界。"""
|
|
||||||
|
|
||||||
def test_empty_stack_no_target(self):
|
|
||||||
mgr = RollbackManager()
|
|
||||||
result = mgr.rollback_to_latest()
|
|
||||||
self.assertEqual(result.status, RollbackStatus.NO_TARGET)
|
|
||||||
self.assertFalse(result.succeeded)
|
|
||||||
|
|
||||||
def test_insufficient_stack_for_n(self):
|
|
||||||
mgr = RollbackManager(restore_action=_restore(True))
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
# 栈仅 1 个,回滚 2 个 → NO_TARGET
|
|
||||||
result = mgr.rollback(n=2)
|
|
||||||
self.assertEqual(result.status, RollbackStatus.NO_TARGET)
|
|
||||||
|
|
||||||
def test_restore_failure_pushes_target_back(self):
|
|
||||||
mgr = RollbackManager(restore_action=_restore(False, "helm rollback 失败"))
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
mgr.snapshot(_make_plan("v1.1.0"))
|
|
||||||
result = mgr.rollback_to_latest()
|
|
||||||
self.assertEqual(result.status, RollbackStatus.FAILED)
|
|
||||||
# 失败时目标压回栈顶,栈仍 2 个
|
|
||||||
self.assertEqual(len(mgr.snapshots), 2)
|
|
||||||
self.assertEqual(mgr.latest().version, "v1.1.0")
|
|
||||||
|
|
||||||
def test_restore_exception_failure(self):
|
|
||||||
mgr = RollbackManager(restore_action=_raising_restore())
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
result = mgr.rollback_to_latest()
|
|
||||||
self.assertEqual(result.status, RollbackStatus.FAILED)
|
|
||||||
self.assertIn("异常", result.detail)
|
|
||||||
|
|
||||||
def test_post_rollback_health_failure(self):
|
|
||||||
# restore 成功但健康检查失败(注入失败的健康检查)
|
|
||||||
def _restore_then_health_fail(snapshot, ctx):
|
|
||||||
return True, "回滚动作成功"
|
|
||||||
mgr = RollbackManager(
|
|
||||||
restore_action=_restore_then_health_fail, post_rollback_health=True)
|
|
||||||
# 覆盖 _health_check 为失败
|
|
||||||
mgr._health_check = lambda target: (False, "/health 仍不可用")
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
result = mgr.rollback_to_latest()
|
|
||||||
self.assertEqual(result.status, RollbackStatus.FAILED)
|
|
||||||
self.assertIn("健康检查", result.detail)
|
|
||||||
# 压回栈顶
|
|
||||||
self.assertEqual(len(mgr.snapshots), 1)
|
|
||||||
|
|
||||||
def test_n_less_than_one_skipped(self):
|
|
||||||
mgr = RollbackManager()
|
|
||||||
result = mgr.rollback(n=0)
|
|
||||||
self.assertEqual(result.status, RollbackStatus.SKIPPED)
|
|
||||||
|
|
||||||
|
|
||||||
class TestResultExport(unittest.TestCase):
|
|
||||||
"""结果序列化。"""
|
|
||||||
|
|
||||||
def test_result_to_dict(self):
|
|
||||||
mgr = RollbackManager(restore_action=_restore(True))
|
|
||||||
mgr.snapshot(_make_plan("v1.0.0"))
|
|
||||||
result = mgr.rollback_to_latest()
|
|
||||||
d = result.to_dict()
|
|
||||||
self.assertEqual(d["status"], "success")
|
|
||||||
self.assertTrue(d["succeeded"])
|
|
||||||
self.assertEqual(d["target"]["version"], "v1.0.0")
|
|
||||||
|
|
||||||
def test_point_to_dict(self):
|
|
||||||
p = RollbackPoint(release="r", namespace="ns", version="v1",
|
|
||||||
chart="c", values_hash="abc123", reason="x")
|
|
||||||
d = p.to_dict()
|
|
||||||
self.assertEqual(d["version"], "v1")
|
|
||||||
self.assertEqual(d["values_hash"], "abc123")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
# 海绵钛驾驶舱布局资产 + 校验器(Issue #55 / PRD 5.5)
|
||||||
|
|
||||||
|
> 父 Issue「⑤ Ti 行业布局模板(四状态流程视图)· 0.5d」
|
||||||
|
|
||||||
|
把海绵钛车间驾驶舱布局落为**对齐 iAOP-cockpit-layout-v1 的资产 + 可校验的纯标准库
|
||||||
|
校验器**(无 node/前端构建环境,零运行时依赖)。
|
||||||
|
|
||||||
|
## 资产:`cockpit.ti.yaml`
|
||||||
|
|
||||||
|
四状态工艺流程(PRD 4.2 海绵钛:**氯化 → 精制 → 还原 → 蒸馏**):
|
||||||
|
|
||||||
|
- `process_view` 主视图(首屏立即加载),`stages` 声明四状态覆盖;
|
||||||
|
- `trend` 实时趋势(氯化炉温度 `CLF-01.TEMP` / 氯气流量 `CLF-01.CL2`);
|
||||||
|
- `kpi_card` KPI(TiCl₄纯度 `RF-01.PURITY` / 杂质 `RF-01.IMP` / 电耗 `E-01.KWH` / 蒸汽 `ST-01.STEAM`);
|
||||||
|
- `alarm_panel` 告警面板 + `nl_query` NL 查询入口。
|
||||||
|
|
||||||
|
所有 `bind` 的 `point_id` 对齐 `templates/ti-cl4/point-dict/point_dict.default.csv`。
|
||||||
|
|
||||||
|
## 校验器:`layout_validator.py`
|
||||||
|
|
||||||
|
`LayoutValidator(layout_yaml, point_dict_csv).validate()` → `LayoutReport`,校验:
|
||||||
|
|
||||||
|
1. **widget 类型合法**:在 `iAOP-cockpit-layout-v1` 允许集合内(process_view/trend/kpi_card/alarm_panel/nl_query);
|
||||||
|
2. **12 列网格不越界**:`0 ≤ x`、`x + w ≤ 12`、`y ≥ 0`、`w/h > 0`;
|
||||||
|
3. **bind point_id 在点位字典内**:防模板漂移(trend/kpi_card 的 bind 必须命中点字典);
|
||||||
|
4. **四状态覆盖完整**:process_view 的 stages 必须覆盖氯化/精制/还原/蒸馏,order 单调递增、id 唯一。
|
||||||
|
|
||||||
|
零依赖 YAML 子集解析(复制 impurity-forecast 的 `_parse_yaml_subset`,无 pyyaml)。
|
||||||
|
|
||||||
|
## 测试
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m unittest discover -s templates/ti-cl4/dashboard/tests -p "test_*.py" -v
|
||||||
|
```
|
||||||
|
|
||||||
|
覆盖正常 + 边界 + 错误(18 用例):真实资产端到端通过、非法类型、网格越界/负坐标/零宽、
|
||||||
|
bind 漂移、缺 process_view、缺必需状态、order 非单调、stage 重复、$schema 头、CSV 加载、空布局。
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""海绵钛驾驶舱布局资产 + 校验器包(Issue #55)。
|
||||||
|
|
||||||
|
对齐 PRD 5.5「⑤ 配置化驾驶舱」布局 JSON Schema(iAOP-cockpit-layout-v1):
|
||||||
|
四状态工艺流程(氯化 → 精制 → 还原 → 蒸馏)。
|
||||||
|
"""
|
||||||
|
from .layout_validator import (
|
||||||
|
LayoutError,
|
||||||
|
LayoutIssue,
|
||||||
|
LayoutReport,
|
||||||
|
LayoutValidator,
|
||||||
|
WidgetSpec,
|
||||||
|
ALLOWED_WIDGET_TYPES,
|
||||||
|
GRID_COLUMNS,
|
||||||
|
REQUIRED_STAGES,
|
||||||
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"LayoutError",
|
||||||
|
"LayoutIssue",
|
||||||
|
"LayoutReport",
|
||||||
|
"LayoutValidator",
|
||||||
|
"WidgetSpec",
|
||||||
|
"ALLOWED_WIDGET_TYPES",
|
||||||
|
"GRID_COLUMNS",
|
||||||
|
"REQUIRED_STAGES",
|
||||||
|
]
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""海绵钛驾驶舱布局冒烟脚本(Issue #55)。
|
||||||
|
|
||||||
|
直接运行 ``python _sanity_check.py`` 验证:cockpit.ti.yaml + point_dict.default.csv
|
||||||
|
端到端校验通过(widget 类型/网格/bind/四状态全覆盖)。零第三方依赖。
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
sys.path.insert(0, HERE)
|
||||||
|
|
||||||
|
from layout_validator import LayoutValidator # noqa: E402
|
||||||
|
|
||||||
|
LAYOUT_YAML = os.path.join(HERE, "cockpit.ti.yaml")
|
||||||
|
POINT_DICT_CSV = os.path.join(
|
||||||
|
HERE, os.pardir, "point-dict", "point_dict.default.csv")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
report = LayoutValidator(LAYOUT_YAML, POINT_DICT_CSV).validate()
|
||||||
|
if not report.passed:
|
||||||
|
print("FAIL")
|
||||||
|
for issue in report.errors:
|
||||||
|
print(f" - [{issue.widget_id}] {issue.field}: {issue.reason}")
|
||||||
|
return 1
|
||||||
|
print(f"OK: {report.widget_count} widgets,"
|
||||||
|
f"类型/网格/bind/四状态校验通过")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# 海绵钛(Ti)车间驾驶舱布局资产(iAOP-Template-Ti,EPIC #12)。
|
||||||
|
#
|
||||||
|
# 对齐 PRD 5.5「⑤ 配置化驾驶舱」布局 JSON Schema(iAOP-cockpit-layout-v1):
|
||||||
|
# 切换行业模板后,驾驶舱按本布局自动重排,无需改前端代码。
|
||||||
|
# widget 类型:process_view(工艺流程视图)/ trend(实时趋势)/ kpi_card(KPI卡片)/
|
||||||
|
# alarm_panel(告警面板)/ nl_query(NL查询入口)。
|
||||||
|
#
|
||||||
|
# 四状态工艺流程(PRD 4.2 海绵钛:氯化 → 精制 → 还原 → 蒸馏):
|
||||||
|
# - 氯化 (CLF-01):TiO₂ + Cl₂ + C → TiCl₄(沸腾氯化炉)
|
||||||
|
# - 精制 :粗 TiCl₄ → 精 TiCl₄(除钒/除硅,常压精馏)
|
||||||
|
# - 还原 (RF-01):TiCl₄ + Mg → 海绵钛(真空还原,Kroll 法)
|
||||||
|
# - 蒸馏 :海绵钛 + 残余 Mg/MgCl₂ 分离(真空蒸馏)
|
||||||
|
# process_view 的 stages 字段声明四状态覆盖,layout_validator 校验完整性。
|
||||||
|
#
|
||||||
|
# bind 的 point_id 对齐 templates/ti-cl4/point-dict/point_dict.default.csv。
|
||||||
|
$schema: iAOP-cockpit-layout-v1
|
||||||
|
title: 海绵钛车间驾驶舱
|
||||||
|
theme: dark
|
||||||
|
widgets:
|
||||||
|
# ---- 四状态工艺流程主视图(首屏立即加载) ---------------------------
|
||||||
|
- type: process_view
|
||||||
|
src: ti_four_state.svg
|
||||||
|
x: 0
|
||||||
|
y: 0
|
||||||
|
w: 12
|
||||||
|
h: 4
|
||||||
|
description: 四状态工艺流程(氯化 → 精制 → 还原 → 蒸馏)
|
||||||
|
stages:
|
||||||
|
- id: chlorination
|
||||||
|
name: 氯化
|
||||||
|
device: CLF-01
|
||||||
|
order: 1
|
||||||
|
- id: purification
|
||||||
|
name: 精制
|
||||||
|
order: 2
|
||||||
|
- id: reduction
|
||||||
|
name: 还原
|
||||||
|
device: RF-01
|
||||||
|
order: 3
|
||||||
|
- id: distillation
|
||||||
|
name: 蒸馏
|
||||||
|
order: 4
|
||||||
|
# ---- 实时趋势:氯化炉温度/氯气流量(工艺核心监控) -------------------
|
||||||
|
- type: trend
|
||||||
|
bind: CLF-01.TEMP
|
||||||
|
x: 0
|
||||||
|
y: 4
|
||||||
|
w: 6
|
||||||
|
h: 2
|
||||||
|
description: 氯化炉温度实时趋势(沸腾氯化炉温 850±50℃)
|
||||||
|
- type: trend
|
||||||
|
bind: CLF-01.CL2
|
||||||
|
x: 6
|
||||||
|
y: 4
|
||||||
|
w: 6
|
||||||
|
h: 2
|
||||||
|
description: 氯气流量实时趋势(流态化监控)
|
||||||
|
# ---- KPI 卡片:还原质量/能耗(海绵钛核心指标) -----------------------
|
||||||
|
- type: kpi_card
|
||||||
|
metric: ticl4_purity
|
||||||
|
bind: RF-01.PURITY
|
||||||
|
label: TiCl₄纯度
|
||||||
|
x: 0
|
||||||
|
y: 6
|
||||||
|
w: 3
|
||||||
|
h: 2
|
||||||
|
description: 还原 TiCl₄ 纯度(%,工艺 ≥ 99.9%)
|
||||||
|
- type: kpi_card
|
||||||
|
metric: ticl4_impurity
|
||||||
|
bind: RF-01.IMP
|
||||||
|
label: 杂质含量
|
||||||
|
x: 3
|
||||||
|
y: 6
|
||||||
|
w: 3
|
||||||
|
h: 2
|
||||||
|
description: 还原杂质含量(%,越低越好)
|
||||||
|
- type: kpi_card
|
||||||
|
metric: energy_per_ton
|
||||||
|
bind: E-01.KWH
|
||||||
|
label: 累计电耗
|
||||||
|
x: 6
|
||||||
|
y: 6
|
||||||
|
w: 3
|
||||||
|
h: 2
|
||||||
|
description: 车间累计电耗(kWh,单吨海绵钛综合能耗输入)
|
||||||
|
- type: kpi_card
|
||||||
|
metric: steam_flow
|
||||||
|
bind: ST-01.STEAM
|
||||||
|
label: 蒸汽流量
|
||||||
|
x: 9
|
||||||
|
y: 6
|
||||||
|
w: 3
|
||||||
|
h: 2
|
||||||
|
description: 蒸汽流量(t/h,公用工程监控)
|
||||||
|
# ---- 告警面板 + NL 查询入口 ------------------------------------------
|
||||||
|
- type: alarm_panel
|
||||||
|
x: 0
|
||||||
|
y: 8
|
||||||
|
w: 9
|
||||||
|
h: 3
|
||||||
|
description: 告警面板(氯化炉温/还原真空度/纯度/杂质异常)
|
||||||
|
- type: nl_query
|
||||||
|
x: 9
|
||||||
|
y: 8
|
||||||
|
w: 3
|
||||||
|
h: 3
|
||||||
|
description: 自然语言查询入口(工艺/质量/能耗问答)
|
||||||
@@ -0,0 +1,500 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""海绵钛驾驶舱布局校验器(Issue #55 / PRD 5.5「⑤ 配置化驾驶舱」)。
|
||||||
|
|
||||||
|
PRD 5.5:切换行业模板后,驾驶舱按布局资产自动重排,无需改前端代码。
|
||||||
|
本模块把布局资产(``cockpit.ti.yaml``)落为**可校验的纯标准库资产 + 校验器**——
|
||||||
|
给定布局 YAML + 点位字典 CSV,校验:
|
||||||
|
|
||||||
|
1. **widget 类型合法**:在 PRD 5.5 ``iAOP-cockpit-layout-v1`` 允许集合内
|
||||||
|
(process_view/trend/kpi_card/alarm_panel/nl_query)。
|
||||||
|
2. **12 列网格不越界**:每个 widget ``0 ≤ x`` 且 ``x + w ≤ 12``,``y ≥ 0``、
|
||||||
|
``h > 0``;坐标为非负整数,w/h 正整数(网格对齐)。
|
||||||
|
3. **bind 的 point_id 在点位字典内**:trend/kpi_card 的 ``bind`` 必须命中
|
||||||
|
``point_dict.default.csv`` 的 ``point_id`` 列(防模板漂移)。
|
||||||
|
4. **四状态视图覆盖完整**:process_view 的 ``stages`` 必须覆盖工艺全流程
|
||||||
|
(氯化/精制/还原/蒸馏),且 order 单调递增、id 唯一。
|
||||||
|
|
||||||
|
校验产出 :class:`LayoutReport`(PASS/FAIL + 逐条 :class:`LayoutIssue`,
|
||||||
|
每条 issue 带 ``reason`` 可解释)。
|
||||||
|
|
||||||
|
设计要点
|
||||||
|
--------
|
||||||
|
- **零依赖 YAML 子集解析**:复制 impurity-forecast features.py 的
|
||||||
|
``_parse_yaml_subset``(无 pyyaml),支持 map/list/标量/行内 flow map。
|
||||||
|
- **纯标准库**:CSV 用标准库 csv,无 numpy/pyyaml 依赖。
|
||||||
|
- **换行业只改资产**:校验器对任何对齐 ``iAOP-cockpit-layout-v1`` 的布局都适用。
|
||||||
|
|
||||||
|
用法::
|
||||||
|
|
||||||
|
report = LayoutValidator(layout_yaml, point_dict_csv).validate()
|
||||||
|
if not report.passed:
|
||||||
|
for issue in report.issues:
|
||||||
|
print(issue.severity, issue.widget_id, issue.reason)
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import csv
|
||||||
|
import os
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
|
#: iAOP-cockpit-layout-v1 允许的 widget 类型集合(对齐 resin _sanity_check)。
|
||||||
|
ALLOWED_WIDGET_TYPES = frozenset({
|
||||||
|
"process_view", "trend", "kpi_card", "alarm_panel", "nl_query",
|
||||||
|
})
|
||||||
|
|
||||||
|
#: 12 列网格(主流前端栅格标准,对齐 cockpit layout v1)。
|
||||||
|
GRID_COLUMNS = 12
|
||||||
|
|
||||||
|
#: 海绵钛四状态工艺流程(PRD 4.2:氯化 → 精制 → 还原 → 蒸馏)。
|
||||||
|
#: process_view 的 stages 必须覆盖这四个 id。
|
||||||
|
REQUIRED_STAGES = ("chlorination", "purification", "reduction", "distillation")
|
||||||
|
|
||||||
|
|
||||||
|
class LayoutError(ValueError):
|
||||||
|
"""布局资产解析/声明错误(YAML 格式错、表头缺字段等)。"""
|
||||||
|
|
||||||
|
|
||||||
|
class Severity(str, Enum):
|
||||||
|
"""问题严重度。"""
|
||||||
|
|
||||||
|
ERROR = "error" # 阻断:布局不可用(类型非法/越界/bind 缺失/状态缺失)
|
||||||
|
WARN = "warn" # 告警:可运行但不规范(重复/顺序乱)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LayoutIssue:
|
||||||
|
"""单条布局校验问题(含 reason 可解释)。"""
|
||||||
|
|
||||||
|
severity: Severity
|
||||||
|
reason: str
|
||||||
|
widget_id: str = "" # 关联 widget(index 或 src/metric)
|
||||||
|
field: str = "" # 关联字段(type/x/bind/stages ...)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_error(self) -> bool:
|
||||||
|
return self.severity is Severity.ERROR
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LayoutReport:
|
||||||
|
"""布局校验报告。"""
|
||||||
|
|
||||||
|
issues: List[LayoutIssue] = field(default_factory=list)
|
||||||
|
widget_count: int = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def errors(self) -> List[LayoutIssue]:
|
||||||
|
return [i for i in self.issues if i.is_error]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def passed(self) -> bool:
|
||||||
|
"""通过 = 无 ERROR(WARN 不阻断)。"""
|
||||||
|
return not any(i.is_error for i in self.issues)
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
"passed": self.passed,
|
||||||
|
"widget_count": self.widget_count,
|
||||||
|
"error_count": len(self.errors),
|
||||||
|
"warn_count": len(self.issues) - len(self.errors),
|
||||||
|
"issues": [
|
||||||
|
{"severity": i.severity.value, "widget_id": i.widget_id,
|
||||||
|
"field": i.field, "reason": i.reason}
|
||||||
|
for i in self.issues
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WidgetSpec:
|
||||||
|
"""单个 widget 的内存模型(从 YAML 解析)。"""
|
||||||
|
|
||||||
|
index: int # 在 widgets 列表中的位置(0 起)
|
||||||
|
type: str
|
||||||
|
x: int = 0
|
||||||
|
y: int = 0
|
||||||
|
w: int = 1
|
||||||
|
h: int = 1
|
||||||
|
bind: str = "" # trend/kpi_card 绑定的 point_id
|
||||||
|
src: str = "" # process_view 的 SVG
|
||||||
|
metric: str = "" # kpi_card 的 metric
|
||||||
|
label: str = ""
|
||||||
|
description: str = ""
|
||||||
|
stages: List[Dict[str, object]] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 零依赖 YAML 子集解析(复制自 impurity-forecast features.py,对齐 data-bus)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _parse_scalar(text: str) -> str:
|
||||||
|
"""去掉标量两侧引号与行内注释。"""
|
||||||
|
t = text.split(" #", 1)[0].strip()
|
||||||
|
if len(t) >= 2 and t[0] == t[-1] and t[0] in ("'", '"'):
|
||||||
|
return t[1:-1]
|
||||||
|
return t
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_flow_value(text: str):
|
||||||
|
"""解析 ``key: value`` 右侧值,支持行内 flow map ``{k: v, k: v}``。"""
|
||||||
|
t = text.split(" #", 1)[0].strip()
|
||||||
|
if t.startswith("{") and t.endswith("}"):
|
||||||
|
inner = t[1:-1].strip()
|
||||||
|
out: Dict[str, object] = {}
|
||||||
|
if not inner:
|
||||||
|
return out
|
||||||
|
for part in inner.split(","):
|
||||||
|
if ":" not in part:
|
||||||
|
raise LayoutError(f"flow map 项不是键值对:{part!r}")
|
||||||
|
k, _, v = part.partition(":")
|
||||||
|
out[k.strip()] = _parse_scalar(v)
|
||||||
|
return out
|
||||||
|
return _parse_scalar(text)
|
||||||
|
|
||||||
|
|
||||||
|
def _strip_comments(lines: List[str]) -> List[Tuple[str, int]]:
|
||||||
|
out: List[Tuple[str, int]] = []
|
||||||
|
for i, ln in enumerate(lines):
|
||||||
|
s = ln.strip()
|
||||||
|
if not s or s.startswith("#"):
|
||||||
|
continue
|
||||||
|
out.append((ln, i + 1))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_node(lines: List[Tuple[str, int]], i: int, indent: int):
|
||||||
|
"""递归解析 YAML 节点(map / list / scalar)。返回 (value, next_i)。"""
|
||||||
|
text, _ = lines[i]
|
||||||
|
# ---- list 节点 ----
|
||||||
|
if text.lstrip(" ").startswith("- "):
|
||||||
|
items: List[object] = []
|
||||||
|
while i < len(lines):
|
||||||
|
t, no = lines[i]
|
||||||
|
stripped = t.lstrip(" ")
|
||||||
|
if not stripped.startswith("- "):
|
||||||
|
break
|
||||||
|
lead_j = len(t) - len(t.lstrip(" "))
|
||||||
|
if lead_j != indent:
|
||||||
|
break
|
||||||
|
item_text = stripped[2:].strip()
|
||||||
|
if not item_text:
|
||||||
|
raise LayoutError(f"cockpit.yaml 第 {no} 行:list 项为空")
|
||||||
|
if ":" in item_text:
|
||||||
|
map_indent = len(t) - len(t.lstrip(" ")) + 2
|
||||||
|
lines[i] = (" " * map_indent + item_text, no)
|
||||||
|
v, i = _parse_node(lines, i, map_indent)
|
||||||
|
items.append(v)
|
||||||
|
else:
|
||||||
|
items.append(_parse_flow_value(item_text))
|
||||||
|
i += 1
|
||||||
|
return items, i
|
||||||
|
# ---- map 节点 ----
|
||||||
|
result: Dict[str, object] = {}
|
||||||
|
while i < len(lines):
|
||||||
|
t, no = lines[i]
|
||||||
|
lead_j = len(t) - len(t.lstrip(" "))
|
||||||
|
if lead_j < indent or t.lstrip(" ").startswith("- "):
|
||||||
|
break
|
||||||
|
if lead_j > indent:
|
||||||
|
raise LayoutError(
|
||||||
|
f"cockpit.yaml 第 {no} 行缩进异常(期望 {indent},实际 {lead_j})")
|
||||||
|
if ":" not in t:
|
||||||
|
raise LayoutError(f"cockpit.yaml 第 {no} 行不是合法键值对:{t!r}")
|
||||||
|
key, _, rest = t.partition(":")
|
||||||
|
key = key.strip()
|
||||||
|
rest = rest.strip()
|
||||||
|
if rest:
|
||||||
|
result[key] = _parse_flow_value(rest)
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
if i + 1 >= len(lines):
|
||||||
|
raise LayoutError(f"cockpit.yaml 第 {no} 行 {key!r} 缺少值")
|
||||||
|
sub_indent = len(lines[i + 1][0]) - len(lines[i + 1][0].lstrip(" "))
|
||||||
|
if sub_indent <= indent:
|
||||||
|
raise LayoutError(f"cockpit.yaml 第 {no} 行 {key!r} 缺少值(无嵌套)")
|
||||||
|
v, i = _parse_node(lines, i + 1, sub_indent)
|
||||||
|
result[key] = v
|
||||||
|
return result, i
|
||||||
|
|
||||||
|
|
||||||
|
def _load_yaml_text(text: str) -> Dict[str, object]:
|
||||||
|
"""解析 YAML 文本为 dict(顶层必须是 map)。"""
|
||||||
|
lines = _strip_comments(text.splitlines())
|
||||||
|
if not lines:
|
||||||
|
return {}
|
||||||
|
top_indent = len(lines[0][0]) - len(lines[0][0].lstrip(" "))
|
||||||
|
value, next_i = _parse_node(lines, 0, top_indent)
|
||||||
|
if not isinstance(value, dict):
|
||||||
|
raise LayoutError("cockpit.yaml 顶层必须是 map")
|
||||||
|
if next_i < len(lines):
|
||||||
|
raise LayoutError(
|
||||||
|
f"cockpit.yaml 第 {lines[next_i][1]} 行:顶层存在多个节点")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 点位字典加载(CSV → point_id 集合)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def load_point_ids(csv_path: str) -> List[str]:
|
||||||
|
"""从点位字典 CSV 加载全部 point_id(保序,对齐 CSV point_id 列)。
|
||||||
|
|
||||||
|
CSV 表头对齐 core/edge-gateway point_dict schema(第二列 point_id)。
|
||||||
|
"""
|
||||||
|
if not os.path.isfile(csv_path):
|
||||||
|
raise LayoutError(f"点位字典 CSV 不存在:{csv_path}")
|
||||||
|
with open(csv_path, "r", encoding="utf-8") as fh:
|
||||||
|
rows = list(csv.reader(fh))
|
||||||
|
if not rows:
|
||||||
|
raise LayoutError(f"点位字典 CSV 为空:{csv_path}")
|
||||||
|
header = [c.strip() for c in rows[0]]
|
||||||
|
if "point_id" not in header:
|
||||||
|
raise LayoutError(
|
||||||
|
f"点位字典 CSV 表头缺 point_id 列:{header}")
|
||||||
|
col = header.index("point_id")
|
||||||
|
ids: List[str] = []
|
||||||
|
for i, row in enumerate(rows[1:], 2):
|
||||||
|
if len(row) <= col:
|
||||||
|
continue
|
||||||
|
pid = row[col].strip()
|
||||||
|
if pid:
|
||||||
|
ids.append(pid)
|
||||||
|
if not ids:
|
||||||
|
raise LayoutError(f"点位字典 CSV 无 point_id 数据行:{csv_path}")
|
||||||
|
return ids
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 校验器
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class LayoutValidator:
|
||||||
|
"""海绵钛驾驶舱布局校验器。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
layout_yaml_path: 布局资产路径(cockpit.ti.yaml)。
|
||||||
|
point_dict_csv_path: 点位字典 CSV 路径(point_dict.default.csv)。
|
||||||
|
grid_columns: 网格列数(默认 12,对齐 cockpit layout v1)。
|
||||||
|
required_stages: process_view 必须覆盖的 stage id(默认海绵钛四状态)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
layout_yaml_path: str,
|
||||||
|
point_dict_csv_path: Optional[str] = None,
|
||||||
|
grid_columns: int = GRID_COLUMNS,
|
||||||
|
required_stages: Tuple[str, ...] = REQUIRED_STAGES,
|
||||||
|
) -> None:
|
||||||
|
if grid_columns <= 0:
|
||||||
|
raise LayoutError(f"grid_columns 必须 > 0,实际 {grid_columns}")
|
||||||
|
self.layout_path = layout_yaml_path
|
||||||
|
self.point_dict_path = point_dict_csv_path
|
||||||
|
self.grid_columns = int(grid_columns)
|
||||||
|
self.required_stages = tuple(required_stages)
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def validate(self) -> LayoutReport:
|
||||||
|
"""执行全部校验,返回报告。"""
|
||||||
|
report = LayoutReport()
|
||||||
|
# 1) 解析布局 YAML
|
||||||
|
try:
|
||||||
|
with open(self.layout_path, "r", encoding="utf-8") as fh:
|
||||||
|
data = _load_yaml_text(fh.read())
|
||||||
|
except LayoutError:
|
||||||
|
raise
|
||||||
|
except OSError as exc:
|
||||||
|
raise LayoutError(f"布局 YAML 读取失败:{self.layout_path} ({exc})") from exc
|
||||||
|
|
||||||
|
# schema 头校验
|
||||||
|
schema = str(data.get("$schema", "")).strip()
|
||||||
|
if schema != "iAOP-cockpit-layout-v1":
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR,
|
||||||
|
field="$schema",
|
||||||
|
reason=f"$schema 应为 'iAOP-cockpit-layout-v1',实际 {schema!r}",
|
||||||
|
))
|
||||||
|
|
||||||
|
# 2) 解析 widgets
|
||||||
|
raw_widgets = data.get("widgets") or []
|
||||||
|
if not isinstance(raw_widgets, list):
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, field="widgets",
|
||||||
|
reason=f"widgets 必须是 list,实际 {type(raw_widgets).__name__}"))
|
||||||
|
return report
|
||||||
|
widgets = self._parse_widgets(raw_widgets, report)
|
||||||
|
report.widget_count = len(widgets)
|
||||||
|
|
||||||
|
if not widgets:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, field="widgets",
|
||||||
|
reason="布局无任何 widget"))
|
||||||
|
return report
|
||||||
|
|
||||||
|
# 3) 加载点位字典(bind 校验需要)
|
||||||
|
point_ids: Optional[set] = None
|
||||||
|
if self.point_dict_path:
|
||||||
|
try:
|
||||||
|
point_ids = set(load_point_ids(self.point_dict_path))
|
||||||
|
except LayoutError as exc:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, field="point_dict",
|
||||||
|
reason=str(exc)))
|
||||||
|
|
||||||
|
# 4) 逐 widget 校验
|
||||||
|
for w in widgets:
|
||||||
|
self._check_widget(w, point_ids, report)
|
||||||
|
|
||||||
|
# 5) process_view 四状态覆盖
|
||||||
|
self._check_process_views(widgets, report)
|
||||||
|
|
||||||
|
return report
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def _parse_widgets(self, raw_widgets: List[object],
|
||||||
|
report: LayoutReport) -> List[WidgetSpec]:
|
||||||
|
widgets: List[WidgetSpec] = []
|
||||||
|
for idx, item in enumerate(raw_widgets):
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=f"[{idx}]",
|
||||||
|
field="widgets",
|
||||||
|
reason=f"widgets[{idx}] 必须是 map,实际 {type(item).__name__}"))
|
||||||
|
continue
|
||||||
|
wtype = str(item.get("type", "")).strip()
|
||||||
|
widgets.append(WidgetSpec(
|
||||||
|
index=idx,
|
||||||
|
type=wtype,
|
||||||
|
x=_to_int(item.get("x"), 0),
|
||||||
|
y=_to_int(item.get("y"), 0),
|
||||||
|
w=_to_int(item.get("w"), 1),
|
||||||
|
h=_to_int(item.get("h"), 1),
|
||||||
|
bind=str(item.get("bind", "")).strip(),
|
||||||
|
src=str(item.get("src", "")).strip(),
|
||||||
|
metric=str(item.get("metric", "")).strip(),
|
||||||
|
label=str(item.get("label", "")).strip(),
|
||||||
|
description=str(item.get("description", "")).strip(),
|
||||||
|
stages=_as_list_of_dict(item.get("stages")),
|
||||||
|
))
|
||||||
|
return widgets
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def _check_widget(self, w: WidgetSpec, point_ids: Optional[set],
|
||||||
|
report: LayoutReport) -> None:
|
||||||
|
wid = f"[{w.index}]({w.type})"
|
||||||
|
# 4a) widget 类型合法
|
||||||
|
if w.type not in ALLOWED_WIDGET_TYPES:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="type",
|
||||||
|
reason=f"非法 widget 类型 {w.type!r}(允许 {sorted(ALLOWED_WIDGET_TYPES)})"))
|
||||||
|
|
||||||
|
# 4b) 12 列网格不越界(坐标非负整数、x+w ≤ columns、h>0)
|
||||||
|
if w.x < 0 or w.y < 0:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="grid",
|
||||||
|
reason=f"坐标不能为负:x={w.x} y={w.y}"))
|
||||||
|
if w.w <= 0 or w.h <= 0:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="grid",
|
||||||
|
reason=f"w/h 必须为正整数:w={w.w} h={w.h}"))
|
||||||
|
if w.x + w.w > self.grid_columns:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="grid",
|
||||||
|
reason=f"越出 {self.grid_columns} 列网格:x={w.x}+w={w.w}"
|
||||||
|
f"={w.x + w.w} > {self.grid_columns}"))
|
||||||
|
|
||||||
|
# 4c) bind 的 point_id 必须在点位字典内(trend/kpi_card)
|
||||||
|
if w.bind:
|
||||||
|
if point_ids is not None and w.bind not in point_ids:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="bind",
|
||||||
|
reason=f"bind point_id {w.bind!r} 不在点位字典内"
|
||||||
|
f"(防模板漂移,对齐 point_dict.default.csv)"))
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def _check_process_views(self, widgets: List[WidgetSpec],
|
||||||
|
report: LayoutReport) -> None:
|
||||||
|
"""校验 process_view 的四状态覆盖完整。"""
|
||||||
|
pv = [w for w in widgets if w.type == "process_view"]
|
||||||
|
if not pv:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, field="process_view",
|
||||||
|
reason="布局缺少 process_view(四状态工艺流程主视图必需)"))
|
||||||
|
return
|
||||||
|
|
||||||
|
covered: Dict[str, WidgetSpec] = {} # stage_id → widget
|
||||||
|
for w in pv:
|
||||||
|
wid = f"[{w.index}](process_view)"
|
||||||
|
if not w.stages:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="stages",
|
||||||
|
reason="process_view 缺少 stages 声明(四状态覆盖必需)"))
|
||||||
|
continue
|
||||||
|
|
||||||
|
stage_ids: List[str] = []
|
||||||
|
orders: List[int] = []
|
||||||
|
seen: set = set()
|
||||||
|
for st in w.stages:
|
||||||
|
sid = str(st.get("id", "")).strip()
|
||||||
|
sname = str(st.get("name", "")).strip()
|
||||||
|
if not sid:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="stages",
|
||||||
|
reason=f"stage 缺少 id(name={sname!r})"))
|
||||||
|
continue
|
||||||
|
if sid in seen:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.WARN, widget_id=wid, field="stages",
|
||||||
|
reason=f"stage id 重复:{sid!r}"))
|
||||||
|
continue
|
||||||
|
seen.add(sid)
|
||||||
|
stage_ids.append(sid)
|
||||||
|
covered.setdefault(sid, w)
|
||||||
|
order = st.get("order")
|
||||||
|
if order is not None:
|
||||||
|
try:
|
||||||
|
orders.append(int(order))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="stages",
|
||||||
|
reason=f"stage {sid!r} order 非整数:{order!r}"))
|
||||||
|
|
||||||
|
# order 单调递增校验
|
||||||
|
if orders and len(orders) == len(stage_ids):
|
||||||
|
if orders != sorted(orders):
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, widget_id=wid, field="stages",
|
||||||
|
reason=f"stage order 非单调递增:{orders}"))
|
||||||
|
|
||||||
|
# 必需四状态全覆盖
|
||||||
|
missing = [s for s in self.required_stages if s not in covered]
|
||||||
|
if missing:
|
||||||
|
report.issues.append(LayoutIssue(
|
||||||
|
severity=Severity.ERROR, field="stages",
|
||||||
|
reason=f"process_view stages 未覆盖必需四状态:{missing}"
|
||||||
|
f"(氯化/精制/还原/蒸馏)"))
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 辅助
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _to_int(value: object, default: int) -> int:
|
||||||
|
"""把 YAML 解析出的值(可能是 str/int)转为 int;失败返回 default。"""
|
||||||
|
if value is None or value == "":
|
||||||
|
return default
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise LayoutError(f"坐标值不是整数:{value!r}")
|
||||||
|
|
||||||
|
|
||||||
|
def _as_list_of_dict(value: object) -> List[Dict[str, object]]:
|
||||||
|
if not isinstance(value, list):
|
||||||
|
return []
|
||||||
|
out: List[Dict[str, object]] = []
|
||||||
|
for item in value:
|
||||||
|
if isinstance(item, dict):
|
||||||
|
out.append(item)
|
||||||
|
return out
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""测试引导:把 dashboard 测试根目录加入 sys.path,使 layout_validator 可导入。
|
||||||
|
|
||||||
|
dashboard 目录名是合法 Python 标识符,直接作为包导入;本引导把父目录
|
||||||
|
(templates/ti-cl4/dashboard)挂到 sys.path,使 ``from layout_validator import ...``
|
||||||
|
在 unittest 发现机制下可解析(与 core 模块测试引导同款)。
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
PKG_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
if PKG_DIR not in sys.path:
|
||||||
|
sys.path.insert(0, PKG_DIR)
|
||||||
@@ -0,0 +1,307 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""海绵钛驾驶舱布局校验器测试(Issue #55)。
|
||||||
|
|
||||||
|
覆盖:
|
||||||
|
1. 真实 cockpit.ti.yaml + point_dict.default.csv 全部通过(端到端);
|
||||||
|
2. widget 类型合法集合(非法类型 → ERROR);
|
||||||
|
3. 12 列网格校验(越界/负坐标/非正 w/h);
|
||||||
|
4. bind point_id 在点位字典内(漂移 → ERROR);
|
||||||
|
5. process_view 四状态覆盖(缺 stage id / order 非单调 / 缺必需状态);
|
||||||
|
6. $schema 头校验;
|
||||||
|
7. YAML 解析(flow map / 嵌套);
|
||||||
|
8. 点位字典 CSV 加载(缺表头/空文件);
|
||||||
|
9. 空布局 / 边界。
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import _bootstrap # noqa: F401 (sys.path 挂载)
|
||||||
|
|
||||||
|
from layout_validator import (
|
||||||
|
GRID_COLUMNS,
|
||||||
|
LayoutError,
|
||||||
|
LayoutValidator,
|
||||||
|
REQUIRED_STAGES,
|
||||||
|
Severity,
|
||||||
|
load_point_ids,
|
||||||
|
)
|
||||||
|
|
||||||
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
DASHBOARD_DIR = os.path.dirname(HERE)
|
||||||
|
LAYOUT_YAML = os.path.join(DASHBOARD_DIR, "cockpit.ti.yaml")
|
||||||
|
POINT_DICT_CSV = os.path.join(
|
||||||
|
DASHBOARD_DIR, os.pardir, "point-dict", "point_dict.default.csv")
|
||||||
|
|
||||||
|
|
||||||
|
def _write_layout(tmp_path: str, content: str) -> str:
|
||||||
|
"""把布局内容写到临时文件,返回路径。"""
|
||||||
|
path = os.path.join(tmp_path, "cockpit.test.yaml")
|
||||||
|
with open(path, "w", encoding="utf-8") as fh:
|
||||||
|
fh.write(content)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def _write_point_dict(tmp_path: str, ids: list) -> str:
|
||||||
|
"""写一个最小点位字典 CSV(仅 point_id 列)。"""
|
||||||
|
path = os.path.join(tmp_path, "points.csv")
|
||||||
|
with open(path, "w", encoding="utf-8") as fh:
|
||||||
|
fh.write("device_id,point_id,name,unit,dataType,sampleRate,qualityCode,opcNode,protocol\n")
|
||||||
|
for i, pid in enumerate(ids):
|
||||||
|
fh.write(f"D{i},{pid},n,u,float,1000,true,n,simulator\n")
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
# 最小合法布局模板(便于构造各类变形)
|
||||||
|
_VALID_LAYOUT = """\
|
||||||
|
$schema: iAOP-cockpit-layout-v1
|
||||||
|
title: 测试驾驶舱
|
||||||
|
theme: dark
|
||||||
|
widgets:
|
||||||
|
- type: process_view
|
||||||
|
src: ti_four_state.svg
|
||||||
|
x: 0
|
||||||
|
y: 0
|
||||||
|
w: 12
|
||||||
|
h: 4
|
||||||
|
description: 四状态工艺流程
|
||||||
|
stages:
|
||||||
|
- id: chlorination
|
||||||
|
name: 氯化
|
||||||
|
order: 1
|
||||||
|
- id: purification
|
||||||
|
name: 精制
|
||||||
|
order: 2
|
||||||
|
- id: reduction
|
||||||
|
name: 还原
|
||||||
|
order: 3
|
||||||
|
- id: distillation
|
||||||
|
name: 蒸馏
|
||||||
|
order: 4
|
||||||
|
- type: trend
|
||||||
|
bind: CLF-01.TEMP
|
||||||
|
x: 0
|
||||||
|
y: 4
|
||||||
|
w: 6
|
||||||
|
h: 2
|
||||||
|
description: 氯化炉温度
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class TestEndToEndRealAssets(unittest.TestCase):
|
||||||
|
"""真实 cockpit.ti.yaml + point_dict.default.csv 端到端校验。"""
|
||||||
|
|
||||||
|
def test_real_layout_passes(self):
|
||||||
|
report = LayoutValidator(LAYOUT_YAML, POINT_DICT_CSV).validate()
|
||||||
|
if not report.passed:
|
||||||
|
for issue in report.errors:
|
||||||
|
print("ERROR:", issue.widget_id, issue.field, issue.reason)
|
||||||
|
self.assertTrue(report.passed, "真实布局应通过全部校验")
|
||||||
|
self.assertGreater(report.widget_count, 0)
|
||||||
|
|
||||||
|
def test_real_layout_has_process_view_with_four_stages(self):
|
||||||
|
report = LayoutValidator(LAYOUT_YAML, POINT_DICT_CSV).validate()
|
||||||
|
# 无 stages 相关 ERROR
|
||||||
|
stage_errors = [i for i in report.errors if i.field == "stages"]
|
||||||
|
self.assertEqual(stage_errors, [])
|
||||||
|
|
||||||
|
|
||||||
|
class TestWidgetType(unittest.TestCase):
|
||||||
|
"""widget 类型合法性。"""
|
||||||
|
|
||||||
|
def test_invalid_widget_type_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
layout = _VALID_LAYOUT.replace("type: trend", "type: radar_chart")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
errors = [i for i in report.errors if i.field == "type"]
|
||||||
|
self.assertEqual(len(errors), 1)
|
||||||
|
self.assertIn("非法 widget 类型", errors[0].reason)
|
||||||
|
|
||||||
|
|
||||||
|
class TestGridBounds(unittest.TestCase):
|
||||||
|
"""12 列网格校验。"""
|
||||||
|
|
||||||
|
def test_x_plus_w_exceeds_columns(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
# trend x=10 w=6 → 16 > 12
|
||||||
|
layout = _VALID_LAYOUT.replace(
|
||||||
|
" bind: CLF-01.TEMP\n x: 0\n y: 4\n w: 6\n h: 2",
|
||||||
|
" bind: CLF-01.TEMP\n x: 10\n y: 4\n w: 6\n h: 2")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
grid_errors = [i for i in report.errors if i.field == "grid"
|
||||||
|
and "越出" in i.reason]
|
||||||
|
self.assertEqual(len(grid_errors), 1)
|
||||||
|
|
||||||
|
def test_negative_x_rejected(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
layout = _VALID_LAYOUT.replace(" x: 0\n y: 4\n w: 6\n h: 2",
|
||||||
|
" x: -1\n y: 4\n w: 6\n h: 2")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
neg = [i for i in report.errors if "坐标不能为负" in i.reason]
|
||||||
|
self.assertEqual(len(neg), 1)
|
||||||
|
|
||||||
|
def test_zero_width_rejected(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
layout = _VALID_LAYOUT.replace(" x: 0\n y: 4\n w: 6\n h: 2",
|
||||||
|
" x: 0\n y: 4\n w: 0\n h: 2")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
wh = [i for i in report.errors if "w/h 必须为正整数" in i.reason]
|
||||||
|
self.assertEqual(len(wh), 1)
|
||||||
|
|
||||||
|
|
||||||
|
class TestBindPointId(unittest.TestCase):
|
||||||
|
"""bind point_id 在点位字典内。"""
|
||||||
|
|
||||||
|
def test_bind_not_in_dict_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
path = _write_layout(td, _VALID_LAYOUT)
|
||||||
|
# 点位字典不含 CLF-01.TEMP
|
||||||
|
csv_path = _write_point_dict(td, ["OTHER-01.X"])
|
||||||
|
report = LayoutValidator(path, csv_path).validate()
|
||||||
|
bind_err = [i for i in report.errors if i.field == "bind"]
|
||||||
|
self.assertEqual(len(bind_err), 1)
|
||||||
|
self.assertIn("不在点位字典内", bind_err[0].reason)
|
||||||
|
|
||||||
|
def test_bind_in_dict_passes(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
path = _write_layout(td, _VALID_LAYOUT)
|
||||||
|
csv_path = _write_point_dict(td, ["CLF-01.TEMP"])
|
||||||
|
report = LayoutValidator(path, csv_path).validate()
|
||||||
|
bind_err = [i for i in report.errors if i.field == "bind"]
|
||||||
|
self.assertEqual(bind_err, [])
|
||||||
|
|
||||||
|
|
||||||
|
class TestProcessViewStages(unittest.TestCase):
|
||||||
|
"""process_view 四状态覆盖。"""
|
||||||
|
|
||||||
|
def test_missing_process_view_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
# 删除 process_view 块(保留 trend)
|
||||||
|
layout = """\
|
||||||
|
$schema: iAOP-cockpit-layout-v1
|
||||||
|
title: t
|
||||||
|
widgets:
|
||||||
|
- type: trend
|
||||||
|
bind: CLF-01.TEMP
|
||||||
|
x: 0
|
||||||
|
y: 0
|
||||||
|
w: 6
|
||||||
|
h: 2
|
||||||
|
"""
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
pv_err = [i for i in report.errors if i.field == "process_view"]
|
||||||
|
self.assertEqual(len(pv_err), 1)
|
||||||
|
|
||||||
|
def test_missing_required_stage_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
# 删除 distillation stage
|
||||||
|
layout = _VALID_LAYOUT.replace(
|
||||||
|
" - id: distillation\n name: 蒸馏\n order: 4\n", "")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
missing = [i for i in report.errors if "未覆盖必需四状态" in i.reason]
|
||||||
|
self.assertEqual(len(missing), 1)
|
||||||
|
self.assertIn("distillation", missing[0].reason)
|
||||||
|
|
||||||
|
def test_non_monotonic_order_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
# 把 reduction order 改为 5(> distillation 的 4)→ 非单调
|
||||||
|
layout = _VALID_LAYOUT.replace(" - id: reduction\n name: 还原\n order: 3",
|
||||||
|
" - id: reduction\n name: 还原\n order: 5")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
order_err = [i for i in report.errors if "order 非单调递增" in i.reason]
|
||||||
|
self.assertEqual(len(order_err), 1)
|
||||||
|
|
||||||
|
def test_duplicate_stage_id_warn(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
# 重复 chlorination(覆盖必需状态校验仍过,但 WARN 重复)
|
||||||
|
layout = _VALID_LAYOUT + """\
|
||||||
|
"""
|
||||||
|
# 构造一个有重复 stage 的 process_view(替换 stages 块)
|
||||||
|
dup_layout = _VALID_LAYOUT.replace(
|
||||||
|
" - id: distillation\n name: 蒸馏\n order: 4",
|
||||||
|
" - id: distillation\n name: 蒸馏\n order: 4\n"
|
||||||
|
" - id: chlorination\n name: 氯化2\n order: 5")
|
||||||
|
path = _write_layout(td, dup_layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
dup = [i for i in report.issues if "stage id 重复" in i.reason]
|
||||||
|
self.assertEqual(len(dup), 1)
|
||||||
|
|
||||||
|
|
||||||
|
class TestSchemaHeader(unittest.TestCase):
|
||||||
|
"""$schema 头校验。"""
|
||||||
|
|
||||||
|
def test_wrong_schema_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
layout = _VALID_LAYOUT.replace("iAOP-cockpit-layout-v1", "some-other-schema")
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
schema_err = [i for i in report.errors if i.field == "$schema"]
|
||||||
|
self.assertEqual(len(schema_err), 1)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPointDictLoader(unittest.TestCase):
|
||||||
|
"""点位字典 CSV 加载。"""
|
||||||
|
|
||||||
|
def test_load_point_ids(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
csv_path = _write_point_dict(td, ["A.X", "B.Y"])
|
||||||
|
ids = load_point_ids(csv_path)
|
||||||
|
self.assertEqual(ids, ["A.X", "B.Y"])
|
||||||
|
|
||||||
|
def test_missing_csv_raises(self):
|
||||||
|
with self.assertRaises(LayoutError):
|
||||||
|
load_point_ids("/nonexistent/points.csv")
|
||||||
|
|
||||||
|
def test_csv_missing_point_id_column_raises(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
path = os.path.join(td, "bad.csv")
|
||||||
|
with open(path, "w", encoding="utf-8") as fh:
|
||||||
|
fh.write("device_id,name\nD1,n\n")
|
||||||
|
with self.assertRaises(LayoutError):
|
||||||
|
load_point_ids(path)
|
||||||
|
|
||||||
|
|
||||||
|
class TestReportExport(unittest.TestCase):
|
||||||
|
"""报告序列化 + 边界。"""
|
||||||
|
|
||||||
|
def test_empty_layout_error(self):
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
layout = """\
|
||||||
|
$schema: iAOP-cockpit-layout-v1
|
||||||
|
title: t
|
||||||
|
widgets: []
|
||||||
|
"""
|
||||||
|
path = _write_layout(td, layout)
|
||||||
|
report = LayoutValidator(path, _write_point_dict(td, ["CLF-01.TEMP"])).validate()
|
||||||
|
self.assertFalse(report.passed)
|
||||||
|
|
||||||
|
def test_report_to_dict(self):
|
||||||
|
report = LayoutValidator(LAYOUT_YAML, POINT_DICT_CSV).validate()
|
||||||
|
d = report.to_dict()
|
||||||
|
self.assertEqual(d["passed"], True)
|
||||||
|
self.assertIn("widget_count", d)
|
||||||
|
self.assertEqual(d["error_count"], 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user