diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/input_integration_test.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/input_integration_test.go index 79522cc73..c63110b23 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/input_integration_test.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/input_integration_test.go @@ -11,6 +11,8 @@ import ( "encoding/hex" "encoding/json" "errors" + "os" + "reflect" "strings" "testing" "time" @@ -19,15 +21,24 @@ import ( "github.com/nats-io/nats.go/jetstream" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" "git.novox.be/novox/mesh-sdk/go/buscalls" "git.novox.be/novox/mesh-sdk/go/catalogue" + "git.novox.be/novox/mesh-sdk/go/seatlist/seatlisttest" "git.novox.be/novox/mesh-sdk/go/stateobjects" + "mesh-delivery-indexer/internal/index" + "mesh-delivery-indexer/judge" ) // TestIndexerAnswersARunsInputOverOneMiBInChunks (design 47 G §712, A-237, §681): an input of 3 MiB, kept in the state inputs with // its object beside it, is answered by the seat mesh-catalog's verb input as a count of chunks and then sent on the call's chunk // subjects, each acknowledged by the caller; the chunks carry the exact bytes. +// +// Design 47 H2 §814 (H-A-441, H-A-443, H-A-453 (3)): with the seat definitions of the tests' seat list in its state, the indexer +// keeps the seat list as one object of seat-lists named by its digest and hands it by the verb seat-list in chunks; a run by +// hand, which reads that object by its address and gives the judge its request as a file, gives the verdicts the live validate +// gives from the verb's bytes, for the builder's and the runner's manifests. func TestIndexerAnswersARunsInputOverOneMiBInChunks(t *testing.T) { body := make([]byte, 3<<20) if _, err := rand.Read(body); err != nil { @@ -35,13 +46,30 @@ func TestIndexerAnswersARunsInputOverOneMiBInChunks(t *testing.T) { } sum := sha256.Sum256(body) digest := hex.EncodeToString(sum[:]) - js, _, _, _ := startIndexer(t, func(context.Context, buildLog) {}, func(ctx context.Context, js jetstream.JetStream) { + var seatLists jetstream.ObjectStore + js, ctx, _, _ := startIndexer(t, func(context.Context, buildLog) {}, func(ctx context.Context, js jetstream.JetStream) { kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: index.Bucket(testIndexer, index.InputsState), History: 1}) if err != nil { t.Fatal(err) } - // The indexer's own seat list (A-186), its state seat-definitions: empty, so the bootstrapped mesh-catalog stands. - if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: index.Bucket(testIndexer, index.SeatDefinitionsState), History: 5}); err != nil { + // The indexer's own seat list (A-186), its state seat-definitions: the trunk's definition of every seat of the tests' + // seat list, as the take-in of the contracts modules keeps them; and its objects seat-lists, which the grants part makes. + defs, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: index.Bucket(testIndexer, index.SeatDefinitionsState), History: 5}) + if err != nil { + t.Fatal(err) + } + for _, d := range seatlisttest.Definitions() { + raw, err := json.Marshal(d) + if err != nil { + t.Fatal(err) + } + sum := sha256.Sum256(raw) + e, _ := json.Marshal(index.SeatDefinitionEntry{Seat: d.Seat, Source: index.SourceTrunk, Digest: hex.EncodeToString(sum[:]), Definition: raw, At: time.Now().UTC()}) + if _, err := defs.Put(ctx, index.SeatDefinitionKey(d.Seat), e); err != nil { + t.Fatal(err) + } + } + if seatLists, err = js.CreateObjectStore(ctx, jetstream.ObjectStoreConfig{Bucket: newBusObjects(nil, testAccount, index.SeatListsObjects).bucket()}); err != nil { t.Fatal(err) } objs, err := js.CreateObjectStore(ctx, jetstream.ObjectStoreConfig{Bucket: catalogue.BusName(testIndexer, catalogue.StateObjectsName(index.InputsState))}) @@ -103,4 +131,102 @@ func TestIndexerAnswersARunsInputOverOneMiBInChunks(t *testing.T) { if !bytes.Equal(got.Bytes(), body) { t.Errorf("the chunks carry %d bytes that are not the input's %d", got.Len(), len(body)) } + + // §814: the live validate's read, the verb seat-list in chunks (the assigner's seatList), and a run by hand's, the object + // seat-lists/ read by its address. + var list jetstream.ObjectInfo + wctx, cancel := context.WithTimeout(ctx, waitBound) + defer cancel() + w, err := seatLists.Watch(wctx) + if err != nil { + t.Fatal(err) + } + defer func() { _ = w.Stop() }() + for info := range w.Updates() { + if info != nil && !info.Deleted { + list = *info + break + } + } + if list.Name == "" { + t.Fatalf("seat-lists holds no seat list after %s", waitBound) + } + listCall := "fedcba9876543210fedcba9876543210" + listSend := buscalls.SeatChunks{Seat: meshcatalog.Seat, Verb: meshcatalog.VerbSeatList, Call: listCall} + listChunks := make(chan *nats.Msg, 16) + listSub, err := nc.ChanSubscribe(listSend.Chunks(), listChunks) + if err != nil { + t.Fatal(err) + } + defer listSub.Unsubscribe() + listReq, _ := json.Marshal(meshcatalog.SeatListInput{Call: listCall}) + m, err := nc.Request("mesh.seat.mesh-catalog.call.seat-list.agent~dev", listReq, 5*time.Second) + if err != nil { + t.Fatal(err) + } + var listReply struct { + Result meshcatalog.SeatListAnswer `json:"result"` + Error string `json:"error"` + } + if err := json.Unmarshal(m.Data, &listReply); err != nil || listReply.Error != "" || listReply.Result.Digest != list.Name || listReply.Result.Size != int64(list.Size) { + t.Fatalf("seat-list = %+v (%q, %v), want the object %s of %d bytes", listReply.Result, listReply.Error, err, list.Name, list.Size) + } + var viaVerb bytes.Buffer + listNext := func() (string, []byte, error) { + select { + case m := <-listChunks: + return m.Subject, m.Data, nil + case <-time.After(20 * time.Second): + return "", nil, buscalls.ErrSeatChunkMissed + } + } + if _, err := listSend.Receive(listReply.Result.Chunks, listNext, ack, &viaVerb); err != nil { + t.Fatal(err) + } + byAddress, err := seatLists.GetBytes(ctx, listReply.Result.Digest) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(viaVerb.Bytes(), byAddress) { + t.Fatalf("the verb's %d bytes are not the object's %d at its address", viaVerb.Len(), len(byAddress)) + } + // Both judged over the builder's and the runner's manifests as they stand in this repository: the live validate's + // request from the verb's bytes, and the hand run's request written to a file and read again as cmd/mesh-judge reads it. + manifests := map[string]composer.Manifest{} + for name, file := range map[string]string{"mesh-delivery-builder": "../../../mesh-delivery-builder/module.json", "mesh-delivery-pipeline": "../../../mesh-delivery-pipeline/module.json"} { + raw, err := os.ReadFile(file) + if err != nil { + t.Fatal(err) + } + manifests[name] = composer.Manifest{Raw: raw} + } + judged := func(seatList []byte, viaFile bool) composer.Verdict { + t.Helper() + var seats []json.RawMessage + if err := json.Unmarshal(seatList, &seats); err != nil || len(seats) == 0 { + t.Fatalf("the seat list is no array of definitions (%v)", err) + } + r := composer.Request{V: composer.Version, Cases: []composer.Case{{Name: "change", Manifests: manifests}}, Seats: seats, SeatList: listReply.Result.Digest} + if viaFile { + file, err := json.Marshal(r) + if err != nil { + t.Fatal(err) + } + if r, err = composer.Decode(file); err != nil { + t.Fatal(err) + } + } + v, err := judge.Request(r) + if err != nil { + t.Fatal(err) + } + return v + } + live, byHand := judged(viaVerb.Bytes(), false), judged(byAddress, true) + _, builder := live.Cases[0].Manifests["mesh-delivery-builder"] + _, runner := live.Cases[0].Manifests["mesh-delivery-pipeline"] + if !builder || !runner || !reflect.DeepEqual(byHand, live) { + t.Errorf("the run by hand judged %+v, want the live validate's %+v for the builder's and the runner's manifests", byHand, live) + } + t.Logf("the verdicts of the builder's and the runner's manifests against seat list %s: %+v", listReply.Result.Digest, live.Cases) }