package selfcompose import ( "context" "encoding/json" "errors" "strings" "sync" "testing" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" ) // Design 47 G A-430, A-430a (the lead's answer A-430b): a want naming its version and no parts is resolved to its artifacts' // digests through the verb versions of the registry seat of each artifact's kind, under `/`; an upstream copy // by the digest its manifest pins. The build is made from those digests. A lookup that fails is said, naming the module, the // version and the seat, and holds the module whole; a want with parts is read by its parts as before. func aWantNamingItsVersionIsResolvedThroughTheRegistrySeats(t *testing.T) { fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) d := func(c string) string { return "sha256:" + strings.Repeat(c, 64) } manifest := map[string]any{"module": "mail", "version": "1", "build": map[string]any{"artifacts": []any{ map[string]any{"name": "server", "kind": "upstream", "from": "alpine@" + d("9")}, map[string]any{"name": "files", "kind": "archive", "from": "files"}, map[string]any{"name": "web", "kind": "image", "from": "Dockerfile"}, }}} entry := meshcatalog.CatalogEntry{Module: "mail", Fp: fp, Manifest: manifest, Artifacts: []meshcatalog.Artifact{ {Ref: "mail:server", Kind: "upstream"}, {Ref: "mail:files", Kind: "archive"}, {Ref: "mail:web", Kind: "image"}}} // The seats hold mail/files and mail/web; versions newest first. held := map[string]string{ "oci-artifact-registry.mail/files": `{"name":"mail/files","versions":[{"name":"mail/files","kind":"archive","digest":"` + d("2") + `","version":"1.3.0"},{"name":"mail/files","kind":"archive","digest":"` + d("1") + `","version":"1.2.3"}]}`, "container-image-registry.mail/web": `{"name":"mail/web","versions":[{"name":"mail/web","kind":"image","digest":"` + d("3") + `","version":"1.2.3"}]}`, "container-image-registry.mail/other": `{"name":"mail/other","versions":[]}`, } for name, row := range map[string]struct { want meshwants.WantsByWantNodeModuleValue down string // a seat whose call fails parts map[string]string missing string // a word the reason holds said []string }{ "resolved": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true}, parts: map[string]string{"mail:server": d("9"), "mail:files": d("1"), "mail:web": d("3")}}, "a version a seat does not hold": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "2.0.0", Self: true}, missing: "2.0.0", said: []string{"mail at version 2.0.0", "oci-artifact-registry", "container-image-registry"}}, "a seat that does not answer": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true}, down: "container-image-registry", missing: "mail:web", said: []string{"mail at version 1.2.3: the seat container-image-registry"}}, "parts as before": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true, Parts: map[string]string{"mail:server": d("9"), "mail:files": d("5"), "mail:web": d("6")}}, parts: map[string]string{"mail:server": d("9"), "mail:files": d("5"), "mail:web": d("6")}}, } { var mu sync.Mutex var said []string env := Env{Say: func(line string) { mu.Lock(); said = append(said, line); mu.Unlock() }} calls := 0 env.Registry = EntryCall(func(_ context.Context, seat, verb string, args any) (json.RawMessage, error) { calls++ if verb != "versions" { t.Errorf("%s: asked %s.%s", name, seat, verb) } if seat == row.down { return nil, errors.New("no responders") } answer, ok := held[seat+"."+args.(map[string]string)["name"]] if !ok { return nil, errors.New("not held") } return json.RawMessage(answer), nil }) env.Entry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) { return nil, errors.New("not asked") }) c := New(env) c.wants["mail"] = row.want c.keptEnt[fp] = keptEntry{entry: entry} parts, missing := c.partsOf("mail", entry, row.want) b := c.buildOf("mail", fp) if row.missing != "" { if parts != nil || b != nil || !strings.Contains(missing, row.missing) { t.Errorf("%s: parts %v, build %v, missing %q: want the module held whole, naming %s", name, parts, b, missing, row.missing) } if c.entryHolds()["mail"] == "" { t.Errorf("%s: the module is not held", name) } for _, w := range row.said { if !strings.Contains(strings.Join(said, "\n"), w) { t.Errorf("%s: said %q, want a line naming %q", name, said, w) } } continue } if missing != "" || len(parts) != len(row.parts) { t.Fatalf("%s: parts %v, missing %q", name, parts, missing) } for ref, digest := range row.parts { if parts[ref] != digest { t.Errorf("%s: %s at %s, want %s", name, ref, parts[ref], digest) } } if b == nil || len(b.Artifacts) != 3 { t.Fatalf("%s: build %+v", name, b) } for _, a := range b.Artifacts { if a.Digest != row.parts["mail:"+a.Name] || a.Reference != referenceOf("mail", a.Name, a.Digest) { t.Errorf("%s: artifact %+v", name, a) } } // A resolution is asked once, and it names the node keeping the build its digests are of. before := calls c.partsOf("mail", entry, row.want) if calls != before { t.Errorf("%s: resolved again: %d calls, then %d", name, before, calls) } c.wants["mail"] = meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: row.want.Version, Parts: row.want.Parts, KeptOn: "g14", Self: true} if node, _, kept, _ := c.KeptBuildOf(context.Background(), "mail", row.parts["mail:files"]); !kept || node != "g14" { t.Errorf("%s: kept on %q %v, want g14", name, node, kept) } if _, _, kept, _ := c.KeptBuildOf(context.Background(), "mail", d("7")); kept { t.Errorf("%s: a digest the want does not resolve to is said kept", name) } } }