github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/purgeuploads_test.go (about) 1 package storage 2 3 import ( 4 "path" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/docker/distribution/context" 10 "github.com/docker/distribution/registry/storage/driver" 11 "github.com/docker/distribution/registry/storage/driver/inmemory" 12 "github.com/docker/distribution/uuid" 13 ) 14 15 func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) (driver.StorageDriver, context.Context) { 16 d := inmemory.New() 17 ctx := context.Background() 18 for i := 0; i < numUploads; i++ { 19 addUploads(ctx, t, d, uuid.Generate().String(), repoName, startedAt) 20 } 21 return d, ctx 22 } 23 24 func addUploads(ctx context.Context, t *testing.T, d driver.StorageDriver, uploadID, repo string, startedAt time.Time) { 25 dataPath, err := pathFor(uploadDataPathSpec{name: repo, id: uploadID}) 26 if err != nil { 27 t.Fatalf("Unable to resolve path") 28 } 29 if err := d.PutContent(ctx, dataPath, []byte("")); err != nil { 30 t.Fatalf("Unable to write data file") 31 } 32 33 startedAtPath, err := pathFor(uploadStartedAtPathSpec{name: repo, id: uploadID}) 34 if err != nil { 35 t.Fatalf("Unable to resolve path") 36 } 37 38 if d.PutContent(ctx, startedAtPath, []byte(startedAt.Format(time.RFC3339))); err != nil { 39 t.Fatalf("Unable to write startedAt file") 40 } 41 42 } 43 44 func TestPurgeGather(t *testing.T) { 45 uploadCount := 5 46 fs, ctx := testUploadFS(t, uploadCount, "test-repo", time.Now()) 47 uploadData, errs := getOutstandingUploads(ctx, fs) 48 if len(errs) != 0 { 49 t.Errorf("Unexepected errors: %q", errs) 50 } 51 if len(uploadData) != uploadCount { 52 t.Errorf("Unexpected upload file count: %d != %d", uploadCount, len(uploadData)) 53 } 54 } 55 56 func TestPurgeNone(t *testing.T) { 57 fs, ctx := testUploadFS(t, 10, "test-repo", time.Now()) 58 oneHourAgo := time.Now().Add(-1 * time.Hour) 59 deleted, errs := PurgeUploads(ctx, fs, oneHourAgo, true) 60 if len(errs) != 0 { 61 t.Error("Unexpected errors", errs) 62 } 63 if len(deleted) != 0 { 64 t.Errorf("Unexpectedly deleted files for time: %s", oneHourAgo) 65 } 66 } 67 68 func TestPurgeAll(t *testing.T) { 69 uploadCount := 10 70 oneHourAgo := time.Now().Add(-1 * time.Hour) 71 fs, ctx := testUploadFS(t, uploadCount, "test-repo", oneHourAgo) 72 73 // Ensure > 1 repos are purged 74 addUploads(ctx, t, fs, uuid.Generate().String(), "test-repo2", oneHourAgo) 75 uploadCount++ 76 77 deleted, errs := PurgeUploads(ctx, fs, time.Now(), true) 78 if len(errs) != 0 { 79 t.Error("Unexpected errors:", errs) 80 } 81 fileCount := uploadCount 82 if len(deleted) != fileCount { 83 t.Errorf("Unexpectedly deleted file count %d != %d", 84 len(deleted), fileCount) 85 } 86 } 87 88 func TestPurgeSome(t *testing.T) { 89 oldUploadCount := 5 90 oneHourAgo := time.Now().Add(-1 * time.Hour) 91 fs, ctx := testUploadFS(t, oldUploadCount, "library/test-repo", oneHourAgo) 92 93 newUploadCount := 4 94 95 for i := 0; i < newUploadCount; i++ { 96 addUploads(ctx, t, fs, uuid.Generate().String(), "test-repo", time.Now().Add(1*time.Hour)) 97 } 98 99 deleted, errs := PurgeUploads(ctx, fs, time.Now(), true) 100 if len(errs) != 0 { 101 t.Error("Unexpected errors:", errs) 102 } 103 if len(deleted) != oldUploadCount { 104 t.Errorf("Unexpectedly deleted file count %d != %d", 105 len(deleted), oldUploadCount) 106 } 107 } 108 109 func TestPurgeOnlyUploads(t *testing.T) { 110 oldUploadCount := 5 111 oneHourAgo := time.Now().Add(-1 * time.Hour) 112 fs, ctx := testUploadFS(t, oldUploadCount, "test-repo", oneHourAgo) 113 114 // Create a directory tree outside _uploads and ensure 115 // these files aren't deleted. 116 dataPath, err := pathFor(uploadDataPathSpec{name: "test-repo", id: uuid.Generate().String()}) 117 if err != nil { 118 t.Fatalf(err.Error()) 119 } 120 nonUploadPath := strings.Replace(dataPath, "_upload", "_important", -1) 121 if strings.Index(nonUploadPath, "_upload") != -1 { 122 t.Fatalf("Non-upload path not created correctly") 123 } 124 125 nonUploadFile := path.Join(nonUploadPath, "file") 126 if err = fs.PutContent(ctx, nonUploadFile, []byte("")); err != nil { 127 t.Fatalf("Unable to write data file") 128 } 129 130 deleted, errs := PurgeUploads(ctx, fs, time.Now(), true) 131 if len(errs) != 0 { 132 t.Error("Unexpected errors", errs) 133 } 134 for _, file := range deleted { 135 if strings.Index(file, "_upload") == -1 { 136 t.Errorf("Non-upload file deleted") 137 } 138 } 139 } 140 141 func TestPurgeMissingStartedAt(t *testing.T) { 142 oneHourAgo := time.Now().Add(-1 * time.Hour) 143 fs, ctx := testUploadFS(t, 1, "test-repo", oneHourAgo) 144 145 err := Walk(ctx, fs, "/", func(fileInfo driver.FileInfo) error { 146 filePath := fileInfo.Path() 147 _, file := path.Split(filePath) 148 149 if file == "startedat" { 150 if err := fs.Delete(ctx, filePath); err != nil { 151 t.Fatalf("Unable to delete startedat file: %s", filePath) 152 } 153 } 154 return nil 155 }) 156 if err != nil { 157 t.Fatalf("Unexpected error during Walk: %s ", err.Error()) 158 } 159 deleted, errs := PurgeUploads(ctx, fs, time.Now(), true) 160 if len(errs) > 0 { 161 t.Errorf("Unexpected errors") 162 } 163 if len(deleted) > 0 { 164 t.Errorf("Files unexpectedly deleted: %s", deleted) 165 } 166 }