Files
mesh-delivery/modules/mesh-delivery-builder/internal/versions/versions_test.go
T
jschoubben 37e1f3c297 B3: the builder's dead readers of the indexer's packages state go
The builder reads package names from the catalog half's entries alone and never
the indexer's state (design 47 G §348): PackagesState, NamesIn, NamedModule,
Carriers and ModuleOf, which read that state and are wired nowhere, go; the
reservation's name test reads its carriers from the entries.

Builds: design 47 G §348, spec 25
2026-11-23 11:54:00 +01:00

195 lines
8.0 KiB
Go

package versions
import (
"context"
"encoding/json"
"reflect"
"strings"
"sync"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds/forms"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
)
// mem is a Store in memory; before runs once before the next Create, to stage a race.
type mem struct {
mu sync.Mutex
vals map[string][]byte
revs map[string]uint64
rev uint64
before func()
down error // answered by KeysOf: a state that cannot be read
}
func newMem() *mem { return &mem{vals: map[string][]byte{}, revs: map[string]uint64{}} }
func (m *mem) Get(_ context.Context, key string) ([]byte, uint64, bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.vals[key]
return v, m.revs[key], ok, nil
}
func (m *mem) Create(ctx context.Context, key string, value []byte) (uint64, error) {
if f := m.before; f != nil {
m.before = nil
f()
}
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.vals[key]; ok {
return 0, ErrConflict
}
m.rev++
m.vals[key], m.revs[key] = value, m.rev
return m.rev, nil
}
func (m *mem) Update(_ context.Context, key string, value []byte, revision uint64) (uint64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.revs[key] != revision {
return 0, ErrConflict
}
m.rev++
m.vals[key], m.revs[key] = value, m.rev
return m.rev, nil
}
func (m *mem) KeysOf(_ context.Context, filter string) ([]string, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.down != nil {
return nil, m.down
}
var out []string
for k := range m.vals {
if FilterMatches(filter, k) { // token by token, as the bus reads a filter
out = append(out, k)
}
}
return out, nil
}
// TestTheBuildersVersions holds design 51 *The versions*, *The floor* and *Tagged versions* (DLV-B1, DLV-B2, DLV-B3):
// one table per rule, the unit cases of the builder's numbering.
func TestTheBuildersVersions(t *testing.T) {
t.Run("keys", func(t *testing.T) {
names := []string{"@novox/mesh-sdk", "git.novox.be/novox/mesh-sdk/go", "git.novox.be/novox/mesh-sdk", "a=40b", "a@b"}
seen := map[string]string{}
for _, n := range names {
k := Key(n, "0.1.14")
if strings.Count(k, ".") != 1 {
t.Errorf("Key(%q) = %q, want two tokens", n, k)
}
if o, dup := seen[k]; dup {
t.Errorf("%q and %q escape alike: %q", n, o, k)
}
seen[k] = n
if !strings.HasPrefix(k, strings.TrimSuffix(NameFilter(n), "*")) {
t.Errorf("Key(%q) = %q is not under NameFilter %q", n, k, NameFilter(n))
}
}
})
t.Run("builder_versions answers from the reservations alone", func(t *testing.T) { // B1/AC2 (DMC-5: design 47 H §756, G §372)
asked := ""
reserve := func(name, version string) (meshbuilds.ReservedAnswer, error) {
asked = name + "@" + version
return meshbuilds.ReservedAnswer{Module: "m", Version: "0.1.4", Artifacts: []meshbuilds.ReservedAnswerArtifactsItem{
{Artifact: "m:program", Digest: "sha256:aa"}}}, nil
}
for _, c := range []struct {
version, digest string
ok bool
}{{"0.1.4", "sha256:aa", true}, {"0.1.4", "sha256:bb", false}, {"0.1.7", "sha256:aa", false}} {
a, err := AnswerVersions("m/program", c.version, c.digest, reserve)
if err != nil || a.Recorded == nil || *a.Recorded != c.ok || (!c.ok && a.Why == "") || a.Reserved == nil || asked != "m/program@"+c.version {
t.Errorf("builder_versions(%s, %s) = %+v, %v, asked %s; want recorded %v from the reservation", c.version, c.digest, a, err, asked, c.ok)
}
}
// A digest not recorded yet takes the publish (A-490j); a name the reservation does not know is a no naming it.
unbuilt := func(string, string) (meshbuilds.ReservedAnswer, error) {
return meshbuilds.ReservedAnswer{Module: "m", Version: "0.1.5", Artifacts: []meshbuilds.ReservedAnswerArtifactsItem{{Artifact: "m:program"}}}, nil
}
if a, _ := AnswerVersions("m/program", "0.1.5", "sha256:cc", unbuilt); a.Recorded == nil || !*a.Recorded {
t.Errorf("an artifact with no digest recorded yet = %+v, want recorded", a)
}
unknown := func(string, string) (meshbuilds.ReservedAnswer, error) {
return meshbuilds.ReservedAnswer{Error: "no entry carries the name x"}, nil
}
if a, _ := AnswerVersions("x", "0.1.0", "sha256:aa", unknown); a.Recorded == nil || *a.Recorded || !strings.Contains(a.Why, "no entry carries") {
t.Errorf("a name no entry carries = %+v, want not recorded naming it", a)
}
if a, err := AnswerVersions("m/program", "", "", reserve); err != nil || a.Recorded != nil || a.Reserved == nil {
t.Errorf("builder_versions with no version = %+v, %v; want the reservation alone", a, err)
}
if _, err := AnswerVersions(" ", "", "", reserve); err == nil {
t.Error("builder_versions with no name answered")
}
})
t.Run("old form", func(t *testing.T) {
// One kind, a first form and a newer one.
fs := forms.ArtifactForms{{Name: "registry", First: true, Kinds: []string{"image"}}, {Name: "oci", Kinds: []string{"image"}}}
both := []string{forms.FormNeed("oci")}
for _, c := range []struct {
name string
counted map[string][]string
dropped map[string]bool
want []string
}{
{"one node lacking the new form holds the old", map[string][]string{"a": both, "b": nil}, nil, []string{"b"}},
{"an away node counts, stating its last", map[string][]string{"a": both, "c": {"bus-state"}}, nil, []string{"c"}},
{"every counted node reads the newest: closed", map[string][]string{"a": both, "c": both}, nil, nil},
{"a retired node dropped by hand counts no more", map[string][]string{"a": both, "c": nil}, map[string]bool{"c": true}, nil},
{"the builder's own forms: no window", nil, nil, nil},
} {
if got := Holding(fs, c.counted, c.dropped); !reflect.DeepEqual(got, c.want) {
t.Errorf("%s: Holding = %v, want %v", c.name, got, c.want)
}
}
// The builder's own forms: the registry seat's form of a bundle is not a first form, so a node stating nothing
// holds the window and one stating form:seat-reference does not.
if got := Holding(forms.Current, map[string][]string{"a": nil, "b": {forms.FormNeed(forms.FormSeatReference)}}, nil); !reflect.DeepEqual(got, []string{"a"}) {
t.Errorf("forms.Current, a node stating nothing and one stating form:seat-reference: Holding = %v, want a", got)
}
t0 := time.Unix(0, 0)
named := []string{"c", "d"}
w, closed, overdue := OldFormWindow{}.Step(named, []string{"c"}, t0)
if closed || len(overdue) != 0 || !w.Open {
t.Fatalf("Step = %+v %v %v", w, closed, overdue)
}
w, _, overdue = w.Step(named, []string{"c"}, t0.Add(FindingAfter))
if !reflect.DeepEqual(overdue, []string{"c"}) {
t.Errorf("seven days held: overdue %v, want c", overdue)
}
w, err := w.Drop("d", "op@shanks", "retired", t0)
if err != nil || !w.DroppedSet()["d"] {
t.Fatalf("Drop = %+v, %v", w, err)
}
if again, _ := w.Drop("d", "op@shanks", "again", t0); len(again.Dropped) != 1 {
t.Errorf("a node dropped twice is kept twice: %+v", again.Dropped)
}
if _, err := w.Drop("e", "op@shanks", "", t0); err == nil {
t.Error("a drop with no why is taken")
}
// Security-upgrade F3 (review round 1), by mesh-sdk's window.StepWindow: a read naming no node (an empty
// node_haves, the assignments not read yet) never closes the window, and its holder's seven days still count.
if held, shut, late := w.Step(nil, nil, t0.Add(FindingAfter+time.Minute)); shut || !held.Open || !reflect.DeepEqual(late, []string{"c"}) {
t.Fatalf("a read naming no node: %+v closed=%v overdue %v, want the window open and c overdue", held, shut, late)
}
w, closed, _ = w.Step(named, nil, t0.Add(FindingAfter+time.Hour))
if !closed || w.Open || !reflect.DeepEqual(w.LastHolding, []string{"c"}) || w.Closing().Dropped[0].Node != "d" {
t.Errorf("closing: %+v closed=%v", w, closed)
}
raw, _ := json.Marshal(w)
if back, err := DecodeOldFormWindow(raw); err != nil || !back.Closed.Equal(w.Closed) {
t.Errorf("the window read back = %+v, %v", back, err)
}
if _, again, _ := w.Step(named, nil, t0.Add(FindingAfter+2*time.Hour)); again {
t.Error("a closed window closes again: old-form-closed would be said twice")
}
})
}