test(maintenance): add policy executor and handler tests
Табличные тесты PolicyExecutor, ConfigProvider reload, memory CRUD политик и 503 для /v1/maintenance/* на memory-бэкенде без PostgreSQL. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package store
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMemoryMaintenancePolicyCRUD(t *testing.T) {
|
||||
m := NewMemory()
|
||||
ret := int(86400)
|
||||
max := 5000
|
||||
created, err := m.CreateMaintenancePolicy(&MaintenancePolicy{
|
||||
Name: "audit cleanup",
|
||||
TableName: "job_audit",
|
||||
Condition: "status = 'succeeded'",
|
||||
RetentionPeriodSec: &ret,
|
||||
MaxRows: &max,
|
||||
VacuumStrategy: VacuumStrategyVacuumAnalyze,
|
||||
Schedule: "0 4 * * *",
|
||||
Enabled: true,
|
||||
DryRunEnabled: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if created.ID == "" {
|
||||
t.Fatal("missing id")
|
||||
}
|
||||
|
||||
items, _, hasMore, err := m.ListMaintenancePolicies("", 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(items) != 1 || hasMore {
|
||||
t.Fatalf("list: len=%d hasMore=%v", len(items), hasMore)
|
||||
}
|
||||
|
||||
got, err := m.GetMaintenancePolicy(created.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Name != "audit cleanup" || got.VacuumStrategy != VacuumStrategyVacuumAnalyze {
|
||||
t.Fatalf("get: %+v", got)
|
||||
}
|
||||
|
||||
newName := "renamed"
|
||||
disabled := false
|
||||
updated, err := m.UpdateMaintenancePolicy(created.ID, &MaintenancePolicyPatch{
|
||||
Name: &newName,
|
||||
Enabled: &disabled,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if updated.Name != newName || updated.Enabled {
|
||||
t.Fatalf("update: %+v", updated)
|
||||
}
|
||||
|
||||
if err := m.TouchMaintenancePolicyRun(created.ID, "succeeded", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
afterTouch, err := m.GetMaintenancePolicy(created.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if afterTouch.LastStatus != "succeeded" || afterTouch.LastRunAt == nil {
|
||||
t.Fatalf("touch: %+v", afterTouch)
|
||||
}
|
||||
|
||||
if err := m.AppendMaintenancePolicyConfigAudit("op:test", created.ID, "update", map[string]any{"name": "old"}, map[string]any{"name": newName}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
audit, next, hasMore, err := m.ListMaintenancePolicyConfigAudit("", 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(audit) != 1 || audit[0].Action != "update" || next != "" || hasMore {
|
||||
t.Fatalf("audit: %+v next=%q hasMore=%v", audit, next, hasMore)
|
||||
}
|
||||
|
||||
if err := m.DeleteMaintenancePolicy(created.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := m.GetMaintenancePolicy(created.ID); err != ErrNotFound {
|
||||
t.Fatalf("after delete: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateMaintenancePolicyInvalid(t *testing.T) {
|
||||
m := NewMemory()
|
||||
_, err := m.CreateMaintenancePolicy(&MaintenancePolicy{Name: "", TableName: "job_audit", Schedule: "0 3 * * *"})
|
||||
if err != ErrInvalidInput {
|
||||
t.Fatalf("want ErrInvalidInput got %v", err)
|
||||
}
|
||||
_, err = m.CreateMaintenancePolicy(&MaintenancePolicy{Name: "x", TableName: "job_audit", Schedule: "0 3 * * *", VacuumStrategy: "bad"})
|
||||
if err != ErrInvalidInput {
|
||||
t.Fatalf("want ErrInvalidInput got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user