Skip to content

Commit 56f9c44

Browse files
dmcgowanestesp
authored andcommitted
Add testcase for commit already exist
Signed-off-by: Derek McGowan <[email protected]>
1 parent 00a121f commit 56f9c44

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
@@ -36,6 +36,7 @@ import (
3636

3737
"github.com/containerd/containerd/content"
3838
"github.com/containerd/containerd/content/testsuite"
39+
"github.com/containerd/containerd/errdefs"
3940
"github.com/containerd/containerd/testutil"
4041
"github.com/gotestyourself/gotestyourself/assert"
4142
"github.com/opencontainers/go-digest"
@@ -173,7 +174,9 @@ func TestContentWriter(t *testing.T) {
173174

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

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/testutil"
3435
"github.com/gotestyourself/gotestyourself/assert"
3536
digest "github.com/opencontainers/go-digest"
@@ -40,6 +41,7 @@ import (
4041
func ContentSuite(t *testing.T, name string, storeFn func(ctx context.Context, root string) (context.Context, content.Store, func() error, error)) {
4142
t.Run("Writer", makeTest(t, name, storeFn, checkContentStoreWriter))
4243
t.Run("UpdateStatus", makeTest(t, name, storeFn, checkUpdateStatus))
44+
t.Run("CommitExists", makeTest(t, name, storeFn, checkCommitExists))
4345
t.Run("Resume", makeTest(t, name, storeFn, checkResumeWriter))
4446
t.Run("ResumeTruncate", makeTest(t, name, storeFn, checkResume(resumeTruncate)))
4547
t.Run("ResumeDiscard", makeTest(t, name, storeFn, checkResume(resumeDiscard)))
@@ -280,6 +282,39 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
280282
}
281283
}
282284

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

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

355-
w1, err := cs.Writer(ctx, "c1", 256, d1)
390+
w1, err := cs.Writer(ctx, "c1-checklabels", 256, d1)
356391
if err != nil {
357392
t.Fatal(err)
358393
}

0 commit comments

Comments
 (0)