package main import ( "bytes" "encoding/json" "reflect" "strings" "testing" reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry" "distribution/internal/holder" ) // twoDigests is a store whose every version's manifest digest and blob differ. type twoDigests struct{ holder.Catalog } func (twoDigests) Digest(string, string) (string, error) { return "sha256:manifest", nil } func (twoDigests) Blob(string, string) (string, error) { return "sha256:blob", nil } func jsonFields(v any) map[string]bool { out := map[string]bool{} t := reflect.TypeOf(v) for i := 0; i < t.NumField(); i++ { name, _, _ := strings.Cut(t.Field(i).Tag.Get("json"), ",") out[name] = true } return out } // TestTheVerbsSpeakTheSeatsContract holds distribution's seat verbs to the contracts mesh-contracts generates from the // two seats' definitions (ADR 0620 *How it is checked*): every argument a verb's tool takes is a field of the // contract's input, and every event body decodes into the contract's type with no field left over. func TestTheVerbsSpeakTheSeatsContract(t *testing.T) { inputs := map[string]any{reg.VerbPublish: reg.PublishInput{}, reg.VerbPublishPart: reg.PublishPartInput{}, reg.VerbVersions: reg.VersionsInput{}, reg.VerbRemove: reg.RemoveInput{}, reg.VerbHold: reg.HoldInput{}, reg.VerbRelease: reg.ReleaseInput{}, reg.VerbMirror: reg.MirrorInput{}, reg.VerbNameOf: reg.NameOfInput{}, reg.VerbTags: reg.TagsInput{}} seen := 0 for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}, nil) { seat, verb, ok := strings.Cut(tool.Name, ".") in, isVerb := inputs[verb] if !ok || !isVerb || (seat != holder.ImageSeat && seat != holder.ArtifactSeat) { continue } seen++ fields := jsonFields(in) props, _ := tool.Input["properties"].(map[string]any) for p := range props { if !fields[p] { t.Errorf("%s takes %q, which the contract's input does not name", tool.Name, p) } } } if seen != 15 { t.Errorf("%d seat verbs served, want 15 (9 on the image seat, 6 on the artifact seat)", seen) } for event, want := range map[string]any{holder.EventPublished: ®.Outcome{}, holder.EventKept: ®.KeptEvent{}, holder.EventHeld: ®.Hold{}, holder.EventReleased: ®.Hold{}} { b, _ := json.Marshal(eventBody(event, holder.Event{Name: "m/a", Kind: holder.KindArchive, Digest: "sha256:aa", Holder: holder.Self, By: "x.artifact-store-tools"})) dec := json.NewDecoder(bytes.NewReader(b)) dec.DisallowUnknownFields() if err := dec.Decode(want); err != nil { t.Errorf("%s's body %s is not the contract's: %v", event, b, err) } } // an other-bytes answer carries its digest and fingerprint and names no transfer (H-A-729, H-A-732) if a := publishAnswer(holder.PublishAnswer{OtherBytes: &holder.OtherBytes{Digest: "sha256:bb", Fingerprint: "f1"}}); a.Transfer != "" || a.OtherBytes == nil || a.OtherBytes.Digest != "sha256:bb" || a.OtherBytes.Fingerprint != "f1" { t.Errorf("publish answers %+v for other bytes", a) } else if b, _ := json.Marshal(a); strings.Contains(string(b), `"transfer"`) { t.Errorf("the other-bytes answer %s names a transfer", b) } if a := publishAnswer(holder.PublishAnswer{Transfer: "t", Missing: []string{"sha256:bb"}}); !reflect.DeepEqual(a.Lacks, []string{"sha256:bb"}) || a.Held { t.Errorf("publish answers %+v", a) } // versions answers the digest a reference names (design 47 G A-430a): the blob on the artifact seat, the manifest on the image seat. cat := twoDigests{} for _, c := range []struct{ seat, want string }{{holder.ArtifactSeat, "sha256:blob"}, {holder.ImageSeat, "sha256:manifest"}} { // and the fingerprint kept for it beside, none where the key names another digest or there is none (H-A-589 (3)) kept := memKept{holder.FingerprintKey("m/files", "0.1.7"): {Digest: c.want, Fingerprint: "f1"}} got := versionsAnswer(&holder.Verbs{Holder: &holder.Holder{Fingerprints: kept}, Catalog: cat}, "", holder.VersionsAnswer{Seat: c.seat, Name: "m/files", Versions: []string{"0.1.7"}}) if len(got.Versions) != 1 || got.Versions[0].Digest != c.want || got.Versions[0].Version != "0.1.7" || got.Versions[0].Fingerprint != "f1" { t.Errorf("versions on %s answers %+v, want digest %s, fingerprint f1", c.seat, got.Versions, c.want) } kept[holder.FingerprintKey("m/files", "0.1.7")] = holder.Kept{Digest: "sha256:other", Fingerprint: "f1"} if got := versionsAnswer(&holder.Verbs{Holder: &holder.Holder{Fingerprints: kept}, Catalog: cat}, "", holder.VersionsAnswer{Seat: c.seat, Name: "m/files", Versions: []string{"0.1.7"}}); got.Versions[0].Fingerprint != "" { t.Errorf("versions on %s answers fingerprint %q of a key naming another digest", c.seat, got.Versions[0].Fingerprint) } } if r, err := partOf(reg.PublishPartInput{Transfer: "t", Part: "sha256:bb", Data: "aGk=", Last: true}); err != nil || string(r.Data) != "hi" || r.Digest != "sha256:bb" { t.Errorf("publish-part read %+v, %v", r, err) } } // memKept is the state fingerprints in memory. type memKept map[string]holder.Kept func (m memKept) Get(k string) (holder.Kept, bool, error) { v, ok := m[k]; return v, ok, nil } func (m memKept) Put(k string, v holder.Kept) error { m[k] = v; return nil } func (m memKept) Delete(k string) error { delete(m, k); return nil }