Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/packages_gone_test.go
T
jschoubben d719a69b7d I3: a dry run keeps its packages in its own scratch state
A branch's take-in, a run's candidate and a branch's re-pin compute as dry
runs whose packages went to the trunk's packages state. They now write them
to the state dry-runs under <branch>.<call>.packages.<name>, read through to
the live state, deleted when the dry run answers and every key at the
indexer's start; the manifests stay create-once by digest as every take-in's.
indexer_dry_runs reads the state; the manifest asks for it, 16 MiB, one
hour's TTL.

Builds: design 47 H2 §804, §820; H-A-413 and its addendum
2026-11-22 01:08:34 +01:00

194 lines
8.2 KiB
Go

package index
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"testing"
"time"
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
)
// Design 47 H H-A-232 (1), H-A-233, H-A-234: a repository's packages are those in the merge's tree. The kept key of a
// package whose directory the trunk's merge no longer holds is dropped, said, and the repository's other packages are
// published all the same (issue 1585's one-directory fallback); a branch's take-in and a dry run, which share the live
// packages state, drop nothing. A repository whose every asked directory is gone, at a commit that holds a tree, drops them
// and raises nothing; a listing that fails for another reason, or a commit with no tree, raises the finding
// packages-not-published (scope merge, id the repository), cleared at the next publish that lists.
func TestARepositorysPackagesAreThoseInTheMergesTree(t *testing.T) {
const (
live = "git.novox.be/novox/mesh-catalog/modules/disk-load/diskreadings"
gone = "git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
liveDir = "modules/disk-load/diskreadings"
goneDir = "modules/mesh-build-go/buildstep"
)
ctx := context.Background()
old, merge := strings.Repeat("d", 40), strings.Repeat("e", 40)
reaching := func(m string) map[string]meshcatalog.ModulesByModuleValue {
return map[string]meshcatalog.ModulesByModuleValue{
"user": {Repository: catalogueRepo, Inputs2: &meshcatalog.ModulesByModuleValueInputs2{Packages: []string{m}}},
}
}
type setup struct {
ix *Indexer
findings *memFindings
said *[]string
down *error // the forge's listing fails for a reason that is not a gone directory
noTree *error // the seat's trees of the commit, without files, fail
}
newSetup := func(t *testing.T, kept ...string) setup {
t.Helper()
var down, noTree error
said := []string{}
files := func(_ context.Context, repository, sha string, dirs []string) (map[string]map[string]string, error) {
if down != nil {
return nil, down
}
out := map[string]map[string]string{}
for _, d := range dirs {
if d != liveDir {
// as filesVia says the seat's not-found naming the directory
return nil, fmt.Errorf("the directory %s is not in %s at %s: %w", d, repository, sha, ErrNotInTree)
}
out[d] = map[string]string{"go.mod": "m1", "readings.go": "r-" + sha[:1]}
}
return out, nil
}
trees := func(context.Context, string, string) ([]string, error) { return []string{"modules/disk-load"}, noTree }
findings := newFindings()
ix := &Indexer{Module: testSelf, Packages: newMem(), Files: files, Trunk: trees, Findings: findings,
Logf: func(format string, args ...any) { said = append(said, fmt.Sprintf(format, args...)) }}
dirOf := map[string]string{live: liveDir, gone: goneDir}
for _, m := range kept {
key, err := PackagesKey(m)
if err != nil {
t.Fatal(err)
}
raw, err := EncodePublishedPackages(meshcatalog.PackagesByModuleValue{Module: m, Repository: catalogueRepo, Dir: dirOf[m], Commit: old,
Requires: "req", At: ptr(time.Unix(0, 0).UTC()), Packages: []meshcatalog.PackagesByModuleValuePackagesItem{{Package: m, Digest: "x"}}})
if err != nil {
t.Fatal(err)
}
ix.Packages.(*memStore).put(key, raw)
}
return setup{ix: ix, findings: findings, said: &said, down: &down, noTree: &noTree}
}
notPublished := func(s setup) bool {
for _, f := range s.findings.Held() {
if f.Kind == PackagesNotPublishedKind && f.Scope == mcc.ScopeMerge && f.ID == keyID(catalogueRepo) {
return true
}
}
return false
}
dropped := "package " + gone + " of " + catalogueRepo + ": its directory is gone at " + merge + ": its kept key dropped"
t.Run("the trunk's merge drops a gone package's key and publishes the others; a failed listing is a finding until one lists", func(t *testing.T) {
s := newSetup(t, live, gone)
*s.down = errors.New("seat:git.trees (failed): the forge did not answer")
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(live), false, TrunkBranch); err == nil {
t.Fatal("a listing that failed published")
}
if !notPublished(s) {
t.Fatalf("a listing that failed raised no finding %s: %v", PackagesNotPublishedKind, s.findings.Held())
}
*s.down = nil
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(live), false, TrunkBranch); err != nil {
t.Fatal(err)
}
have := s.ix.publishedAll(ctx)
if have[live].Commit != merge {
t.Errorf("packages of %s at %q, want the merge %s: a gone sibling kept it from being published", live, have[live].Commit, merge)
}
if _, kept := have[gone]; kept {
t.Errorf("the kept key of %s, whose directory the merge does not hold, stays", gone)
}
if !strings.Contains(strings.Join(*s.said, "\n"), dropped) {
t.Errorf("the journal does not say %q:\n%s", dropped, strings.Join(*s.said, "\n"))
}
if notPublished(s) {
t.Error("the finding packages-not-published stays after a publish that listed")
}
})
for _, row := range []struct {
name string
dry, scratch bool
branch string
}{
{"a branch's take-in keeps the key: only the trunk's merge says a directory is gone", false, false, "feat/x"},
{"a dry run keeps the key: its packages are a working copy over the live state", true, false, TrunkBranch},
// Design 47 H2 §804: over the state dry-runs, a dry run's packages are kept there alone, under its branch and call,
// read by indexer_dry_runs and gone at its end; the live packages state is left as it was.
{"a dry run over the state dry-runs writes its packages there alone and drops them at its end", true, true, TrunkBranch},
} {
t.Run(row.name, func(t *testing.T) {
s := newSetup(t, live, gone)
if row.scratch {
s.ix.DryRuns = newMem()
}
before := s.ix.publishedAll(ctx)
ix, d := s.ix, (*dryRun)(nil)
if row.dry {
d = s.ix.dry("beta", "t")
ix = d.ix
}
if err := ix.publish(ctx, catalogueRepo, merge, reaching(live), false, row.branch); err != nil {
t.Fatal(err)
}
if _, kept := s.ix.publishedAll(ctx)[gone]; !kept {
t.Errorf("the kept key of %s was dropped", gone)
}
if !row.scratch {
return
}
if got := s.ix.publishedAll(ctx); !reflect.DeepEqual(got, before) {
t.Errorf("the live packages state is %+v after the dry run, want %+v as before", got, before)
}
if mine := ix.publishedAll(ctx); mine[live].Commit != merge {
t.Errorf("the dry run reads %s at %q, want its own write at %s", live, mine[live].Commit, merge)
}
read, err := DryRunsTools(s.ix.DryRuns)[0].Run(map[string]any{})
a, _ := read.(DryRunsAnswer)
if err != nil || a.Total == 0 || len(a.Keys) != a.Total || !strings.HasPrefix(a.Keys[0].Key, DryRunPrefix("beta", "t")+PackagesState+".") {
t.Errorf("indexer_dry_runs = %+v (%v), want the dry run's packages under beta.t.packages.", read, err)
}
d.end(ctx)
if a, err := ReadDryRuns(ctx, s.ix.DryRuns); err != nil || a.Total != 0 {
t.Errorf("dry-runs holds %+v (%v) after the dry run's end, want none", a, err)
}
})
}
t.Run("a one-key repository whose directory is gone drops it and raises nothing, when the commit holds a tree", func(t *testing.T) {
s := newSetup(t, gone)
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(gone), false, TrunkBranch); err != nil {
t.Fatal(err)
}
if _, kept := s.ix.publishedAll(ctx)[gone]; kept {
t.Errorf("the kept key of %s, the repository's one package, whose directory is gone, stays", gone)
}
if notPublished(s) {
t.Error("a gone directory raised packages-not-published")
}
})
t.Run("every asked directory missing at a commit with no tree is the tree's failure: the keys stay and it is a finding", func(t *testing.T) {
s := newSetup(t, gone)
*s.noTree = errors.New("seat:git.trees (not-found): Gitea API GET: 404 Not Found")
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(gone), false, TrunkBranch); err == nil {
t.Fatal("a commit with no tree published")
}
if _, kept := s.ix.publishedAll(ctx)[gone]; !kept {
t.Errorf("the kept key of %s was dropped at a commit with no tree", gone)
}
if !notPublished(s) {
t.Error("a commit with no tree raised no finding")
}
})
}