Files
mesh-delivery/modules/mesh-delivery-builder/internal/versions/republish_test.go
T

199 lines
8.9 KiB
Go

package versions
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
)
type queueMem struct {
*reserveMem
bytes uint64
}
func (q *queueMem) Bytes(context.Context) (uint64, error) { return q.bytes, nil }
// republishBed is a Republisher over memory states: reservations of module m (a bundle by the oci seat and a Go package by
// the Go seat), one holder module `oci` that claims the oci seat, and a Send and a Unreleased that record.
type republishBed struct {
*Republisher
reserved *reserveMem
queue *queueMem
now time.Time
sent []Publish
said []string
refuse string // the words Send fails with, empty for none
// holders are the registry seats' holders the seat-holders view names (design 47 G A-416).
holders map[string][]string
}
func newRepublishBed(t *testing.T) *republishBed {
t.Helper()
b := &republishBed{reserved: &reserveMem{mem: newMem()}, queue: &queueMem{reserveMem: &reserveMem{mem: newMem()}}, now: time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)}
res := &Reserver{Store: b.reserved, Now: func() time.Time { return b.now }, ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }}
b.Republisher = &Republisher{Reserved: res, Queue: b.queue, Now: func() time.Time { return b.now }, Go: func(f func()) { f() },
Entry: func(_ context.Context, fp string) (meshcatalog.CatalogEntry, error) {
if fp == "src2:holder" {
return meshcatalog.CatalogEntry{Serves: []string{SeatArtifacts + ".publish", SeatArtifacts + ".versions"}}, nil
}
m := strings.TrimPrefix(fp, "src2:")
return meshcatalog.CatalogEntry{Artifacts: []meshcatalog.Artifact{
{Ref: m + ":prog", Kind: "bundle", Language: "go"},
{Ref: m + ":lib", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/" + m + "/go"}}}}, nil
},
Send: func(_ context.Context, p Publish) error {
b.sent = append(b.sent, p)
if b.refuse != "" {
return errors.New(b.refuse)
}
return nil
},
Unreleased: func(_ context.Context, v ReservedVersion, why string) {
b.said = append(b.said, v.Module+" "+v.Version+": "+why)
},
Logf: t.Logf}
b.Holders = func(seat string) []string { return b.holders[seat] }
return b
}
// reserve reserves and builds a version of module m (its patch given by the reservation order), its build ended long ago.
func (b *republishBed) reserve(t *testing.T, module string) ReservedVersion {
t.Helper()
b.now = b.now.Add(time.Minute)
ctx := context.Background()
v, err := b.Reserved.Reserve(ctx, module, "0.1", []string{module + ":prog", module + ":lib"}, Source{Fingerprint: "src2:" + module})
if err != nil {
t.Fatal(err)
}
for ref, d := range map[string]string{module + ":prog": "sha256:aa", module + ":lib": "sha256:bb"} {
if _, err := b.Reserved.Built(ctx, module, v.Version, ref, d, nil); err != nil {
t.Fatal(err)
}
}
b.now = b.now.Add(UnreleasedBound + time.Minute)
return v
}
// TestRepublish: design 47 G §588 (A-73, A-76): a reserved version left unpublished is published again, on a registry
// holder's running and on its node's returned, once per message by its stream sequence, as a publish by digest with no parts,
// at most the setting's number per event, newest first; an artifact no store holds is said once and not tried again.
func TestRepublish(t *testing.T) {
ctx := context.Background()
t.Run("a done build is published as a digest with no parts, to its seat's holder", func(t *testing.T) {
b := newRepublishBed(t)
v := b.reserve(t, "m")
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
if v.Fingerprint != "src2:m" {
t.Fatalf("the reservation keeps the entry's fingerprint: %q", v.Fingerprint)
}
if q, err := b.Republish(ctx, "", ""); err != nil || len(b.sent) != 1 || fmt.Sprint(q) != "[m "+v.Version+"]" {
t.Fatalf("queued %v sent %d (%v), want 1", q, len(b.sent), err)
}
p := b.sent[0]
if p.Seat != SeatArtifacts || p.Name != "m/prog" || p.Kind != "bundle" || p.Version != v.Version || p.Digest != "sha256:aa" || p.Fingerprint != "src2:m" ||
p.Transfer != "republish-m-"+strings.ReplaceAll(v.Version, ".", "-")+"-prog" || strings.ContainsAny(p.Transfer, "/. *>") {
t.Errorf("sent %+v, want the bundle by digest to the oci seat, the Go package left to its own seat's holder", p)
}
if keys, _ := b.queue.KeysOf(ctx, AllFilter); len(keys) != 0 {
t.Errorf("a key stays in the queue after its outcome: %v", keys)
}
})
t.Run("an entry that cannot be read stops neither the other versions nor the drain", func(t *testing.T) {
b := newRepublishBed(t)
b.reserve(t, "m1")
b.reserve(t, "m2") // newest: queued before m1's entry is met
entry := b.Entry
b.Entry = func(ctx context.Context, fp string) (meshcatalog.CatalogEntry, error) {
if fp == "src2:m1" {
return meshcatalog.CatalogEntry{}, errors.New("no entry of fingerprint src2:m1 is kept")
}
return entry(ctx, fp)
}
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
if _, err := b.Republish(ctx, "", ""); err == nil || !strings.Contains(err.Error(), "src2:m1") {
t.Errorf("the unreadable entry is not said: %v", err)
}
if len(b.sent) != 1 || !strings.HasPrefix(b.sent[0].Name, "m2/") {
t.Errorf("sent %+v, want m2 drained though m1's entry cannot be read", b.sent)
}
})
t.Run("an artifact no store holds is said once and not tried again", func(t *testing.T) {
b := newRepublishBed(t)
b.reserve(t, "m")
b.refuse = "oci-artifact-registry's publish of m/prog: not stored: no blob sha256:aa"
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
_, _ = b.Republish(ctx, "", "")
_, _ = b.Republish(ctx, "", "")
if len(b.sent) != 1 || len(b.said) != 1 || !strings.Contains(b.said[0], "not stored: ") {
t.Errorf("sent %d, said %v: want one try and one saying of not stored", len(b.sent), b.said)
}
})
t.Run("a refusal in other words is said each time", func(t *testing.T) {
b := newRepublishBed(t)
b.reserve(t, "m")
b.refuse = "the holder is full"
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
_, _ = b.Republish(ctx, "", "")
_, _ = b.Republish(ctx, "", "")
if len(b.sent) != 2 || len(b.said) != 2 {
t.Errorf("sent %d, said %v: want both events tried and said", len(b.sent), b.said)
}
})
t.Run("a queue past its cap says queue full", func(t *testing.T) {
b := newRepublishBed(t)
b.reserve(t, "m")
b.queue.bytes = PublishQueueMiB << 20
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
_, _ = b.Republish(ctx, "", "")
if len(b.sent) != 0 || len(b.said) != 1 || !strings.HasSuffix(b.said[0], QueueFull) {
t.Errorf("sent %d, said %v, want nothing sent and queue full said", len(b.sent), b.said)
}
})
t.Run("a restart sends the queued again, a begun one anew", func(t *testing.T) {
b := newRepublishBed(t)
v := b.reserve(t, "m")
raw, _ := json.Marshal(QueuedPublish{V: 1, Holder: "oci", Module: "m", Version: v.Version, Artifacts: map[string]string{"m:prog": "sha256:aa"}, Queued: b.now, Begun: true})
if _, err := b.queue.Create(ctx, QueueKey("oci", "m", v.Version), raw); err != nil {
t.Fatal(err)
}
if err := b.Resume(ctx); err != nil || len(b.sent) != 1 {
t.Errorf("resume sent %d (%v), want the begun version sent anew", len(b.sent), err)
}
})
t.Run("after a fresh start the tool queues at the seat's holder, no running heard", func(t *testing.T) {
// Design 47 G A-416, A-416a: the holder is the one the seat-holders view names from the start, not one learnt from a
// running heard since; a seat with no holder queues nothing.
b := newRepublishBed(t)
b.reserve(t, "m1")
b.reserve(t, "m2")
if q, err := b.Republish(ctx, "", ""); err != nil || len(q) != 0 {
t.Errorf("no holder named: queued %v (%v), want none", q, err)
}
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
q, err := b.Republish(ctx, "", "")
if err != nil || fmt.Sprint(q) != "[m2 0.1.0 m1 0.1.0]" || len(b.sent) != 2 || b.sent[0].Seat != SeatArtifacts {
t.Errorf("the tool with neither queued %v (%v), sent %+v: want both versions at oci, with no limit", q, err, b.sent)
}
if _, err := b.Republish(ctx, "m1", "0.1.9"); err == nil {
t.Error("a version no reservation holds is not refused")
}
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
_, _ = b.Republish(ctx, "", "")
items := Reader{Keys: func() ([]string, error) { return []string{"publish.m.0.1.0", "reserve-done.m.x"}, nil },
Get: func(k string) (json.RawMessage, bool, error) {
return json.RawMessage(`{"mode":"publish","module":"m","version":"0.1.0","tries":3,"next-try":"2026-11-17T01:00:00Z","why":"w"}`), true, nil
}}
none := Reader{Keys: func() ([]string, error) { return nil, nil }}
answer, err := AnswerQueue(none, none, items)
if err != nil || len(answer.Items) != 1 || answer.Items[0].Tries != 3 || answer.Items[0].Version != "0.1.0" {
t.Errorf("builder_publish_queue items = %+v (%v), want the one publish item with its tries", answer.Items, err)
}
})
}