保存后台取消结果修复和报告迁移实库回归基线
This commit is contained in:
1 parent
099351cd9e
commit
cf3bc7aaa5
7 files changed
+159
-14
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
func TestCertificateCancellationPreservesDeploymentOutcome(t *testing.T) {
|
||||
for _, scenario := range []struct {
|
||||
name string
|
||||
report deployment.Report
|
||||
expected string
|
||||
}{
|
||||
{"partial", deployment.Report{Status: "partial", Total: 2, Success: 1, Failed: 1}, "partial_success"},
|
||||
{"recovery_unconfirmed", deployment.Report{Status: "failed", RecoveryStatus: "unconfirmed", Total: 1, Failed: 1}, "failed"},
|
||||
{"recovery_failed", deployment.Report{Status: "failed", RecoveryStatus: "failed", Total: 1, Failed: 1}, "failed"},
|
||||
{"restored", deployment.Report{Status: "failed", RecoveryStatus: "success", Total: 1, Failed: 1}, "cancelled"},
|
||||
} {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
w, a := newCertificateTaskFixture(t)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
scenario.report.Results = []deployment.Result{{ServerID: "cancel-target", Status: "failed", Message: "secret-output"}}
|
||||
if scenario.report.Success > 0 {
|
||||
scenario.report.Results = append(scenario.report.Results, deployment.Result{ServerID: "completed-target", Status: "success"})
|
||||
}
|
||||
calls := 0
|
||||
_, err := w.run(ctx, "task-domain", "renewal_run", "certificate_issue", 3, time.Nanosecond,
|
||||
func(context.Context, string) (CertificateWorkflowResult, error) {
|
||||
calls++
|
||||
cancel()
|
||||
return CertificateWorkflowResult{Deployment: scenario.report}, context.Canceled
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) || calls != 1 {
|
||||
t.Fatalf("取消后仍重试或丢失原始取消原因:%d %v", calls, err)
|
||||
}
|
||||
task := assertCertificateTask(t, a, scenario.expected, scenario.expected)
|
||||
results, err := a.Tasks.ListTaskDeploymentResults(t.Context(), task.ID)
|
||||
if err != nil || len(results) != len(scenario.report.Results) {
|
||||
t.Fatalf("取消后必须保留实际部署结果:%+v %v", results, err)
|
||||
}
|
||||
for _, result := range results {
|
||||
if strings.Contains(result.Message, "secret-output") {
|
||||
t.Fatal("取消后的部署结果泄露原始远端输出")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -152,10 +152,11 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
})
|
||||
result, resultErr = execute(issueCtx, id)
|
||||
cancel()
|
||||
if recordErr := w.tasks.RecordTaskDeploymentResults(taskCtx, task.ID, attempt.ID, number, taskDeploymentResults(result.Deployment)); recordErr != nil {
|
||||
// 取消只停止外部操作,结果保存仍使用独立且有限时的收尾上下文。
|
||||
finishCtx, finishCancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
if recordErr := w.tasks.RecordTaskDeploymentResults(finishCtx, task.ID, attempt.ID, number, taskDeploymentResults(result.Deployment)); recordErr != nil {
|
||||
resultErr = errors.Join(resultErr, recordErr)
|
||||
}
|
||||
finishCtx, finishCancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
finished := time.Now().UTC()
|
||||
status, message := certificateTaskOutcome(result, resultErr)
|
||||
recordErr := w.tasks.FinishAttempt(finishCtx, attempt.ID, status, message, &finished)
|
||||
@@ -196,16 +197,17 @@ func taskDeploymentResults(report deployment.Report) []repository.TaskDeployment
|
||||
}
|
||||
|
||||
func certificateTaskOutcome(result CertificateWorkflowResult, err error) (string, string) {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return "cancelled", "\u8bc1\u4e66\u5de5\u4f5c\u6d41\u5df2\u53d6\u6d88"
|
||||
}
|
||||
if result.Deployment.Status == "partial" {
|
||||
return "partial_success", "\u8bc1\u4e66\u5de5\u4f5c\u6d41\u5df2\u5b8c\u6210\u90e8\u5206\u90e8\u7f72"
|
||||
return "partial_success", "证书工作流已完成部分部署"
|
||||
}
|
||||
// 未确认或失败的恢复不能被取消意图掩盖,部分成功也必须保留真实状态。
|
||||
if errors.Is(err, context.Canceled) && result.Deployment.RecoveryStatus != "unconfirmed" && result.Deployment.RecoveryStatus != "failed" {
|
||||
return "cancelled", "证书工作流已取消"
|
||||
}
|
||||
if err != nil {
|
||||
return "failed", "\u8bc1\u4e66\u5de5\u4f5c\u6d41\u672a\u5b8c\u6210\uff0c\u8bf7\u68c0\u67e5\u53d7\u4fdd\u62a4\u7684\u670d\u52a1\u65e5\u5fd7"
|
||||
return "failed", "证书工作流未完成,请检查受保护的服务日志"
|
||||
}
|
||||
return "success", "\u8bc1\u4e66\u5de5\u4f5c\u6d41\u6267\u884c\u5b8c\u6210"
|
||||
return "success", "证书工作流执行完成"
|
||||
}
|
||||
|
||||
var _ certificate.CertificateIssuer = (*certificateTaskWorkflow)(nil)
|
||||
|
||||
@@ -62,7 +62,7 @@ var (
|
||||
mysqlIntegerWidthPattern = regexp.MustCompile(`^int\([0-9]+\)$`)
|
||||
)
|
||||
|
||||
// 只解析这三份内嵌迁移使用的语法;出现未知定义时拒绝迁移,避免漏检结构。
|
||||
// 只解析部署与报告内嵌迁移使用的语法;出现未知定义时拒绝迁移,避免漏检结构。
|
||||
func parseMySQLDeploymentShape(statement string) (mysqlDeploymentShape, error) {
|
||||
var shape mysqlDeploymentShape
|
||||
lines := strings.Split(strings.TrimSpace(statement), "\n")
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMySQLRealTaskReportMigrationRecovery(t *testing.T) {
|
||||
f := startRealMySQL(t)
|
||||
baseline := f.database(t)
|
||||
if err := Migrate(t.Context(), baseline.DB, "mysql"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedSchema := mysqlSchemaSnapshot(t, baseline.DB)
|
||||
content, err := json.Marshal(map[string]string{"message": strings.Repeat("部署报告", 20000)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, boundary := range []string{"before_create", "after_create", "with_report"} {
|
||||
t.Run(boundary, func(t *testing.T) {
|
||||
db := f.database(t)
|
||||
mysqlPrepareVersion(t, db.DB, 19)
|
||||
if boundary != "before_create" {
|
||||
mysqlDeploymentExec(t, db.DB, mysqlDeploymentStatements(t, 20)[0])
|
||||
}
|
||||
if boundary == "with_report" {
|
||||
if _, err := db.DB.ExecContext(t.Context(), `INSERT INTO task_deployment_reports
|
||||
(task_id, attempt_id, attempt_number, report_json) VALUES ('report-task', 'report-attempt', 1, ?)`, string(content)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
mysqlAssertUpgrade(t, db.DB, expectedSchema)
|
||||
if boundary == "with_report" {
|
||||
var stored string
|
||||
if err := db.DB.QueryRowContext(t.Context(), `SELECT report_json FROM task_deployment_reports
|
||||
WHERE task_id = 'report-task' AND attempt_id = 'report-attempt'`).Scan(&stored); err != nil || stored != string(content) {
|
||||
t.Fatalf("恢复迁移必须保留超过普通文本字段容量的中文报告:%v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, invalid := range []struct {
|
||||
name string
|
||||
statement string
|
||||
}{
|
||||
{"short_report", "ALTER TABLE task_deployment_reports MODIFY COLUMN report_json TEXT NOT NULL"},
|
||||
{"wrong_primary_order", "ALTER TABLE task_deployment_reports DROP PRIMARY KEY, ADD PRIMARY KEY (attempt_id, task_id)"},
|
||||
{"nullable_attempt", "ALTER TABLE task_deployment_reports MODIFY COLUMN attempt_number INT NULL"},
|
||||
{"wrong_engine", "ALTER TABLE task_deployment_reports ENGINE=MyISAM"},
|
||||
{"extra_column", "ALTER TABLE task_deployment_reports ADD COLUMN unexpected INT NOT NULL DEFAULT 0"},
|
||||
} {
|
||||
t.Run(invalid.name, func(t *testing.T) {
|
||||
db := f.database(t)
|
||||
mysqlPrepareVersion(t, db.DB, 19)
|
||||
mysqlDeploymentExec(t, db.DB, mysqlDeploymentStatements(t, 20)[0])
|
||||
mysqlDeploymentExec(t, db.DB, `INSERT INTO task_deployment_reports
|
||||
(task_id, attempt_id, attempt_number, report_json) VALUES ('kept-task', 'kept-attempt', 1, '{"status":"failed"}')`)
|
||||
mysqlDeploymentExec(t, db.DB, invalid.statement)
|
||||
before := fmt.Sprint(mysqlSchemaSnapshot(t, db.DB))
|
||||
assertMySQLDeploymentMigrationRejected(t, db.DB, 19)
|
||||
if after := fmt.Sprint(mysqlSchemaSnapshot(t, db.DB)); after != before {
|
||||
t.Fatal("报告表冲突不得被自动覆盖或修复")
|
||||
}
|
||||
var stored string
|
||||
if err := db.DB.QueryRowContext(t.Context(), `SELECT report_json FROM task_deployment_reports
|
||||
WHERE task_id = 'kept-task' AND attempt_id = 'kept-attempt'`).Scan(&stored); err != nil || stored != `{"status":"failed"}` {
|
||||
t.Fatalf("拒绝迁移时不得修改已有报告:%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user