158 lines
4.4 KiB
Go
158 lines
4.4 KiB
Go
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))
|
|
}
|
|
}
|