Skip to content

Commit da817e9

Browse files
aledbfclaude
andcommitted
fix(oci): fail a publish at the first tag the registry rejects
PushArtifact only warned when a tag's manifest PUT failed and went on with the remaining tags, so `features publish` and `templates publish` reported the subset that made it and exited 0, and pushed the collection metadata as well. The reference CLI's putManifestWithTags stops at the first rejected tag, logs "Failed to PUT manifest for tag <tag>", and publish logs the "(!) ERR: Failed to publish" lines and exits 1 before the collection metadata and before printing a result. Go now does the same. Tags pushed before the failure stay pushed, as in TS. TestPublishParity gains a tag_failure case that runs both CLIs against a registry that rejects one tag and compares stdout, the exit code, the error lines and the PUTs each one made. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d258b90 commit da817e9

5 files changed

Lines changed: 264 additions & 28 deletions

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package cli
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"errors"
7+
"fmt"
8+
"io"
9+
"net"
10+
"net/http"
11+
"net/http/httptest"
12+
"path/filepath"
13+
"slices"
14+
"strings"
15+
"sync"
16+
"testing"
17+
18+
coreerrors "github.com/devcontainers/cli/internal/errors"
19+
"github.com/devcontainers/cli/internal/log"
20+
"github.com/devcontainers/cli/internal/oci"
21+
)
22+
23+
type pushRegistry struct {
24+
host string
25+
failTag string
26+
27+
mu sync.Mutex
28+
blobs map[string][]byte
29+
manifests map[string][]byte
30+
attempts map[string][]string
31+
}
32+
33+
func newPushRegistry(t *testing.T, failTag string) *pushRegistry {
34+
t.Helper()
35+
r := &pushRegistry{failTag: failTag, blobs: map[string][]byte{}, manifests: map[string][]byte{}, attempts: map[string][]string{}}
36+
srv := httptest.NewServer(http.HandlerFunc(r.serve))
37+
t.Cleanup(srv.Close)
38+
// A localhost registry is plain HTTP for both CLIs.
39+
_, port, err := net.SplitHostPort(srv.Listener.Addr().String())
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
r.host = "localhost:" + port
44+
return r
45+
}
46+
47+
func (r *pushRegistry) tagAttempts(repo string) []string {
48+
r.mu.Lock()
49+
defer r.mu.Unlock()
50+
return slices.Clone(r.attempts[repo])
51+
}
52+
53+
func (r *pushRegistry) serve(w http.ResponseWriter, req *http.Request) {
54+
r.mu.Lock()
55+
defer r.mu.Unlock()
56+
path := strings.TrimPrefix(req.URL.Path, "/v2/")
57+
notFound := func() {
58+
w.Header().Set("Content-Type", "application/json")
59+
w.WriteHeader(http.StatusNotFound)
60+
_, _ = w.Write([]byte(`{"errors":[{"code":"NAME_UNKNOWN","message":"not found"}]}`))
61+
}
62+
switch {
63+
case path == "" || path == "/":
64+
w.WriteHeader(http.StatusOK)
65+
case strings.HasSuffix(path, "/tags/list"):
66+
notFound()
67+
case strings.Contains(path, "/blobs/uploads/") && req.Method == http.MethodPost:
68+
w.Header().Set("Location", req.URL.Path+"session")
69+
w.WriteHeader(http.StatusAccepted)
70+
case strings.Contains(path, "/blobs/uploads/") && req.Method == http.MethodPut:
71+
body, _ := io.ReadAll(req.Body)
72+
digest := req.URL.Query().Get("digest")
73+
r.blobs[digest] = body
74+
w.Header().Set("Docker-Content-Digest", digest)
75+
w.WriteHeader(http.StatusCreated)
76+
case strings.Contains(path, "/blobs/"):
77+
body, ok := r.blobs[path[strings.LastIndex(path, "/")+1:]]
78+
if !ok {
79+
notFound()
80+
return
81+
}
82+
w.Header().Set("Content-Length", fmt.Sprint(len(body)))
83+
if req.Method != http.MethodHead {
84+
_, _ = w.Write(body)
85+
}
86+
case strings.Contains(path, "/manifests/"):
87+
i := strings.Index(path, "/manifests/")
88+
repo, ref := path[:i], path[i+len("/manifests/"):]
89+
if req.Method != http.MethodPut {
90+
body, ok := r.manifests[repo+":"+ref]
91+
if !ok {
92+
notFound()
93+
return
94+
}
95+
w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json")
96+
w.Header().Set("Content-Length", fmt.Sprint(len(body)))
97+
if req.Method != http.MethodHead {
98+
_, _ = w.Write(body)
99+
}
100+
return
101+
}
102+
r.attempts[repo] = append(r.attempts[repo], ref)
103+
if ref == r.failTag {
104+
w.Header().Set("Content-Type", "application/json")
105+
w.WriteHeader(http.StatusBadRequest)
106+
_, _ = w.Write([]byte(`{"errors":[{"code":"TAG_INVALID","message":"tag rejected by test"}]}`))
107+
return
108+
}
109+
body, _ := io.ReadAll(req.Body)
110+
r.manifests[repo+":"+ref] = body
111+
sum := sha256.Sum256(body)
112+
w.Header().Set("Docker-Content-Digest", "sha256:"+hex.EncodeToString(sum[:]))
113+
w.WriteHeader(http.StatusCreated)
114+
default:
115+
notFound()
116+
}
117+
}
118+
119+
func writeTagFailureCollection(t *testing.T, collectionType string) string {
120+
t.Helper()
121+
src := filepath.Join(t.TempDir(), "src")
122+
if collectionType == "feature" {
123+
writeSeamFeature(t, src, "good", "1.0.0")
124+
return src
125+
}
126+
writeFile(t, filepath.Join(src, "good", "devcontainer-template.json"), `{"id":"good","version":"1.0.0","name":"Good"}`, 0o644)
127+
writeFile(t, filepath.Join(src, "good", ".devcontainer.json"), `{"image":"alpine"}`, 0o644)
128+
return src
129+
}
130+
131+
func TestPublishCollection_FailedTagPushExits(t *testing.T) {
132+
for _, collectionType := range []string{"feature", "template"} {
133+
t.Run(collectionType, func(t *testing.T) {
134+
reg := newPushRegistry(t, "1.0")
135+
out := &captureOutput{}
136+
logger := log.New(log.Options{Level: log.LevelInfo, Format: "text", Writer: out.Stderr()})
137+
client := oci.NewClientWithAuthPolicy(logger, map[string]string{"DOCKER_CONFIG": t.TempDir()}, oci.DefaultAuthPolicy())
138+
139+
err := publishCollectionWith(t.Context(), out, client, writeTagFailureCollection(t, collectionType), reg.host, "me/coll", collectionType, "info")
140+
if exit, ok := errors.AsType[*coreerrors.ExitCodeError](err); !ok || exit.Code != 1 {
141+
t.Fatalf("err = %v, want exit code 1 after the rejected tag", err)
142+
}
143+
if out.out.Len() != 0 {
144+
t.Errorf("stdout = %q, want no result after a failed tag push", out.out.String())
145+
}
146+
147+
resource := reg.host + "/me/coll/good"
148+
stderr := out.err.String()
149+
for _, want := range []string{
150+
"Failed to PUT manifest for tag 1.0\n",
151+
fmt.Sprintf("(!) ERR: Failed to publish %s: '%s'", collectionType, resource),
152+
fmt.Sprintf("(!) ERR: Failed to publish '%s'", resource),
153+
} {
154+
if !strings.Contains(stderr, want) {
155+
t.Errorf("stderr is missing %q:\n%s", want, stderr)
156+
}
157+
}
158+
if strings.Contains(stderr, "Published "+collectionType) {
159+
t.Errorf("stderr reports a successful publish:\n%s", stderr)
160+
}
161+
162+
if got, want := reg.tagAttempts("me/coll/good"), []string{"1", "1.0"}; !slices.Equal(got, want) {
163+
t.Errorf("tag PUTs = %v, want %v: no tag is tried after the first failure", got, want)
164+
}
165+
if got := reg.tagAttempts("me/coll"); len(got) != 0 {
166+
t.Errorf("collection metadata PUTs = %v, want none after a failed publish", got)
167+
}
168+
})
169+
}
170+
}

‎internal/cli/publish_parity_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package cli
22

33
import (
4+
"bytes"
5+
"errors"
46
"os"
57
"os/exec"
68
"path/filepath"
79
"reflect"
10+
"slices"
811
"sort"
12+
"strings"
913
"testing"
1014
"time"
1115

@@ -25,6 +29,8 @@ func TestPublishParity(t *testing.T) {
2529
if _, err := os.Stat(goCLI); err != nil {
2630
t.Skip("Go CLI not built")
2731
}
32+
t.Run("tag failure", func(t *testing.T) { publishTagFailureParity(t, repoRoot, goCLI) })
33+
2834
if !isDockerAvailable() {
2935
t.Skip("docker required")
3036
}
@@ -115,3 +121,56 @@ func TestPublishParity(t *testing.T) {
115121
})
116122
}
117123
}
124+
125+
func publishTagFailureParity(t *testing.T, repoRoot, goCLI string) {
126+
for _, collectionType := range []string{"feature", "template"} {
127+
t.Run(collectionType, func(t *testing.T) {
128+
reg := newPushRegistry(t, "1.0")
129+
src := writeTagFailureCollection(t, collectionType)
130+
type side struct {
131+
stdout string
132+
errLines []string
133+
exit int
134+
attempts []string
135+
metadata []string
136+
}
137+
publish := func(cli []string, namespace string) side {
138+
args := append(slices.Clone(cli[1:]), collectionType+"s", "publish", src, "-r", reg.host, "-n", namespace)
139+
cmd := exec.CommandContext(t.Context(), cli[0], args...)
140+
cmd.Dir = repoRoot
141+
cmd.Env = append(os.Environ(), "DOCKER_CONFIG="+t.TempDir())
142+
var stdout, stderr bytes.Buffer
143+
cmd.Stdout, cmd.Stderr = &stdout, &stderr
144+
err := cmd.Run()
145+
var exitErr *exec.ExitError
146+
if err != nil && !errors.As(err, &exitErr) {
147+
t.Fatalf("run %v: %v", cli, err)
148+
}
149+
var errLines []string
150+
for line := range strings.Lines(strings.ReplaceAll(stderr.String(), namespace, "<ns>")) {
151+
for _, marker := range []string{"(!) ERR:", "Failed to PUT manifest for tag"} {
152+
if i := strings.Index(line, marker); i >= 0 {
153+
errLines = append(errLines, strings.TrimSpace(line[i:]))
154+
}
155+
}
156+
}
157+
return side{
158+
stdout: stdout.String(),
159+
errLines: errLines,
160+
exit: cmd.ProcessState.ExitCode(),
161+
attempts: reg.tagAttempts(namespace + "/good"),
162+
metadata: reg.tagAttempts(namespace),
163+
}
164+
}
165+
166+
ts := publish([]string{"node", filepath.Join(repoRoot, "reference", "devcontainer.js")}, "tsns/coll")
167+
goSide := publish([]string{goCLI}, "gons/coll")
168+
if !reflect.DeepEqual(ts, goSide) {
169+
t.Errorf("publish with a rejected tag differs:\n--- TS\n%+v\n--- Go\n%+v", ts, goSide)
170+
}
171+
if ts.exit != 1 || !slices.Equal(ts.attempts, []string{"1", "1.0"}) {
172+
t.Errorf("TS side = %+v: the fake registry did not exercise the failed tag", ts)
173+
}
174+
})
175+
}
176+
}

