Files
mesh-delivery/modules/mesh-delivery-builder/internal/build/buildread_test.go
T
jschoubben fcac1fc5d0 H-A-487: prove a listing reads no history when its entries carry no time
Review round 1 found the fingerprint guard of the build read unproven: every
entry of the 100-key listing carried a time. The listing is now also read with
entries that say none, and no history may be read.

Builds: design 47 H2 H-A-487 (review round 1 finding)
2026-11-22 19:37:22 +01:00

110 lines
5.7 KiB
Go

package build
import (
"encoding/json"
"fmt"
"sort"
"strings"
"testing"
"time"
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
)
const fp2 = "src2:2222222222222222222222222222222222222222222222222222222222222222"
func stateOf(vals map[string][]byte) StateRead {
return StateRead{
Keys: func() ([]string, error) {
var ks []string
for k := range vals {
ks = append(ks, k)
}
sort.Strings(ks)
return ks, nil
},
Get: func(k string) (json.RawMessage, time.Time, error) { return vals[k], time.Time{}, nil }}
}
// Design 47 G §589: builder_build by module answers its keys newest first with whole fingerprints, event-builds and the open
// ask, and names the watched branches whose newest entry names a key's fingerprint; a module none of whose keys the newest
// entry names says so; with a fingerprint, that key alone.
func TestBuilderBuildAnswersAModulesKeysWhole(t *testing.T) {
failed, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildFailed, EventBuilds: 3, Manifest: mdigest('a'),
Error: "transport fault: connection, node-build-go ace, dial tcp: connection refused"})
done, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, Manifest: mdigest('a'), Artifacts: []builds.BuiltArtifact{{Name: "x", Kind: "file", Digest: "sha256:" + strings.Repeat("a", 64)}}})
k1, _ := builds.BuilderBuildKey("m", fp1)
k2, _ := builds.BuilderBuildKey("m", fp2)
other, _ := builds.BuilderBuildKey("other", fp1)
vals := map[string][]byte{k1: failed, k2: done, other: done}
ask := occ.Kept{Ask: oc.Ask{ID: "retake-1", About: k1, Expires: occ.FormatTime(time.Now().Add(time.Hour))}, Asked: time.Now()}
askRaw, _ := json.Marshal(ask)
r := &BuildReader{Builds: stateOf(vals), Asked: stateOf(map[string][]byte{"ask.retake-1": askRaw}),
Newest: func(string) (map[string]string, error) { return map[string]string{"main": fp1, "feature": fp2}, nil }}
a, err := r.Build("m", "")
if err != nil || len(a.Builds) != 2 {
t.Fatalf("builds = %+v, %v, want the module's two keys", a, err)
}
var row BuildRow
for _, b := range a.Builds {
if b.Fingerprint == fp1 {
row = b
}
}
if row.Fingerprint != fp1 || len(row.Fingerprint) != len("src2:")+64 || row.State != builds.BuildFailed || row.EventBuilds != 3 || row.Node != "ace" ||
!strings.HasPrefix(row.Error, "transport fault: connection, node-build-go ace, dial tcp") || row.Ask == nil || row.Ask.ID != "retake-1" || len(row.Newest) != 1 || row.Newest[0] != "main" {
t.Errorf("the failed key = %+v, want its fingerprint whole, its error whole, event-builds 3, the open ask and newest main", row)
}
if one, err := r.Build("m", fp2); err != nil || len(one.Builds) != 1 || one.Builds[0].Fingerprint != fp2 || one.Builds[0].Newest[0] != "feature" {
t.Errorf("one key = %+v, %v", one, err)
}
r.Newest = func(string) (map[string]string, error) {
return map[string]string{"main": "src2:" + strings.Repeat("c", 64)}, nil
}
if n, err := r.Build("m", ""); err != nil || !strings.Contains(n.Note, "no build key of m is the newest entry's") || !strings.Contains(n.Note, "main names src2:"+strings.Repeat("c", 64)) {
t.Errorf("a module none of whose keys the newest entry names: %+v, %v", n, err)
}
// H-A-487: a module of 100 keys is read by each entry's own write time, no key's history read, and the listing is cut at the
// 20 newest, saying so; a fingerprint's key with no time in its entry has its one history read.
many, at := map[string][]byte{}, map[string]time.Time{}
base := time.Date(2026, 11, 22, 20, 0, 0, 0, time.UTC)
for i := range 100 {
k, _ := builds.BuilderBuildKey("big", fmt.Sprintf("src2:%064x", i))
many[k], at[k] = done, base.Add(time.Duration(i)*time.Minute)
}
histories := 0
st := stateOf(many)
st.Written = func(string) time.Time { histories++; return base }
big := &BuildReader{Builds: st, Entry: func(k string) (json.RawMessage, uint64, time.Time, error) { return many[k], 1, at[k], nil }}
a, err = big.Build("big", "")
if err != nil || len(a.Builds) != MostBuilds || !a.Builds[0].Since.Equal(base.Add(99*time.Minute)) || histories != 0 ||
!strings.Contains(a.Note, "20 of big's 100 build keys are answered") {
t.Fatalf("100 keys: %d rows, newest %v, %d histories read, note %q; want the 20 newest, no history, the cut said", len(a.Builds), a.Builds[0].Since, histories, a.Note)
}
big.Entry = func(k string) (json.RawMessage, uint64, time.Time, error) { return many[k], 1, time.Time{}, nil }
if a, err := big.Build("big", ""); err != nil || len(a.Builds) != MostBuilds || histories != 0 {
t.Fatalf("100 keys with no time in their entries: %d rows (%v), %d histories read; want no history read for a listing", len(a.Builds), err, histories)
}
if one, err := big.Build("big", fmt.Sprintf("src2:%064x", 7)); err != nil || len(one.Builds) != 1 || !one.Builds[0].Since.Equal(base) || histories != 1 {
t.Errorf("one fingerprint with no time in its entry: %+v (%v), %d histories; want its one history read", one, err, histories)
}
}
// Design 47 G §589: builder_stuck answers each key's fingerprint whole, as the build-input fingerprint the catalog graph entry carries it.
func TestBuilderStuckAnswersTheFingerprintWhole(t *testing.T) {
building, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildBuilding, Step: "asked", Manifest: mdigest('a')})
k, _ := builds.BuilderBuildKey("m", fp1)
out, err := StuckReader{Builds: stateOf(map[string][]byte{k: building})}.Stuck("m")
if err != nil {
t.Fatal(err)
}
rows := out["stuck"].([]Stuck)
if len(rows) != 1 || rows[0].FP != fp1 {
t.Fatalf("stuck = %+v, want the fingerprint %s whole", rows, fp1)
}
}