Make Storage Driver API calls context aware.
- Change driver interface to take a context as its first argument - Make newFileReader take a context as its first argument - Make newFileWriter take a context as its first argument - Make blobstore exists and delete take a context as a first argument - Pass the layerreader's context to the storage layer - Pass the app's context to purgeuploads - Store the app's context into the blobstore (was previously null) - Pass the trace'd context to the storage drivers Signed-off-by: Richard Scothern <richard.scothern@gmail.com>
This commit is contained in:
parent
c0d297c011
commit
5d9105bd25
30 changed files with 383 additions and 343 deletions
|
@ -7,26 +7,28 @@ import (
|
|||
"time"
|
||||
|
||||
"code.google.com/p/go-uuid/uuid"
|
||||
"github.com/docker/distribution/context"
|
||||
"github.com/docker/distribution/registry/storage/driver"
|
||||
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
||||
)
|
||||
|
||||
var pm = defaultPathMapper
|
||||
|
||||
func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) driver.StorageDriver {
|
||||
func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) (driver.StorageDriver, context.Context) {
|
||||
d := inmemory.New()
|
||||
ctx := context.Background()
|
||||
for i := 0; i < numUploads; i++ {
|
||||
addUploads(t, d, uuid.New(), repoName, startedAt)
|
||||
addUploads(ctx, t, d, uuid.New(), repoName, startedAt)
|
||||
}
|
||||
return d
|
||||
return d, ctx
|
||||
}
|
||||
|
||||
func addUploads(t *testing.T, d driver.StorageDriver, uploadID, repo string, startedAt time.Time) {
|
||||
func addUploads(ctx context.Context, t *testing.T, d driver.StorageDriver, uploadID, repo string, startedAt time.Time) {
|
||||
dataPath, err := pm.path(uploadDataPathSpec{name: repo, uuid: uploadID})
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to resolve path")
|
||||
}
|
||||
if err := d.PutContent(dataPath, []byte("")); err != nil {
|
||||
if err := d.PutContent(ctx, dataPath, []byte("")); err != nil {
|
||||
t.Fatalf("Unable to write data file")
|
||||
}
|
||||
|
||||
|
@ -35,7 +37,7 @@ func addUploads(t *testing.T, d driver.StorageDriver, uploadID, repo string, sta
|
|||
t.Fatalf("Unable to resolve path")
|
||||
}
|
||||
|
||||
if d.PutContent(startedAtPath, []byte(startedAt.Format(time.RFC3339))); err != nil {
|
||||
if d.PutContent(ctx, startedAtPath, []byte(startedAt.Format(time.RFC3339))); err != nil {
|
||||
t.Fatalf("Unable to write startedAt file")
|
||||
}
|
||||
|
||||
|
@ -43,8 +45,8 @@ func addUploads(t *testing.T, d driver.StorageDriver, uploadID, repo string, sta
|
|||
|
||||
func TestPurgeGather(t *testing.T) {
|
||||
uploadCount := 5
|
||||
fs := testUploadFS(t, uploadCount, "test-repo", time.Now())
|
||||
uploadData, errs := getOutstandingUploads(fs)
|
||||
fs, ctx := testUploadFS(t, uploadCount, "test-repo", time.Now())
|
||||
uploadData, errs := getOutstandingUploads(ctx, fs)
|
||||
if len(errs) != 0 {
|
||||
t.Errorf("Unexepected errors: %q", errs)
|
||||
}
|
||||
|
@ -54,9 +56,9 @@ func TestPurgeGather(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPurgeNone(t *testing.T) {
|
||||
fs := testUploadFS(t, 10, "test-repo", time.Now())
|
||||
fs, ctx := testUploadFS(t, 10, "test-repo", time.Now())
|
||||
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
||||
deleted, errs := PurgeUploads(fs, oneHourAgo, true)
|
||||
deleted, errs := PurgeUploads(ctx, fs, oneHourAgo, true)
|
||||
if len(errs) != 0 {
|
||||
t.Error("Unexpected errors", errs)
|
||||
}
|
||||
|
@ -68,13 +70,13 @@ func TestPurgeNone(t *testing.T) {
|
|||
func TestPurgeAll(t *testing.T) {
|
||||
uploadCount := 10
|
||||
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
||||
fs := testUploadFS(t, uploadCount, "test-repo", oneHourAgo)
|
||||
fs, ctx := testUploadFS(t, uploadCount, "test-repo", oneHourAgo)
|
||||
|
||||
// Ensure > 1 repos are purged
|
||||
addUploads(t, fs, uuid.New(), "test-repo2", oneHourAgo)
|
||||
addUploads(ctx, t, fs, uuid.New(), "test-repo2", oneHourAgo)
|
||||
uploadCount++
|
||||
|
||||
deleted, errs := PurgeUploads(fs, time.Now(), true)
|
||||
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
||||
if len(errs) != 0 {
|
||||
t.Error("Unexpected errors:", errs)
|
||||
}
|
||||
|
@ -88,15 +90,15 @@ func TestPurgeAll(t *testing.T) {
|
|||
func TestPurgeSome(t *testing.T) {
|
||||
oldUploadCount := 5
|
||||
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
||||
fs := testUploadFS(t, oldUploadCount, "library/test-repo", oneHourAgo)
|
||||
fs, ctx := testUploadFS(t, oldUploadCount, "library/test-repo", oneHourAgo)
|
||||
|
||||
newUploadCount := 4
|
||||
|
||||
for i := 0; i < newUploadCount; i++ {
|
||||
addUploads(t, fs, uuid.New(), "test-repo", time.Now().Add(1*time.Hour))
|
||||
addUploads(ctx, t, fs, uuid.New(), "test-repo", time.Now().Add(1*time.Hour))
|
||||
}
|
||||
|
||||
deleted, errs := PurgeUploads(fs, time.Now(), true)
|
||||
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
||||
if len(errs) != 0 {
|
||||
t.Error("Unexpected errors:", errs)
|
||||
}
|
||||
|
@ -109,7 +111,7 @@ func TestPurgeSome(t *testing.T) {
|
|||
func TestPurgeOnlyUploads(t *testing.T) {
|
||||
oldUploadCount := 5
|
||||
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
||||
fs := testUploadFS(t, oldUploadCount, "test-repo", oneHourAgo)
|
||||
fs, ctx := testUploadFS(t, oldUploadCount, "test-repo", oneHourAgo)
|
||||
|
||||
// Create a directory tree outside _uploads and ensure
|
||||
// these files aren't deleted.
|
||||
|
@ -123,11 +125,11 @@ func TestPurgeOnlyUploads(t *testing.T) {
|
|||
}
|
||||
|
||||
nonUploadFile := path.Join(nonUploadPath, "file")
|
||||
if err = fs.PutContent(nonUploadFile, []byte("")); err != nil {
|
||||
if err = fs.PutContent(ctx, nonUploadFile, []byte("")); err != nil {
|
||||
t.Fatalf("Unable to write data file")
|
||||
}
|
||||
|
||||
deleted, errs := PurgeUploads(fs, time.Now(), true)
|
||||
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
||||
if len(errs) != 0 {
|
||||
t.Error("Unexpected errors", errs)
|
||||
}
|
||||
|
@ -140,13 +142,14 @@ func TestPurgeOnlyUploads(t *testing.T) {
|
|||
|
||||
func TestPurgeMissingStartedAt(t *testing.T) {
|
||||
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
||||
fs := testUploadFS(t, 1, "test-repo", oneHourAgo)
|
||||
err := Walk(fs, "/", func(fileInfo driver.FileInfo) error {
|
||||
fs, ctx := testUploadFS(t, 1, "test-repo", oneHourAgo)
|
||||
|
||||
err := Walk(ctx, fs, "/", func(fileInfo driver.FileInfo) error {
|
||||
filePath := fileInfo.Path()
|
||||
_, file := path.Split(filePath)
|
||||
|
||||
if file == "startedat" {
|
||||
if err := fs.Delete(filePath); err != nil {
|
||||
if err := fs.Delete(ctx, filePath); err != nil {
|
||||
t.Fatalf("Unable to delete startedat file: %s", filePath)
|
||||
}
|
||||
}
|
||||
|
@ -155,7 +158,7 @@ func TestPurgeMissingStartedAt(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Unexpected error during Walk: %s ", err.Error())
|
||||
}
|
||||
deleted, errs := PurgeUploads(fs, time.Now(), true)
|
||||
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
||||
if len(errs) > 0 {
|
||||
t.Errorf("Unexpected errors")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue