Probe output destinations with temporary files
This commit is contained in:
@@ -89,6 +89,8 @@ retain their existing publication and cleanup behavior when constructed from it.
|
||||
|
||||
## Stage 2: Add temporary write-readiness probes
|
||||
|
||||
**Completed.**
|
||||
|
||||
Work:
|
||||
|
||||
1. Add a context-aware probe operation in the output adapter that accepts checked
|
||||
|
||||
71
internal/core/diagnostics/output_probe.go
Normal file
71
internal/core/diagnostics/output_probe.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package diagnostics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type probeFile interface {
|
||||
Name() string
|
||||
Close() error
|
||||
}
|
||||
|
||||
type probeOperations struct {
|
||||
create func(string, string) (probeFile, error)
|
||||
remove func(string) error
|
||||
}
|
||||
|
||||
// ProbeOutput checks whether temporary files can be created beside active regular outputs.
|
||||
func ProbeOutput(ctx context.Context, checked *CheckedDestinations) error {
|
||||
return probeOutput(ctx, checked, probeOperations{
|
||||
create: func(dir, pattern string) (probeFile, error) { return os.CreateTemp(dir, pattern) },
|
||||
remove: os.Remove,
|
||||
})
|
||||
}
|
||||
|
||||
func probeOutput(ctx context.Context, checked *CheckedDestinations, ops probeOperations) error {
|
||||
type target struct{ display, path string }
|
||||
var targets []target
|
||||
if checked.transcript != "" && !checked.direct {
|
||||
targets = append(targets, target{checked.transcript, checked.publishPath})
|
||||
}
|
||||
if checked.enabled {
|
||||
for i, path := range checked.reportDestinations {
|
||||
targets = append(targets, target{checked.reports[i], path})
|
||||
}
|
||||
}
|
||||
for _, target := range targets {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return fmt.Errorf("probe output %q: %w", target.display, err)
|
||||
}
|
||||
pattern := ".audita-probe-*"
|
||||
if strings.HasPrefix(filepath.Base(target.path), ".audita-probe-") {
|
||||
pattern = ".audita-check-*"
|
||||
}
|
||||
file, err := ops.create(filepath.Dir(target.path), pattern)
|
||||
if err != nil {
|
||||
return errors.Join(fmt.Errorf("probe output %q: create temporary file: %w", target.display, err), ctx.Err())
|
||||
}
|
||||
name := file.Name()
|
||||
closeErr := file.Close()
|
||||
removeErr := ops.remove(name)
|
||||
var failures []error
|
||||
if closeErr != nil {
|
||||
failures = append(failures, fmt.Errorf("probe output %q: close temporary file %q: %w", target.display, name, closeErr))
|
||||
}
|
||||
if removeErr != nil {
|
||||
failures = append(failures, fmt.Errorf("probe output %q: remove temporary file %q: %w", target.display, name, removeErr))
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
failures = append(failures, fmt.Errorf("probe output %q: %w", target.display, err))
|
||||
}
|
||||
if err := errors.Join(failures...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
157
internal/core/diagnostics/output_probe_test.go
Normal file
157
internal/core/diagnostics/output_probe_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package diagnostics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProbeOutputPreservesDestinations(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
output := filepath.Join(dir, "output.json")
|
||||
for _, name := range []string{output, filepath.Join(dir, ErrorsFile)} {
|
||||
if err := os.WriteFile(name, []byte("existing"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
checked, err := CheckOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ProbeOutput(context.Background(), checked); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil || len(entries) != 2 {
|
||||
t.Fatalf("unexpected files: %v, %v", entries, err)
|
||||
}
|
||||
for _, name := range []string{output, filepath.Join(dir, ErrorsFile)} {
|
||||
data, err := os.ReadFile(name)
|
||||
if err != nil || string(data) != "existing" {
|
||||
t.Fatalf("destination changed: %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type testProbeFile struct {
|
||||
name string
|
||||
closeErr error
|
||||
closed bool
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (f *testProbeFile) Name() string { return f.name }
|
||||
func (f *testProbeFile) Close() error {
|
||||
f.closed = true
|
||||
if f.cancel != nil {
|
||||
f.cancel()
|
||||
}
|
||||
return f.closeErr
|
||||
}
|
||||
|
||||
func TestProbeFailuresAndCancellation(t *testing.T) {
|
||||
failure := errors.New("probe failure")
|
||||
for _, mode := range []string{"create", "close", "remove", "cancel"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
checked, err := CheckOutput(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "out")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
file := &testProbeFile{name: "temporary"}
|
||||
removed := false
|
||||
ops := probeOperations{
|
||||
create: func(_, _ string) (probeFile, error) {
|
||||
if mode == "create" {
|
||||
return nil, failure
|
||||
}
|
||||
return file, nil
|
||||
},
|
||||
remove: func(string) error {
|
||||
removed = true
|
||||
if mode == "remove" {
|
||||
return failure
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
if mode == "close" {
|
||||
file.closeErr = failure
|
||||
}
|
||||
if mode == "cancel" {
|
||||
file.cancel = cancel
|
||||
}
|
||||
err = probeOutput(ctx, checked, ops)
|
||||
want := failure
|
||||
if mode == "cancel" {
|
||||
want = context.Canceled
|
||||
}
|
||||
if !errors.Is(err, want) {
|
||||
t.Fatalf("error %v does not contain %v", err, want)
|
||||
}
|
||||
if mode != "create" && (!file.closed || !removed) {
|
||||
t.Fatal("probe was not closed and removed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeSkipsInactiveAndStreamTargets(t *testing.T) {
|
||||
checked, err := CheckOutput(OutputSelection{TranscriptPath: os.DevNull, ReportDir: filepath.Join(t.TempDir(), "missing")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls := 0
|
||||
err = probeOutput(context.Background(), checked, probeOperations{create: func(_, _ string) (probeFile, error) { calls++; return nil, errors.New("unexpected") }, remove: os.Remove})
|
||||
if err != nil || calls != 0 {
|
||||
t.Fatalf("stream or inactive reports probed: %d, %v", calls, err)
|
||||
}
|
||||
if _, err := CheckOutput(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "missing", "out")}); err == nil {
|
||||
t.Fatal("missing parent accepted")
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "input")
|
||||
if _, err := CheckOutput(OutputSelection{TranscriptPath: path, ProtectedPaths: []string{path}}); err == nil {
|
||||
t.Fatal("collision accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeVisitsActiveTargetsAndStopsOnCancellation(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
output := filepath.Join(dir, "out")
|
||||
checked, err := CheckOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
var visited int
|
||||
ops := probeOperations{
|
||||
create: func(_, _ string) (probeFile, error) {
|
||||
visited++
|
||||
return &testProbeFile{name: "temporary", cancel: cancel}, nil
|
||||
},
|
||||
remove: func(string) error { return nil },
|
||||
}
|
||||
if err := probeOutput(ctx, checked, ops); !errors.Is(err, context.Canceled) || visited != 1 {
|
||||
t.Fatalf("cancellation: %v, visits %d", err, visited)
|
||||
}
|
||||
|
||||
var paths []string
|
||||
ops.create = func(dir, pattern string) (probeFile, error) {
|
||||
file, err := os.CreateTemp(dir, pattern)
|
||||
if err == nil {
|
||||
paths = append(paths, file.Name())
|
||||
}
|
||||
return file, err
|
||||
}
|
||||
ops.remove = os.Remove
|
||||
if err := probeOutput(context.Background(), checked, ops); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(paths) != 4 {
|
||||
t.Fatalf("expected four probes, got %d", len(paths))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user