forked from longtao.wu/droboticflow
167 lines
7.0 KiB
Go
167 lines
7.0 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/deliveryindex"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/sourceidentity"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type identityIndexBrowser struct {
|
|
*indexTestBrowser
|
|
heads int
|
|
}
|
|
|
|
func (b *identityIndexBrowser) GetObjectSummary(ctx context.Context, bucket, key string) (domain.ObjectStorageObjectSummary, error) {
|
|
info, err := b.indexTestBrowser.GetObjectSummary(ctx, bucket, key)
|
|
if err == nil {
|
|
info.ETag = deliveryindex.Hash(b.objects[key])
|
|
b.heads++
|
|
}
|
|
return info, err
|
|
}
|
|
|
|
func seedOriginalIdentityIndex(t *testing.T, ds store.Store) (*identityIndexBrowser, *authenticatedPrincipal) {
|
|
t.Helper()
|
|
t.Setenv("DELIVERY_CLASSIFICATION_TEST", "1")
|
|
t.Setenv("DELIVERY_V4_FIXTURE_DIR", "")
|
|
base, p, root := seedIndexFixture(t, ds)
|
|
b := &identityIndexBrowser{indexTestBrowser: base}
|
|
for _, part := range []string{"part0001", "part0002"} {
|
|
prefix := root + "/capture-A/" + part
|
|
body := base.objects[prefix+"/part.json"]
|
|
replacements := 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{}
|
|
for role, rel := range replacements {
|
|
oldRel := part + "/" + role
|
|
if role == "cameraCalibration" || role == "imuCalibration" {
|
|
oldRel = role + ".yaml"
|
|
}
|
|
if role == "metadata" {
|
|
oldRel = "meta_capture-A.json"
|
|
}
|
|
body = bytes.ReplaceAll(body, []byte(`"`+oldRel+`"`), []byte(`"`+rel+`"`))
|
|
key := "incoming/capture-A/" + rel
|
|
if role != "metadata" {
|
|
base.objects[key] = []byte("original synthetic object")
|
|
}
|
|
keys = append(keys, key)
|
|
objects = append(objects, sourceidentity.Object{Key: key, ETag: deliveryindex.Hash(base.objects[key]), Size: int64(len(base.objects[key]))})
|
|
}
|
|
base.objects[prefix+"/part.json"] = body
|
|
entries := []deliveryindex.Entry{{Path: "part.json", Key: prefix + "/part.json", SizeBytes: int64(len(body)), SHA256: deliveryindex.Hash(body)}}
|
|
base.objects[prefix+"/manifest.json"], _ = json.Marshal(map[string]any{"schemaVersion": 1, "kind": "directory", "fileCount": 1, "totalBytes": len(body), "entries": entries})
|
|
child, err := ds.GetRun(t.Context(), "package-run-"+part)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := range child.Artifacts {
|
|
if domain.IsInternalDeliveryAudit(child.Artifacts[i]) {
|
|
child.Artifacts[i].Data["partSha256"] = deliveryindex.Hash(body)
|
|
}
|
|
}
|
|
child.NodeExecutions[0].Artifacts = child.Artifacts
|
|
if err = ds.UpsertRun(t.Context(), *child); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
capture, err := ds.GetCaptureRecordByRunID(t.Context(), "source-run-"+part)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original := domain.RunRecord{ID: capture.RunID, OrganizationID: capture.OrganizationID, ProjectID: capture.ProjectID, WorkflowID: capture.WorkflowID, Inputs: map[string]domain.ArtifactPayload{"source": {Ref: &domain.ArtifactReference{Provider: domain.StorageProviderBOS, Kind: domain.ArtifactRefKindDirectory, ConnectionID: capture.ConnectionID, Bucket: capture.Bucket, Prefix: capture.SourcePrefix, CaptureUnit: capture.CaptureID, ObjectKeys: keys, VersionID: sourceidentity.Fingerprint(objects)}}}}
|
|
if err = ds.UpsertRun(t.Context(), original); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return b, nil, nil
|
|
}
|
|
return b, p
|
|
}
|
|
|
|
func TestClassificationBatchOriginalIdentityHTTP(t *testing.T) {
|
|
for _, scenario := range []string{"correct", "same-size-etag", "changed-size", "missing", "missing-frozen-version", "wrong-tenant", "extra-selection"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
ds := store.NewMemoryStore()
|
|
b, p := seedOriginalIdentityIndex(t, ds)
|
|
h := NewHandler(ds, nil)
|
|
query := "/api/delivery-index-batches?projectId=project-A&packageId=package-part0001"
|
|
preview := indexCall(h, p, http.MethodGet, query, nil)
|
|
if preview.Code != 200 {
|
|
t.Fatalf("valid preview: %d %s", preview.Code, preview.Body.String())
|
|
}
|
|
var plan struct {
|
|
ID string `json:"snapshotId"`
|
|
}
|
|
json.Unmarshal(preview.Body.Bytes(), &plan)
|
|
key := "incoming/capture-A/part0001/capture-A_camera_left_part0001.mp4"
|
|
switch scenario {
|
|
case "same-size-etag":
|
|
b.objects[key] = bytes.Repeat([]byte("x"), len(b.objects[key]))
|
|
case "changed-size":
|
|
b.objects[key] = append(b.objects[key], 'x')
|
|
case "missing":
|
|
delete(b.objects, key)
|
|
case "missing-frozen-version", "wrong-tenant", "extra-selection":
|
|
run, _ := ds.GetRun(t.Context(), "source-run-part0001")
|
|
switch scenario {
|
|
case "missing-frozen-version":
|
|
run.Inputs["source"].Ref.VersionID = ""
|
|
case "wrong-tenant":
|
|
run.OrganizationID = "other"
|
|
case "extra-selection":
|
|
run.Inputs["source"].Ref.ObjectKeys = append(run.Inputs["source"].Ref.ObjectKeys, "incoming/capture-A/extra.json")
|
|
}
|
|
if err := ds.UpsertRun(t.Context(), *run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
result := indexCall(h, p, http.MethodPost, "/api/delivery-index-batches", indexBatchRequest{ProjectID: "project-A", PackageIDs: []string{"package-part0001"}, SnapshotID: plan.ID})
|
|
if scenario == "correct" {
|
|
if result.Code != 200 {
|
|
t.Fatalf("publish: %d %s", result.Code, result.Body.String())
|
|
}
|
|
if b.heads < 16 {
|
|
t.Fatalf("POST did not recheck: %d", b.heads)
|
|
}
|
|
} else if result.Code != 409 || len(b.puts) != 0 {
|
|
t.Fatalf("changed source published: %d %s writes=%d", result.Code, result.Body.String(), len(b.puts))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClassificationBatchRejectsDeliveryFilesOutsideFrozenSelection(t *testing.T) {
|
|
ds := store.NewMemoryStore()
|
|
b, p := seedOriginalIdentityIndex(t, ds)
|
|
// Change the immutable run selection while preserving a valid selection shape.
|
|
run, _ := ds.GetRun(t.Context(), "source-run-part0001")
|
|
ref := run.Inputs["source"].Ref
|
|
for i, key := range ref.ObjectKeys {
|
|
if strings.HasSuffix(key, "_left_part0001.mp4") {
|
|
ref.ObjectKeys[i] = strings.TrimSuffix(key, ".mp4") + "_alternate.mp4"
|
|
b.objects[ref.ObjectKeys[i]] = b.objects[key]
|
|
}
|
|
}
|
|
objects := []sourceidentity.Object{}
|
|
for _, key := range ref.ObjectKeys {
|
|
objects = append(objects, sourceidentity.Object{Key: key, ETag: deliveryindex.Hash(b.objects[key]), Size: int64(len(b.objects[key]))})
|
|
}
|
|
ref.VersionID = sourceidentity.Fingerprint(objects)
|
|
if err := ds.UpsertRun(t.Context(), *run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := indexCall(NewHandler(ds, nil), p, http.MethodGet, "/api/delivery-index-batches?projectId=project-A&packageId=package-part0001", nil)
|
|
if result.Code != 409 || len(b.puts) > 0 {
|
|
t.Fatalf("files outside frozen selection accepted: %d %s", result.Code, result.Body.String())
|
|
}
|
|
}
|