‎internal/oci/push.go‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,17 @@ func (c *Client) PushArtifact(ctx context.Context, ref *Ref, tgzPath string, tag
7979
return nil, err
8080
}
8181

82-
var publishedTags []string
8382
for _, tag := range tags {
8483
if err := repo.PushReference(ctx, manifestDesc, bytes.NewReader(manifestBytes), tag); err != nil {
85-
c.log.Write(fmt.Sprintf("Failed to push tag %s: %v", tag, err), log.LevelWarning)
86-
continue
84+
c.log.Write(fmt.Sprintf("Failed to PUT manifest for tag %s\n%v", tag, err), log.LevelError)
85+
return nil, fmt.Errorf("push tag %s of %s: %w", tag, ref.Resource, err)
8786
}
88-
publishedTags = append(publishedTags, tag)
8987
c.log.Write(fmt.Sprintf("Published tag: %s", tag), log.LevelInfo)
9088
}
9189

92-
if len(publishedTags) == 0 {
93-
return nil, fmt.Errorf("failed to publish any tags")
94-
}
95-
9690
return &PushResult{
9791
Digest: manifestDesc.Digest.String(),
98-
PublishedTags: publishedTags,
92+
PublishedTags: tags,
9993
}, nil
10094
}
10195

‎internal/oci/push_partial_test.go‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package oci
22

