Its states' buckets, its stream's subjects and its finding observer take the module its credential names (the tools: the module the runtime serves them as) instead of constants of its name; a guard test finds none in its code. Builds: design 47 G A-296, §428 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
959 lines
42 KiB
Go
959 lines
42 KiB
Go
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats-server/v2/server"
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"mesh-delivery-forge-bridge/internal/gitfixture"
|
|
groupkeys "mesh-delivery-forge-bridge/internal/groups"
|
|
)
|
|
|
|
// busConf is the shape of the composed accounts file (the grants part's buscompose): one account with JetStream, the
|
|
// controller's user, and the bridge's own principal <node>.<module> with the permissions composed for its manifest, and
|
|
// no others, so a permission the bridge lacks fails a test here and not on the mesh (issue 1279).
|
|
//
|
|
// From module.json: the seat git's events heard through its durable on EVENTS; what the gate and the checker owe heard
|
|
// through its durable on each seat's own stream owed (SEAT_<seat>_owed); the seat git's verbs, the seat mesh-delivery's
|
|
// groups and the seat mesh-hand-acts' hand-act called on mesh.seat.<seat>.call.<verb>.<token> (or its plain fallback);
|
|
// its own states heads, inputs, end-asks and owed-asks, and performed under the name its binding gives (the seat
|
|
// mesh-broker's show); its stream forge. It reads no state of the gate's or
|
|
// the checker's (design 47, acceptance 49 and 53). `**` is written `>` on the wire. The controller's own user keeps all
|
|
// rights, as it has.
|
|
const busConf = `
|
|
listen: 127.0.0.1:-1
|
|
jetstream { store_dir: %q }
|
|
accounts {
|
|
MESH {
|
|
jetstream: enabled
|
|
users = [
|
|
{ user: "ctl", password: "pw" }
|
|
{ user: "n1.mesh-delivery-forge-bridge", password: "pw"
|
|
permissions {
|
|
publish { allow: [
|
|
"$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_heads.>"
|
|
"$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_heads.>"
|
|
"$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_heads.>"
|
|
"$JS.API.STREAM.INFO.KV_mesh-delivery-forge-bridge_heads"
|
|
"$JS.FC.KV_mesh-delivery-forge-bridge_heads.>"
|
|
"$KV.mesh-delivery-forge-bridge_heads.>"
|
|
"$JS.API.CONSUMER.CREATE.KV_mod_mesh-delivery-forge-bridge_performed_b1.>"
|
|
"$JS.API.CONSUMER.DELETE.KV_mod_mesh-delivery-forge-bridge_performed_b1.>"
|
|
"$JS.API.DIRECT.GET.KV_mod_mesh-delivery-forge-bridge_performed_b1.>"
|
|
"$JS.API.STREAM.INFO.KV_mod_mesh-delivery-forge-bridge_performed_b1"
|
|
"$JS.FC.KV_mod_mesh-delivery-forge-bridge_performed_b1.>"
|
|
"$KV.mod_mesh-delivery-forge-bridge_performed_b1.>"
|
|
"$JS.API.CONSUMER.CREATE.KV_mod_mesh-delivery-forge-bridge_findings_b1.>"
|
|
"$JS.API.CONSUMER.DELETE.KV_mod_mesh-delivery-forge-bridge_findings_b1.>"
|
|
"$JS.API.DIRECT.GET.KV_mod_mesh-delivery-forge-bridge_findings_b1.>"
|
|
"$JS.API.STREAM.INFO.KV_mod_mesh-delivery-forge-bridge_findings_b1"
|
|
"$JS.FC.KV_mod_mesh-delivery-forge-bridge_findings_b1.>"
|
|
"$KV.mod_mesh-delivery-forge-bridge_findings_b1.>"
|
|
"$JS.API.STREAM.NAMES"
|
|
"$JS.API.CONSUMER.CREATE.EVENTS"
|
|
"$JS.API.CONSUMER.CREATE.EVENTS.>"
|
|
"$JS.API.CONSUMER.DELETE.EVENTS.>"
|
|
"$JS.API.CONSUMER.MSG.NEXT.EVENTS.>"
|
|
"mesh.bus.mesh-delivery-forge-bridge.finding-events.finding-written.n1"
|
|
"mesh.bus.mesh-delivery-forge-bridge.finding-events.finding-cleared.n1"
|
|
"mesh.bus.mesh-delivery-forge-bridge.finding-events.findings-stated.n1"
|
|
"$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_inputs.>"
|
|
"$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_inputs.>"
|
|
"$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_inputs.>"
|
|
"$JS.API.STREAM.INFO.KV_mesh-delivery-forge-bridge_inputs"
|
|
"$JS.FC.KV_mesh-delivery-forge-bridge_inputs.>"
|
|
"$KV.mesh-delivery-forge-bridge_inputs.>"
|
|
"$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_end-asks.>"
|
|
"$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_end-asks.>"
|
|
"$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_end-asks.>"
|
|
"$JS.API.STREAM.INFO.KV_mesh-delivery-forge-bridge_end-asks"
|
|
"$JS.FC.KV_mesh-delivery-forge-bridge_end-asks.>"
|
|
"$KV.mesh-delivery-forge-bridge_end-asks.>"
|
|
"$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_owed-asks.>"
|
|
"$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_owed-asks.>"
|
|
"$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_owed-asks.>"
|
|
"$JS.API.STREAM.INFO.KV_mesh-delivery-forge-bridge_owed-asks"
|
|
"$JS.FC.KV_mesh-delivery-forge-bridge_owed-asks.>"
|
|
"$KV.mesh-delivery-forge-bridge_owed-asks.>"
|
|
"$JS.ACK.EVENTS.n1_mesh-delivery-forge-bridge.>"
|
|
"$JS.API.CONSUMER.INFO.EVENTS.n1_mesh-delivery-forge-bridge"
|
|
"$JS.API.CONSUMER.MSG.NEXT.EVENTS.n1_mesh-delivery-forge-bridge"
|
|
"$JS.API.STREAM.INFO.SEAT_mesh-delivery_owed"
|
|
"$JS.ACK.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge.>"
|
|
"$JS.API.CONSUMER.INFO.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge"
|
|
"$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge"
|
|
"$JS.API.STREAM.INFO.SEAT_mesh-delivery-checker_owed"
|
|
"$JS.ACK.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge.>"
|
|
"$JS.API.CONSUMER.INFO.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge"
|
|
"$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge"
|
|
"mesh.seat.mesh-broker.call.show.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.mesh-broker.tool.show"
|
|
"mesh.seat.mesh-catalog.call.registrations.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.mesh-catalog.tool.registrations"
|
|
"mesh.seat.mesh-delivery.call.source.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.mesh-delivery.tool.source"
|
|
"mesh.seat.git.call.branch.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.branch"
|
|
"$JS.API.STREAM.INFO.SEAT_git_branch-moved"
|
|
"$JS.ACK.SEAT_git_branch-moved.n1_mesh-delivery-forge-bridge.>"
|
|
"$JS.API.CONSUMER.INFO.SEAT_git_branch-moved.n1_mesh-delivery-forge-bridge"
|
|
"$JS.API.CONSUMER.MSG.NEXT.SEAT_git_branch-moved.n1_mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.call.status.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.status"
|
|
"mesh.seat.git.call.comment.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.comment"
|
|
"mesh.seat.git.call.view.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.view"
|
|
"mesh.seat.git.call.note.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.note"
|
|
"mesh.seat.git.call.merge-pull.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.merge-pull"
|
|
"mesh.seat.git.call.open-pulls.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.open-pulls"
|
|
"mesh.seat.git.call.trees.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.trees"
|
|
"mesh.seat.git.call.file.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.file"
|
|
"mesh.seat.git.call.pull.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.pull"
|
|
"mesh.seat.git.call.tags.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.git.tool.tags"
|
|
"mesh.seat.mesh-delivery.call.groups.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.mesh-delivery.tool.groups"
|
|
"mesh.seat.mesh-hand-acts.call.hand-act.n1~mesh-delivery-forge-bridge"
|
|
"mesh.seat.mesh-hand-acts.tool.hand-act"
|
|
"mesh.stream.mesh-delivery-forge-bridge.event.forge.>"
|
|
"mesh.stream.mesh-delivery-forge-bridge.event.owed.ended"
|
|
] }
|
|
subscribe { allow: [
|
|
"_INBOX.n1.mesh-delivery-forge-bridge.>"
|
|
"mesh.assignment.n1.mesh-delivery-forge-bridge"
|
|
"mesh.mod.mesh-delivery-forge-bridge.call.>"
|
|
"mesh.mod.mesh-delivery-forge-bridge.tool.>"
|
|
] }
|
|
allow_responses: true
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
`
|
|
|
|
func startBus(t *testing.T) *server.Server {
|
|
t.Helper()
|
|
dir, err := os.MkdirTemp(os.Getenv("TMPDIR"), "b")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(dir) })
|
|
conf := filepath.Join(dir, "bus.conf")
|
|
if err := os.WriteFile(conf, []byte(fmt.Sprintf(busConf, filepath.Join(dir, "js"))), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
opts, err := server.ProcessConfigFile(conf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
opts.NoLog, opts.NoSigs = true, true
|
|
srv, err := server.NewServer(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go srv.Start()
|
|
if !srv.ReadyForConnections(10 * time.Second) {
|
|
t.Fatal("the bus did not come up")
|
|
}
|
|
t.Cleanup(srv.Shutdown)
|
|
return srv
|
|
}
|
|
|
|
// startBridge runs the program against an embedded bus with the stream EVENTS, the bridge's stream FORGE and its durable
|
|
// consumer <node>_<module> on EVENTS, as the controller makes them (mesh-control internal/broker streams.go:192,
|
|
// nats.go:1387-1396, modstreams.go). It returns a control connection, its JetStream and a context.
|
|
func startBridge(t *testing.T, openReply string) (*nats.Conn, jetstream.JetStream, context.Context, <-chan error) {
|
|
b := startBed(t, openReply, nil)
|
|
return b.ctl, b.js, b.ctx, b.done
|
|
}
|
|
|
|
// bed is a started bridge with the bus it runs against.
|
|
type bed struct {
|
|
srv *server.Server
|
|
ctl *nats.Conn
|
|
js jetstream.JetStream
|
|
ctx context.Context
|
|
done <-chan error
|
|
reply func(string)
|
|
// files sets what the stand-in seat git answers to file from then on; it is asked with the verb's input.
|
|
files func(func(repository, path, ref string) (string, bool))
|
|
// answer sets the stand-in's answer to one verb of a seat (`<seat>.<verb>`) from then on.
|
|
answer func(verb string, answer string)
|
|
// calls are the verbs the stand-in was called with, in order, as `<seat>.<verb> <input>`.
|
|
calls func() []string
|
|
// restart stops the bridge, runs between, and starts it again on the same bus.
|
|
restart func(between func())
|
|
}
|
|
|
|
// answerOf is a verb's answer in the reply envelope of mesh-sdk go/process/call.go ({result, error, node}).
|
|
func answerOf(answer string) []byte { return []byte(`{"result":` + answer + `,"node":"n0"}`) }
|
|
|
|
// fileAnswer is the seat git's answer to file: the content, or absent.
|
|
func fileAnswer(path, ref, content string, found bool) string {
|
|
if !found {
|
|
return `{"absent":true}`
|
|
}
|
|
b, _ := json.Marshal(git.FileAnswer{Path: path, Ref: ref, Content: content, Size: int64(len(content))})
|
|
return string(b)
|
|
}
|
|
|
|
// boundPerformed is the bus name the stand-in seat mesh-broker's show answers for the state performed.
|
|
const boundPerformed = "mod_mesh-delivery-forge-bridge_performed_b1"
|
|
|
|
// boundFindings is the bus name the stand-in seat mesh-broker's show answers for the state findings.
|
|
const boundFindings = "mod_mesh-delivery-forge-bridge_findings_b1"
|
|
|
|
// boundFindingEvents and boundFindingEventsSubject are the stream and subject the stand-in seat mesh-broker's show answers
|
|
// for the bridge's finding events (design 47 G §529, A-274).
|
|
const (
|
|
boundFindingEvents = "mod_mesh-delivery-forge-bridge_finding-events_b1"
|
|
boundFindingEventsSubject = "mesh.bus.mesh-delivery-forge-bridge.finding-events"
|
|
)
|
|
|
|
// startBed is startBridge with a step between the streams and the consumers: prepare writes what the streams hold before
|
|
// the bridge's consumers are made, as the grants part makes them, from now (ADR 0443 §1), and reply sets the answer of the
|
|
// seat git's open-pulls from then on. A stand-in holds the seats the bridge calls: git (every verb, trees r0 by default,
|
|
// a module.json of {} at every path), mesh-delivery's groups (none by default) and mesh-hand-acts' hand-act.
|
|
func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, js jetstream.JetStream)) *bed {
|
|
srv := startBus(t)
|
|
ctl, err := nats.Connect(srv.ClientURL(), nats.UserInfo("ctl", "pw"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(ctl.Close)
|
|
js, err := jetstream.New(ctl)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
t.Cleanup(cancel)
|
|
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "EVENTS", Subjects: []string{"mesh.mod.*.event.>", "mesh.seat.*.event.>"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "FORGE", Subjects: []string{"mesh.stream.mesh-delivery-forge-bridge.event.forge.>", "mesh.stream.mesh-delivery-forge-bridge.event.owed.ended"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The bridge's own stream of its findings' events, its requires entry finding-events under the names the seat
|
|
// mesh-broker's show answers (design 47 G §529, A-274), which its finding writer says into.
|
|
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: boundFindingEvents, Subjects: []string{boundFindingEventsSubject + ".>"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Each seat's own stream owed, as its contract keeps it (Stream<E>, SEAT_<seat>_owed), as the grants part makes it: no
|
|
// subjects of its own, sourced from EVENTS filtered by the event's subject.
|
|
for name, subjects := range map[string]string{seat.StreamOwed: seat.SubjectOwedAll, checker.StreamOwed: checker.SubjectOwedAll, git.StreamBranchMoved: git.SubjectBranchMoved} {
|
|
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: name, MaxAge: 7 * 24 * time.Hour,
|
|
Sources: []*jetstream.StreamSource{{Name: "EVENTS", FilterSubject: subjects}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// The bridge's states, made from the manifest's state entries and its requires before the process starts; the state
|
|
// performed under the bus name the seat mesh-broker's show answers for it (boundPerformed), never an old name (§577).
|
|
for _, name := range []string{headsBucket(forgeBridgeModule()), boundPerformed, boundFindings, inputsBucket(forgeBridgeModule()), groupkeys.EndAsksBucket(forgeBridgeModule()), groupkeys.OwedAsksBucket(forgeBridgeModule())} {
|
|
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: name, History: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if prepare != nil {
|
|
prepare(ctx, js)
|
|
}
|
|
acct := process.Account{Node: "n1", Module: "mesh-delivery-forge-bridge"}
|
|
if _, err := js.CreateOrUpdateConsumer(ctx, "EVENTS", jetstream.ConsumerConfig{
|
|
Durable: acct.EventConsumerName(), AckPolicy: jetstream.AckExplicitPolicy, DeliverPolicy: jetstream.DeliverNewPolicy,
|
|
FilterSubjects: []string{git.SubjectPullUpdated, git.SubjectPullMerged, git.SubjectPullClosed, git.SubjectForgeDelete,
|
|
"mesh.seat.mesh-catalog.event.registration-changed", "mesh.seat.mesh-delivery.event.source-moved"},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, subjects := range map[string]string{seat.StreamOwed: seat.SubjectOwedAll, checker.StreamOwed: checker.SubjectOwedAll, git.StreamBranchMoved: git.SubjectBranchMoved} {
|
|
if _, err := js.CreateOrUpdateConsumer(ctx, name, acct.SeatEventConsumerConfig([]string{subjects}, false)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// The stand-in seats, answering on the caller-named subject and its plain fallback.
|
|
var mu sync.Mutex
|
|
answers := map[string]string{
|
|
"git.open-pulls": openReply, "git.trees": `{"root":"r0","trees":{"":"t0","modules/a":"t1"}}`,
|
|
"mesh-delivery.groups": `{"groups":[]}`, "mesh-hand-acts.hand-act": `{"recorded":true}`,
|
|
// No registration yet: the watched branches are the gate's source's, main (design 47 G §507).
|
|
"mesh-catalog.registrations": `{"registrations":[]}`, "mesh-delivery.source": `{"branches":["main"]}`,
|
|
"git.pull": `{"error":"no pull request merged at that commit","error-kind":"not-found"}`,
|
|
"mesh-broker.show": `{"state":"created","binding":{"local":"performed","storage":"key-value","template":"state","bucket":"` + boundPerformed + `"}}`,
|
|
"mesh-broker.show findings": `{"state":"created","binding":{"local":"findings","storage":"key-value","template":"state","bucket":"` + boundFindings + `"}}`,
|
|
"mesh-broker.show finding-events": `{"state":"created","binding":{"local":"finding-events","storage":"stream","template":"events","stream":"` +
|
|
boundFindingEvents + `","subject":"` + boundFindingEventsSubject + `"}}`,
|
|
}
|
|
for _, verb := range []string{"status", "comment", "view", "note", "merge-pull"} {
|
|
answers["git."+verb] = string(gitfixture.Given(t, verb+" answers", "verb:"+verb+".answer"))
|
|
}
|
|
filesOf := func(_, path, _ string) (string, bool) { return "{}", strings.HasSuffix(path, "module.json") }
|
|
var called []string
|
|
for _, subject := range []string{"mesh.seat.*.call.*.*", "mesh.seat.*.tool.*"} {
|
|
sub, err := ctl.Subscribe(subject, func(m *nats.Msg) {
|
|
parts := strings.Split(m.Subject, ".")
|
|
verb := parts[2] + "." + parts[4]
|
|
mu.Lock()
|
|
called = append(called, verb+" "+string(m.Data))
|
|
answer, files := answers[verb], filesOf
|
|
var in struct {
|
|
Repository string
|
|
Local string `json:"local-name"`
|
|
}
|
|
if json.Unmarshal(m.Data, &in) == nil && answers[verb+" "+in.Repository] != "" {
|
|
answer = answers[verb+" "+in.Repository] // a move's branch and pull, set per repository (landedOn)
|
|
}
|
|
if in.Local != "" && answers[verb+" "+in.Local] != "" {
|
|
answer = answers[verb+" "+in.Local] // show answers each entry of the module's requires by its local name
|
|
}
|
|
mu.Unlock()
|
|
if verb == "git.file" {
|
|
var in git.FileInput
|
|
_ = json.Unmarshal(m.Data, &in)
|
|
content, found := files(in.Repository, in.Path, in.Ref)
|
|
answer = fileAnswer(in.Path, in.Ref, content, found)
|
|
}
|
|
_ = m.Respond(answerOf(answer))
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
|
}
|
|
if err := ctl.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
broker := filepath.Join(t.TempDir(), "broker")
|
|
cred, _ := json.Marshal(process.Account{URL: srv.ClientURL(), Node: "n1", Module: "mesh-delivery-forge-bridge", User: "n1.mesh-delivery-forge-bridge", Password: "pw"})
|
|
if err := os.WriteFile(broker, cred, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
env := map[string]string{"MESH_BROKER_FILE": broker}
|
|
done := make(chan error, 1)
|
|
var stop context.CancelFunc
|
|
start := func() {
|
|
var runCtx context.Context
|
|
runCtx, stop = context.WithCancel(ctx)
|
|
go func() { done <- run(runCtx, func(k string) string { return env[k] }, io.Discard) }()
|
|
}
|
|
start()
|
|
t.Cleanup(func() { stop() })
|
|
set := func(verb, answer string) { mu.Lock(); answers[verb] = answer; mu.Unlock() }
|
|
return &bed{srv: srv, ctl: ctl, js: js, ctx: ctx, done: done, reply: func(s string) { set("git.open-pulls", s) },
|
|
files: func(f func(repository, path, ref string) (string, bool)) { mu.Lock(); filesOf = f; mu.Unlock() },
|
|
answer: set,
|
|
calls: func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), called...) },
|
|
restart: func(between func()) {
|
|
stop()
|
|
<-done
|
|
between()
|
|
start()
|
|
}}
|
|
}
|
|
|
|
// landedOn sets the stand-in seat git so main of repository is protected at at, which the pull request number on branch
|
|
// at prHead merged.
|
|
func landedOn(b *bed, repository string, number int, branch, prHead, at string) {
|
|
b.answer("git.branch "+repository, `{"repository":"`+repository+`","branch":"main","head":"`+at+`","protected":true}`)
|
|
b.answer("git.pull "+repository, fmt.Sprintf(`{"pull":{"repository":%q,"number":%d,"branch":%q,"head":%q,"base":"main","state":"merged","merge-commit":%q}}`,
|
|
repository, number, branch, prHead, at))
|
|
}
|
|
|
|
// moveMain says main of repository moved, as the seat git says it, naming a head the bridge never takes (§11).
|
|
func moveMain(t *testing.T, b *bed, repository string) {
|
|
t.Helper()
|
|
ev, _ := json.Marshal(git.BranchMovedEvent{Repository: repository, Branch: "main", Head: strings.Repeat("f", 40)})
|
|
if _, err := b.js.Publish(b.ctx, git.SubjectBranchMoved, ev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// TestBridgeWritesPullMessages starts the program the way the node does (its entry, run, with MESH_BROKER_FILE).
|
|
func TestBridgeWritesPullMessages(t *testing.T) { // 1235/AC2
|
|
ctl, js, ctx, _ := startBridge(t, `{"pulls":[]}`)
|
|
defer branchDeletedCase(t, ctl.ConnectedUrl(), js, ctx)
|
|
|
|
// pull.updated as the seat git says it (its contract's PullUpdatedEvent).
|
|
ev, _ := json.Marshal(git.PullUpdatedEvent{Repository: "novox/mesh-delivery", Number: 12, Title: "t", Base: "main", Branch: "feat/x", Head: head,
|
|
Change: &git.Change{Paths: []string{"modules/a/x.go"}, ModuleDirs: []string{"modules/a"}}})
|
|
if _, err := js.Publish(ctx, git.SubjectPullUpdated, ev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
forge, err := js.Stream(ctx, "FORGE")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var info *jetstream.StreamInfo
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
if info, err = forge.Info(ctx); err == nil && info.State.Msgs >= 2 {
|
|
break
|
|
}
|
|
}
|
|
// The open message the bridge writes at its start (ADR 0443 §5), and the event's message.
|
|
if info.State.Msgs != 2 {
|
|
t.Fatalf("FORGE holds %d messages within 20 s, want 2", info.State.Msgs)
|
|
}
|
|
raw, err := forge.GetLastMsgForSubject(ctx, "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.pull")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := forgemsg.DecodeForge(raw.Data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := forgemsg.Forge{V: keys.Version, Kind: "pull", Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head,
|
|
Action: "updated", Dirs: []string{"modules/a"}, DirsKnown: true, Manifests: map[string]string{"modules/a": "{}"}}
|
|
if !forgemsg.SameForge(f, want) {
|
|
t.Errorf("message = %+v, want %+v", f, want)
|
|
}
|
|
|
|
// The event is acknowledged: the consumer owes nothing and has nothing pending.
|
|
cons, err := js.Consumer(ctx, "EVENTS", "n1_mesh-delivery-forge-bridge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
ci, err := cons.Info(ctx)
|
|
if err == nil && ci.NumAckPending == 0 && ci.NumPending == 0 && ci.Delivered.Consumer >= 1 {
|
|
return
|
|
}
|
|
}
|
|
ci, _ := cons.Info(ctx)
|
|
t.Errorf("the event is not acknowledged: %+v", ci)
|
|
}
|
|
|
|
// branchDeletedCase is DLV-FB1's case of the bridge's whole-module test (design 51): the seat git's forge.delete of a
|
|
// branch is a branch-deleted message on FORGE, and a forge.delete from another account than the seat's holder is refused
|
|
// by the bus.
|
|
func branchDeletedCase(t *testing.T, srv string, js jetstream.JetStream, ctx context.Context) {
|
|
t.Helper()
|
|
ev, _ := json.Marshal(git.ForgeDeleteEvent{Repository: "novox/mesh-sdk", Ref: "feat/x", RefType: "branch"})
|
|
if _, err := js.Publish(ctx, git.SubjectForgeDelete, ev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
forge, err := js.Stream(ctx, "FORGE")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subject := "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-sdk.branch-deleted"
|
|
var raw *jetstream.RawStreamMsg
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
if raw, err = forge.GetLastMsgForSubject(ctx, subject); err == nil {
|
|
break
|
|
}
|
|
}
|
|
if err != nil || !strings.Contains(string(raw.Data), `"branch":"feat/x"`) {
|
|
t.Fatalf("no branch-deleted message on FORGE within 20 s: %v", err)
|
|
}
|
|
other, err := nats.Connect(srv, nats.UserInfo("n1.mesh-delivery-forge-bridge", "pw"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer other.Close()
|
|
ojs, _ := jetstream.New(other)
|
|
octx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
if _, err := ojs.Publish(octx, git.SubjectForgeDelete, ev); err == nil {
|
|
t.Error("a forge.delete from the bridge's account was taken by the bus: only the seat git's holder may say it")
|
|
}
|
|
}
|
|
|
|
const merge = "89abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
// openPull is one entry of the seat git's open-pulls answer.
|
|
func openPull(repo string, number int, branch, head string) string {
|
|
b, _ := json.Marshal(git.OpenPull{Pull: git.Pull{Repository: "novox/" + repo, Number: int64(number), Base: "main", Branch: branch, Head: head, State: "open"},
|
|
Checks: map[string]git.Status{}, Required: []string{}})
|
|
return string(b)
|
|
}
|
|
|
|
// TestBridgeWritesMergedMessage: the stand-in seat git answers trees and file on the caller-named subjects.
|
|
func TestBridgeWritesMergedMessage(t *testing.T) { // 1236/AC4, 1448/AC5
|
|
bed := startBed(t, `{"pulls":[]}`, nil)
|
|
js, ctx := bed.js, bed.ctx
|
|
// The stand-in forge's files are two real files of this repository (this module's module.json and go.sum, read from the
|
|
// working tree), in the seat git's file answer: the root has a manifest only, modules/a a manifest and go.sum.
|
|
manifest, err := os.ReadFile("../../module.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum, err := os.ReadFile("../../go.sum")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bed.files(func(_, path, ref string) (string, bool) {
|
|
if ref != merge {
|
|
t.Errorf("the forge was asked for %s at %s, want the merge commit %s", path, ref, merge)
|
|
}
|
|
switch path {
|
|
case "module.json", "modules/a/module.json":
|
|
return string(manifest), true
|
|
case "modules/a/go.sum":
|
|
return string(sum), true
|
|
}
|
|
return "", false
|
|
})
|
|
// design 47 G §11, §509, §714: main moved, read by the seat's branch, and the forge names #12 merged at its head.
|
|
landedOn(bed, "novox/mesh-delivery", 12, "feat/x", head, merge)
|
|
moveMain(t, bed, "novox/mesh-delivery")
|
|
|
|
forge, err := js.Stream(ctx, "FORGE")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var info *jetstream.StreamInfo
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
if info, err = forge.Info(ctx); err == nil && info.State.Msgs >= 2 {
|
|
break
|
|
}
|
|
}
|
|
// The open message the bridge writes at its start (ADR 0443 §5), and the event's message.
|
|
if info.State.Msgs != 2 {
|
|
t.Fatalf("FORGE holds %d messages within 20 s, want 2", info.State.Msgs)
|
|
}
|
|
raw, err := forge.GetLastMsgForSubject(ctx, "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.merged")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := forgemsg.DecodeForge(raw.Data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := forgemsg.Forge{V: keys.Version, Kind: "merged", Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head,
|
|
Merge: merge, Base: "main", Root: "r0", Trees: map[string]string{"": "t0", "modules/a": "t1"},
|
|
Manifests: map[string]string{"": string(manifest), "modules/a": string(manifest)},
|
|
// No mesh package: only the indexer computes what a fingerprint takes (ADR 0593 §2).
|
|
Locks: map[string]map[string]string{"modules/a": {"go.sum": hex.EncodeToString(sha256Sum(sum))}}}
|
|
if !forgemsg.SameForge(f, want) {
|
|
t.Errorf("message = %+v, want %+v", f, want)
|
|
}
|
|
var trees []string
|
|
for _, c := range bed.calls() {
|
|
if strings.HasPrefix(c, "git.trees ") {
|
|
trees = append(trees, c)
|
|
}
|
|
}
|
|
in, _ := json.Marshal(git.TreesInput{Repository: "novox/mesh-delivery", Commit: merge})
|
|
if len(trees) != 1 || trees[0] != "git.trees "+string(in) {
|
|
t.Errorf("the seat git was asked for trees %v, want once at the merge commit", trees)
|
|
}
|
|
cons, err := js.Consumer(ctx, git.StreamBranchMoved, "n1_mesh-delivery-forge-bridge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
ci, err := cons.Info(ctx)
|
|
if err == nil && ci.NumAckPending == 0 && ci.NumPending == 0 && ci.Delivered.Consumer >= 1 {
|
|
return
|
|
}
|
|
}
|
|
ci, _ := cons.Info(ctx)
|
|
t.Errorf("the event is not acknowledged: %+v", ci)
|
|
}
|
|
|
|
// TestBridgeSaysTheOpenPullsAtStart: the forge holds two open pull requests when the bridge starts; one message each is on
|
|
// FORGE, and the open message naming both after them (ADR 0443 §5).
|
|
func TestBridgeSaysTheOpenPullsAtStart(t *testing.T) { // 1237/AC1
|
|
// The entries are the seat git's open-pulls answer (its contract's OpenPull).
|
|
_, js, ctx, _ := startBridge(t, `{"pulls":[
|
|
`+openPull("mesh-delivery", 12, "feat/x", head)+`,
|
|
`+openPull("mesh-catalog", 300, "feat/x", strings.Repeat("a", 40))+`]}`)
|
|
forge, err := js.Stream(ctx, "FORGE")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var info *jetstream.StreamInfo
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
if info, err = forge.Info(ctx); err == nil && info.State.Msgs >= 3 {
|
|
break
|
|
}
|
|
}
|
|
if info.State.Msgs != 3 {
|
|
t.Fatalf("FORGE holds %d messages within 20 s, want 3", info.State.Msgs)
|
|
}
|
|
for _, repo := range []string{"mesh-delivery", "mesh-catalog"} {
|
|
raw, err := forge.GetLastMsgForSubject(ctx, "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox."+repo+".pull")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f, err := forgemsg.DecodeForge(raw.Data); err != nil || f.Action != "updated" || f.DirsKnown || f.Branch != "feat/x" {
|
|
t.Errorf("%s: %+v err %v", repo, f, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBridgeExitsWhenTheOpenPullsCannotBeRead: the process manager starts it again.
|
|
func TestBridgeExitsWhenTheOpenPullsCannotBeRead(t *testing.T) { // 1237/AC3
|
|
_, _, _, done := startBridge(t, `{"error":"the forge said 502","error-kind":"failed","pulls":[]}`)
|
|
select {
|
|
case err := <-done:
|
|
if err == nil || !strings.Contains(err.Error(), "open-pulls") || !strings.Contains(err.Error(), "the forge said 502") {
|
|
t.Errorf("run returned %v", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("the bridge did not exit")
|
|
}
|
|
}
|
|
|
|
// TestBridgeReadsTheForgeAtStartAndAfterAReconnectAndHearsNoOlderEvent starts the bridge whole against a fake forge with two
|
|
// open pull requests, one of them with its head already kept in the bucket heads, and a bus whose EVENTS holds older pull events from
|
|
// before its consumer was made, under the permissions the controller composes for the bridge. At start it writes the one pull message that differs and the open message naming both;
|
|
// the bus connection closed, it reads the forge again and writes the open message alone, since every head is held now;
|
|
// for the older events it writes nothing (ADR 0443 §1, §5).
|
|
func TestBridgeReadsTheForgeAtStartAndAfterAReconnectAndHearsNoOlderEvent(t *testing.T) { // 1279/AC8
|
|
h2 := strings.Repeat("a", 40)
|
|
pullSubject := func(repo string) string {
|
|
return "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox." + repo + ".pull"
|
|
}
|
|
openSubject := "mesh.stream.mesh-delivery-forge-bridge.event.forge.open"
|
|
b := startBed(t, `{"pulls":[`+openPull("mesh-delivery", 12, "feat/x", head)+`,`+openPull("mesh-catalog", 300, "feat/x", h2)+`]}`,
|
|
func(ctx context.Context, js jetstream.JetStream) {
|
|
// Older events: a pull request long closed and one long updated, from before the bridge's consumer.
|
|
for subject, ev := range map[string]any{
|
|
git.SubjectPullUpdated: git.PullUpdatedEvent{Repository: "novox/mesh-old", Number: 5, Base: "main", Branch: "feat/old", Head: h2},
|
|
git.SubjectPullClosed: git.PullClosedEvent{Repository: "novox/mesh-older", Number: 6, Base: "main", Branch: "feat/older", Head: h2},
|
|
} {
|
|
raw, _ := json.Marshal(ev)
|
|
if _, err := js.Publish(ctx, subject, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// What the bridge kept of an earlier write: the head of mesh-delivery#12.
|
|
kv, err := js.KeyValue(ctx, headsBucket(forgeBridgeModule()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := kv.PutString(ctx, "novox.mesh-delivery.12", head); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
forge, err := b.js.Stream(b.ctx, "FORGE")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitMsgs := func(want uint64) {
|
|
t.Helper()
|
|
var info *jetstream.StreamInfo
|
|
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
if info, err = forge.Info(b.ctx); err == nil && info.State.Msgs >= want {
|
|
break
|
|
}
|
|
}
|
|
time.Sleep(300 * time.Millisecond) // and nothing more arrives
|
|
if info, err = forge.Info(b.ctx); err != nil || info.State.Msgs != want {
|
|
t.Fatalf("FORGE holds %d messages (%v), want %d", info.State.Msgs, err, want)
|
|
}
|
|
}
|
|
named := func(seq uint64) []forgemsg.OpenPull {
|
|
t.Helper()
|
|
raw, err := forge.GetMsg(b.ctx, seq)
|
|
if err != nil || raw.Subject != openSubject {
|
|
t.Fatalf("message %d: %v %v, want the open message", seq, raw, err)
|
|
}
|
|
f, err := forgemsg.DecodeForge(raw.Data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return f.Open
|
|
}
|
|
bothOpen := []forgemsg.OpenPull{{Repository: "novox/mesh-catalog", Number: 300}, {Repository: "novox/mesh-delivery", Number: 12}}
|
|
same := func(got []forgemsg.OpenPull) bool {
|
|
return len(got) == 2 && got[0] == bothOpen[0] && got[1] == bothOpen[1]
|
|
}
|
|
|
|
// At start: the one pull message that differs (1), the open message naming both (2).
|
|
waitMsgs(2)
|
|
if raw, err := forge.GetMsg(b.ctx, 1); err != nil || raw.Subject != pullSubject("mesh-catalog") {
|
|
t.Fatalf("message 1 = %v %v, want mesh-catalog's pull message", raw, err)
|
|
}
|
|
if got := named(2); !same(got) {
|
|
t.Fatalf("the open message names %v, want %v", got, bothOpen)
|
|
}
|
|
|
|
// The connection closes and reopens: the forge is read again, every head is held, the open message alone follows.
|
|
conns, err := b.srv.Connz(&server.ConnzOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
closed := 0
|
|
for _, c := range conns.Conns {
|
|
if c.Name == "n1.mesh-delivery-forge-bridge" {
|
|
if err := b.srv.DisconnectClientByID(c.Cid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
closed++
|
|
}
|
|
}
|
|
if closed != 1 {
|
|
t.Fatalf("%d connections of the bridge found", closed)
|
|
}
|
|
waitMsgs(3)
|
|
if got := named(3); !same(got) {
|
|
t.Fatalf("after the reconnect the open message names %v, want %v", got, bothOpen)
|
|
}
|
|
|
|
// For the older events nothing was written.
|
|
for _, repo := range []string{"mesh-old", "mesh-older"} {
|
|
if raw, err := forge.GetLastMsgForSubject(b.ctx, pullSubject(repo)); err == nil {
|
|
t.Errorf("a message for %s's older event: %+v", repo, raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
// heard is every message published under the bridge's stream subjects from now, by subject.
|
|
func heard(t *testing.T, b *bed) <-chan *nats.Msg {
|
|
t.Helper()
|
|
ch := make(chan *nats.Msg, 64)
|
|
sub, err := b.ctl.ChanSubscribe("mesh.stream.mesh-delivery-forge-bridge.event.>", ch)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
|
if err := b.ctl.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return ch
|
|
}
|
|
|
|
// next is the next message heard on subject within a bound; the others are passed by.
|
|
func next(t *testing.T, ch <-chan *nats.Msg, subject string) []byte {
|
|
t.Helper()
|
|
bound := time.After(20 * time.Second)
|
|
for {
|
|
select {
|
|
case m := <-ch:
|
|
if m.Subject == subject {
|
|
return m.Data
|
|
}
|
|
case <-bound:
|
|
t.Fatalf("nothing on %s within 20 s", subject)
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// refusedRead says the bridge's account cannot read the bucket: a key-value read of another module's state is refused.
|
|
func refusedRead(t *testing.T, b *bed, bucket string) {
|
|
t.Helper()
|
|
if _, err := b.js.CreateKeyValue(b.ctx, jetstream.KeyValueConfig{Bucket: bucket}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
nc, err := nats.Connect(b.srv.ClientURL(), nats.UserInfo("n1.mesh-delivery-forge-bridge", "pw"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer nc.Close()
|
|
js, _ := jetstream.New(nc)
|
|
ctx, cancel := context.WithTimeout(b.ctx, 2*time.Second)
|
|
defer cancel()
|
|
if kv, err := js.KeyValue(ctx, bucket); err == nil {
|
|
if _, err := kv.Get(ctx, "any"); err == nil || errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
t.Errorf("the bridge's account reads %s", bucket)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Acceptance 49: the forge bridge sets the group on each member's merged message and says the group's end once, after the
|
|
// last member's, from the seat mesh-delivery's verb groups; its account reads no state of the gate's.
|
|
func TestBridgeSaysAGroupFromTheSeatsGroups(t *testing.T) { // 1585, ADR 0633 §4
|
|
b := startBed(t, `{"pulls":[]}`, nil)
|
|
headB := strings.Repeat("b", 40)
|
|
mergeB := strings.Repeat("e", 40)
|
|
groups, _ := json.Marshal(seat.GroupsAnswer{Groups: []seat.Group{{ID: "feat/x", State: "merging", Members: []seat.Member{
|
|
{Repository: "novox/mesh-delivery", Number: 12, Head: head, State: "merged", Base: "main"}, {Repository: "novox/mesh-sdk", Number: 4, Head: headB, State: "merging", Base: "main"}}}}})
|
|
b.answer("mesh-delivery.groups", string(groups))
|
|
ch := heard(t, b)
|
|
landedOn(b, "novox/mesh-delivery", 12, "feat/x", head, merge)
|
|
landedOn(b, "novox/mesh-sdk", 4, "feat/x", headB, mergeB)
|
|
for _, m := range []git.PullMergedEvent{{Repository: "novox/mesh-delivery"}, {Repository: "novox/mesh-sdk"}} {
|
|
moveMain(t, b, m.Repository)
|
|
repo := strings.TrimPrefix(m.Repository, "novox/")
|
|
f, err := forgemsg.DecodeForge(next(t, ch, "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox."+repo+".merged"))
|
|
if err != nil || f.Group != "feat/x" || len(f.Members) != 2 || f.Members[1].Number != 4 {
|
|
t.Fatalf("%s's merged message: %+v %v, want the group feat/x with both members", repo, f, err)
|
|
}
|
|
}
|
|
end, err := forgemsg.DecodeForgeGroupEnd(next(t, ch, forgeSubject(forgemsg.ForgeGroupEndEvent)))
|
|
if err != nil || end.Group != "feat/x" || end.Base != "main" || len(end.Members) != 2 || end.Members[0].Merge != merge || end.Members[1].Merge != mergeB {
|
|
t.Errorf("the group's end: %+v %v", end, err)
|
|
}
|
|
refusedRead(t, b, "mesh-delivery-gate_merges")
|
|
}
|
|
|
|
// Acceptance 53 (design 47 *What the gate owes the forge*): an owed act said on a seat's own stream owed is performed through
|
|
// the seat git and its outcome said as owed.ended; said twice it is performed once; one it cannot read is said undecodable;
|
|
// one said while the bridge is stopped is performed once after its restart; bridge_owed_again's ask is answered by the
|
|
// process; and its account reads neither the gate's nor the checker's state.
|
|
func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
|
|
b := startBed(t, `{"pulls":[]}`, nil)
|
|
ch := heard(t, b)
|
|
ended := func() forgemsg.OwedEnded {
|
|
t.Helper()
|
|
e, err := forgemsg.DecodeOwedEnded(next(t, ch, forgeSubject(forgemsg.OwedEndedEvent)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return e
|
|
}
|
|
count := func(verb string) int {
|
|
n := 0
|
|
for _, c := range b.calls() {
|
|
if strings.HasPrefix(c, verb+" ") {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
gateOwed := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a merge", "event:owed")[0]
|
|
subject := seat.SubjectOwed("novox~mesh-sdk", "178")
|
|
for range 2 {
|
|
if _, err := b.js.Publish(b.ctx, subject, gateOwed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if e := ended(); e.Outcome != forgemsg.OwedPerformed || e.Of != seat.Seat {
|
|
t.Errorf("the gate's merge: %+v, want performed", e)
|
|
}
|
|
if _, err := b.js.Publish(b.ctx, subject, []byte("{")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e := ended(); e.Outcome != forgemsg.OwedUndecodable {
|
|
t.Errorf("an act it cannot read: %+v, want undecodable", e)
|
|
}
|
|
if n := count("git.merge-pull"); n != 1 {
|
|
t.Errorf("an act said twice was performed %d times", n)
|
|
}
|
|
// Said while the bridge is stopped: performed once after its restart.
|
|
checkerOwed := gitfixture.Seat(t, checker.Seat, "events", "owed is said", "event:owed")[0]
|
|
b.restart(func() {
|
|
if _, err := b.js.Publish(b.ctx, checker.SubjectOwed("novox~mesh-sdk", "178"), checkerOwed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if e := ended(); e.Of != checker.Seat || e.Outcome != forgemsg.OwedPerformed {
|
|
t.Errorf("the checker's status after the restart: %+v, want performed", e)
|
|
}
|
|
if n := count("git.status"); n != 1 {
|
|
t.Errorf("the checker's status was performed %d times, want once", n)
|
|
}
|
|
// bridge_owed_again asks through the state owed-asks; the process performs the act again and answers there.
|
|
var g struct {
|
|
ID string `json:"id"`
|
|
}
|
|
_ = json.Unmarshal(gateOwed, &g)
|
|
key, value, err := groupkeys.NewOwedAsk(groupkeys.OwedAgain, seat.Seat, g.ID, "a test", "lead@n1", time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
asks, err := b.js.KeyValue(b.ctx, groupkeys.OwedAsksBucket(forgeBridgeModule()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w, err := asks.Watch(b.ctx, key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer w.Stop()
|
|
if _, err := asks.Put(b.ctx, key, value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bound := time.After(20 * time.Second)
|
|
for answered := false; !answered; {
|
|
select {
|
|
case e := <-w.Updates():
|
|
if e == nil {
|
|
continue
|
|
}
|
|
if a, err := groupkeys.DecodeOwedAsk(e.Value()); err == nil && a.Done() {
|
|
answered = true
|
|
if a.Outcome != forgemsg.OwedPerformed || count("git.merge-pull") != 2 {
|
|
t.Errorf("bridge_owed_again: %+v, merge-pull asked %d times; want performed again", a, count("git.merge-pull"))
|
|
}
|
|
}
|
|
case <-bound:
|
|
t.Fatal("bridge_owed_again was not answered within 20 s")
|
|
}
|
|
}
|
|
refusedRead(t, b, "mesh-delivery-gate_owed")
|
|
refusedRead(t, b, "mesh-delivery-checker_owed")
|
|
}
|
|
|
|
func sha256Sum(b []byte) []byte { s := sha256.Sum256(b); return s[:] }
|
|
|
|
// Design 47 G §17, §383, §529, A-251 (FB2): the bridge started whole against the real bus says a move of a branch whose protection
|
|
// was removed as the finding branch-unprotected, into its bound state findings and its finding stream, and writes no message.
|
|
func TestBridgeSaysAnUnprotectedBranchAsAFinding(t *testing.T) {
|
|
b := startBed(t, `{"pulls":[]}`, nil)
|
|
b.answer("git.branch novox/mesh-delivery", `{"repository":"novox/mesh-delivery","branch":"main","head":"`+merge+`","protected":false}`)
|
|
moveMain(t, b, "novox/mesh-delivery")
|
|
kv, err := b.js.KeyValue(b.ctx, boundFindings)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var e jetstream.KeyValueEntry
|
|
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
|
if e, err = kv.Get(b.ctx, "merge.novox/mesh-delivery/main.branch-unprotected"); err == nil {
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("the finding is not in the bound state findings: %v", err)
|
|
}
|
|
var f struct{ Kind, Scope, ID, Observer string }
|
|
if err := json.Unmarshal(e.Value(), &f); err != nil || f.Kind != "branch-unprotected" || f.Observer != "mesh-delivery-forge-bridge" {
|
|
t.Errorf("the finding = %s %v", e.Value(), err)
|
|
}
|
|
// A-274: the finding is said under the bridge's binding finding-events, never on the old stream.
|
|
events, err := b.js.Stream(b.ctx, boundFindingEvents)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for deadline := time.Now().Add(20 * time.Second); ; time.Sleep(50 * time.Millisecond) {
|
|
if _, err = events.GetLastMsgForSubject(b.ctx, boundFindingEventsSubject+".finding-written.n1"); err == nil || time.Now().After(deadline) {
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
t.Errorf("no finding-written under the binding finding-events: %v", err)
|
|
}
|
|
forge, err := b.js.Stream(b.ctx, "FORGE")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info, err := forge.Info(b.ctx); err != nil || info.State.Msgs != 1 { // the open message of its start only
|
|
t.Errorf("FORGE holds %+v %v: an unprotected branch's move writes no message", info, err)
|
|
}
|
|
}
|