77 lines
2.6 KiB
Go
77 lines
2.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestResetRunningOnStartup(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
e1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
|
e2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
|
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: e1, DstEndpointID: e2})
|
|
accID, _ := s.CreateAccount(ctx, Account{TaskID: taskID, SrcLogin: "u", SrcPassEnc: "x", DstLogin: "v", DstPassEnc: "y"})
|
|
// simulate a crash mid-run
|
|
_ = s.SetTaskStatus(ctx, taskID, "running")
|
|
_ = s.SetAccountStatus(ctx, accID, "running")
|
|
runID, _ := s.CreateRun(ctx, taskID, "manual") // phantom run: never finished
|
|
|
|
tn, an, err := s.ResetRunningOnStartup(ctx)
|
|
if err != nil {
|
|
t.Fatalf("reset: %v", err)
|
|
}
|
|
if tn != 1 || an != 1 {
|
|
t.Fatalf("reset counts tasks=%d accounts=%d, want 1/1", tn, an)
|
|
}
|
|
task, _ := s.GetTask(ctx, taskID)
|
|
if task.Status == "running" {
|
|
t.Fatal("task still running after reset")
|
|
}
|
|
accs, _ := s.ListAccountsByTask(ctx, taskID)
|
|
if accs[0].Status == "running" {
|
|
t.Fatal("account still running after reset")
|
|
}
|
|
runs, _ := s.ListRunsByTask(ctx, taskID)
|
|
var found bool
|
|
for _, r := range runs {
|
|
if r.ID == runID {
|
|
found = true
|
|
if r.Status == "running" || r.FinishedAt == nil {
|
|
t.Fatalf("phantom run %d still running: status=%s finished_at=%v", runID, r.Status, r.FinishedAt)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("run %d not found", runID)
|
|
}
|
|
}
|
|
|
|
func TestClearStuckAccountAndReconcile(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
e1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
|
e2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
|
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: e1, DstEndpointID: e2})
|
|
accID, _ := s.CreateAccount(ctx, Account{TaskID: taskID, SrcLogin: "u", SrcPassEnc: "x", DstLogin: "v", DstPassEnc: "y"})
|
|
_ = s.SetTaskStatus(ctx, taskID, "running")
|
|
_ = s.SetAccountStatus(ctx, accID, "running")
|
|
|
|
cleared, err := s.ClearStuckAccount(ctx, accID)
|
|
if err != nil || !cleared {
|
|
t.Fatalf("clear stuck: cleared=%v err=%v", cleared, err)
|
|
}
|
|
// second call finds nothing to clear
|
|
again, _ := s.ClearStuckAccount(ctx, accID)
|
|
if again {
|
|
t.Fatal("second clear should be false")
|
|
}
|
|
if err := s.ReconcileTaskStatus(ctx, taskID); err != nil {
|
|
t.Fatalf("reconcile: %v", err)
|
|
}
|
|
task, _ := s.GetTask(ctx, taskID)
|
|
if task.Status == "running" {
|
|
t.Fatal("task should no longer be running after reconcile")
|
|
}
|
|
}
|