33
import (
4+
"bytes"
5+
"maps"
46
"os"
57
"path/filepath"
8+
"slices"
9+
"strings"
610
"testing"
711

812
"github.com/devcontainers/cli/internal/log"
@@ -17,38 +21,45 @@ func writeTarball(t *testing.T, content string) string {
1721
return p
1822
}
1923

20-
func TestPushArtifact_BearerAuthAndPartialTagFailure(t *testing.T) {
24+
func TestPushArtifact_BearerAuthAndStopsAtFirstFailedTag(t *testing.T) {
2125
const user, pass = "u", "p"
2226
reg := newFakeOCIRegistry()
2327
reg.requireBearer = true
2428
reg.wantUser, reg.wantPass = user, pass
25-
reg.failTags = map[string]bool{"latest": true}
29+
reg.failTags = map[string]bool{"1.0": true}
2630
registry := reg.start(t)
2731

2832
ref, err := ParseRef(registry + "/ns/hello:1.0.0")
2933
if err != nil {
3034
t.Fatal(err)
3135
}
32-
client := NewClient(log.Null, dockerConfigEnv(t, registry, user, pass))
36+
var logs bytes.Buffer
37+
client := NewClient(log.New(log.Options{Writer: &logs, Level: log.LevelInfo}), dockerConfigEnv(t, registry, user, pass))
3338

34-
res, err := client.PushArtifact(t.Context(), ref, writeTarball(t, "layer"), []string{"1.0.0", "latest"}, "feature", nil)
35-
if err != nil {
36-
t.Fatalf("push returned error, want partial success: %v", err)
39+
res, err := client.PushArtifact(t.Context(), ref, writeTarball(t, "layer"), []string{"1", "1.0", "1.0.0", "latest"}, "feature", nil)
40+
if err == nil {
41+
t.Fatalf("push returned %+v, want an error for the rejected tag 1.0 (reference putManifestWithTags returns undefined)", res)
3742
}
38-
39-
if got := res.PublishedTags; len(got) != 1 || got[0] != "1.0.0" {
40-
t.Fatalf("PublishedTags = %v, want [1.0.0] (partial, no rollback)", got)
43+
if res != nil {
44+
t.Errorf("result = %+v, want nil alongside the error", res)
45+
}
46+
if !strings.Contains(err.Error(), "1.0") || !strings.Contains(err.Error(), ref.Resource) {
47+
t.Errorf("error %q does not name the tag and resource", err)
4148
}
49+
if !strings.Contains(logs.String(), "Failed to PUT manifest for tag 1.0\n") {
50+
t.Errorf("log does not report the rejected tag:\n%s", logs.String())
51+
}
52+
4253
reg.mu.Lock()
43-
_, has100 := reg.manifests["1.0.0"]
44-
_, hasLatest := reg.manifests["latest"]
54+
attempted := slices.Clone(reg.putManifestAttempt)
55+
tagged := slices.Sorted(maps.Keys(reg.manifests))
4556
tokenReqs := reg.tokenRequests
4657
reg.mu.Unlock()
47-
if !has100 {
48-
t.Error("1.0.0 tag was not published")
58+
if want := []string{"1", "1.0"}; !slices.Equal(attempted, want) {
59+
t.Errorf("tag PUTs attempted = %v, want %v: tags after the first failure must not be tried", attempted, want)
4960
}
50-
if hasLatest {
51-
t.Error("latest tag was published despite the injected failure")
61+
if want := []string{"1"}; !slices.Equal(tagged, want) {
62+
t.Errorf("tags in registry = %v, want %v (tags before the failure stay pushed)", tagged, want)
5263
}
5364
if tokenReqs == 0 {
5465
t.Error("token endpoint never called; the Bearer auth loop did not run")

‎internal/oci/push_registry_test.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ type fakeOCIRegistry struct {
2727
failTags map[string]bool
2828
failAllManifests bool
2929

30-
tokenRequests int
31-
putManifestOK []string
32-
blobUploads int
33-
blobHeadHits int
30+
tokenRequests int
31+
putManifestAttempt []string
32+
putManifestOK []string
33+
blobUploads int
34+
blobHeadHits int
3435
}
3536

3637
func newFakeOCIRegistry() *fakeOCIRegistry {
@@ -159,6 +160,7 @@ func (r *fakeOCIRegistry) handleManifest(w http.ResponseWriter, req *http.Reques
159160

160161
switch req.Method {
161162
case http.MethodPut:
163+
r.putManifestAttempt = append(r.putManifestAttempt, ref)
162164
if r.failAllManifests || r.failTags[ref] {
163165
// 400 is a non-retryable client error, so oras returns immediately
164166
// (a 5xx would trigger retry backoff and slow the test), while still

0 commit comments

Comments
 (0)