A number whose tag stood at another commit was reserved again, and a Go package outside mesh-sdk's go directory was tagged go/v<version>, which the Go toolchain never reads. The builder now reads the Go package's tags once per catalog revision, skips a number tagged at another commit with the finding version-tagged-elsewhere (cleared at the next release), and tags <directory>/v<version>, the directory read from the package's module path. Builds: design 47 H2 §796
478 lines
24 KiB
Go
478 lines
24 KiB
Go
package versions
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
type reserveMem struct {
|
|
*mem
|
|
onUpdate func() // runs once before the next Update
|
|
}
|
|
|
|
func (m *reserveMem) Delete(_ context.Context, key string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.vals, key)
|
|
delete(m.revs, key)
|
|
return nil
|
|
}
|
|
|
|
func (m *reserveMem) Update(ctx context.Context, key string, value []byte, rev uint64) (uint64, error) {
|
|
if f := m.onUpdate; f != nil {
|
|
m.onUpdate = nil
|
|
f()
|
|
}
|
|
return m.mem.Update(ctx, key, value, rev)
|
|
}
|
|
|
|
// TestReservation: design 47 G §369 (test 102): two builds close together reserve two versions, the key goes on
|
|
// version-released, a built reservation with an artifact unpublished past the bound is unreleased, and nothing is reserved
|
|
// before versions-filled.
|
|
func TestReservation(t *testing.T) {
|
|
ctx := context.Background()
|
|
now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
|
|
released := []string{"0.1.18", "0.1.19", "0.2.7"}
|
|
filled := true
|
|
store := &reserveMem{mem: newMem()}
|
|
r := &Reserver{Store: store, Filled: func() bool { return filled }, Now: func() time.Time { return now },
|
|
ReleasedOf: func(context.Context, string) ([]string, error) { return released, nil }}
|
|
arts := []string{"m:a", "m:b"}
|
|
|
|
filled = false
|
|
if _, err := r.Reserve(ctx, "m", "0.1", arts, Source{}); !errors.Is(err, ErrNotFilled) {
|
|
t.Fatalf("before versions-filled: %v, want ErrNotFilled", err)
|
|
}
|
|
if got, _ := r.All(ctx); len(got) != 0 {
|
|
t.Fatalf("a reservation was written before the fill: %v", got)
|
|
}
|
|
filled = true
|
|
|
|
first, err := r.Reserve(ctx, "m", "0.1", arts, Source{})
|
|
if err != nil || first.Version != "0.1.20" {
|
|
t.Fatalf("first build reserves %q (%v), want 0.1.20 after the highest released 0.1.19", first.Version, err)
|
|
}
|
|
var tookOlder, tookNewer []string
|
|
r.Took = func(_ context.Context, older, newer ReservedVersion) {
|
|
tookOlder, tookNewer = append(tookOlder, older.Version), append(tookNewer, newer.Version)
|
|
}
|
|
second, err := r.Reserve(ctx, "m", "0.1", arts, Source{})
|
|
if len(tookOlder) != 1 || tookOlder[0] != "0.1.20" || tookNewer[0] != "0.1.21" {
|
|
t.Fatalf("takeover of the older reservation told %v by %v, want 0.1.20 by 0.1.21 (design 47 G A-455)", tookOlder, tookNewer)
|
|
}
|
|
if err != nil || second.Version != "0.1.21" {
|
|
t.Fatalf("second build reserves %q (%v), want 0.1.21: never the first's", second.Version, err)
|
|
}
|
|
// Two builds at once: the rival reserves between this one's read and write, and this one takes the next.
|
|
store.onUpdate = func() {
|
|
if _, err := r.Reserve(ctx, "m", "0.1", arts, Source{}); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
third, err := r.Reserve(ctx, "m", "0.1", arts, Source{})
|
|
if err != nil || third.Version != "0.1.23" {
|
|
t.Fatalf("a build that lost the compare-and-set reserves %q (%v), want 0.1.23", third.Version, err)
|
|
}
|
|
if v, _ := r.Reserve(ctx, "m", "0.3", arts, Source{}); v.Version != "0.3.0" {
|
|
t.Errorf("a series with nothing released reserves %q, want 0.3.0", v.Version)
|
|
}
|
|
|
|
// The artifacts' digests, then the bound.
|
|
for _, a := range arts {
|
|
if ok, err := r.Built(ctx, "m", "0.1.23", a, "sha256:"+a, nil); err != nil || !ok {
|
|
t.Fatalf("built %s: %v %v", a, ok, err)
|
|
}
|
|
}
|
|
if ok, _ := r.Built(ctx, "m", "0.1.22", "m:a", "x", nil); ok {
|
|
t.Error("a digest was written under a version a newer build superseded")
|
|
}
|
|
published := func(_, _, a string) bool { return a == "m:a" }
|
|
if got, _ := r.Overdue(ctx, published); len(got) != 0 {
|
|
t.Fatalf("unreleased before the bound: %v", got)
|
|
}
|
|
now = now.Add(UnreleasedBound)
|
|
got, err := r.Overdue(ctx, published)
|
|
want := []Unreleased{{Module: "m", Version: "0.1.23", Missing: []string{"m:b"}}}
|
|
if err != nil || !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("past the bound: %v (%v), want %v", got, err, want)
|
|
}
|
|
|
|
// version-released: its key goes, and the other series' keys of the module (the manifest moved on).
|
|
if gone, err := r.Released(ctx, "m", "0.1.23"); err != nil || len(gone) != 2 {
|
|
t.Fatalf("released 0.1.23 deleted %v (%v), want the 0.1 key and the 0.3 key", gone, err)
|
|
}
|
|
if all, _ := r.All(ctx); len(all) != 0 {
|
|
t.Errorf("reservations left: %v", all)
|
|
}
|
|
if again, _ := r.Reserve(ctx, "m", "0.1", arts, Source{}); again.Version != "0.1.20" {
|
|
t.Logf("with the catalog list unchanged the next is %s", again.Version)
|
|
}
|
|
if ok, _ := r.Drop(ctx, "m", "0.1"); !ok {
|
|
t.Error("builder_reserved_drop found no reservation")
|
|
}
|
|
}
|
|
|
|
// TestReservationIsReadAndMarkedPublished: design 47 G §369 — the reservation is read by builder_reserved and by the seat's
|
|
// verb reserved with each artifact's digest, a registry seat's published marks the artifacts of that version and digest, and
|
|
// the missing artifacts it names are the unpublished ones.
|
|
func TestReservationIsReadAndMarkedPublished(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &reserveMem{mem: newMem()}
|
|
r := &Reserver{Store: store, ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"1.4.2"}, nil }}
|
|
v, err := r.Reserve(ctx, "m", "1.4", []string{"m:a", "m:b"}, Source{Branch: "main", Revision: 41, CatalogRevision: 44})
|
|
if err != nil || v.Version != "1.4.3" {
|
|
t.Fatalf("reserved %q (%v), want 1.4.3", v.Version, err)
|
|
}
|
|
deps := []meshbuilds.Dependency{{Kind: 1, Part: "mesh-sdk:go", Version: "0.1.14", Digest: "sha256:dd"}, {Kind: 3, Part: "go-toolchain:program", Version: "go1.27", Digest: "sha256:tt"}}
|
|
if _, err := r.Built(ctx, "m", "1.4.3", "m:a", "sha256:aa", deps); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rd := Reader{
|
|
Keys: func() ([]string, error) { return store.KeysOf(ctx, AllFilter) },
|
|
Get: func(key string) (json.RawMessage, bool, error) {
|
|
raw, _, ok, err := store.Get(ctx, key)
|
|
return raw, ok, err
|
|
}}
|
|
ans, err := AnswerReservedVerb(rd, "m", "")
|
|
want := meshbuilds.ReservedAnswer{Module: "m", Version: "1.4.3", Artifacts: []meshbuilds.ReservedAnswerArtifactsItem{{Artifact: "m:a", Digest: "sha256:aa"}, {Artifact: "m:b"}}}
|
|
if err != nil || !reflect.DeepEqual(ans, want) {
|
|
t.Fatalf("reserved answers %+v (%v), want %+v", ans, err, want)
|
|
}
|
|
if _, err := AnswerReservedVerb(rd, "m", "1.4.9"); err == nil {
|
|
t.Error("a version never reserved was answered")
|
|
}
|
|
if got, err := AnswerReserved(rd, "other"); err != nil || len(got["reserved"].([]ReservedVersion)) != 0 {
|
|
t.Errorf("builder_reserved of another module = %v (%v), want none", got, err)
|
|
}
|
|
marked, _, err := r.Published(ctx, "m-a", "v1.4.3", "sha256:aa")
|
|
if err != nil || len(marked) != 1 {
|
|
t.Fatalf("a published of 1.4.3 with a built digest marked %v (%v)", marked, err)
|
|
}
|
|
// Design 47 G §52, §673, A-57: the package-published of that artifact carries the digest published, the branch, the source
|
|
// revision and the catalog revision (H-A-218) the reservation was made for, and the dependencies its step result named.
|
|
at := time.Unix(100, 0)
|
|
said := marked[0].PackagePublished("sha256:aa", at)
|
|
atUTC := at.UTC()
|
|
if wantSaid := (meshbuilds.PackagePublished{V: 1, Artifact: "m:a", At: &atUTC, Branch: "main", Source: 41, Revision: 44, Module: "m", Version: "1.4.3", Digest: "sha256:aa", Dependencies: deps}); !reflect.DeepEqual(said, wantSaid) {
|
|
t.Errorf("package-published = %+v, want %+v", said, wantSaid)
|
|
}
|
|
if marked, _, _ := r.Published(ctx, "m-a", "1.4.3", "sha256:other"); len(marked) != 0 {
|
|
t.Errorf("a published with another digest marked %v", marked)
|
|
}
|
|
cur, _, _ := r.At(ctx, ReservedKey("m", "1.4"))
|
|
if got := cur.Missing(); !reflect.DeepEqual(got, []string{"m:b"}) {
|
|
t.Errorf("missing = %v, want m:b", got)
|
|
}
|
|
if key, ok := ReservedBoundOf(ReservedBoundKey("m", "1.4")); !ok || key != ReservedKey("m", "1.4") || ReservedBoundFor(key) != ReservedBoundKey("m", "1.4") {
|
|
t.Errorf("the bound key does not name the reservation back: %q %v", key, ok)
|
|
}
|
|
}
|
|
|
|
// TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes: design 47 G A-456a — the version-released of a version records
|
|
// that the build of its fingerprint was released, so a rerun of its step finds it after the reservation is deleted.
|
|
func TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes(t *testing.T) {
|
|
ctx := context.Background()
|
|
rel := &reserveMem{mem: newMem()}
|
|
r := &Reserver{Store: &reserveMem{mem: newMem()}, Releases: rel, ReleasedKept: 2, Filled: func() bool { return true },
|
|
ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }}
|
|
v, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "src2:aa"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, _ := r.ReleasedFor(ctx, "m", "src2:aa"); ok {
|
|
t.Fatal("a build not released is said released")
|
|
}
|
|
if _, err := r.Released(ctx, "m", v.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, err := r.ReleasedFor(ctx, "m", "src2:aa"); !ok || err != nil {
|
|
t.Errorf("released for the fingerprint: %v (%v), want true", ok, err)
|
|
}
|
|
if ok, _ := r.ReleasedFor(ctx, "m", "src2:bb"); ok {
|
|
t.Error("another fingerprint is said released")
|
|
}
|
|
if all, _ := r.All(ctx); len(all) != 0 {
|
|
t.Errorf("reservations left: %v", all)
|
|
}
|
|
// One key per module holds the last releases, the oldest dropped (A-456b).
|
|
for _, fp := range []string{"src2:b1", "src2:b2"} {
|
|
n, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: fp})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := r.Released(ctx, "m", n.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if keys, _ := rel.KeysOf(ctx, AllFilter); len(keys) != 1 {
|
|
t.Errorf("release keys %v, want one per module", keys)
|
|
}
|
|
if old, _ := r.ReleasedFor(ctx, "m", "src2:aa"); old {
|
|
t.Error("the oldest release is kept past released-kept")
|
|
}
|
|
if ok, _ := r.ReleasedFor(ctx, "m", "src2:b2"); !ok {
|
|
t.Error("the newest release is not kept")
|
|
}
|
|
}
|
|
|
|
// TestPublishedAnswersByTheBuildFingerprint: design 47 H-A-328 — the verb published answers published for a reservation of the
|
|
// fingerprint whose artifact a registry's record marked published, with its version and digest, and for a release of it; an
|
|
// artifact not yet marked, and another fingerprint, are not published.
|
|
func TestPublishedAnswersByTheBuildFingerprint(t *testing.T) {
|
|
ctx := context.Background()
|
|
heard := time.Date(2026, 11, 21, 19, 0, 0, 0, time.UTC)
|
|
store := &reserveMem{mem: newMem()}
|
|
r := &Reserver{Store: store, Releases: &reserveMem{mem: newMem()}, ReleasedKept: 2, Filled: func() bool { return true },
|
|
ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }, Now: func() time.Time { return heard }}
|
|
v, err := r.Reserve(ctx, "mesh-grants", "0.1", []string{"mesh-grants:busgrants", "mesh-grants:grantsstate"}, Source{Fingerprint: "src2:aa"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := r.Built(ctx, "mesh-grants", v.Version, "mesh-grants:busgrants", "sha256:bb", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ask := func(artifact, fp string) meshbuilds.PublishedAnswer {
|
|
t.Helper()
|
|
a, err := r.PublishedOf(ctx, "mesh-grants", artifact, fp)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return a
|
|
}
|
|
if a := ask("mesh-grants:busgrants", "src2:aa"); a.Published {
|
|
t.Errorf("a built artifact no registry holds is answered %+v, want not published", a)
|
|
}
|
|
if marked, _, err := r.Published(ctx, "mesh-grants-busgrants", v.Version, "sha256:bb"); err != nil || len(marked) != 1 {
|
|
t.Fatalf("the registry's published marked %v (%v)", marked, err)
|
|
}
|
|
// H-A-331: the mark keeps when the publish was heard, answered as at.
|
|
if a := ask("mesh-grants:busgrants", "src2:aa"); !a.Published || a.Version != v.Version || a.Digest != "sha256:bb" || a.At == nil || !a.At.Equal(heard) {
|
|
t.Errorf("published answered %+v, want %s at sha256:bb, heard %s", a, v.Version, heard)
|
|
}
|
|
// A mark written before the publish time was kept answers no time, never an invented one; a withdrawn number is never
|
|
// answered published.
|
|
key := ReservedKey("mesh-grants", "0.1")
|
|
cur, rev, _, _ := r.get(ctx, key)
|
|
older := cur
|
|
older.PublishedAt = nil
|
|
raw, _ := json.Marshal(older)
|
|
if rev, err = store.Update(ctx, key, raw, rev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a := ask("mesh-grants:busgrants", "src2:aa"); !a.Published || a.At != nil {
|
|
t.Errorf("an older mark answered %+v, want published with no at", a)
|
|
}
|
|
withdrawn := older
|
|
withdrawn.Withdrawn = map[string]string{v.Version: "src2:aa"}
|
|
raw, _ = json.Marshal(withdrawn)
|
|
if rev, err = store.Update(ctx, key, raw, rev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a := ask("mesh-grants:busgrants", "src2:aa"); a.Published {
|
|
t.Errorf("a withdrawn number is answered %+v, want not published", a)
|
|
}
|
|
raw, _ = json.Marshal(cur)
|
|
if _, err = store.Update(ctx, key, raw, rev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a := ask("mesh-grants:grantsstate", "src2:aa"); a.Published {
|
|
t.Errorf("an artifact not marked is answered %+v", a)
|
|
}
|
|
if a := ask("mesh-grants:busgrants", "src2:other"); a.Published {
|
|
t.Errorf("another fingerprint is answered %+v", a)
|
|
}
|
|
if _, err := r.Released(ctx, "mesh-grants", v.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a := ask("mesh-grants:grantsstate", "src2:aa"); !a.Published || a.Version != v.Version {
|
|
t.Errorf("after its release the fingerprint's artifact is answered %+v, want published at %s", a, v.Version)
|
|
}
|
|
// H-A-331: the kept release keeps each published digest with its version.
|
|
if a := ask("mesh-grants:busgrants", "src2:aa"); !a.Published || a.Digest != "sha256:bb" {
|
|
t.Errorf("after its release busgrants is answered %+v, want its digest sha256:bb", a)
|
|
}
|
|
if _, err := r.PublishedOf(ctx, "mesh-grants", "", "src2:aa"); err == nil {
|
|
t.Error("an ask naming no artifact is answered")
|
|
}
|
|
}
|
|
|
|
// TestTheWithdrawnVersionsAreDroppedOnceReleasedPast: design 47 H H-A-225, H-A-226, H-A-244 — a dropped number stays a gap, kept in
|
|
// the series key's Withdrawn while it is above the catalog's highest released; at each write those at or below it go, so the
|
|
// map never grows without end.
|
|
func TestTheWithdrawnVersionsAreDroppedOnceReleasedPast(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &reserveMem{mem: newMem()}
|
|
highest := "0.1.3"
|
|
r := &Reserver{Store: store, Filled: func() bool { return true },
|
|
ReleasedOf: func(context.Context, string) ([]string, error) { return []string{highest}, nil }}
|
|
for _, fp := range []string{"src2:a", "src2:b", "src2:c"} {
|
|
if _, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: fp}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cur, _, _, _ := r.get(ctx, ReservedKey("m", "0.1"))
|
|
if cur.Version != "0.1.6" || len(cur.Withdrawn) != 2 || cur.Withdrawn["0.1.4"] != "src2:a" || cur.Withdrawn["0.1.5"] != "src2:b" {
|
|
t.Fatalf("reservation %+v; want 0.1.6 with 0.1.4 and 0.1.5 withdrawn", cur)
|
|
}
|
|
// H-A-244: the build whose number was withdrawn finds the reservation its series moved to; the one standing finds none.
|
|
if by, took, err := r.TakenOver(ctx, "m", "src2:a"); err != nil || !took || by.Fingerprint != "src2:c" || by.Version != "0.1.6" {
|
|
t.Errorf("taken over from src2:a by %+v (%v, %v); want src2:c 0.1.6", by, took, err)
|
|
}
|
|
if _, took, err := r.TakenOver(ctx, "m", "src2:c"); err != nil || took {
|
|
t.Errorf("the standing build src2:c is said taken over (%v, %v)", took, err)
|
|
}
|
|
highest = "0.1.5" // the catalog released past both withdrawn numbers but the newest
|
|
got, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "src2:d"})
|
|
if err != nil || got.Version != "0.1.7" || len(got.Withdrawn) != 1 || got.Withdrawn["0.1.6"] != "src2:c" {
|
|
t.Errorf("reserved %+v (%v); want 0.1.7 keeping only 0.1.6 withdrawn", got, err)
|
|
}
|
|
}
|
|
|
|
// TestARebuildReservesAboveTheCatalogsHighestWhateverItsStandingNumberWas: design 47 G A-490b as H-A-225 amends it — a build again
|
|
// of a done key, or a re-run after a restart, keeps its fingerprint's standing reservation while it is unreleased and above the
|
|
// catalog's highest; one at or below it is replaced (Took told) by the next above the catalog's highest alone; another
|
|
// fingerprint's standing reservation stays a floor and is withdrawn.
|
|
func TestARebuildReservesAboveTheCatalogsHighestWhateverItsStandingNumberWas(t *testing.T) {
|
|
ctx := context.Background()
|
|
for _, c := range []struct {
|
|
name, standing, standingFp string
|
|
want string
|
|
rebuild, kept bool
|
|
}{
|
|
{name: "standing below the catalog's highest", standing: "0.1.0", standingFp: "fp", want: "0.1.17", rebuild: true},
|
|
{name: "H-A-225: standing above the catalog's highest is kept by a build again", standing: "0.1.30", standingFp: "fp", want: "0.1.30", rebuild: true, kept: true},
|
|
{name: "H-A-225: a re-run after a restart keeps its standing number", standing: "0.1.111", standingFp: "fp", want: "0.1.111", kept: true},
|
|
{name: "another fingerprint's standing number stays a floor", standing: "0.1.30", standingFp: "other", want: "0.1.31", rebuild: true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
store := &reserveMem{mem: newMem()}
|
|
r := &Reserver{Store: store, Filled: func() bool { return true },
|
|
ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"0.1.16"}, nil }}
|
|
raw, _ := json.Marshal(ReservedVersion{V: 1, Module: "m", Series: "0.1", Version: c.standing, Fingerprint: c.standingFp, Artifacts: map[string]string{"m:a": "d"}})
|
|
if _, err := store.Create(ctx, ReservedKey("m", "0.1"), raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var took []string
|
|
r.Took = func(_ context.Context, older, newer ReservedVersion) {
|
|
took = append(took, older.Version+">"+newer.Version)
|
|
}
|
|
got, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "fp", Rebuild: c.rebuild})
|
|
if c.kept {
|
|
if err != nil || got.Version != c.want || len(took) != 0 || got.Artifacts["m:a"] != "d" {
|
|
t.Fatalf("reserved %+v (%v), took %v; want the standing %s kept as it stands", got, err, took, c.want)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || got.Version != c.want || len(took) != 1 || took[0] != c.standing+">"+c.want {
|
|
t.Fatalf("reserved %q (%v), took %v; want %s replacing %s", got.Version, err, took, c.want, c.standing)
|
|
}
|
|
if w := got.Withdrawn[c.standing]; (c.standingFp != "fp") != (w == c.standingFp) {
|
|
t.Errorf("withdrawn %v; want %s withdrawn only when another fingerprint stood", got.Withdrawn, c.standing)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestANumberARegistryHoldsIsNeverReserved: design 47 H H-A-275 (the rule of H-A-245 for a registry's record) — a reserve skips
|
|
// every number a registry seat holds of the module's artifacts and says the skipped ones; a read that fails lets it proceed; a
|
|
// done build whose number a registry holds at another digest has it withdrawn as a gap (H-A-226), and its build again reserves the
|
|
// next free number with fresh digests (H-A-279), the index following it.
|
|
func TestANumberARegistryHoldsIsNeverReserved(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &reserveMem{mem: newMem()}
|
|
held := []string{"0.1.37", "0.1.38", "0.2.0"}
|
|
var readErr error
|
|
var skipped []string
|
|
r := &Reserver{Store: store, ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"0.1.36"}, nil },
|
|
HeldNumbers: func(_ context.Context, module, fp string, refs []string) ([]string, error) {
|
|
if module != "node-tools" || fp != "fp1" || len(refs) != 1 {
|
|
t.Errorf("the registries are read for %s %s %v", module, fp, refs)
|
|
}
|
|
return held, readErr
|
|
},
|
|
HeldElsewhere: func(_ context.Context, _ string, s []string) { skipped = append(skipped, s...) }}
|
|
v, err := r.Reserve(ctx, "node-tools", "0.1", []string{"node-tools:program"}, Source{Fingerprint: "fp1", GoPackage: true})
|
|
if err != nil || v.Version != "0.1.39" || len(skipped) != 2 {
|
|
t.Fatalf("reserved %q (%v), skipped %v: want 0.1.39 past the two numbers the registry holds", v.Version, err, skipped)
|
|
}
|
|
// The build ends; the registry holds 0.1.39 at another digest meanwhile: the number is withdrawn, and the build again of the same
|
|
// fingerprint reserves the next number no registry holds, with no digest yet (H-A-279).
|
|
if _, err := r.Built(ctx, "node-tools", "0.1.39", "node-tools:program", "sha256:7c9b10", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w, ok, err := r.Withdraw(ctx, "node-tools", "0.1.39"); err != nil || !ok || w.Withdrawn["0.1.39"] != "fp1" {
|
|
t.Fatalf("withdrew %+v (%v %v), want 0.1.39 withdrawn for fp1", w, ok, err)
|
|
}
|
|
held = []string{"0.1.37", "0.1.38", "0.1.39", "0.1.40", "0.2.0"}
|
|
// The build again of a done key reserves as a rebuild (A-490b): a withdrawn number is never its floor's to reuse.
|
|
again, err := r.Reserve(ctx, "node-tools", "0.1", []string{"node-tools:program"}, Source{Fingerprint: "fp1", GoPackage: true, Rebuild: true})
|
|
if err != nil || again.Version != "0.1.41" || again.Artifacts["node-tools:program"] != "" || again.Withdrawn["0.1.39"] != "fp1" || again.Tag != "" {
|
|
t.Fatalf("the build again reserved %+v (%v), want 0.1.41 with no digest, 0.1.39 kept withdrawn, and no tag (no Go package name known)", again, err)
|
|
}
|
|
if at, _ := r.AtVersion(ctx, "0.1.39"); len(at) != 0 {
|
|
t.Errorf("the index by version still names the withdrawn number: %v", at)
|
|
}
|
|
if _, ok, _ := r.Withdraw(ctx, "node-tools", "0.1.39"); ok {
|
|
t.Error("a number no reservation stands at any more was withdrawn")
|
|
}
|
|
// A registry that cannot be read lets the reserve proceed.
|
|
readErr, skipped = errors.New("registry down"), nil
|
|
if v, err := r.Reserve(ctx, "node-tools", "0.2", []string{"node-tools:program"}, Source{Fingerprint: "fp1"}); err != nil || v.Version != "0.2.1" {
|
|
t.Errorf("with the registry down reserved %q (%v), want 0.2.1 (what it answered still skipped)", v.Version, err)
|
|
}
|
|
// Design 47 H2 §796: a Go package's tags are read before it reserves, once per catalog revision; a number whose tag
|
|
// `<directory>/v<version>` stands at another commit is skipped and said, one at the build's own commit is not; the version's
|
|
// tag names the Go module's directory; a read met away lets the reservation go on, any other failure refuses it.
|
|
r.HeldNumbers = nil
|
|
var tagReads int
|
|
var tagsErr error
|
|
var tagsAway bool
|
|
var elsewhere []string
|
|
tags := []git.Tag{{Tag: "modules/nt/v0.3.0", Commit: "c-other"}, {Tag: "modules/nt/v0.3.1", Commit: "c-mine"}, {Tag: "modules/nt/v0.3.2", Commit: "c-other2"}}
|
|
r.Names = func(_ context.Context, module, artifact string) (string, bool, error) {
|
|
return "git.example/o/r/modules/nt", module == "node-tools" && artifact == "lib", nil
|
|
}
|
|
r.Tags = func(_ context.Context, repository, prefix string) ([]git.Tag, bool, error) {
|
|
tagReads++
|
|
if repository != "o/r" || prefix != "modules/nt/v0.3." {
|
|
t.Errorf("tags read of %s %q, want o/r modules/nt/v0.3.", repository, prefix)
|
|
}
|
|
return tags, tagsAway, tagsErr
|
|
}
|
|
r.TaggedElsewhere = func(_ context.Context, _ string, s []string) { elsewhere = append(elsewhere, s...) }
|
|
at := Source{Fingerprint: "fp3", GoPackage: true, GoArtifact: "lib", Repository: "o/r", Commit: "c-mine", Branch: "main", CatalogRevision: 7}
|
|
v3, err := r.Reserve(ctx, "node-tools", "0.3", []string{"node-tools:lib"}, at)
|
|
if err != nil || v3.Version != "0.3.1" || v3.Tag != "modules/nt/v0.3.1" || len(elsewhere) != 1 || !strings.Contains(elsewhere[0], "0.3.0") {
|
|
t.Fatalf("reserved %+v (%v), tagged elsewhere %v: want 0.3.1 (0.3.0 tagged at another commit, 0.3.1 at the build's own), tag modules/nt/v0.3.1", v3, err, elsewhere)
|
|
}
|
|
at.Fingerprint, at.Commit, elsewhere = "fp4", "c-new", nil
|
|
if v4, err := r.Reserve(ctx, "node-tools", "0.3", []string{"node-tools:lib"}, at); err != nil || v4.Version != "0.3.3" || tagReads != 1 || len(elsewhere) != 1 || !strings.Contains(elsewhere[0], "0.3.2") {
|
|
t.Fatalf("reserved %+v (%v) after %d reads, tagged elsewhere %v: want 0.3.3 past 0.3.2's tag, the tags read once for revision main.7", v4, err, tagReads, elsewhere)
|
|
}
|
|
at.CatalogRevision, at.Fingerprint, tagsErr, tagsAway = 8, "fp5", errors.New("the forge is away"), true
|
|
if v5, err := r.Reserve(ctx, "node-tools", "0.3", []string{"node-tools:lib"}, at); err != nil || v5.Version != "0.3.4" || tagReads != 2 {
|
|
t.Fatalf("with the tags away reserved %+v (%v) after %d reads, want 0.3.4 and the reservation going on", v5, err, tagReads)
|
|
}
|
|
at.CatalogRevision, at.Fingerprint, tagsAway = 9, "fp6", false
|
|
if _, err := r.Reserve(ctx, "node-tools", "0.3", []string{"node-tools:lib"}, at); err == nil || !strings.Contains(err.Error(), "tags") {
|
|
t.Errorf("a tags read refused not away reserved anyway (%v), want the reservation refused naming the tags", err)
|
|
}
|
|
if d, ok := GoDirectory("git.example/novox/mesh-sdk/go", "https://git.example/novox/mesh-sdk.git"); !ok || d != "go" || GoTagOf(d, "0.1.9") != "go/v0.1.9" {
|
|
t.Errorf("mesh-sdk's Go package: directory %q (%v), want go and the tag go/v0.1.9", d, ok)
|
|
}
|
|
if d, ok := GoDirectory("git.example/o/r", "o/r"); !ok || d != "" || GoTagOf(d, "1.0.0") != "v1.0.0" {
|
|
t.Errorf("a root Go module: directory %q (%v), want the root and the tag v1.0.0", d, ok)
|
|
}
|
|
if _, ok := GoDirectory("git.example/o/rr/x", "o/r"); ok {
|
|
t.Error("a module path of another repository was given a directory")
|
|
}
|
|
}
|