Merge pull request #3324 from crosbymichael/content-close

Ensure close in content test
This commit is contained in:
Michael Crosby 2019-06-05 16:37:05 -04:00 committed by GitHub
commit 40f54dc076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -35,7 +35,6 @@ import (
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/containerd/continuity"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -661,6 +660,19 @@ func writeTimestampFile(p string, t time.Time) error {
if err != nil { if err != nil {
return err return err
} }
return atomicWrite(p, b, 0666)
return continuity.AtomicWriteFile(p, b, 0666) }
func atomicWrite(path string, data []byte, mode os.FileMode) error {
tmp := fmt.Sprintf("%s.tmp", path)
f, err := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_SYNC, mode)
if err != nil {
return errors.Wrap(err, "create tmp file")
}
_, err = f.Write(data)
f.Close()
if err != nil {
return errors.Wrap(err, "write atomic data")
}
return os.Rename(tmp, path)
} }

View File

@ -338,7 +338,7 @@ func checkRefNotAvailable(ctx context.Context, t *testing.T, cs content.Store, r
w, err := cs.Writer(ctx, content.WithRef(ref)) w, err := cs.Writer(ctx, content.WithRef(ref))
if err == nil { if err == nil {
w.Close() defer w.Close()
t.Fatal("writer created with ref, expected to be in use") t.Fatal("writer created with ref, expected to be in use")
} }
if !errdefs.IsUnavailable(err) { if !errdefs.IsUnavailable(err) {
@ -402,6 +402,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
} }
t.Fatalf("Unexpected error: %+v", err) t.Fatalf("Unexpected error: %+v", err)
} }
w.Close()
w, err = cs.Writer(ctx, content.WithRef(ref)) w, err = cs.Writer(ctx, content.WithRef(ref))
if err != nil { if err != nil {
@ -425,6 +426,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
t.Errorf("Unexpected error: %+v", err) t.Errorf("Unexpected error: %+v", err)
} }
w.Close()
w, err = cs.Writer(ctx, content.WithRef(ref)) w, err = cs.Writer(ctx, content.WithRef(ref))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -440,6 +442,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
t.Errorf("Unexpected error: %+v", err) t.Errorf("Unexpected error: %+v", err)
} }
w.Close()
w, err = cs.Writer(ctx, content.WithRef(ref)) w, err = cs.Writer(ctx, content.WithRef(ref))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -455,6 +458,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
t.Fatalf("Failed to commit: %+v", err) t.Fatalf("Failed to commit: %+v", err)
} }
w.Close()
// Create another writer with same reference // Create another writer with same reference
w, err = cs.Writer(ctx, content.WithRef(ref)) w, err = cs.Writer(ctx, content.WithRef(ref))
if err != nil { if err != nil {
@ -481,6 +485,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store)
t.Fatalf("Unexpected error: %+v", err) t.Fatalf("Unexpected error: %+v", err)
} }
w.Close()
w, err = cs.Writer(ctx, content.WithRef(ref)) w, err = cs.Writer(ctx, content.WithRef(ref))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)