127 lines
5.4 KiB
Go
127 lines
5.4 KiB
Go
|
|
package notarius
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func inspectionDocument(t *testing.T, lanes ...string) string {
|
||
|
|
t.Helper()
|
||
|
|
module := map[string]any{"module": "synthetic", "execution_class": "deterministic"}
|
||
|
|
entries := make([]map[string]any, 0, len(lanes))
|
||
|
|
for _, lane := range lanes {
|
||
|
|
entries = append(entries, map[string]any{"id": lane, "artifact_kind": "synthetic", "extract": module, "merge": module, "normalize": module})
|
||
|
|
}
|
||
|
|
data, err := json.Marshal(map[string]any{"pipeline": "dnd-session", "input": module, "chunk": module, "output": module, "steps": []any{map[string]any{"id": "step", "lanes": entries}}})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return string(data)
|
||
|
|
}
|
||
|
|
|
||
|
|
func readinessRequest(t *testing.T) ReadinessRequest {
|
||
|
|
t.Helper()
|
||
|
|
req := validRunRequest(t)
|
||
|
|
req.DebugDirectory = filepath.Join(filepath.Dir(req.LogPath), "debug")
|
||
|
|
return ReadinessRequest{RunRequest: req, RequiredLanes: []string{"alpha", "beta"}, InputDescription: "synthetic transcript", InspectionPath: filepath.Join(filepath.Dir(req.LogPath), "inspection.json"), InspectionLogPath: filepath.Join(filepath.Dir(req.LogPath), "inspection.log")}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotariusPreflightSharesExecutionControlsAndPreservesWarnings(t *testing.T) {
|
||
|
|
req := readinessRequest(t)
|
||
|
|
credential := loadTestCredential(t, "NARRATIO_PREFLIGHT_NOTARIUS_KEY", "synthetic-value")
|
||
|
|
req.Credentials = []configkit.Secret{*credential}
|
||
|
|
mode, overlay := "bypass", ""
|
||
|
|
req.Cache, req.ConfigOverlay, req.Debug = &mode, &overlay, true
|
||
|
|
req.References = []ReferenceBinding{{Selector: "party", Path: req.InputPath}}
|
||
|
|
var calls []subprocess.RunRequest
|
||
|
|
runner := &SubprocessRunner{run: func(_ context.Context, process subprocess.RunRequest) (subprocess.RunResult, error) {
|
||
|
|
calls = append(calls, process)
|
||
|
|
body := inspectionDocument(t, "beta", "alpha")
|
||
|
|
if process.Args[0] == "preflight" {
|
||
|
|
body = `{"pipeline":"dnd-session","ready":true,"checks":[{"id":"cache","scope":{"path":"cache"},"result":"warning","message":"cache unavailable"}]}`
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(process.StdoutLogPath, []byte(body), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return subprocess.RunResult{}, nil
|
||
|
|
}}
|
||
|
|
result, err := runner.Preflight(context.Background(), req)
|
||
|
|
if err != nil || !result.Ready || !result.InspectionComplete || !result.AssessmentComplete || len(result.Checks) != 1 || result.Checks[0].Result != "warning" {
|
||
|
|
t.Fatalf("result = %#v, %v", result, err)
|
||
|
|
}
|
||
|
|
if len(calls) != 2 || !reflect.DeepEqual(calls[1].Args, runArgs("preflight", req.RunRequest, req.References)) {
|
||
|
|
t.Fatalf("calls = %#v", calls)
|
||
|
|
}
|
||
|
|
for _, call := range calls {
|
||
|
|
if call.EnvOverrides["NARRATIO_PREFLIGHT_NOTARIUS_KEY"] != "synthetic-value" || !reflect.DeepEqual(call.SensitiveEnvNames, []string{"NARRATIO_PREFLIGHT_NOTARIUS_KEY"}) {
|
||
|
|
t.Fatal("credential forwarding differs from execution")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !strings.Contains(strings.Join(calls[0].Args, "|"), "--overlay||--format|json") {
|
||
|
|
t.Fatalf("inspection arguments = %#v", calls[0].Args)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotariusPreflightRejectsIncompleteOrFailedReports(t *testing.T) {
|
||
|
|
for _, test := range []struct {
|
||
|
|
name, inspection, assessment string
|
||
|
|
status int
|
||
|
|
failure error
|
||
|
|
wantCalls int
|
||
|
|
}{
|
||
|
|
{"missing lane", inspectionDocument(t, "alpha"), "", 0, nil, 1},
|
||
|
|
{"incomplete inspection", `{"pipeline":"dnd-session","steps":[{"id":"s"}]}`, "", 0, nil, 1},
|
||
|
|
{"partial assessment", inspectionDocument(t, "alpha", "beta"), `{"pipeline":"dnd-session","ready":true}`, 0, nil, 2},
|
||
|
|
{"wrong pipeline", inspectionDocument(t, "alpha", "beta"), `{"pipeline":"other","ready":true,"checks":[]}`, 0, nil, 2},
|
||
|
|
{"not ready", inspectionDocument(t, "alpha", "beta"), `{"pipeline":"dnd-session","ready":false,"checks":[{"id":"provider","scope":{},"result":"failed","message":"offline"}]}`, 0, nil, 2},
|
||
|
|
{"nonzero ready", inspectionDocument(t, "alpha", "beta"), `{"pipeline":"dnd-session","ready":true,"checks":[]}`, 7, errors.New("failed delivery"), 2},
|
||
|
|
} {
|
||
|
|
t.Run(test.name, func(t *testing.T) {
|
||
|
|
req := readinessRequest(t)
|
||
|
|
calls := 0
|
||
|
|
runner := &SubprocessRunner{run: func(_ context.Context, process subprocess.RunRequest) (subprocess.RunResult, error) {
|
||
|
|
calls++
|
||
|
|
body := test.inspection
|
||
|
|
if process.Args[0] == "preflight" {
|
||
|
|
body = test.assessment
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(process.StdoutLogPath, []byte(body), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if process.Args[0] == "preflight" {
|
||
|
|
return subprocess.RunResult{ExitCode: test.status}, test.failure
|
||
|
|
}
|
||
|
|
return subprocess.RunResult{}, nil
|
||
|
|
}}
|
||
|
|
result, err := runner.Preflight(context.Background(), req)
|
||
|
|
if err == nil || calls != test.wantCalls {
|
||
|
|
t.Fatalf("result = %#v, error = %v, calls = %d", result, err, calls)
|
||
|
|
}
|
||
|
|
if test.name == "nonzero ready" && (!result.AssessmentComplete || !result.Ready) {
|
||
|
|
t.Fatalf("failed delivery lost available assessment: %#v", result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotariusPreflightCancellationPreservesCause(t *testing.T) {
|
||
|
|
req := readinessRequest(t)
|
||
|
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
|
cancel()
|
||
|
|
runner := &SubprocessRunner{run: func(ctx context.Context, _ subprocess.RunRequest) (subprocess.RunResult, error) {
|
||
|
|
return subprocess.RunResult{}, ctx.Err()
|
||
|
|
}}
|
||
|
|
if _, err := runner.Preflight(ctx, req); !errors.Is(err, context.Canceled) {
|
||
|
|
t.Fatalf("cancellation = %v", err)
|
||
|
|
}
|
||
|
|
}
|