Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/step_integration_test.go
T

175 lines
6.9 KiB
Go

//go:build integration
package main
import (
"encoding/json"
"runtime"
"strings"
"testing"
"time"
"github.com/nats-io/nats.go"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"git.novox.be/novox/mesh-sdk/go/buscompose"
)
// The runner's build step ask on the seat mesh-builds' accept subject, naming an entry by its fingerprint, is built through the
// module's one claim and answered as the seat's step-ended.<node> under the ask's id (design 47 G A-97, A-99).
func TestTheRunnersBuildStepAskIsAnsweredAsStepEnded(t *testing.T) {
b := startBed(t)
b.module(t, "netcheck", "catalogue-a67f0196-netcheck.module.json", "modules/netcheck")
sub, err := nats.Connect(b.srvURL, nats.UserInfo("ctl", "pw"))
if err != nil {
t.Fatal(err)
}
defer sub.Close()
ended := make(chan *nats.Msg, 4)
s, err := sub.ChanSubscribe(meshbuilds.SubjectStepEndedAll, ended)
if err != nil {
t.Fatal(err)
}
defer s.Unsubscribe()
taken := make(chan *nats.Msg, 4)
st, err := sub.ChanSubscribe(meshbuilds.SubjectAskTakenAll, taken)
if err != nil {
t.Fatal(err)
}
defer st.Unsubscribe()
// Design 47 H2 §777: an ask put on the seat's work queue while the builder is stopped waits there, and is taken (ask-taken)
// and ended once the builder starts.
run := meshbuilds.StepRun{Pipeline: "main.1.netcheck", Step: meshbuilds.StepRunStepBuild, Branch: "main", Revision: 1,
Entries: []meshbuilds.BuildEntry{{Module: "netcheck", Fingerprint: fp}}}
data, _ := json.Marshal(run)
msg := nats.NewMsg(meshbuilds.AcceptSubjectStep)
msg.Data = data
msg.Header.Set("x-event-id", "ask-1")
msg.Header.Set(nats.MsgIdHdr, "ask-1")
if _, err := b.js.PublishMsg(b.ctx, msg); err != nil {
t.Fatalf("the ask is not put on the seat's work queue: %v", err)
}
b.start(t, "n1", nil)
select {
case m := <-taken:
var at meshbuilds.AskTakenEvent
if err := json.Unmarshal(m.Data, &at); err != nil || at.Ask != "ask-1" || at.Node != "n1" || at.At.IsZero() || m.Subject != meshbuilds.SubjectAskTaken("ask-1") {
t.Fatalf("ask-taken on %s = %+v, %v; want ask-1 taken on n1", m.Subject, at, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no ask-taken within 20 s of the start")
}
select {
case m := <-ended:
var end meshbuilds.StepEnded
if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-1" || end.Outcome != meshbuilds.StepEndedOutcomePassed || end.Pipeline != "main.1.netcheck" || m.Subject != meshbuilds.SubjectStepEnded("n1") {
t.Fatalf("step-ended on %s = %+v, %v; want the ask ask-1 passed on n1", m.Subject, end, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no step-ended within 20 s")
}
// Design 47 H2 H-A-577 (1), AC-B2: an ask whose revision the picture does not hold says wait-held (step build, on revision:<R>, kept
// under <module>.<fingerprint>.revision) and wait-ended with how ran once it does.
waits := make(chan *nats.Msg, 8)
for _, ev := range []string{"wait-held", "wait-ended"} {
w, err := sub.ChanSubscribe("mesh.mod.*.event."+ev+".netcheck.>", waits)
if err != nil {
t.Fatal(err)
}
defer w.Unsubscribe()
}
revisionWait := func(event string) waitBody {
t.Helper()
for bound := time.After(20 * time.Second); ; {
select {
case m := <-waits:
var w waitBody
if err := json.Unmarshal(m.Data, &w); err != nil {
t.Fatal(err)
}
if strings.Contains(m.Subject, ".event."+event+".") && strings.HasSuffix(m.Subject, ".revision") {
return w
}
case <-bound:
t.Fatalf("no %s of the revision wait within 20 s", event)
}
}
}
// H-A-155: an ask naming a source revision the builder's picture of main does not hold yet waits for it, said in the
// journal, and is answered once a revision taken in from it arrives.
run.Pipeline, run.Revision, run.Source = "main.2.netcheck", 2, 2
data, _ = json.Marshal(run)
msg = nats.NewMsg(meshbuilds.AcceptSubjectStep)
msg.Data = data
msg.Header.Set("x-event-id", "ask-2")
if err := sub.PublishMsg(msg); err != nil {
t.Fatal(err)
}
b.awaitSaid(t, "main.2.netcheck: revision 2 of main not in the builder's picture yet")
if w := revisionWait("wait-held"); w.On != "revision:2" || w.Step != "build" || w.Module != "netcheck" || w.Fingerprint != fp {
t.Errorf("wait-held of the ask for revision 2 = %+v, want step build, on revision:2, netcheck at %s", w, fp)
}
b.catalog.mu.Lock()
b.catalog.source = 2
b.catalog.mu.Unlock()
// Revision 2, taken in from source 2, moves another module: netcheck's entry is the one the picture holds already.
b.moduleFP(t, "ca-trust", "catalogue-a67f0196-ca-trust.module.json", "modules/ca-trust", nil, commit, "src2:"+strings.Repeat("4", 64), 0)
select {
case m := <-ended:
var end meshbuilds.StepEnded
if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-2" || end.Outcome != meshbuilds.StepEndedOutcomePassed {
t.Fatalf("step-ended = %+v, %v; want the ask ask-2 passed once the picture held source revision 2", end, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no step-ended for ask-2 within 20 s")
}
if w := revisionWait("wait-ended"); w.On != "revision:2" || w.How != "ran" {
t.Errorf("wait-ended of the ask for revision 2 = %+v, want on revision:2 ended ran", w)
}
// Each ask is acknowledged once its end was said: the seat's worker holds none pending.
stream, worker := buscompose.SeatQueueStreamName(meshbuilds.Seat), buscompose.SeatQueueWorkerName(meshbuilds.Seat)
deadline := time.Now().Add(10 * time.Second)
for {
c, err := b.js.Consumer(b.ctx, stream, worker)
if err != nil {
t.Fatal(err)
}
info, err := c.Info(b.ctx)
if err != nil {
t.Fatal(err)
}
if info.NumPending == 0 && info.NumAckPending == 0 {
break
}
if time.Now().After(deadline) {
t.Fatalf("the seat's worker holds %d waiting and %d taken after both asks ended, want none", info.NumPending, info.NumAckPending)
}
runtime.Gosched()
}
// H-A-468 (2): the worker deleted and made again by the broker's part is bound again, and an ask put after it is ended.
if err := b.js.DeleteConsumer(b.ctx, stream, worker); err != nil {
t.Fatal(err)
}
b.awaitSaid(t, "work queue "+worker+" on "+stream+" was deleted")
b.seatQueue(t)
b.awaitSaid(t, "pulling the seat "+meshbuilds.Seat+"'s asks from "+worker)
run.Pipeline = "main.3.netcheck"
data, _ = json.Marshal(run)
msg = nats.NewMsg(meshbuilds.AcceptSubjectStep)
msg.Data = data
msg.Header.Set("x-event-id", "ask-3")
msg.Header.Set(nats.MsgIdHdr, "ask-3")
if _, err := b.js.PublishMsg(b.ctx, msg); err != nil {
t.Fatal(err)
}
select {
case m := <-ended:
var end meshbuilds.StepEnded
if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-3" || end.Outcome != meshbuilds.StepEndedOutcomePassed {
t.Fatalf("step-ended = %+v, %v; want the ask ask-3 passed through the worker made again", end, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no step-ended for ask-3 within 20 s of the worker made again: the builder did not bind it again")
}
}