65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package faults
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Only an operator-mounted directory enables faults; clients cannot set them.
|
|
// Rename makes each injected crash one-shot across automatic container restarts.
|
|
func Take(point, id string) bool {
|
|
directory := os.Getenv("PIPELINE_FAULT_DIR")
|
|
if directory == "" {
|
|
return false
|
|
}
|
|
if _, err := uuid.Parse(id); err != nil {
|
|
return false
|
|
}
|
|
name := filepath.Join(directory, point+"."+id)
|
|
return os.Rename(name, name+".hit") == nil
|
|
}
|
|
func Crash(point, id string) {
|
|
if Take(point, id) {
|
|
log.Printf("injected pipeline crash: %s, attachment %s", point, id)
|
|
os.Exit(86)
|
|
}
|
|
}
|
|
|
|
// Observe records a test-only branch without exposing an API or a lease token.
|
|
func Observe(point, id string) {
|
|
directory := os.Getenv("PIPELINE_FAULT_DIR")
|
|
if directory == "" {
|
|
return
|
|
}
|
|
if _, err := uuid.Parse(id); err != nil {
|
|
return
|
|
}
|
|
_ = os.WriteFile(filepath.Join(directory, point+"."+id+".observed"), []byte("observed\n"), 0644)
|
|
}
|
|
|
|
// Pause is operator-only and ends on release or context expiry.
|
|
func Pause(ctx context.Context, point, id string) {
|
|
if !Take(point, id) {
|
|
return
|
|
}
|
|
release := filepath.Join(os.Getenv("PIPELINE_FAULT_DIR"), point+"."+id+".release")
|
|
ticker := time.NewTicker(50 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
if _, err := os.Stat(release); err == nil {
|
|
_ = os.WriteFile(release+".observed", []byte("released\n"), 0644)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|