Files
mesh-catalog/modules/distribution/internal/holder/reserved_test.go
T

55 lines
2.1 KiB
Go

package holder
import (
"encoding/json"
"errors"
"strings"
"testing"
)
// A publish is checked against the builder's reservation by the seat mesh-builds' verb reserved, asked with the package's name
// (design 47 §381, §478).
func reservedChecksAPublishAgainstTheReservation(t *testing.T) {
for _, c := range []struct {
name string
answer string
err error
ok bool
why string // part of the reason, or of the error
}{
{name: "the reserved version and digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true},
{name: "another artifact of it carries the digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:aux"},{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true},
{name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "another digest than sha256:aa"},
{name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "reserved 0.1.8 for registry/mesh-image, not 0.1.7"},
{name: "the builder refused the name", err: errors.New("no entry carries registry/mesh-image"), why: "no entry carries registry/mesh-image"},
{name: "an answer that is none", answer: `{"error":"x"}`, why: "answered no reservation"},
} {
asked := ""
r := Reserved{Ask: func(address string, body any) (json.RawMessage, error) {
b, _ := json.Marshal(body)
asked = address + " " + string(b)
return json.RawMessage(c.answer), c.err
}}
ok, why, err := r.Recorded("registry/mesh-image", "0.1.7", "sha256:aa")
if asked != `seat:mesh-builds.reserved {"name":"registry/mesh-image","version":"0.1.7"}` {
t.Fatalf("%s: asked %s", c.name, asked)
}
if c.ok {
if !ok || err != nil {
t.Errorf("%s: not accepted: %v %v", c.name, why, err)
}
continue
}
if ok || !strings.Contains(why+fmtErr(err), c.why) {
t.Errorf("%s: ok=%v why=%q err=%v, want %q", c.name, ok, why, err, c.why)
}
}
}
func fmtErr(err error) string {
if err == nil {
return ""
}
return err.Error()
}