A publish item skipped while the registry held the version would stay skipped for ever once its published event was lost; and node-tools 0.1.37, held by the artifact registry at another digest than the build's, was asked again for ever and never released. When every missing artifact is held at its version with the built digest the builder marks it published from the registry's record and says package-published, as a heard published would; the release follows from the catalog. Before reserving, the registry seats' versions of the module's artifacts are read and every number one holds is skipped (version-held-elsewhere, scope module); a read that fails lets the reserve proceed. A done build whose number a registry holds at another digest moves to the next free number, its digests kept, the old number withdrawn as a gap; its old item and bound end and its publish is asked under the new number. The finding clears at the module's version-released. Builds: design 47 H H-A-274, H-A-275, H-A-245, H-A-226
238 lines
12 KiB
Go
238 lines
12 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)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestTheRegistriesRecordSaysWhetherAVersionIsHeld: design 47 H H-A-272, H-A-274, H-A-275 — a missing artifact its registry seat
|
|
// holds at the version (the Go form too) with the built digest is held; at the version with another digest it is held elsewhere;
|
|
// a version the seat lacks is said by name. The numbers a registry holds of the module's artifacts are what reserving skips.
|
|
func TestTheRegistriesRecordSaysWhetherAVersionIsHeld(t *testing.T) {
|
|
ctx := context.Background()
|
|
entry := meshcatalog.CatalogEntry{Artifacts: []meshcatalog.Artifact{{Ref: "m:lib", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.example/m"}}}}
|
|
r := &Republisher{Entry: func(context.Context, string) (meshcatalog.CatalogEntry, error) { return entry, nil }}
|
|
v := ReservedVersion{Module: "m", Version: "0.1.37", Fingerprint: "fp", Artifacts: map[string]string{"m:lib": "sha256:a"}}
|
|
for _, c := range []struct {
|
|
name string
|
|
holds []HeldVersion
|
|
all, elsewhere, lacks bool
|
|
}{
|
|
{name: "held in the Go form with the built digest", holds: []HeldVersion{{Version: "v0.1.36", Digest: "sha256:z"}, {Version: "v0.1.37", Digest: "sha256:a"}}, all: true},
|
|
{name: "another digest at the version", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:b"}}, elsewhere: true},
|
|
{name: "the version lacked", holds: []HeldVersion{{Version: "0.1.36", Digest: "sha256:a"}}, lacks: true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var seat, name string
|
|
versionsOf := func(_ context.Context, s, n string) ([]HeldVersion, error) { seat, name = s, n; return c.holds, nil }
|
|
j, err := r.Held(ctx, v, versionsOf)
|
|
if err != nil || j.All != c.all || (len(j.Elsewhere) == 1) != c.elsewhere || (j.Lacks != "") != c.lacks || seat != SeatGoPackages || name != "git.example/m" {
|
|
t.Fatalf("judged %+v (%v) asking %s for %s, want all %v, elsewhere %v, lacks %v asking %s for git.example/m", j, err, seat, name, c.all, c.elsewhere, c.lacks, SeatGoPackages)
|
|
}
|
|
if c.lacks && !strings.Contains(j.Lacks, "0.1.37") {
|
|
t.Errorf("what the registry lacks is said as %q", j.Lacks)
|
|
}
|
|
nums, err := r.HeldNumbers(ctx, "m", "fp", []string{"m:lib"}, versionsOf)
|
|
if err != nil || len(nums) != len(c.holds) {
|
|
t.Errorf("the held numbers are %v (%v), want one per version the seat holds", nums, err)
|
|
}
|
|
})
|
|
}
|
|
v.Published = map[string]bool{"m:lib": true}
|
|
if j, err := r.Held(ctx, v, nil); err != nil || !j.All {
|
|
t.Errorf("a version with no missing artifact is judged %+v (%v), want all held without a read", j, err)
|
|
}
|
|
}
|