You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.6 KiB
Go
145 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
)
|
|
|
|
type fakeWorkerStateCleaner struct {
|
|
mu sync.Mutex
|
|
calls []string
|
|
failOnce bool
|
|
}
|
|
|
|
func (c *fakeWorkerStateCleaner) Cleanup(operationID string) error {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.calls = append(c.calls, operationID)
|
|
if c.failOnce {
|
|
c.failOnce = false
|
|
return errors.New("injected cleanup failure")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func terminalOperation(t *testing.T, journal *operation.Journal, operationID string) operation.CompletionReceipt {
|
|
t.Helper()
|
|
spec := operation.Spec{
|
|
OperationID: operationID,
|
|
ResourceID: "vm-completion-cleanup",
|
|
Action: "delete-vm",
|
|
Fingerprint: operation.Fingerprint("DELETE", "/v1/vms/vm-completion-cleanup", []byte(`{}`)),
|
|
Destructive: true,
|
|
}
|
|
if _, _, err := journal.Execute(context.Background(), spec, func(context.Context) (json.RawMessage, error) {
|
|
return json.RawMessage(`{"deleted":true}`), nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claimed, err := journal.ClaimPendingCompletions(1)
|
|
if err != nil || len(claimed) != 1 {
|
|
t.Fatalf("claim completion = %#v err=%v", claimed, err)
|
|
}
|
|
return claimed[0]
|
|
}
|
|
|
|
func TestCompletionAckCleansWorkerStateAndKeepsJournalTombstone(t *testing.T) {
|
|
journal, err := operation.Open(filepath.Join(t.TempDir(), "operations.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer journal.Close()
|
|
receipt := terminalOperation(t, journal, "operation-ack-cleanup")
|
|
cleaner := &fakeWorkerStateCleaner{failOnce: true}
|
|
outbox := &workerCompletionOutbox{journal: journal, cleaner: cleaner}
|
|
|
|
if err := outbox.AcknowledgeCompletion(receipt.EventID); err == nil {
|
|
t.Fatal("cleanup failure consumed completion ACK")
|
|
}
|
|
if pending, err := journal.PendingCompletionOperationIDs(); err != nil {
|
|
t.Fatal(err)
|
|
} else if _, exists := pending[receipt.OperationID]; !exists {
|
|
t.Fatal("cleanup failure removed pending completion")
|
|
}
|
|
if err := outbox.AcknowledgeCompletion(receipt.EventID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pending, err := journal.PendingCompletionOperationIDs(); err != nil || len(pending) != 0 {
|
|
t.Fatalf("acked completion remains pending: %#v err=%v", pending, err)
|
|
}
|
|
record, err := journal.Get(receipt.OperationID)
|
|
if err != nil || record.State != operation.StateSucceeded {
|
|
t.Fatalf("ACK changed permanent operation tombstone: %#v err=%v", record, err)
|
|
}
|
|
cleaner.mu.Lock()
|
|
calls := append([]string(nil), cleaner.calls...)
|
|
cleaner.mu.Unlock()
|
|
if len(calls) != 2 || calls[0] != receipt.OperationID || calls[1] != receipt.OperationID {
|
|
t.Fatalf("cleanup calls = %#v", calls)
|
|
}
|
|
}
|
|
|
|
func TestExpiredCompletionCleansWorkerStateWithoutChangingTerminalResult(t *testing.T) {
|
|
journal, err := operation.Open(filepath.Join(t.TempDir(), "operations.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer journal.Close()
|
|
now := time.Date(2026, 10, 19, 0, 0, 0, 0, time.UTC)
|
|
journal.SetClockForTesting(func() time.Time { return now })
|
|
receipt := terminalOperation(t, journal, "operation-expired-cleanup")
|
|
now = now.Add(25 * time.Hour)
|
|
cleaner := &fakeWorkerStateCleaner{}
|
|
outbox := &workerCompletionOutbox{journal: journal, cleaner: cleaner}
|
|
var logs bytes.Buffer
|
|
previousLogWriter := log.Writer()
|
|
log.SetOutput(&logs)
|
|
defer log.SetOutput(previousLogWriter)
|
|
claimed, err := outbox.ClaimPendingCompletions(1, nil)
|
|
if err != nil || len(claimed) != 0 {
|
|
t.Fatalf("expired claim = %#v err=%v", claimed, err)
|
|
}
|
|
record, err := journal.Get(receipt.OperationID)
|
|
if err != nil || record.State != operation.StateSucceeded || string(record.Result) != `{"deleted":true}` {
|
|
t.Fatalf("expiry changed terminal operation: %#v err=%v", record, err)
|
|
}
|
|
cleaner.mu.Lock()
|
|
calls := append([]string(nil), cleaner.calls...)
|
|
cleaner.mu.Unlock()
|
|
if len(calls) != 1 || calls[0] != receipt.OperationID {
|
|
t.Fatalf("expired cleanup calls = %#v", calls)
|
|
}
|
|
if !strings.Contains(logs.String(), "completion outbox exhausted operation_id="+receipt.OperationID) {
|
|
t.Fatalf("missing bounded completion expiry log: %q", logs.String())
|
|
}
|
|
}
|
|
|
|
func TestRunControlUntilStoppedCancelsOnBackgroundFinalizationFailure(t *testing.T) {
|
|
failures := make(chan error, 1)
|
|
failures <- errors.New("injected journal write failure")
|
|
stopped := make(chan struct{})
|
|
err := runControlUntilStopped(context.Background(), failures, func(ctx context.Context) error {
|
|
<-ctx.Done()
|
|
close(stopped)
|
|
return ctx.Err()
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), "durable background operation finalization failed") {
|
|
t.Fatalf("run result = %v", err)
|
|
}
|
|
select {
|
|
case <-stopped:
|
|
default:
|
|
t.Fatal("control channel was not stopped before restart")
|
|
}
|
|
}
|