K3 answered-digest, K4 kits: a publish answered other-bytes with the publish's own fingerprint is the held digest in the step result and both in the log, else refused; the publish kit's rows follow H-A-738, the reproducible kit
The shared publish names the ask's own fingerprint on a publish that names none. The kit's Bed gains HoldWith, PublishWith and Held. Builds: design 47 H2 H-A-589 (4), H-A-589 (5), H-A-591, H-A-593 (2) (3), H-A-738 (1)
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package steprun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// **A publish answered with the held digest** (design 47 H2 H-A-589 (4), H-A-738 (1)): a registry holds a name and version
|
||||
// with another digest than the build made and answers `other-bytes` with that digest and the fingerprint it keeps. The
|
||||
// shared publish compares that fingerprint with the publish's own (the ask's); equal, the publish is answered as published
|
||||
// with the held digest, nothing stored, and the ask's publish records the digest the seat answered, which the ask's step
|
||||
// result names for the artifact (ADR 0605 §9: a done key names what its seat answered) while the step log names both
|
||||
// digests; other or absent, the publish is refused naming both.
|
||||
|
||||
type publishKey struct{}
|
||||
|
||||
// heldPublish is one artifact's publish of an ask: the fingerprint it publishes under, and the digest its seat answered when that
|
||||
// is not the digest built.
|
||||
type heldPublish struct {
|
||||
mu sync.Mutex
|
||||
fingerprint string
|
||||
answered string
|
||||
}
|
||||
|
||||
// withPublish is ctx for the publish of one artifact of an ask whose fingerprint is fingerprint, and the record the ask
|
||||
// reads the answered digest from.
|
||||
func withPublish(ctx context.Context, fingerprint string) (context.Context, *heldPublish) {
|
||||
a := &heldPublish{fingerprint: fingerprint}
|
||||
return context.WithValue(ctx, publishKey{}, a), a
|
||||
}
|
||||
|
||||
func publishOf(ctx context.Context) *heldPublish {
|
||||
a, _ := ctx.Value(publishKey{}).(*heldPublish)
|
||||
return a
|
||||
}
|
||||
|
||||
// Answered is the digest the seat answered for the artifact when it held the version as another digest, empty when it
|
||||
// answered the digest built.
|
||||
func (a *heldPublish) Answered() string {
|
||||
if a == nil {
|
||||
return ""
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
return a.answered
|
||||
}
|
||||
|
||||
// heldAnswer reads a publish answered `other-bytes` of the digest built: the held digest it is published as, or the refusal.
|
||||
func heldAnswer(ctx context.Context, seat, name, version, built, heldDigest, heldFingerprint, fingerprint string) error {
|
||||
if fingerprint == "" || heldFingerprint != fingerprint {
|
||||
return fmt.Errorf("%s holds %s %s as %s with the fingerprint %q, and the publish names %s with the fingerprint %q: a held version offered with other bytes (ADR 0605 §4)",
|
||||
seat, name, version, heldDigest, heldFingerprint, built, fingerprint)
|
||||
}
|
||||
if a := publishOf(ctx); a != nil {
|
||||
a.mu.Lock()
|
||||
a.answered = heldDigest
|
||||
a.mu.Unlock()
|
||||
}
|
||||
Phase(ctx, fmt.Sprintf("%s answered %s %s as published with the held digest %s, built %s: the same fingerprint, nothing stored", seat, name, version, heldDigest, built))
|
||||
return nil
|
||||
}
|
||||
@@ -254,8 +254,9 @@ func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version strin
|
||||
}
|
||||
}
|
||||
var (
|
||||
out any
|
||||
err error
|
||||
out any
|
||||
err error
|
||||
published *heldPublish
|
||||
)
|
||||
if a.From != "" {
|
||||
if s.Copy == nil {
|
||||
@@ -285,7 +286,9 @@ func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version strin
|
||||
args["version"] = version
|
||||
}
|
||||
Phase(ctx, "build "+a.Artifact)
|
||||
out, err = s.Build(ctx, args)
|
||||
var buildCtx context.Context
|
||||
buildCtx, published = withPublish(ctx, job.Fp)
|
||||
out, err = s.Build(buildCtx, args)
|
||||
}
|
||||
if err != nil {
|
||||
// The compare's out-of-step is answered with the failure, so the build's end still names it.
|
||||
@@ -306,6 +309,12 @@ func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version strin
|
||||
if err := json.Unmarshal(r, &built); err != nil || built.Digest == "" {
|
||||
return nil, fmt.Errorf("the build of %s answered no digest: %s", a.Artifact, r)
|
||||
}
|
||||
// A version its seat held as another digest with the same fingerprint is the held digest, which the done key names
|
||||
// (H-A-589 (4), H-A-738 (1)).
|
||||
if held := published.Answered(); held != "" {
|
||||
Phase(ctx, fmt.Sprintf("%s: built %s, answered %s: the step result names %s", a.Artifact, built.Digest, held, held))
|
||||
built.Digest = held
|
||||
}
|
||||
answer.Artifacts = append(answer.Artifacts, sdkstep.AskBuilt{Artifact: a.Artifact, Digest: built.Digest, Reference: built.Reference})
|
||||
if built.Kept != "" {
|
||||
answer.Kept = strings.TrimPrefix(built.Kept, "sha256:")
|
||||
|
||||
@@ -15,9 +15,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
)
|
||||
@@ -324,3 +328,43 @@ func runsCounted(puller func(read func(key string) (json.RawMessage, error)) *st
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reproducible holds a build module to giving one digest per artifact from two folders (design 47 H2 H-A-591, H-A-593 (3)):
|
||||
// fill writes the module's files into a folder, build builds its artifacts there and answers each one's digest by name. The
|
||||
// two folders have different names and the second one's files other times, so a digest that moved with a path or a time
|
||||
// fails. A catalogue check (failurekit) refuses a build module whose tests do not call it.
|
||||
func Reproducible(t *testing.T, fill func(dir string) error, build func(dir string) (map[string]string, error)) {
|
||||
t.Helper()
|
||||
var digests [2]map[string]string
|
||||
for i, name := range []string{"first-folder", "a-second-folder-elsewhere"} {
|
||||
dir := filepath.Join(t.TempDir(), name)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := fill(dir); err != nil {
|
||||
t.Fatalf("the module's files are not written into %s: %v", dir, err)
|
||||
}
|
||||
if i == 1 {
|
||||
later := time.Now().Add(-24 * time.Hour)
|
||||
_ = filepath.WalkDir(dir, func(p string, _ fs.DirEntry, err error) error {
|
||||
if err == nil {
|
||||
_ = os.Chtimes(p, later, later)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
out, err := build(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("the build in %s: %v", dir, err)
|
||||
}
|
||||
digests[i] = out
|
||||
}
|
||||
if len(digests[0]) == 0 {
|
||||
t.Error("the build gave no artifact, so none is held to one digest")
|
||||
}
|
||||
for artifact, first := range digests[0] {
|
||||
if second, ok := digests[1][artifact]; !ok || second != first {
|
||||
t.Errorf("the artifact %s has the digest %s built in one folder and %q in another: its bytes depend on where or when it was built", artifact, first, second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -301,3 +303,22 @@ func TestRunsCountedRefusesAPullerThatDoesNotReadTheStepsState(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// H-A-593 (3): the shared pack gives one digest from two folders; one that writes a folder's name into its bytes does not.
|
||||
func TestTheSharedPackIsReproducible(t *testing.T) {
|
||||
fill := func(dir string) error {
|
||||
if err := os.MkdirAll(filepath.Join(dir, "src"), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
for name, body := range map[string]string{"b.txt": "b", "src/a.txt": "a", "a.txt": "a"} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reproducible(t, fill, func(dir string) (map[string]string, error) {
|
||||
_, digest, err := steprun.Pack(dir)
|
||||
return map[string]string{"archive": digest}, err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -78,7 +78,13 @@ func SentHow(n int) func(parts.Sent) string {
|
||||
// PublishParts publishes in with ps as its parts on seat, through that seat's client: in's fields are the seat's own
|
||||
// (the four registry seats share the publish verb's input, design 51), the parts are written by the client.
|
||||
func PublishParts(ctx context.Context, call SeatCall, seat string, in reg.PublishInput, ps []parts.Part) (parts.Sent, error) {
|
||||
if a := publishOf(ctx); a != nil && in.Fingerprint == "" {
|
||||
in.Fingerprint = a.fingerprint // the ask's own: the holder keeps it for the version (H-A-589 (2))
|
||||
}
|
||||
sent, err := publishParts(ctx, call, seat, in, ps)
|
||||
if err == nil && sent.Other != nil {
|
||||
err = heldAnswer(ctx, seat, in.Name, in.Version, in.Digest, sent.Other.Digest, sent.Other.Fingerprint, in.Fingerprint)
|
||||
}
|
||||
return sent, PublishFault(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package steprun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts"
|
||||
)
|
||||
|
||||
// Design 47 H2 H-A-589 (4), H-A-738 (1): a publish answered other-bytes carrying the publish's own fingerprint is answered
|
||||
// as published with the held digest: the ask's record names it, the step log names both digests, and nothing is sent; with
|
||||
// another or no fingerprint it is refused naming both.
|
||||
func TestAPublishOfAHeldVersionIsAnsweredWithTheHeldDigest(t *testing.T) {
|
||||
const built, held = "sha256:aaaa", "sha256:bbbb"
|
||||
for _, c := range []struct {
|
||||
name, kept, ask string
|
||||
answered string
|
||||
refused bool
|
||||
}{
|
||||
{"the same fingerprint", "src2:f1", "src2:f1", held, false},
|
||||
{"another fingerprint", "src2:f2", "src2:f1", "", true},
|
||||
{"no kept fingerprint", "", "src2:f1", "", true},
|
||||
{"no fingerprint of its own", "src2:f1", "", "", true},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
var log []string
|
||||
ctx := context.WithValue(context.Background(), phaseKey{}, &phases{logf: func(f string, a ...any) { log = append(log, fmt.Sprintf(f, a...)) }})
|
||||
ctx, record := withPublish(ctx, c.ask)
|
||||
calls := 0
|
||||
call := func(key string, in any) (json.RawMessage, error) {
|
||||
calls++
|
||||
raw, _ := json.Marshal(in)
|
||||
if want := c.ask; want != "" && !strings.Contains(string(raw), want) && strings.HasSuffix(key, ".publish") {
|
||||
t.Errorf("the publish %s names no fingerprint %s of the ask", raw, want)
|
||||
}
|
||||
other := map[string]any{"digest": held}
|
||||
if c.kept != "" {
|
||||
other["fingerprint"] = c.kept
|
||||
}
|
||||
return json.Marshal(map[string]any{"other-bytes": other})
|
||||
}
|
||||
in := reg.PublishInput{Name: "x/y", Kind: reg.PublishInputKindArchive, Version: "0.1.0", Digest: built, Transfer: "t"}
|
||||
sent, err := PublishParts(ctx, call, GoSeat, in, []parts.Part{{Digest: built, Size: 1, Open: nil}})
|
||||
if (err != nil) != c.refused || record.Answered() != c.answered || calls != 1 || sent.Other == nil {
|
||||
t.Fatalf("answered %q, err %v after %d calls (other %+v), want answered %q and refused %v with the one publish call", record.Answered(), err, calls, sent.Other, c.answered, c.refused)
|
||||
}
|
||||
if c.refused && !(strings.Contains(err.Error(), built) && strings.Contains(err.Error(), held)) {
|
||||
t.Errorf("the refusal %v does not name both digests", err)
|
||||
}
|
||||
if !c.refused && !(strings.Contains(strings.Join(log, "\n"), built) && strings.Contains(strings.Join(log, "\n"), held)) {
|
||||
t.Errorf("the step log %q does not name both digests", log)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user