118 lines
5.1 KiB
Go
118 lines
5.1 KiB
Go
package preflight
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
|
)
|
|
|
|
type auditaFunc func(context.Context, audita.PolishRequest) (audita.PreflightResult, error)
|
|
|
|
func (f auditaFunc) Preflight(ctx context.Context, r audita.PolishRequest) (audita.PreflightResult, error) {
|
|
return f(ctx, r)
|
|
}
|
|
|
|
func TestNotariusReadinessRequiresCompleteMatchingEvidence(t *testing.T) {
|
|
request := notarius.ReadinessRequest{RunRequest: notarius.RunRequest{PipelineID: "expected"}, RequiredLanes: []string{"required"}}
|
|
for _, tc := range []struct {
|
|
name string
|
|
result notarius.ReadinessResult
|
|
failure error
|
|
success bool
|
|
}{
|
|
{"ready with warning", notarius.ReadinessResult{Pipeline: "expected", Lanes: []string{"required"}, InspectionComplete: true, AssessmentComplete: true, Ready: true, Checks: []notarius.ReadinessCheck{{ID: "cache", Result: "warning", Message: "cache inaccessible"}}}, nil, true},
|
|
{"missing lane", notarius.ReadinessResult{Pipeline: "expected", InspectionComplete: true, AssessmentComplete: true, Ready: true}, nil, false},
|
|
{"incomplete", notarius.ReadinessResult{Pipeline: "expected", InspectionComplete: false, AssessmentComplete: true, Ready: true}, nil, false},
|
|
{"not ready", notarius.ReadinessResult{Pipeline: "expected", Lanes: []string{"required"}, InspectionComplete: true, AssessmentComplete: true}, nil, false},
|
|
{"failed delivery", notarius.ReadinessResult{Pipeline: "expected", Lanes: []string{"required"}, InspectionComplete: true, AssessmentComplete: true, Ready: true}, errors.New("delivery failed"), false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fake := ¬arius.FakeRunner{PreflightResult: &tc.result, PreflightErr: tc.failure}
|
|
result, err := CheckNotarius(context.Background(), fake, request)
|
|
if (err == nil) != tc.success {
|
|
t.Fatalf("result = %#v, error = %v", result, err)
|
|
}
|
|
if tc.name == "ready with warning" && (len(result.Findings) != 1 || result.Findings[0].Severity != SeverityWarning) {
|
|
t.Fatalf("warning = %#v", result.Findings)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDownstreamChecksPropagateFailureAndCancellation(t *testing.T) {
|
|
failure := errors.New("provider refused synthetic input")
|
|
checker := auditaFunc(func(context.Context, audita.PolishRequest) (audita.PreflightResult, error) {
|
|
return audita.PreflightResult{ExitCode: 2}, failure
|
|
})
|
|
result, err := CheckAudita(context.Background(), checker, audita.PolishRequest{})
|
|
if !errors.Is(err, failure) || len(result.Findings) != 1 || result.Findings[0].Severity != SeverityError || !strings.Contains(result.Findings[0].Message, "provider refused") {
|
|
t.Fatalf("failure: %+v %v", result, err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
called := false
|
|
checker = auditaFunc(func(context.Context, audita.PolishRequest) (audita.PreflightResult, error) {
|
|
called = true
|
|
return audita.PreflightResult{}, nil
|
|
})
|
|
_, err = CheckAudita(ctx, checker, audita.PolishRequest{})
|
|
if !errors.Is(err, context.Canceled) || called {
|
|
t.Fatalf("cancellation: called=%v err=%v", called, err)
|
|
}
|
|
}
|
|
|
|
func TestNotariusPreservesMultipleFailuresAndAdapterCause(t *testing.T) {
|
|
cause := errors.New("provider unavailable")
|
|
adapterErr := &os.PathError{Op: "probe", Path: "synthetic", Err: cause}
|
|
fake := ¬arius.FakeRunner{PreflightErr: adapterErr, PreflightResult: ¬arius.ReadinessResult{
|
|
Checks: []notarius.ReadinessCheck{
|
|
{ID: "provider", Result: "failed", Message: "provider check failed"},
|
|
{ID: "output", Result: "failed", Message: "output check failed"},
|
|
{ID: "cache", Result: "warning", Message: "cache unavailable"},
|
|
},
|
|
}}
|
|
result, err := CheckNotarius(context.Background(), fake, notarius.ReadinessRequest{})
|
|
var typed *os.PathError
|
|
if !errors.Is(err, cause) || !errors.As(err, &typed) || typed != adapterErr {
|
|
t.Fatalf("adapter cause lost: %v", err)
|
|
}
|
|
for _, message := range []string{"provider check failed", "output check failed"} {
|
|
if !strings.Contains(err.Error(), message) {
|
|
t.Fatalf("missing failure %q: %v", message, err)
|
|
}
|
|
}
|
|
for _, check := range fake.PreflightResult.Checks {
|
|
found := false
|
|
for _, finding := range result.Findings {
|
|
if finding.Name == check.ID && finding.Message == check.Message {
|
|
want := SeverityError
|
|
if check.Result == "warning" {
|
|
want = SeverityWarning
|
|
}
|
|
found = finding.Severity == want
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("missing or reclassified check %q: %+v", check.ID, result.Findings)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAuditaPreservesFailureAndCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
failure := errors.New("provider failure")
|
|
checker := auditaFunc(func(context.Context, audita.PolishRequest) (audita.PreflightResult, error) {
|
|
cancel()
|
|
return audita.PreflightResult{}, failure
|
|
})
|
|
result, err := CheckAudita(ctx, checker, audita.PolishRequest{})
|
|
if !errors.Is(err, failure) || !errors.Is(err, context.Canceled) || len(result.Findings) != 1 || result.Findings[0].Severity != SeverityError {
|
|
t.Fatalf("result = %+v, error = %v", result, err)
|
|
}
|
|
}
|