diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index aa76a8bcb..6329d6f54 100644 --- a/modules/distribution/internal/holder/holder_test.go +++ b/modules/distribution/internal/holder/holder_test.go @@ -63,24 +63,32 @@ func (b fakeBuilder) Recorded(string, string, string) (bool, string, error) { type fakeStore struct { has map[string]bool - held map[string]string // name@version to the digest the store holds there + held map[string]heldAt // name@version to what the store holds there put []map[string][]byte } +// heldAt is what a version's tag names in the store: the manifest's digest and, for a bundle or an archive, the one layer +// that manifest keeps, two digests as the store holds them (H-A-474). +type heldAt struct{ manifest, layer string } + func (s *fakeStore) Has(_, d string) (bool, error) { return s.has[d], nil } func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, error) { s.put = append(s.put, parts) return t.Name + "@" + t.Digest, nil } -func (s *fakeStore) Held(name, version, digest string) (string, string, error) { - switch d := s.held[name+"@"+version]; d { - case "": +func (s *fakeStore) Held(r PublishRequest) (string, string, error) { + at, ok := s.held[r.Name+"@"+r.Version] + if !ok { return "", "", nil - case digest: - return name + "@" + d, "", nil - default: + } + d := at.layer + if r.Kind == KindImage || r.Kind == KindUpstream { + d = at.manifest + } + if d != r.Digest { return "", d, nil } + return r.Name + "@" + d, "", nil } func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil } @@ -346,7 +354,7 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { return err }, "the builder did not reserve it"}, {"a version the store holds at another digest", func(b *bed) error { - b.store.held = map[string]string{"m/program@0.1.4": "sha256:bb"} + b.store.held = map[string]heldAt{"m/program@0.1.4": {manifest: "sha256:cc", layer: "sha256:bb"}} r := req("t") r.Digest = "sha256:aa" _, err := b.h.Publish(ArtifactSeat, builder, r) // the reservation names no digest yet: the bed's builder says yes @@ -388,6 +396,47 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { } } }) + // H-A-474: the store judges a held version like with like, through its own OCI code over a store in memory. A bundle + // is stored as a manifest whose digest is not its layer's: the same bytes published again are already held (ended, no + // error, nothing stored again, also on a repeated ask); other bytes are refused naming the layer the store holds. An + // image is judged by its manifest's digest. + t.Run("a held version is judged by the artifact's own digest", func(t *testing.T) { + reg := newFakeRegistry() + sts := httptest.NewServer(reg) + defer sts.Close() + u, _ := url.Parse(sts.URL) + o := &OCI{Base: u} + if _, err := o.Put(req("first"), map[string][]byte{part.Digest: blob}); err != nil { + t.Fatal(err) + } + if m, _ := o.Digest("m/program", "0.1.4"); m == "" || m == part.Digest { + t.Fatalf("the stored manifest's digest %q; want one, not the layer's %s", m, part.Digest) + } + b := newBed(true) + b.h.Store = o + uploads := len(reg.blobs) + a, err := b.h.Publish(ArtifactSeat, builder, req("again")) + if err != nil || !a.Ended || len(a.Missing) != 0 || len(b.said) != 1 || b.said[0].event != EventPublished || b.findings["publish-refused m/program@0.1.4"] { + t.Errorf("the same bundle again = %+v, %v; said %+v; want already held, published said, nothing refused", a, err, b.said) + } + if a, err := b.h.Repeat(ArtifactSeat, builder, req("again")); err != nil || !a.Ended { + t.Errorf("a repeated ask of the held bundle = %+v, %v; want ended", a, err) + } + other := req("other") + other.Digest = digestOf([]byte("other bytes")) + if _, err := b.h.Publish(ArtifactSeat, builder, other); err == nil || !strings.Contains(err.Error(), "at digest "+part.Digest+", not "+other.Digest) { + t.Errorf("other bytes at the held version = %v; want refused naming the held layer %s", err, part.Digest) + } + if len(reg.blobs) != uploads { + t.Errorf("the store took %d blobs more; want none", len(reg.blobs)-uploads) + } + img := []byte(`{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","layers":[]}`) + reg.manifests["m/image:0.2.0"], reg.types["m/image:0.2.0"] = img, mediaManifest + ir := PublishRequest{Name: "m/image", Version: "0.2.0", Kind: KindImage, Digest: digestOf(img)} + if ref, other, err := o.Held(ir); err != nil || ref != "m/image@"+digestOf(img) || other != "" { + t.Errorf("an image held at its manifest = %q, %q, %v; want held", ref, other, err) + } + }) t.Run("only the missing layers are sent, and an idle transfer is dropped", func(t *testing.T) { b := newBed(true) other := []byte("layer two") diff --git a/modules/distribution/internal/holder/oci.go b/modules/distribution/internal/holder/oci.go index 111ce3549..74a7d1a07 100644 --- a/modules/distribution/internal/holder/oci.go +++ b/modules/distribution/internal/holder/oci.go @@ -207,16 +207,25 @@ func (o *OCI) layerSize(t PublishRequest, parts map[string][]byte) (int64, error return resp.ContentLength, nil } -// Held is the reference of name at version when the store holds digest under it. -func (o *OCI) Held(name, version, digest string) (string, string, error) { - d, err := o.Digest(name, version) +// Held is the reference of r's name at r's version when the store holds r's digest under it, compared like with like +// (H-A-474): an image's by the manifest's digest the version's tag names, a bundle's or an archive's by the one layer +// that manifest keeps (the blob Put stored and the reference names). When the store holds the version at another digest, +// other names that digest of the same kind. +func (o *OCI) Held(r PublishRequest) (string, string, error) { + var d string + var err error + if r.Kind == KindImage || r.Kind == KindUpstream { + d, err = o.Digest(r.Name, r.Version) + } else { + d, err = o.Blob(r.Name, r.Version) + } if err != nil || d == "" { return "", "", err } - if d != digest { + if d != r.Digest { return "", d, nil } - return name + "@" + d, "", nil + return r.Name + "@" + d, "", nil } // Holds is whether the store holds what r's digest names under r's name: an image's manifest by its digest, or a bundle's diff --git a/modules/distribution/internal/holder/transfer.go b/modules/distribution/internal/holder/transfer.go index 706f8e989..4114ae5e3 100644 --- a/modules/distribution/internal/holder/transfer.go +++ b/modules/distribution/internal/holder/transfer.go @@ -140,10 +140,11 @@ type Store interface { Has(name, digest string) (bool, error) // Put stores a publish's parts under its name and version and answers the reference's path (SeatReference). Put(r PublishRequest, parts map[string][]byte) (reference string, err error) - // Held is the reference's path of name at version when the store already holds digest under it: a rebuild of a held - // fingerprint answers it and stores nothing. When the store holds the version at another digest, other names that digest - // and reference is empty; neither is set when the store holds no such version. - Held(name, version, digest string) (reference, other string, err error) + // Held is the reference's path of a publish's name at its version when the store already holds its digest under it: a + // rebuild of a held fingerprint answers it and stores nothing. The digest is compared like with like (H-A-474): an + // image's manifest digest, a bundle's or an archive's blob digest. When the store holds the version at another digest, + // other names that digest and reference is empty; neither is set when the store holds no such version. + Held(r PublishRequest) (reference, other string, err error) // Holds says whether the store holds what a publish's digest names under its name: an image's manifest, or a bundle's or // an archive's blob. The store is the artifact store (A-50): `artifact-store:///blobs/` is that blob. Holds(r PublishRequest) (bool, error) @@ -269,7 +270,7 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, if r.Version != "" { var ref, other string err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) { - ref, other, err = h.Store.Held(r.Name, r.Version, r.Digest) + ref, other, err = h.Store.Held(r) return err }) if err != nil { @@ -417,7 +418,7 @@ func (h *Holder) Repeat(seat, caller string, r PublishRequest) (PublishAnswer, e if r.Version != "" { var ref string err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) { - ref, _, err = h.Store.Held(r.Name, r.Version, r.Digest) + ref, _, err = h.Store.Held(r) return err }) if err != nil { diff --git a/modules/mesh-build-image/cmd/mesh-build-image/build_test.go b/modules/mesh-build-image/cmd/mesh-build-image/build_test.go index b110cc5ba..b8a6cdf14 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/build_test.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/build_test.go @@ -160,7 +160,7 @@ func TestBuildContextIsTheModulesDirectoryForADockerfileInASubdirectory(t *testi } // 1703c, issue 1703: a failing Dockerfile step fails the step, its reason saying buildah's error from stderr, never the -// last STEP line buildah writes on stdout; nothing is pushed. +// last STEP line buildah writes on stdout; nothing is pushed. A long stderr keeps its head and its tail (H-A-475). func TestBuildFailsWithBuildahsError(t *testing.T) { steps := "STEP 1/3: FROM docker.io/library/alpine\nSTEP 2/3: RUN apk add --no-cache ca-certificates\nfetch https://dl-cdn.alpinelinux.org/x\n" long := strings.Repeat("x", 2*CauseBytes) @@ -174,8 +174,8 @@ func TestBuildFailsWithBuildahsError(t *testing.T) { []string{"exit status 1", "Interactive authentication required", `building at STEP "RUN apk add`}, []string{"STEP 3/3", "COPY"}}, "a failed RUN": {forgekey.Ran{Status: 1, Stdout: steps, Stderr: "Error: building at STEP \"RUN false\": exit status 1\n"}, []string{`STEP "RUN false"`}, []string{"dl-cdn"}}, - "a long stderr is bounded, its end kept": {forgekey.Ran{Status: 125, Stdout: steps, Stderr: "early noise\n" + long + "\nError: the end\n"}, - []string{"Error: the end"}, []string{"early noise", long}}, + "a long stderr is bounded, its head and its end kept (H-A-475)": {forgekey.Ran{Status: 125, Stdout: steps, Stderr: "Error: the head\n" + long + "\nError: the end\n"}, + []string{"Error: the head", "bytes left out", "Error: the end"}, []string{long}}, "nothing on stderr": {forgekey.Ran{Status: 2, Stdout: steps}, []string{"exit status 2", "nothing on stderr", "dl-cdn"}, nil}, "never ran": {forgekey.Ran{Err: "ENOENT"}, []string{"ENOENT", "wrote nothing"}, nil}, "a pull the network cut": {forgekey.Ran{Status: 125, Stdout: steps, Stderr: "Error: creating build container: initializing source docker://alpine: pinging container registry registry-1.docker.io: Get \"https://registry-1.docker.io/v2/\": dial tcp: lookup registry-1.docker.io on 10.10.0.3:53: i/o timeout\n"}, diff --git a/modules/mesh-build-image/cmd/mesh-build-image/cause.go b/modules/mesh-build-image/cmd/mesh-build-image/cause.go index f0368b766..4c959fcfe 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/cause.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/cause.go @@ -2,7 +2,8 @@ package main // Why a command failed, as the command said it (issue 1703). buildah writes its progress (STEP n/m, the RUN's own // output) on stdout and its error on stderr, so the last line of both together is a STEP line and hides the error: -// a failure's reason is stderr's last lines, never stdout's, and the whole tail of stderr is said in the journal. +// a failure's reason is stderr's lines, never stdout's, its head and its tail kept within the bound (H-A-475), and more of +// both is said in the journal. import ( "fmt" @@ -18,15 +19,16 @@ const ( TailBytes = 4096 ) -// cause says why a command failed: its exit status (or why it never ran to one) and the last lines it wrote on stderr, -// at most CauseBytes of them, joined with " | ". Only when stderr is empty does stdout's tail stand in, said as such. +// cause says why a command failed: its exit status (or why it never ran to one) and the lines it wrote on stderr, +// joined with " | ", their head and their tail within CauseBytes. Only when stderr is empty does stdout stand in, said +// as such. func cause(r forgekey.Ran) string { head := exitOf(r) - if said := tailLines(r.Stderr, CauseBytes, " | "); said != "" { + if said := keptLines(r.Stderr, CauseBytes, " | "); said != "" { return head + ": " + said } - if said := tailLines(r.Stdout, CauseBytes, " | "); said != "" { - return head + ", nothing on stderr; stdout ends: " + said + if said := keptLines(r.Stdout, CauseBytes, " | "); said != "" { + return head + ", nothing on stderr; stdout says: " + said } return head + ", and it wrote nothing" } @@ -39,32 +41,31 @@ func exitOf(r forgekey.Ran) string { return fmt.Sprintf("exit status %d", r.Status) } -// tailLines is the last non-empty lines of s, trimmed, joined with sep, at most max bytes: the lines are taken whole -// from the end while they fit, and when even the last one does not, its end is kept behind "…". -func tailLines(s string, max int, sep string) string { +// keptLines is the non-empty lines of s, trimmed, joined with sep, at most max bytes: when they do not fit, their +// first and their last part within max, with "… bytes left out …" between them (H-A-475), so a tool's error line +// at the start is never pushed out by what follows it, nor the end by what precedes it. +func keptLines(s string, max int, sep string) string { var lines []string for _, l := range strings.Split(s, "\n") { if l = strings.TrimSpace(l); l != "" { lines = append(lines, l) } } - var keep []string - n := 0 - for i := len(lines) - 1; i >= 0; i-- { - add := len(lines[i]) - if len(keep) > 0 { - add += len(sep) - } - if n+add > max { - if len(keep) == 0 { - keep = append(keep, "…"+cutFront(lines[i], max-len("…"))) - } - break - } - keep = append([]string{lines[i]}, keep...) - n += add + all := strings.Join(lines, sep) + if len(all) <= max { + return all } - return strings.Join(keep, sep) + mark := func(n int) string { return fmt.Sprintf(" … %d bytes left out … ", n) } + room := max - len(mark(len(all))) + if room < 2 { + room = 2 + } + head := all[:room/2] + for len(head) > 0 && !utf8.RuneStart(all[len(head)]) { + head = head[:len(head)-1] + } + tail := cutFront(all, room-room/2) + return head + mark(len(all)-len(head)-len(tail)) + tail } // cutFront is the last at most max bytes of s, starting on a whole character. @@ -82,9 +83,9 @@ func cutFront(s string, max int) string { return s } -// sayFailure writes the tail of a failed command's stderr (and of its stdout, where buildah's RUN output goes) to the -// journal, so what the reason had to leave out can still be read there. +// sayFailure writes the head and the tail of a failed command's stderr (and of its stdout, where buildah's RUN output +// goes) to the journal, so what the reason had to leave out can still be read there. func (m *Module) sayFailure(what string, r forgekey.Ran) { - m.say("%s failed (%s); stderr ends:\n%s\nstdout ends:\n%s", what, exitOf(r), - tailLines(r.Stderr, TailBytes, "\n"), tailLines(r.Stdout, TailBytes, "\n")) + m.say("%s failed (%s); stderr:\n%s\nstdout:\n%s", what, exitOf(r), + keptLines(r.Stderr, TailBytes, "\n"), keptLines(r.Stdout, TailBytes, "\n")) } diff --git a/modules/mesh-build-node/cmd/mesh-build-node/build_test.go b/modules/mesh-build-node/cmd/mesh-build-node/build_test.go index 328d80ae4..ad25c87e2 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/build_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/build_test.go @@ -273,8 +273,9 @@ func TestTheLaunchersAreTheSDKsRule(t *testing.T) { } } -// issue 1703: a failed command's reason keeps stderr's end and stdout's start, each bounded, so a long stdout (npm's -// progress) never pushes npm's error out, and tsc's first error on stdout stays in. +// issue 1703: a failed command's reason keeps stderr's head and end and stdout's start, each bounded, so a long stdout +// (npm's progress) never pushes npm's error out, and tsc's first error on stdout stays in. H-A-475: esbuild's error line +// at the head of stderr stays in before its Node launcher's stack trace, the part left out said by its size. func TestRanFailedKeepsTheError(t *testing.T) { noise := strings.Repeat("npm progress line\n", 200) cases := map[string]struct { @@ -283,9 +284,11 @@ func TestRanFailedKeepsTheError(t *testing.T) { wantNot []string }{ "passed": {forgekey.Ran{Stdout: "ok"}, nil, nil}, - "npm, long stdout": {forgekey.Ran{Status: 1, Stdout: noise, Stderr: "npm ERR! early\n" + noise + "npm ERR! code E404\n"}, []string{"status 1", "npm ERR! code E404"}, []string{"npm ERR! early"}}, - "tsc on stdout": {forgekey.Ran{Status: 2, Stdout: "src/a.ts(1,7): error TS2322: wrong\n" + noise}, []string{"error TS2322"}, nil}, - "never ran": {forgekey.Ran{Err: "ENOENT"}, []string{"ENOENT"}, nil}, + "npm, long stdout": {forgekey.Ran{Status: 1, Stdout: noise, Stderr: "npm ERR! early\n" + noise + "npm ERR! code E404\n"}, []string{"status 1", "npm ERR! early", "bytes left out", "npm ERR! code E404"}, nil}, + "esbuild, a launcher's stack trace": {forgekey.Ran{Status: 1, Stderr: "✘ [ERROR] Could not resolve \"./missing\"\n" + strings.Repeat(" at Object. (/node_modules/esbuild/lib/main.js:1:1)\n", 40)}, + []string{"status 1", `[ERROR] Could not resolve "./missing"`, "bytes left out", "main.js:1:1)"}, nil}, + "tsc on stdout": {forgekey.Ran{Status: 2, Stdout: "src/a.ts(1,7): error TS2322: wrong\n" + noise}, []string{"error TS2322"}, nil}, + "never ran": {forgekey.Ran{Err: "ENOENT"}, []string{"ENOENT"}, nil}, } for name, c := range cases { got := ranFailed(c.ran) diff --git a/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go b/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go index f984f9a22..a6bee5544 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go @@ -172,13 +172,8 @@ func (t Toolchain) open(dir string) error { return os.Chmod(mark, 0o644) } -func tail(s string) string { - s = strings.TrimSpace(s) - if len(s) > 400 { - s = s[len(s)-400:] - } - return s -} +// tail is what a failed toolchain install wrote on stderr, its head and its tail within 400 bytes (H-A-475). +func tail(s string) string { return headAndTail(strings.TrimSpace(s), 400) } func (t *Toolchain) homeOrEmpty() string { if t == nil { diff --git a/modules/mesh-build-node/cmd/mesh-build-node/workspace.go b/modules/mesh-build-node/cmd/mesh-build-node/workspace.go index 11f1936ce..0ad4edaf5 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/workspace.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/workspace.go @@ -15,6 +15,7 @@ import ( "path/filepath" "regexp" "strings" + "unicode/utf8" "git.novox.be/novox/mesh-sdk/go/forgekey" ) @@ -180,18 +181,17 @@ func (m *Module) runIn(ctx context.Context, root string, write bool, dir string, return run(ctx, extra, "env", append([]string{"-C", dir, name}, args...)...) } -// ranFailed says why a command did not pass, or "" when it did (issue 1703): its exit status, the end of what it wrote -// on stderr (npm's error is its last lines) and the start of what it wrote on stdout (tsc writes its errors there, -// first first), each bounded, so a long stdout never pushes stderr's error out of the reason. +// ranFailed says why a command did not pass, or "" when it did (issue 1703): its exit status, the head and the tail of +// what it wrote on stderr (npm's error is its last lines; esbuild's is its first, before its Node launcher's stack +// trace, H-A-475) and the start of what it wrote on stdout (tsc writes its errors there, first first), each bounded, so +// a long stdout never pushes stderr's error out of the reason. func ranFailed(r forgekey.Ran) string { if r.Status == 0 && r.Err == "" { return "" } why := fmt.Sprintf("status %d %s", r.Status, r.Err) if e := strings.TrimSpace(r.Stderr); e != "" { - if len(e) > failedBytes/2 { - e = "... " + e[len(e)-failedBytes/2:] - } + e = headAndTail(e, failedBytes/2) why += ": stderr: " + e } if o := strings.TrimSpace(r.Stdout); o != "" { @@ -205,3 +205,26 @@ func ranFailed(r forgekey.Ran) string { // failedBytes bounds what ranFailed carries of a command's output, stderr and stdout half each. const failedBytes = 1500 + +// headAndTail is s when it fits in max bytes, else its first and its last part within max, with "… bytes left +// out …" between them (H-A-475): a launcher's stack trace at the end never pushes the tool's own error line at the +// start out of what is kept, nor the reverse. Both parts are cut on whole characters. +func headAndTail(s string, max int) string { + if len(s) <= max { + return s + } + mark := func(n int) string { return fmt.Sprintf(" … %d bytes left out … ", n) } + room := max - len(mark(len(s))) + if room < 2 { + room = 2 + } + head := s[:room/2] + for len(head) > 0 && !utf8.RuneStart(s[len(head)]) { + head = head[:len(head)-1] + } + tail := s[len(s)-(room-room/2):] + for len(tail) > 0 && !utf8.RuneStart(tail[0]) { + tail = tail[1:] + } + return head + mark(len(s)-len(head)-len(tail)) + tail +}