Skip to content

Commit 40f54dc

Browse files
Merge pull request #3324 from crosbymichael/content-close
Ensure close in content test
2 parents 04e7747 + cf7fb14 commit 40f54dc

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

content/local/store.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/containerd/containerd/log"
3636
"github.com/sirupsen/logrus"
3737

38-
"github.com/containerd/continuity"
3938
digest "github.com/opencontainers/go-digest"
4039
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
4140
"github.com/pkg/errors"
@@ -661,6 +660,19 @@ func writeTimestampFile(p string, t time.Time) error {
661660
if err != nil {
662661
return err
663662
}
663+
return atomicWrite(p, b, 0666)
664+
}
664665

665-
return continuity.AtomicWriteFile(p, b, 0666)
666+
func atomicWrite(path string, data []byte, mode os.FileMode) error {
667+
tmp := fmt.Sprintf("%s.tmp", path)
668+
f, err := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_SYNC, mode)
669+
if err != nil {
670+
return errors.Wrap(err, "create tmp file")
671+
}
672+
_, err = f.Write(data)
673+
f.Close()
674+
if err != nil {
675+
return errors.Wrap(err, "write atomic data")
676+
}
677+
return os.Rename(tmp, path)
666678
}

content/testsuite/testsuite.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func checkRefNotAvailable(ctx context.Context, t *testing.T, cs content.Store, r
338338

339339
w, err := cs.Writer(ctx, content.WithRef(ref))
340340
if err == nil {
341-
w.Close()
341+
defer w.Close()
342342
t.Fatal("writer created with ref, expected to be in use")
343343
}
344344
if !errdefs.IsUnavailable(err) {
@@ -402,6 +402,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
402402
}
403403
t.Fatalf("Unexpected error: %+v", err)
404404
}
405+
w.Close()
405406

406407
w, err = cs.Writer(ctx, content.WithRef(ref))
407408
if err != nil {
@@ -425,6 +426,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
425426
t.Errorf("Unexpected error: %+v", err)
426427
}
427428

429+
w.Close()
428430
w, err = cs.Writer(ctx, content.WithRef(ref))
429431
if err != nil {
430432
t.Fatal(err)
@@ -440,6 +442,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
440442
t.Errorf("Unexpected error: %+v", err)
441443
}
442444

445+
w.Close()
443446
w, err = cs.Writer(ctx, content.WithRef(ref))
444447
if err != nil {
445448
t.Fatal(err)
@@ -455,6 +458,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
455458
t.Fatalf("Failed to commit: %+v", err)
456459
}
457460

461+
w.Close()
458462
// Create another writer with same reference
459463
w, err = cs.Writer(ctx, content.WithRef(ref))
460464
if err != nil {
@@ -481,6 +485,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
481485
t.Fatalf("Unexpected error: %+v", err)
482486
}
483487

488+
w.Close()
484489
w, err = cs.Writer(ctx, content.WithRef(ref))
485490
if err != nil {
486491
t.Fatal(err)

0 commit comments

Comments
 (0)