Live, a published at a version fourteen reservations share cost three seconds: the fallback by version read every reservation there, and the journal line read them all again, so the backlog drained at fifty a minute. The published now hands the reservations it read (each once, a second pass taking what the first read) to its journal line. The fallback reads the new index key of the reservations at a version whose build has not recorded every digest (kept at reserve and where a digest is recorded, written again at the start), the only ones a published can be kept on; the line counts the rest from the index by version without reading them. Before that pass the handler says in progress to the bus. Builds: design 47 H H-A-280, H-A-271
535 lines
27 KiB
Go
535 lines
27 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// memReserve is the state reserved in memory.
|
|
type memReserve struct {
|
|
vals map[string][]byte
|
|
revs map[string]uint64
|
|
}
|
|
|
|
func (m *memReserve) Get(_ context.Context, key string) ([]byte, uint64, bool, error) {
|
|
v, ok := m.vals[key]
|
|
return v, m.revs[key], ok, nil
|
|
}
|
|
func (m *memReserve) Create(_ context.Context, key string, v []byte) (uint64, error) {
|
|
if _, ok := m.vals[key]; ok {
|
|
return 0, versions.ErrConflict
|
|
}
|
|
m.vals[key], m.revs[key] = v, 1
|
|
return 1, nil
|
|
}
|
|
func (m *memReserve) Update(_ context.Context, key string, v []byte, rev uint64) (uint64, error) {
|
|
if m.revs[key] != rev {
|
|
return 0, versions.ErrConflict
|
|
}
|
|
m.vals[key], m.revs[key] = v, rev+1
|
|
return rev + 1, nil
|
|
}
|
|
func (m *memReserve) KeysOf(_ context.Context, filter string) ([]string, error) {
|
|
var out []string
|
|
for k := range m.vals {
|
|
if versions.FilterMatches(filter, k) { // as the bus reads a filter
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
func (m *memReserve) Delete(_ context.Context, key string) error {
|
|
delete(m.vals, key)
|
|
return nil
|
|
}
|
|
|
|
// boundsKV keeps the keys created and deleted in the state bounds.
|
|
type boundsKV struct {
|
|
jetstream.KeyValue
|
|
keys map[string]bool
|
|
}
|
|
|
|
func (b *boundsKV) Create(_ context.Context, key string, _ []byte, _ ...jetstream.KVCreateOpt) (uint64, error) {
|
|
b.keys[key] = true
|
|
return 1, nil
|
|
}
|
|
func (b *boundsKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error {
|
|
delete(b.keys, key)
|
|
return nil
|
|
}
|
|
|
|
// TestTheCatalogsWordsMoveTheReservations: design 47 G §369, §372 (test 102) — versions-filled opens the reserving and wakes
|
|
// the builds that wait; a built reservation arms its bound and its end raises version-unreleased naming the artifacts
|
|
// missing; the catalog's version-released deletes the key and the bound and clears the finding; builder_reserved_drop drops one.
|
|
func TestTheCatalogsWordsMoveTheReservations(t *testing.T) {
|
|
ctx := context.Background()
|
|
st := &memFindings{vals: map[string][]byte{}}
|
|
found := &findings{module: builds.BuilderModule, w: st, logf: t.Logf, now: func() time.Time { return time.Unix(0, 0) }}
|
|
bounds := &boundsKV{keys: map[string]bool{}}
|
|
woke := 0
|
|
call := func(addr string, body any) (json.RawMessage, error) {
|
|
if addr != "seat:mesh-catalog.versions" {
|
|
t.Errorf("the released versions were asked of %q", addr)
|
|
}
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m", Versions: []meshcatalog.VersionEntry{{Module: "m", Version: "1.4.2"}}})
|
|
}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return bounds }, found,
|
|
func(context.Context) { woke++ }, t.Logf)
|
|
var items []string // design 47 G A-457a: the end of a build asks its publish, the release ends it
|
|
rv.built = func(_ context.Context, m, v string) error { items = append(items, "asked "+m+" "+v); return nil }
|
|
rv.released = func(_ context.Context, m, v string) { items = append(items, "ended "+m+" "+v) }
|
|
if _, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a"}, versions.Source{}); err == nil || rv.Filled() {
|
|
t.Fatalf("a version was reserved before versions-filled (%v)", err)
|
|
}
|
|
filled := &wakeMsg{subject: meshcatalog.SubjectVersionsFilled, data: []byte(`{"v":1}`)}
|
|
if !rv.heard(ctx, filled) || !filled.acked || !rv.Filled() || woke != 1 {
|
|
t.Fatalf("versions-filled: acked %v, filled %v, woke %d", filled.acked, rv.Filled(), woke)
|
|
}
|
|
var said []meshbuilds.PackagePublished
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
|
res, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a", "m:b"}, versions.Source{Branch: "main", Revision: 41})
|
|
if err != nil || res.Version != "1.4.3" {
|
|
t.Fatalf("reserved %q (%v), want 1.4.3 after the catalog's 1.4.2", res.Version, err)
|
|
}
|
|
items = nil
|
|
if err := rv.Built(ctx, "m", "1.4.3", map[string]string{"m:a": "sha256:a", "m:b": "sha256:b"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The build's end asks the publish item once, the one mechanism (design 47 G A-457a, A-473).
|
|
if len(items) != 1 || items[0] != "asked m 1.4.3" {
|
|
t.Fatalf("the build's end asked %v, want the publish item of m 1.4.3 once (A-457a, A-473)", items)
|
|
}
|
|
boundKey := versions.ReservedBoundKey("m", "1.4")
|
|
if !bounds.keys[boundKey] {
|
|
t.Fatalf("the publish's bound is not armed: %v", bounds.keys)
|
|
}
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: "1.4.3", Digest: "sha256:a"}, nil)
|
|
// Design 47 G §52, A-57: the holder's published is said once, per artifact, as the seat mesh-builds' package-published with the
|
|
// branch and source revision of the reservation; a second published of the same artifact says nothing.
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: "1.4.3", Digest: "sha256:a"}, nil)
|
|
if len(said) != 1 || said[0].Artifact != "m:a" || said[0].Version != "1.4.3" || said[0].Digest != "sha256:a" || said[0].Branch != "main" || said[0].Source != 41 {
|
|
t.Fatalf("package-published said %+v, want one of m:a at 1.4.3 for main at source 41", said)
|
|
}
|
|
if !rv.ended(ctx, purged{key: boundKey}) {
|
|
t.Fatal("a reservation's bound was not taken in")
|
|
}
|
|
key := meshconditionsclient.Key(meshconditionsclient.ScopeModule, "m", versions.VersionUnreleased)
|
|
got, err := decodeFinding(st.vals[key])
|
|
if err != nil || !strings.Contains(got.Summary, "m:b unpublished") || strings.Contains(got.Summary, "m:a unpublished") || !strings.Contains(got.Summary, "1.4.3") {
|
|
t.Fatalf("finding = %+v (%v), want version-unreleased naming m:b of 1.4.3", got, err)
|
|
}
|
|
released := &wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"v":1,"module":"m","version":"1.4.3","source":1,"artifacts":[]}`)}
|
|
if !rv.heard(ctx, released) || !released.acked {
|
|
t.Fatalf("version-released was not taken in (acked %v)", released.acked)
|
|
}
|
|
if strings.Join(items, ", ") != "asked m 1.4.3, ended m 1.4.3" {
|
|
t.Errorf("publish items %v, want the build's end to ask the publish and the release to end it", items)
|
|
}
|
|
if all, _ := rv.r.All(ctx); len(all) != 0 || bounds.keys[boundKey] || len(st.vals) != 0 {
|
|
t.Errorf("after version-released: reservations %v, bounds %v, findings %v; want none", all, bounds.keys, st.vals)
|
|
}
|
|
if _, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a"}, versions.Source{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if answer, err := rv.dropped(ctx, "m", "1.4", "op", "never released", time.Unix(0, 0)); err != nil || !strings.Contains(answer, "dropped") {
|
|
t.Errorf("builder_reserved_drop: %q (%v)", answer, err)
|
|
}
|
|
if _, err := rv.dropped(ctx, "m", "1.4", "op", "again", time.Unix(0, 0)); err == nil {
|
|
t.Error("a reservation that is gone was dropped again")
|
|
}
|
|
}
|
|
|
|
// TestAReleasedVersionIsTaggedAtTheCommitItWasBuiltFrom: design 47 G §370 — the git tag go/vMAJOR.MINOR.PATCH is written through
|
|
// the seat git's tag on the catalog's version-released, at the commit the version's reservation was made from; a module with no
|
|
// Go package, and a version never released (a superseded build's), are tagged nowhere; a tag the forge did not write leaves
|
|
// version-released to be heard again with the reservation standing.
|
|
func TestAReleasedVersionIsTaggedAtTheCommitItWasBuiltFrom(t *testing.T) {
|
|
ctx := context.Background()
|
|
var tags []git.TagInput
|
|
refuse := ""
|
|
call := func(addr string, body any) (json.RawMessage, error) {
|
|
if addr != "seat:git.tag" {
|
|
return json.Marshal(meshcatalog.VersionsAnswer{})
|
|
}
|
|
tags = append(tags, body.(git.TagInput))
|
|
return json.Marshal(git.TagAnswer{Performed: refuse == "", Error: refuse})
|
|
}
|
|
found := &findings{module: builds.BuilderModule, w: &memFindings{vals: map[string][]byte{}}, logf: t.Logf, now: func() time.Time { return time.Unix(0, 0) }}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} }, found, nil, t.Logf)
|
|
rv.filled.Store(true)
|
|
goPkg := versions.Source{Repository: "o/r", Commit: "c0ffee", GoPackage: true}
|
|
if _, err := rv.Reserve(ctx, "pkg", "0.1", []string{"pkg:lib"}, goPkg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := rv.Reserve(ctx, "prog", "2.0", []string{"prog:bin"}, versions.Source{Repository: "o/p", Commit: "beef"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
release := func(module, version string) *wakeMsg {
|
|
m := &wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"v":1,"module":"` + module + `","version":"` + version + `","source":1,"artifacts":[]}`)}
|
|
rv.heard(ctx, m)
|
|
return m
|
|
}
|
|
if m := release("prog", "2.0.0"); !m.acked || len(tags) != 0 {
|
|
t.Fatalf("a module with no Go package: acked %v, tags %v; want none written", m.acked, tags)
|
|
}
|
|
refuse = "forge down"
|
|
if m := release("pkg", "0.1.0"); !m.naked || m.acked {
|
|
t.Fatalf("a tag the forge did not write: naked %v acked %v; want the release heard again", m.naked, m.acked)
|
|
}
|
|
if all, _ := rv.r.All(ctx); len(all) != 1 {
|
|
t.Fatalf("the reservation went with an unwritten tag: %v", all)
|
|
}
|
|
refuse = ""
|
|
tags = nil
|
|
if m := release("pkg", "0.1.0"); !m.acked || len(tags) != 1 || tags[0] != (git.TagInput{Repository: "o/r", Commit: "c0ffee", Tag: "go/v0.1.0"}) {
|
|
t.Fatalf("the release: acked %v, tags %+v; want go/v0.1.0 at c0ffee of o/r", m.acked, tags)
|
|
}
|
|
tags = nil
|
|
if _, err := rv.Reserve(ctx, "pkg", "0.1", []string{"pkg:lib"}, goPkg); err != nil { // build A: 0.1.0 (the stub catalog holds none released)
|
|
t.Fatal(err)
|
|
}
|
|
if v, err := rv.Reserve(ctx, "pkg", "0.1", []string{"pkg:lib"}, versions.Source{Repository: "o/r", Commit: "b0b", GoPackage: true}); err != nil || v.Version != "0.1.1" { // build B supersedes A
|
|
t.Fatalf("second build reserved %q (%v)", v.Version, err)
|
|
}
|
|
release("pkg", "0.1.0") // A's version is never released in fact; were it said, it is no reservation of the key
|
|
if len(tags) != 0 {
|
|
t.Fatalf("a superseded build's version was tagged: %+v", tags)
|
|
}
|
|
if release("pkg", "0.1.1"); len(tags) != 1 || tags[0].Commit != "b0b" {
|
|
t.Fatalf("the newer build's version: tags %+v; want go/v0.1.1 at b0b", tags)
|
|
}
|
|
}
|
|
|
|
// Design 47 G A-415: a published heard before the build recorded its digests is kept on its reservation, and when the build
|
|
// records them it marks its artifact and package-published is said then; a kept one whose digest is none of the built digests
|
|
// is said in the log.
|
|
func TestAPublishedHeardBeforeTheDigestsIsSaidWhenTheBuildRecordsThem(t *testing.T) {
|
|
ctx := context.Background()
|
|
fs := &memFindings{vals: map[string][]byte{}}
|
|
var logs []string
|
|
logf := func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
|
|
none := func(string, any) (json.RawMessage, error) {
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m"})
|
|
}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, none, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} },
|
|
&findings{module: builds.BuilderModule, w: fs, logf: t.Logf, now: time.Now}, nil, logf)
|
|
var said []meshbuilds.PackagePublished
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
|
rv.filled.Store(true)
|
|
res, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a", "m:b"}, versions.Source{Branch: "main", Revision: 41})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: res.Version, Digest: "sha256:a"}, nil)
|
|
rv.published(ctx, versions.Outcome{Name: "m-x", Version: res.Version, Digest: "sha256:x"}, nil)
|
|
if len(said) != 0 {
|
|
t.Fatalf("package-published said before the digests are recorded: %+v", said)
|
|
}
|
|
if err := rv.Built(ctx, "m", res.Version, map[string]string{"m:a": "sha256:a", "m:b": "sha256:b"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(said) != 1 || said[0].Artifact != "m:a" || said[0].Version != res.Version || said[0].Digest != "sha256:a" || said[0].Branch != "main" || said[0].Source != 41 {
|
|
t.Fatalf("package-published said %+v, want one of m:a at %s for main at source 41", said, res.Version)
|
|
}
|
|
if log := strings.Join(logs, "\n"); !strings.Contains(log, "is kept on") || !strings.Contains(log, "m-x@"+res.Version+" kept on m "+res.Version+" marks no artifact") {
|
|
t.Fatalf("log = %q, want the published kept and the one never matching said", logs)
|
|
}
|
|
// A-490j: the one that never matched is raised at once, naming the version taken and the holder's digest.
|
|
raised := ""
|
|
for _, v := range fs.vals {
|
|
raised += string(v)
|
|
}
|
|
if !strings.Contains(raised, "sha256:x") || !strings.Contains(raised, "is taken and is never released") {
|
|
t.Fatalf("no finding raised for the digest the holder holds: %s", raised)
|
|
}
|
|
// Taken in once: a published of m:a heard again says nothing more.
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: res.Version, Digest: "sha256:a"}, nil)
|
|
if len(said) != 1 {
|
|
t.Fatalf("package-published said again: %+v", said)
|
|
}
|
|
}
|
|
|
|
// TestABuildTakingOverAnOlderReservationDropsItsBoundAndSaysSo: design 47 G A-455 — a build that reserves while the series is
|
|
// reserved by an older build never released drops the older one's bound and finding and says so in the builder's journal; and
|
|
// H-A-225 — the older build is withdrawn, so nothing publishes under the dropped number, while a re-run of the newer keeps it.
|
|
func TestABuildTakingOverAnOlderReservationDropsItsBoundAndSaysSo(t *testing.T) {
|
|
ctx := context.Background()
|
|
st := &memFindings{vals: map[string][]byte{}}
|
|
found := &findings{module: builds.BuilderModule, w: st, logf: t.Logf, now: func() time.Time { return time.Unix(0, 0) }}
|
|
bounds := &boundsKV{keys: map[string]bool{}}
|
|
var journal []string
|
|
call := func(string, any) (json.RawMessage, error) {
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m"})
|
|
}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return bounds }, found,
|
|
nil, func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) })
|
|
rv.filled.Store(true)
|
|
var withdrawn []string
|
|
rv.withdraw = func(module, fp, why string) { withdrawn = append(withdrawn, module+" "+fp+": "+why) }
|
|
older, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: "fp-old"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := rv.Built(ctx, "m", older.Version, map[string]string{"m:a": "sha256:a"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rv.unreleased(ctx, older, "nothing within the bound")
|
|
if !bounds.keys[versions.ReservedBoundKey("m", "0.1")] || len(st.vals) != 1 {
|
|
t.Fatalf("the older reservation's bound %v or finding %v is not there", bounds.keys, st.vals)
|
|
}
|
|
newer, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: "fp-new"})
|
|
if err != nil || newer.Version != "0.1.1" {
|
|
t.Fatalf("reserved %q (%v), want 0.1.1", newer.Version, err)
|
|
}
|
|
if len(bounds.keys) != 0 || len(st.vals) != 0 {
|
|
t.Errorf("after the takeover: bounds %v, findings %v; want none", bounds.keys, st.vals)
|
|
}
|
|
if !slices.ContainsFunc(journal, func(l string) bool {
|
|
return strings.Contains(l, "0.1.0") && strings.Contains(l, "dropped") && strings.Contains(l, "0.1.1")
|
|
}) {
|
|
t.Errorf("journal %q does not say the reservation of 0.1.0 was dropped for 0.1.1", journal)
|
|
}
|
|
if got, ok, _ := rv.ReservedFor(ctx, "m", "fp-new"); !ok || got.Version != "0.1.1" {
|
|
t.Errorf("reserved for fp-new: %+v %v", got, ok)
|
|
}
|
|
if len(withdrawn) != 1 || withdrawn[0] != "m fp-old: its reservation 0.1.0 was taken over by the build of fp-new, which reserves 0.1.1" {
|
|
t.Errorf("withdrawn %q; want the build of fp-old withdrawn once", withdrawn)
|
|
}
|
|
if again, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: "fp-new"}); err != nil || again.Version != "0.1.1" || len(withdrawn) != 1 {
|
|
t.Errorf("a re-run of fp-new reserved %q (%v), withdrew %q; want 0.1.1 kept, nothing withdrawn", again.Version, err, withdrawn)
|
|
}
|
|
}
|
|
|
|
// ansKV records the value versionActed writes back to a reasks key.
|
|
type ansKV struct {
|
|
jetstream.KeyValue
|
|
wrote chan []byte
|
|
}
|
|
|
|
func (a *ansKV) Update(_ context.Context, _ string, v []byte, _ uint64) (uint64, error) {
|
|
a.wrote <- v
|
|
return 2, nil
|
|
}
|
|
|
|
type ansEntry struct {
|
|
jetstream.KeyValueEntry
|
|
key string
|
|
value []byte
|
|
}
|
|
|
|
func (e ansEntry) Key() string { return e.key }
|
|
func (e ansEntry) Revision() uint64 { return 1 }
|
|
func (e ansEntry) Value() []byte { return e.value }
|
|
func (e ansEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
|
|
|
|
// TestAReserveDoneIsAnsweredWithTheVersionAndItsQueuedPublish: design 47 G A-455 — the reasks value of builder_reserve_done reaches
|
|
// the act, which reserves for the done build and queues the publish as publish again does, and its answer names both; a
|
|
// refusal of the reserving queues nothing and is the answer; a publish that cannot be queued says the version is reserved.
|
|
func TestAReserveDoneIsAnsweredWithTheVersionAndItsQueuedPublish(t *testing.T) {
|
|
ctx := context.Background()
|
|
var queued []string
|
|
var logged []string
|
|
logf := func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }
|
|
reserve := func(_ context.Context, module, fp string) (versions.ReservedVersion, error) {
|
|
if fp == "refused" {
|
|
return versions.ReservedVersion{}, fmt.Errorf("the build key of %s at %s is not done", module, fp)
|
|
}
|
|
return versions.ReservedVersion{Module: module, Version: "0.1.13", Fingerprint: fp}, nil
|
|
}
|
|
republish := func(_ context.Context, module, version string) ([]string, error) {
|
|
if module == "nope" {
|
|
return nil, fmt.Errorf("the queue is full")
|
|
}
|
|
queued = append(queued, module+"@"+version)
|
|
return []string{module + "@" + version}, nil
|
|
}
|
|
acts := versions.Acts{ReserveDone: reserveDoneAct(reserve, republish, logf)}
|
|
for _, c := range []struct {
|
|
module, fp, want string
|
|
refused bool
|
|
}{
|
|
{"node-tools", "src2:7df8", "reserved 0.1.13 for node-tools at src2:7df8 and queued 1 versions to publish: node-tools@0.1.13", false},
|
|
{"node-tools", "refused", "not done", true},
|
|
{"nope", "src2:1", "0.1.13 is reserved for nope at src2:1, but its publish is not queued", true},
|
|
} {
|
|
value, err := json.Marshal(build.ReAsk{V: 1, Why: "w", By: "me", Mode: build.ModeReserveDone, Name: c.module, FP: c.fp})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
kv := &ansKV{wrote: make(chan []byte, 1)}
|
|
// reAsked sends a reserve-done to the version acts (b is not asked), and the answer is written back to the key.
|
|
reAsked(ctx, nil, acts, nil, kv, ansEntry{key: "reserve-done." + c.module, value: value}, logf)
|
|
var wrote []byte
|
|
select {
|
|
case wrote = <-kv.wrote:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatalf("%s at %s: no answer was written to the key", c.module, c.fp)
|
|
}
|
|
var got build.ReAsk
|
|
if err := json.Unmarshal(wrote, &got); err != nil || got.Refused != c.refused || !strings.Contains(got.Answer, c.want) || got.Answered.IsZero() {
|
|
t.Errorf("%s at %s: written %s (%v), want refused=%v containing %q", c.module, c.fp, wrote, err, c.refused, c.want)
|
|
}
|
|
}
|
|
if !slices.Equal(queued, []string{"node-tools@0.1.13"}) {
|
|
t.Errorf("queued %v, want only node-tools@0.1.13", queued)
|
|
}
|
|
if !slices.ContainsFunc(logged, func(l string) bool { return strings.Contains(l, "0.1.13 is reserved by hand") }) {
|
|
t.Errorf("the reserving by hand is not said in the log: %q", logged)
|
|
}
|
|
}
|
|
|
|
// TestARegistrysRecordEndsOrMovesAPublish: design 47 H H-A-274 — the artifacts a registry's record holds with the built digest are
|
|
// marked published from it and package-published is said, as a heard published would; H-A-275 — a done build whose number a
|
|
// registry holds at another digest (H-A-279) has its number withdrawn, raises version-held-elsewhere naming both digests, and is
|
|
// built again: at the next number no registry holds, with a fresh digest, published there; the old number's item ends.
|
|
func TestARegistrysRecordEndsOrMovesAPublish(t *testing.T) {
|
|
ctx := context.Background()
|
|
fs := &memFindings{vals: map[string][]byte{}}
|
|
none := func(string, any) (json.RawMessage, error) {
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Module: "node-tools"})
|
|
}
|
|
bounds := &boundsKV{keys: map[string]bool{}}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, none, func() jetstream.KeyValue { return bounds },
|
|
&findings{module: builds.BuilderModule, w: fs, logf: t.Logf, now: time.Now}, nil, t.Logf)
|
|
var said []meshbuilds.PackagePublished
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
|
var asked, ended []string
|
|
rv.built = func(_ context.Context, m, v string) error { asked = append(asked, m+" "+v); return nil }
|
|
rv.released = func(_ context.Context, m, v string) { ended = append(ended, m+" "+v) }
|
|
registry := []string{"0.1.37"}
|
|
rv.r.HeldNumbers = func(context.Context, string, string, []string) ([]string, error) { return registry, nil }
|
|
rv.filled.Store(true)
|
|
res, err := rv.Reserve(ctx, "node-tools", "0.1", []string{"node-tools:program"}, versions.Source{Fingerprint: "fp1"})
|
|
if err != nil || res.Version != "0.1.0" {
|
|
t.Fatalf("reserved %q (%v)", res.Version, err)
|
|
}
|
|
if err := rv.Built(ctx, "node-tools", res.Version, map[string]string{"node-tools:program": "sha256:7c9b10"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cur, _, _ := rv.r.Of(ctx, "node-tools", res.Version)
|
|
held := versions.HeldArtifact{Ref: "node-tools:program", Seat: versions.SeatArtifacts, Name: "node-tools/program", Version: res.Version, Digest: "sha256:7c9b10"}
|
|
rv.markHeld(ctx, cur, []versions.HeldArtifact{held})
|
|
if len(said) != 1 || said[0].Artifact != "node-tools:program" || said[0].Version != res.Version {
|
|
t.Fatalf("package-published said %+v, want node-tools:program at %s from the registry's record", said, res.Version)
|
|
}
|
|
if cur, _, _ = rv.r.Of(ctx, "node-tools", res.Version); len(cur.Missing()) != 0 {
|
|
t.Errorf("the record marked nothing: missing %v", cur.Missing())
|
|
}
|
|
// Another reservation of the module, done, whose number the registry holds at another digest: withdrawn, built again at the
|
|
// next free number with a fresh digest, and published there (H-A-279).
|
|
if _, err := rv.r.Released(ctx, "node-tools", res.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
asked, ended, said, registry = nil, nil, nil, []string{"0.1.0", "0.1.1", "0.1.2"}
|
|
raw, _ := json.Marshal(versions.ReservedVersion{V: 1, Module: "node-tools", Series: "0.1", Version: "0.1.1", Fingerprint: "fp2",
|
|
Artifacts: map[string]string{"node-tools:program": "sha256:7c9b10"}, BuiltAt: time.Now()})
|
|
if _, err := rv.r.Store.Create(ctx, versions.ReservedKey("node-tools", "0.1"), raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rv.rebuild = func(ctx context.Context, module, fp, _ string) error {
|
|
again, err := rv.Reserve(ctx, module, "0.1", []string{"node-tools:program"}, versions.Source{Fingerprint: fp, Rebuild: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := rv.Built(ctx, module, again.Version, map[string]string{"node-tools:program": "sha256:fresh"}, nil); err != nil {
|
|
return err
|
|
}
|
|
rv.published(ctx, versions.Outcome{Name: "node-tools/program", Version: again.Version, Digest: "sha256:fresh"}, nil)
|
|
return nil
|
|
}
|
|
cur, _, _ = rv.r.Of(ctx, "node-tools", "0.1.1")
|
|
elsewhere := held
|
|
elsewhere.Version, elsewhere.Digest = "0.1.1", "sha256:83bec1"
|
|
if err := rv.movedElsewhere(ctx, cur, versions.Judged{Elsewhere: []versions.HeldArtifact{elsewhere}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
again, ok, _ := rv.r.Of(ctx, "node-tools", "")
|
|
if !ok || again.Version != "0.1.3" || again.Withdrawn["0.1.1"] != "fp2" || again.Artifacts["node-tools:program"] != "sha256:fresh" {
|
|
t.Fatalf("built again as %+v, want 0.1.3 with a fresh digest and 0.1.1 withdrawn", again)
|
|
}
|
|
if len(said) != 1 || said[0].Version != "0.1.3" || said[0].Digest != "sha256:fresh" {
|
|
t.Errorf("package-published said %+v, want the fresh build at 0.1.3", said)
|
|
}
|
|
if len(asked) != 1 || asked[0] != "node-tools 0.1.3" || len(ended) != 1 || ended[0] != "node-tools 0.1.1" {
|
|
t.Errorf("asked %v, ended %v: want the new number's publish asked and the old one's item ended", asked, ended)
|
|
}
|
|
raised := ""
|
|
for _, v := range fs.vals {
|
|
raised += string(v)
|
|
}
|
|
if !strings.Contains(raised, versions.VersionHeldElsewhere) || !strings.Contains(raised, "sha256:83bec1") || !strings.Contains(raised, "sha256:7c9b10") {
|
|
t.Errorf("no version-held-elsewhere naming both digests: %s", raised)
|
|
}
|
|
}
|
|
|
|
// countedReserve counts the reads of reservation keys (two tokens).
|
|
type countedReserve struct {
|
|
*memReserve
|
|
reads int
|
|
}
|
|
|
|
func (c *countedReserve) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) {
|
|
if !strings.HasPrefix(key, "index.") {
|
|
c.reads++
|
|
}
|
|
return c.memReserve.Get(ctx, key)
|
|
}
|
|
|
|
// TestAPublishedThatMarksNothingIsSaidFromWhatItRead: design 47 H H-A-280 — a published whose digest no reservation holds, at a
|
|
// version many reservations share, reads only the unbuilt ones there, each once; its journal line is made from those reads with
|
|
// no second read; and the handler is told to say in progress before that pass.
|
|
func TestAPublishedThatMarksNothingIsSaidFromWhatItRead(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &countedReserve{memReserve: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}}
|
|
none := func(string, any) (json.RawMessage, error) { return json.Marshal(meshcatalog.VersionsAnswer{}) }
|
|
var logs []string
|
|
rv := newReservations(store, none, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} },
|
|
&findings{module: builds.BuilderModule, w: &memFindings{vals: map[string][]byte{}}, logf: t.Logf, now: time.Now}, nil,
|
|
func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) })
|
|
rv.filled.Store(true)
|
|
for i := range 14 {
|
|
m := fmt.Sprintf("m%02d", i)
|
|
res, err := rv.Reserve(ctx, m, "0.1", []string{m + ":a"}, versions.Source{Fingerprint: "fp" + m})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if i > 0 { // one stays unbuilt
|
|
if err := rv.Built(ctx, m, res.Version, map[string]string{m + ":a": "sha256:" + m}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
store.reads = 0
|
|
told := 0
|
|
rv.published(ctx, versions.Outcome{Name: "x", Version: "0.1.0", Digest: "sha256:none"}, func() { told++ })
|
|
if store.reads != 1 || told != 1 {
|
|
t.Errorf("%d reservation reads, in progress told %d times: want the one unbuilt read once, and in progress said before", store.reads, told)
|
|
}
|
|
if line := strings.Join(logs, "\n"); !strings.Contains(line, "kept on") {
|
|
t.Errorf("the journal says %q, want the published kept on the unbuilt reservation", line)
|
|
}
|
|
logs, store.reads = nil, 0
|
|
// A digest of a built one whose artifact is published already marks nothing: the line names it from the one read.
|
|
rv.published(ctx, versions.Outcome{Name: "m05-a", Version: "0.1.0", Digest: "sha256:m05"}, nil)
|
|
rv.published(ctx, versions.Outcome{Name: "m05-a", Version: "0.1.0", Digest: "sha256:m05"}, nil)
|
|
if store.reads != 2 || !strings.Contains(strings.Join(logs, "\n"), "m05 holds 1 built digests") || !strings.Contains(strings.Join(logs, "\n"), "14 reservations stand at it") {
|
|
t.Errorf("%d reads for two publisheds, journal %q: want one read each and the line made from it", store.reads, logs)
|
|
}
|
|
}
|