171 lines
7.9 KiB
Go
171 lines
7.9 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/deliveryindex"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/objectsync"
|
|
"droboticflow/internal/sourceidentity"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type originalIdentityPublisher struct {
|
|
deliveryPublisher
|
|
heads map[string]objectsync.ObjectMetadata
|
|
calls int
|
|
}
|
|
|
|
func (p *originalIdentityPublisher) Head(ctx context.Context, c domain.StorageConnectionRecord, b, k string) (objectsync.ObjectMetadata, error) {
|
|
if m, ok := p.heads[k]; ok {
|
|
p.calls++
|
|
return m, nil
|
|
}
|
|
return p.deliveryPublisher.Head(ctx, c, b, k)
|
|
}
|
|
|
|
func originalWorkerFixture(t *testing.T) (*Runtime, *originalIdentityPublisher, domain.RunRecord, domain.WorkflowNode, domain.ExecutionArtifact) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
db := store.NewMemoryStore()
|
|
pub := &originalIdentityPublisher{deliveryPublisher: deliveryPublisher{memoryArtifactPublisher: memoryArtifactPublisher{objects: map[string][]byte{}}}, heads: map[string]objectsync.ObjectMetadata{}}
|
|
root, part := "incoming/capture-A", "part0001"
|
|
roles := map[string]string{"leftVideo": part + "/capture-A_camera_left_" + part + ".mp4", "rightVideo": part + "/capture-A_camera_right_" + part + ".mp4", "leftPts": part + "/capture-A_camera_left_" + part + "_pts.csv", "rightPts": part + "/capture-A_camera_right_" + part + "_pts.csv", "imu": part + "/capture-A_imu_" + part + ".csv", "cameraCalibration": "capture-A_calibration_camera.yaml", "imuCalibration": "capture-A_calibration_imu.yaml", "metadata": "meta_capture-A.json"}
|
|
keys := []string{}
|
|
objects := []sourceidentity.Object{}
|
|
files := map[string]any{}
|
|
for role, rel := range roles {
|
|
key := root + "/" + rel
|
|
keys = append(keys, key)
|
|
objects = append(objects, sourceidentity.Object{Key: key, ETag: "original", Size: 100})
|
|
pub.heads[key] = objectsync.ObjectMetadata{Exists: true, ETag: "original", SizeBytes: 100}
|
|
files[role] = map[string]string{"path": rel}
|
|
}
|
|
ref := &domain.ArtifactReference{Provider: domain.StorageProviderBOS, Kind: domain.ArtifactRefKindDirectory, ConnectionID: "c", Bucket: "b", Prefix: root, CaptureUnit: "capture-A/" + part, ObjectKeys: keys, VersionID: sourceidentity.Fingerprint(objects)}
|
|
parent := domain.RunRecord{ID: "original", OrganizationID: "org", ProjectID: "p", WorkflowID: "w", TaskID: "capture", Inputs: map[string]domain.ArtifactPayload{"source": {Ref: ref}}}
|
|
child := domain.RunRecord{ID: "release-child", OrganizationID: "org", ProjectID: "p", WorkflowID: "w", TaskID: "capture", Trigger: domain.RunTriggerHumanRelease, ParentRunID: parent.ID}
|
|
for _, run := range []domain.RunRecord{parent, child} {
|
|
if err := db.UpsertRun(ctx, run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: "c", OrganizationID: "org", Provider: domain.StorageProviderBOS}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.UpsertCaptureRecord(ctx, domain.CaptureRecord{ID: "capture", OrganizationID: "org", ProjectID: "p", WorkflowID: "w", RunID: child.ID, CaptureID: ref.CaptureUnit, ConnectionID: "c", Bucket: "b", SourcePrefix: root, Prefix: root + "/" + part, Fingerprint: "changed-current-discovery", SourceKeys: []string{"do-not-use"}, Delivery: &domain.CaptureDeliveryTarget{ConnectionID: "c", Bucket: "b", Prefix: "deliveries/project__p/capture-A/" + part}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
body, _ := json.Marshal(map[string]any{"schemaVersion": deliveryindex.ClassificationPartSchema, "capture": map[string]string{"captureId": "capture-A", "prefix": root}, "partId": part, "originalSource": map[string]any{"provider": "BOS", "bucket": "b", "prefix": root, "partPrefix": root + "/" + part, "files": files}})
|
|
if err := os.WriteFile(filepath.Join(dir, "part.json"), body, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
node := domain.WorkflowNode{ID: "package", Data: domain.WorkflowNodeData{OperatorID: "operator-builtin-package", Hyperparams: []domain.HyperParam{{Key: "DELIVERY_JSON_V4", Value: "true"}}}}
|
|
return &Runtime{store: db, artifactPublisher: pub}, pub, child, node, domain.ExecutionArtifact{Name: "delivery.directory.index", Path: dir, Type: domain.PortTypeFile}
|
|
}
|
|
|
|
func TestV4WorkerOriginalIdentityGuards(t *testing.T) {
|
|
for _, scenario := range []string{"correct", "same-size-etag", "changed-size", "missing", "wrong-tenant", "wrong-parent", "missing-input", "wrong-files"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
r, pub, run, node, artifact := originalWorkerFixture(t)
|
|
key := "incoming/capture-A/part0001/capture-A_camera_left_part0001.mp4"
|
|
m := pub.heads[key]
|
|
switch scenario {
|
|
case "same-size-etag":
|
|
m.ETag = "tampered"
|
|
pub.heads[key] = m
|
|
case "changed-size":
|
|
m.SizeBytes++
|
|
pub.heads[key] = m
|
|
case "missing":
|
|
m.Exists = false
|
|
pub.heads[key] = m
|
|
case "wrong-tenant":
|
|
run.OrganizationID = "foreign"
|
|
case "wrong-parent":
|
|
run.ParentRunID = "nonexistent"
|
|
case "missing-input":
|
|
parent, _ := r.store.GetRun(t.Context(), run.ParentRunID)
|
|
parent.Inputs = nil
|
|
r.store.UpsertRun(t.Context(), *parent)
|
|
case "wrong-files":
|
|
name := filepath.Join(artifact.Path, "part.json")
|
|
raw, _ := os.ReadFile(name)
|
|
var doc map[string]any
|
|
json.Unmarshal(raw, &doc)
|
|
doc["originalSource"].(map[string]any)["files"].(map[string]any)["leftVideo"] = map[string]string{"path": "part0001/other.mp4"}
|
|
raw, _ = json.Marshal(doc)
|
|
os.WriteFile(name, raw, 0600)
|
|
}
|
|
err := r.preflightV4OriginalSource(t.Context(), &run, node)
|
|
if (err == nil) != (scenario == "correct" || scenario == "wrong-files") {
|
|
t.Fatalf("preflight err=%v", err)
|
|
}
|
|
err = r.recheckV4DeliveryArtifact(t.Context(), &run, artifact)
|
|
if (err == nil) != (scenario == "correct") {
|
|
t.Fatalf("artifact guard err=%v", err)
|
|
}
|
|
if scenario == "correct" {
|
|
if _, err = r.publishCaptureDelivery(t.Context(), &run, "package", artifact); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := pub.objects["b/deliveries/project__p/capture-A/part0001/manifest.json"]; !ok {
|
|
t.Fatal("valid delivery did not complete")
|
|
}
|
|
}
|
|
if scenario != "correct" {
|
|
if _, err = r.publishCaptureDelivery(t.Context(), &run, "package", artifact); err == nil {
|
|
t.Fatal("guard allowed publication")
|
|
}
|
|
if len(pub.puts) > 0 || len(pub.published) > 0 {
|
|
t.Fatal("guard wrote destination/staging before rejection")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestV4ExecutionGuardRunsBeforePackageLaunch(t *testing.T) {
|
|
r, pub, run, node, _ := originalWorkerFixture(t)
|
|
parent, _ := r.store.GetRun(t.Context(), run.ParentRunID)
|
|
parent.Inputs = nil
|
|
r.store.UpsertRun(t.Context(), *parent)
|
|
_, job, err := r.executeNode(t.Context(), &run, node, nil, nil)
|
|
if err == nil || job != nil || pub.calls != 0 {
|
|
t.Fatalf("missing frozen identity reached execution: err=%v job=%v heads=%d", err, job, pub.calls)
|
|
}
|
|
}
|
|
|
|
func TestOriginalSourceGuardLeavesLegacyPackageAlone(t *testing.T) {
|
|
r, _, run, node, artifact := originalWorkerFixture(t)
|
|
run.ParentRunID = "missing"
|
|
node.Data.Hyperparams = nil
|
|
if err := r.preflightV4OriginalSource(t.Context(), &run, node); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, schema := range []string{"droboticflow.delivery.part-index/v1", "droboticflow.delivery.part-index/v2", "droboticflow.delivery.part-index/v3"} {
|
|
body, _ := json.Marshal(map[string]string{"schemaVersion": schema})
|
|
os.WriteFile(filepath.Join(artifact.Path, "part.json"), body, 0600)
|
|
if err := r.recheckV4DeliveryArtifact(t.Context(), &run, artifact); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOriginalSourceGuardPreservesLargeLegacyPart(t *testing.T) {
|
|
r, _, run, _, artifact := originalWorkerFixture(t)
|
|
run.ParentRunID = "missing"
|
|
body := []byte(`{"schemaVersion":"droboticflow.delivery.part-index/v1","filler":"` + strings.Repeat("x", 9<<20) + `"}`)
|
|
if err := os.WriteFile(filepath.Join(artifact.Path, "part.json"), body, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.recheckV4DeliveryArtifact(t.Context(), &run, artifact); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|