Skip to content

Commit c0cb2f2

Browse files
committed
Add testcase for commit already exist
Signed-off-by: Derek McGowan <[email protected]>
1 parent 83668f4 commit c0cb2f2

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

content/local/store_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/containerd/containerd/content"
3737
"github.com/containerd/containerd/content/testsuite"
38+
"github.com/containerd/containerd/errdefs"
3839
"github.com/containerd/containerd/pkg/testutil"
3940

4041
"github.com/opencontainers/go-digest"
@@ -174,7 +175,9 @@ func TestContentWriter(t *testing.T) {
174175

175176
// now, attempt to write the same data again
176177
checkCopy(t, int64(len(p)), cw, bufio.NewReader(ioutil.NopCloser(bytes.NewReader(p))))
177-
if err := cw.Commit(ctx, int64(len(p)), expected); err != nil {
178+
if err := cw.Commit(ctx, int64(len(p)), expected); err == nil {
179+
t.Fatal("expected already exists error")
180+
} else if !errdefs.IsAlreadyExists(err) {
178181
t.Fatal(err)
179182
}
180183

content/testsuite/testsuite.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"time"
3131

3232
"github.com/containerd/containerd/content"
33+
"github.com/containerd/containerd/errdefs"
3334
"github.com/containerd/containerd/pkg/testutil"
3435
digest "github.com/opencontainers/go-digest"
3536
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -41,6 +42,7 @@ import (
4142
func ContentSuite(t *testing.T, name string, storeFn func(ctx context.Context, root string) (context.Context, content.Store, func() error, error)) {
4243
t.Run("Writer", makeTest(t, name, storeFn, checkContentStoreWriter))
4344
t.Run("UpdateStatus", makeTest(t, name, storeFn, checkUpdateStatus))
45+
t.Run("CommitExists", makeTest(t, name, storeFn, checkCommitExists))
4446
t.Run("Resume", makeTest(t, name, storeFn, checkResumeWriter))
4547
t.Run("ResumeTruncate", makeTest(t, name, storeFn, checkResume(resumeTruncate)))
4648
t.Run("ResumeDiscard", makeTest(t, name, storeFn, checkResume(resumeDiscard)))
@@ -281,6 +283,39 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
281283
}
282284
}
283285

286+
func checkCommitExists(ctx context.Context, t *testing.T, cs content.Store) {
287+
c1, d1 := createContent(256)
288+
if err := content.WriteBlob(ctx, cs, "c1", bytes.NewReader(c1), ocispec.Descriptor{Digest: d1}); err != nil {
289+
t.Fatal(err)
290+
}
291+
292+
for i, tc := range []struct {
293+
expected digest.Digest
294+
}{
295+
{
296+
expected: d1,
297+
},
298+
{},
299+
} {
300+
w, err := cs.Writer(ctx, content.WithRef(fmt.Sprintf("c1-commitexists-%d", i)))
301+
if err != nil {
302+
t.Fatal(err)
303+
}
304+
if _, err := w.Write(c1); err != nil {
305+
w.Close()
306+
t.Fatal(err)
307+
}
308+
err = w.Commit(ctx, int64(len(c1)), tc.expected)
309+
w.Close()
310+
if err == nil {
311+
t.Errorf("(%d) Expected already exists error", i)
312+
} else if !errdefs.IsAlreadyExists(err) {
313+
t.Fatalf("(%d) Unexpected error: %+v", i, err)
314+
}
315+
316+
}
317+
}
318+
284319
func checkUpdateStatus(ctx context.Context, t *testing.T, cs content.Store) {
285320
c1, d1 := createContent(256)
286321

@@ -353,7 +388,7 @@ func checkUpdateStatus(ctx context.Context, t *testing.T, cs content.Store) {
353388
func checkLabels(ctx context.Context, t *testing.T, cs content.Store) {
354389
c1, d1 := createContent(256)
355390

356-
w1, err := cs.Writer(ctx, content.WithRef("c1"), content.WithDescriptor(ocispec.Descriptor{Size: 256, Digest: d1}))
391+
w1, err := cs.Writer(ctx, content.WithRef("c1-checklabels"), content.WithDescriptor(ocispec.Descriptor{Size: 256, Digest: d1}))
357392
if err != nil {
358393
t.Fatal(err)
359394
}

0 commit comments

Comments
 (0)