package selfcompose import ( "encoding/json" "strings" "testing" "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" "github.com/novox/mesh-host/internal/compositions" ) // A-484: the manifest of the build a module runs here is answered in one place, from the entry of the running build, else the // composition this node runs of it, else the module's trunk manifest; it says its source once per module and source, and where // none answers its words name the three. func TestTheManifestOfTheRunningBuildIsReadInOnePlace(t *testing.T) { running := meshcatalogclient.SourcePrefix2 + strings.Repeat("1", 64) other := meshcatalogclient.SourcePrefix2 + strings.Repeat("2", 64) body := `{"module":"xorg","version":"1","provides":[{"name":"x11-display"}]}` for _, tc := range []struct { name string entry string // the entry held, by fingerprint ("running", "other") or none composition string // the composition this node runs: its build ("running", "other") or none data bool trunk bool statement bool // the running event names a manifest this node holds (A-484a) wantSource string wantKnown bool }{ {"its entry answers first", "running", "running", false, true, false, SourceEntry, true}, {"the entry of the composition's build is the running build's", "other", "other", true, true, false, SourceEntry, true}, {"a composition with no manifest keeps the trunk's manifest and its data", "", "running", true, true, false, SourceTrunk, true}, {"a composition and no trunk answer: its build and its data alone", "", "running", true, false, false, SourceComposition, false}, {"the trunk alone", "", "", false, true, false, SourceTrunk, true}, {"its running statement's manifest comes before the trunk's", "", "running", true, true, true, SourceStatement, true}, {"none answers", "", "", false, false, false, "", false}, } { t.Run(tc.name, func(t *testing.T) { env := newFakeBus().env() env.Node = "n1" var said []string env.Say = func(s string) { said = append(said, s) } if tc.composition != "" { fp := map[string]string{"running": running, "other": other}[tc.composition] env.Running = func() []compositions.Composed { return []compositions.Composed{{Node: "n1", Module: "xorg", FP: fp, Data: tc.data}} } } c := New(env) c.mu.Lock() c.assignments["n1"] = meshmoduleassigner.Assignments{Node: "n1", Modules: []meshmoduleassigner.PlacedModule{{Module: "xorg"}}} c.heard = map[string]map[string]heardRun{"n1": {"xorg": {fingerprint: running}}} if tc.statement { digest := "sha256:" + strings.Repeat("9", 64) c.heard["n1"]["xorg"] = heardRun{fingerprint: running, manifest: digest} c.manifests[digest] = []byte(body) } if tc.trunk { var man map[string]any _ = json.Unmarshal([]byte(body), &man) c.trunkBuilds = map[string]trunkBuild{"xorg": {running: running, fp: other, manifest: man}} } c.mu.Unlock() if tc.entry != "" { c.holdEntry(t, "xorg", map[string]string{"running": running, "other": other}[tc.entry], body) } a := c.runningManifest("n1", "xorg", "") again := c.runningManifest("n1", "xorg", "") if a.Source != tc.wantSource || a.Known != tc.wantKnown || (a.Known && len(a.Manifest.Provides) != 1) { t.Fatalf("answered %q (known %v, provides %d), want %q (known %v)", a.Source, a.Known, len(a.Manifest.Provides), tc.wantSource, tc.wantKnown) } if tc.wantSource != "" && (len(said) != 1 || again.Said != "" || !strings.Contains(said[0], tc.wantSource)) { t.Errorf("said %q (and %q again), want its source once", said, again.Said) } if tc.composition != "" && a.Data != tc.data && a.Source != SourceEntry && a.Source != SourceStatement { t.Errorf("the composition's data = %v, want %v", a.Data, tc.data) } if !tc.wantKnown && !strings.Contains(a.Why, SourceEntry) || !tc.wantKnown && !strings.Contains(a.Why, SourceStatement) || !tc.wantKnown && !strings.Contains(a.Why, SourceTrunk) { t.Errorf("the words of an answer that reads no manifest are %q, want the three sources named", a.Why) } }) } }