fix: clarify reconciliation and preserve container image tags
This commit is contained in:
@@ -171,3 +171,88 @@ func TestRunPrintsUsageWithoutCommand(t *testing.T) {
|
||||
t.Fatal("missing usage output")
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseDiagnosisArgsAcceptsService 验证 status/doctor/reconcile 子命令能接受 backend 服务,
|
||||
// 且 reconcile 能接受 --apply。
|
||||
func TestParseDiagnosisArgsAcceptsService(t *testing.T) {
|
||||
for _, command := range []string{"status", "doctor", "reconcile"} {
|
||||
request, err := parseDiagnosisArgs(command, []string{"--service", "backend"}, &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatalf("parse %s arguments: %v", command, err)
|
||||
}
|
||||
if request.service != serviceBackend {
|
||||
t.Fatalf("unexpected %s service: %+v", command, request)
|
||||
}
|
||||
}
|
||||
request, err := parseDiagnosisArgs("reconcile", []string{"--service", "backend", "--apply"}, &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatalf("parse reconcile apply arguments: %v", err)
|
||||
}
|
||||
if !request.apply {
|
||||
t.Fatal("reconcile --apply was not retained")
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseDiagnosisArgsRejectsUnsupportedInput 验证诊断子命令拒绝缺失服务、
|
||||
// 非 backend 服务、位置参数以及非 reconcile 命令使用 --apply。
|
||||
func TestParseDiagnosisArgsRejectsUnsupportedInput(t *testing.T) {
|
||||
for name, test := range map[string]struct {
|
||||
command string
|
||||
arguments []string
|
||||
}{
|
||||
"missing service": {"status", nil},
|
||||
"other service": {"doctor", []string{"--service", "frontend"}},
|
||||
"positional": {"status", []string{"--service", "backend", "extra"}},
|
||||
"apply on status": {"status", []string{"--service", "backend", "--apply"}},
|
||||
"apply on doctor": {"doctor", []string{"--service", "backend", "--apply"}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if _, err := parseDiagnosisArgs(test.command, test.arguments, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("expected diagnosis argument rejection")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteDiagnosisFiltersByCommand 验证 writeDiagnosis 按子命令过滤诊断项:
|
||||
// status 只展示漂移与可修复项,doctor 展示全部,reconcile 只展示可修复项。
|
||||
func TestWriteDiagnosisFiltersByCommand(t *testing.T) {
|
||||
diagnosis := daemonapi.Diagnosis{
|
||||
Service: "backend",
|
||||
Type: "container",
|
||||
Healthy: true,
|
||||
Items: []daemonapi.DiagnosisItem{
|
||||
{Level: daemonapi.DiagnosisLevelOK, Code: "active", Message: "活动容器健康"},
|
||||
{Level: daemonapi.DiagnosisLevelFixable, Code: "nginx", Message: "Nginx 指向错误", Action: "切流到 8080"},
|
||||
},
|
||||
}
|
||||
|
||||
var statusOut, doctorOut, reconcileOut bytes.Buffer
|
||||
writeDiagnosis(&statusOut, "status", diagnosis)
|
||||
writeDiagnosis(&doctorOut, "doctor", diagnosis)
|
||||
writeDiagnosis(&reconcileOut, "reconcile", diagnosis)
|
||||
|
||||
if !strings.Contains(statusOut.String(), "Nginx 指向错误") || strings.Contains(statusOut.String(), "活动容器健康") {
|
||||
t.Fatalf("status should hide ok items: %q", statusOut.String())
|
||||
}
|
||||
if !strings.Contains(doctorOut.String(), "活动容器健康") || !strings.Contains(doctorOut.String(), "Nginx 指向错误") {
|
||||
t.Fatalf("doctor should show all items: %q", doctorOut.String())
|
||||
}
|
||||
if !strings.Contains(reconcileOut.String(), "Nginx 指向错误") || strings.Contains(reconcileOut.String(), "活动容器健康") {
|
||||
t.Fatalf("reconcile should only show fixable items: %q", reconcileOut.String())
|
||||
}
|
||||
if !strings.Contains(statusOut.String(), "status=REPAIRABLE") {
|
||||
t.Fatalf("status should distinguish fixable drift: %q", statusOut.String())
|
||||
}
|
||||
if !strings.Contains(reconcileOut.String(), "未执行") || !strings.Contains(reconcileOut.String(), "--apply") {
|
||||
t.Fatalf("reconcile plan should explain that no fix was applied: %q", reconcileOut.String())
|
||||
}
|
||||
|
||||
var appliedOut bytes.Buffer
|
||||
diagnosis.RepairApplied = true
|
||||
diagnosis.RepairTransactionID = "TX-1"
|
||||
writeDiagnosis(&appliedOut, "reconcile", diagnosis)
|
||||
if !strings.Contains(appliedOut.String(), "APPLIED") || !strings.Contains(appliedOut.String(), "TX-1") {
|
||||
t.Fatalf("reconcile apply should show transaction: %q", appliedOut.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user