ananke/internal/state/testhooks.go

99 lines
3.9 KiB
Go
Raw Normal View History

package state
import (
"os"
"sync"
)
var testHookOverrideMu sync.Mutex
// TestHookQuarantineCorruptFile runs one orchestration or CLI step.
// Signature: TestHookQuarantineCorruptFile(path string, payload []byte, replacement []byte, mode os.FileMode) error.
// Why: exposes corrupt-file healing internals to the top-level testing module without package-local tests.
func TestHookQuarantineCorruptFile(path string, payload []byte, replacement []byte, mode os.FileMode) error {
return quarantineCorruptFile(path, payload, replacement, mode)
}
// TestHookWriteIntentDefault runs one orchestration or CLI step.
// Signature: TestHookWriteIntentDefault(path string, in Intent) error.
// Why: lets top-level tests delegate to production WriteIntent behavior while
// selectively forcing deterministic failures for specific branches.
func TestHookWriteIntentDefault(path string, in Intent) error {
return writeIntentDefault(path, in)
}
// TestHookReadIntentDefault runs one orchestration or CLI step.
// Signature: TestHookReadIntentDefault(path string) (Intent, error).
// Why: lets top-level tests delegate to production ReadIntent behavior while
// selectively forcing deterministic read sequences for lifecycle branches.
func TestHookReadIntentDefault(path string) (Intent, error) {
return readIntentDefault(path)
}
// TestHookSetReadIntentOverride runs one orchestration or CLI step.
// Signature: TestHookSetReadIntentOverride(fn func(path string) (Intent, error)) (restore func()).
// Why: enables deterministic intent-read failure injection without sleeping
// goroutines that race slower CI agents.
func TestHookSetReadIntentOverride(fn func(path string) (Intent, error)) (restore func()) {
testHookOverrideMu.Lock()
prev := readIntentImpl
if fn == nil {
readIntentImpl = readIntentDefault
} else {
readIntentImpl = fn
}
testHookOverrideMu.Unlock()
return func() {
testHookOverrideMu.Lock()
readIntentImpl = prev
testHookOverrideMu.Unlock()
}
}
// TestHookSetWriteIntentOverride runs one orchestration or CLI step.
// Signature: TestHookSetWriteIntentOverride(fn func(path string, in Intent) error) (restore func()).
// Why: enables deterministic intent-write failure injection from the top-level
// testing module when tests run as root.
func TestHookSetWriteIntentOverride(fn func(path string, in Intent) error) (restore func()) {
testHookOverrideMu.Lock()
prev := writeIntentImpl
if fn == nil {
writeIntentImpl = writeIntentDefault
} else {
writeIntentImpl = fn
}
testHookOverrideMu.Unlock()
return func() {
testHookOverrideMu.Lock()
writeIntentImpl = prev
testHookOverrideMu.Unlock()
}
}
// TestHookQuarantineCorruptFileDefault runs one orchestration or CLI step.
// Signature: TestHookQuarantineCorruptFileDefault(path string, payload []byte, replacement []byte, mode os.FileMode) error.
// Why: lets top-level tests fall through to production quarantine behavior when
// they only want to force a subset of calls.
func TestHookQuarantineCorruptFileDefault(path string, payload []byte, replacement []byte, mode os.FileMode) error {
return quarantineCorruptFileDefault(path, payload, replacement, mode)
}
// TestHookSetQuarantineCorruptFileOverride runs one orchestration or CLI step.
// Signature: TestHookSetQuarantineCorruptFileOverride(fn func(path string, payload []byte, replacement []byte, mode os.FileMode) error) (restore func()).
// Why: enables deterministic auto-heal failure tests in privileged/root execution.
func TestHookSetQuarantineCorruptFileOverride(fn func(path string, payload []byte, replacement []byte, mode os.FileMode) error) (restore func()) {
testHookOverrideMu.Lock()
prev := quarantineCorruptFileImpl
if fn == nil {
quarantineCorruptFileImpl = quarantineCorruptFileDefault
} else {
quarantineCorruptFileImpl = fn
}
testHookOverrideMu.Unlock()
return func() {
testHookOverrideMu.Lock()
quarantineCorruptFileImpl = prev
testHookOverrideMu.Unlock()
}
}