2015-04-07 22:52:48 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-04-07 22:52:48 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
|
2023-10-25 11:00:01 +00:00
|
|
|
"github.com/google/uuid"
|
2015-04-07 22:52:48 +00:00
|
|
|
)
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) (driver.StorageDriver, context.Context) {
|
2015-04-07 22:52:48 +00:00
|
|
|
d := inmemory.New()
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := context.Background()
|
2015-04-07 22:52:48 +00:00
|
|
|
for i := 0; i < numUploads; i++ {
|
2023-10-25 11:00:01 +00:00
|
|
|
addUploads(ctx, t, d, uuid.NewString(), repoName, startedAt)
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
2015-04-27 22:58:58 +00:00
|
|
|
return d, ctx
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
func addUploads(ctx context.Context, t *testing.T, d driver.StorageDriver, uploadID, repo string, startedAt time.Time) {
|
2015-08-18 01:51:05 +00:00
|
|
|
dataPath, err := pathFor(uploadDataPathSpec{name: repo, id: uploadID})
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to resolve path")
|
|
|
|
}
|
2015-04-27 22:58:58 +00:00
|
|
|
if err := d.PutContent(ctx, dataPath, []byte("")); err != nil {
|
2015-04-07 22:52:48 +00:00
|
|
|
t.Fatalf("Unable to write data file")
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:51:05 +00:00
|
|
|
startedAtPath, err := pathFor(uploadStartedAtPathSpec{name: repo, id: uploadID})
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to resolve path")
|
|
|
|
}
|
|
|
|
|
2023-11-18 06:50:40 +00:00
|
|
|
if err := d.PutContent(ctx, startedAtPath, []byte(startedAt.Format(time.RFC3339))); err != nil {
|
2015-04-07 22:52:48 +00:00
|
|
|
t.Fatalf("Unable to write startedAt file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeGather(t *testing.T) {
|
|
|
|
uploadCount := 5
|
2015-04-27 22:58:58 +00:00
|
|
|
fs, ctx := testUploadFS(t, uploadCount, "test-repo", time.Now())
|
|
|
|
uploadData, errs := getOutstandingUploads(ctx, fs)
|
2015-04-07 22:52:48 +00:00
|
|
|
if len(errs) != 0 {
|
2022-01-24 12:02:57 +00:00
|
|
|
t.Errorf("Unexpected errors: %q", errs)
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
if len(uploadData) != uploadCount {
|
|
|
|
t.Errorf("Unexpected upload file count: %d != %d", uploadCount, len(uploadData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeNone(t *testing.T) {
|
2015-04-27 22:58:58 +00:00
|
|
|
fs, ctx := testUploadFS(t, 10, "test-repo", time.Now())
|
2015-04-07 22:52:48 +00:00
|
|
|
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
2015-04-27 22:58:58 +00:00
|
|
|
deleted, errs := PurgeUploads(ctx, fs, oneHourAgo, true)
|
2015-04-07 22:52:48 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Error("Unexpected errors", errs)
|
|
|
|
}
|
|
|
|
if len(deleted) != 0 {
|
|
|
|
t.Errorf("Unexpectedly deleted files for time: %s", oneHourAgo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeAll(t *testing.T) {
|
|
|
|
uploadCount := 10
|
|
|
|
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
2015-04-27 22:58:58 +00:00
|
|
|
fs, ctx := testUploadFS(t, uploadCount, "test-repo", oneHourAgo)
|
2015-04-07 22:52:48 +00:00
|
|
|
|
|
|
|
// Ensure > 1 repos are purged
|
2023-10-25 11:00:01 +00:00
|
|
|
addUploads(ctx, t, fs, uuid.NewString(), "test-repo2", oneHourAgo)
|
2015-04-07 22:52:48 +00:00
|
|
|
uploadCount++
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
2015-04-07 22:52:48 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Error("Unexpected errors:", errs)
|
|
|
|
}
|
|
|
|
fileCount := uploadCount
|
|
|
|
if len(deleted) != fileCount {
|
|
|
|
t.Errorf("Unexpectedly deleted file count %d != %d",
|
|
|
|
len(deleted), fileCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeSome(t *testing.T) {
|
|
|
|
oldUploadCount := 5
|
|
|
|
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
2015-04-27 22:58:58 +00:00
|
|
|
fs, ctx := testUploadFS(t, oldUploadCount, "library/test-repo", oneHourAgo)
|
2015-04-07 22:52:48 +00:00
|
|
|
|
|
|
|
newUploadCount := 4
|
|
|
|
|
|
|
|
for i := 0; i < newUploadCount; i++ {
|
2023-10-25 11:00:01 +00:00
|
|
|
addUploads(ctx, t, fs, uuid.NewString(), "test-repo", time.Now().Add(1*time.Hour))
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
2015-04-07 22:52:48 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Error("Unexpected errors:", errs)
|
|
|
|
}
|
|
|
|
if len(deleted) != oldUploadCount {
|
|
|
|
t.Errorf("Unexpectedly deleted file count %d != %d",
|
|
|
|
len(deleted), oldUploadCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeOnlyUploads(t *testing.T) {
|
|
|
|
oldUploadCount := 5
|
|
|
|
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
2015-04-27 22:58:58 +00:00
|
|
|
fs, ctx := testUploadFS(t, oldUploadCount, "test-repo", oneHourAgo)
|
2015-04-07 22:52:48 +00:00
|
|
|
|
|
|
|
// Create a directory tree outside _uploads and ensure
|
|
|
|
// these files aren't deleted.
|
2023-10-25 11:00:01 +00:00
|
|
|
dataPath, err := pathFor(uploadDataPathSpec{name: "test-repo", id: uuid.NewString()})
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
nonUploadPath := strings.Replace(dataPath, "_upload", "_important", -1)
|
2019-02-05 00:01:04 +00:00
|
|
|
if strings.Contains(nonUploadPath, "_upload") {
|
2015-04-07 22:52:48 +00:00
|
|
|
t.Fatalf("Non-upload path not created correctly")
|
|
|
|
}
|
|
|
|
|
|
|
|
nonUploadFile := path.Join(nonUploadPath, "file")
|
2015-04-27 22:58:58 +00:00
|
|
|
if err = fs.PutContent(ctx, nonUploadFile, []byte("")); err != nil {
|
2015-04-07 22:52:48 +00:00
|
|
|
t.Fatalf("Unable to write data file")
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
2015-04-07 22:52:48 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Error("Unexpected errors", errs)
|
|
|
|
}
|
|
|
|
for _, file := range deleted {
|
2019-02-05 00:01:04 +00:00
|
|
|
if !strings.Contains(file, "_upload") {
|
2015-04-07 22:52:48 +00:00
|
|
|
t.Errorf("Non-upload file deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurgeMissingStartedAt(t *testing.T) {
|
|
|
|
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
2015-04-27 22:58:58 +00:00
|
|
|
fs, ctx := testUploadFS(t, 1, "test-repo", oneHourAgo)
|
|
|
|
|
2018-01-18 20:10:20 +00:00
|
|
|
err := fs.Walk(ctx, "/", func(fileInfo driver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
filePath := fileInfo.Path()
|
|
|
|
_, file := path.Split(filePath)
|
|
|
|
|
|
|
|
if file == "startedat" {
|
2015-04-27 22:58:58 +00:00
|
|
|
if err := fs.Delete(ctx, filePath); err != nil {
|
2015-04-07 22:52:48 +00:00
|
|
|
t.Fatalf("Unable to delete startedat file: %s", filePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error during Walk: %s ", err.Error())
|
|
|
|
}
|
2015-04-27 22:58:58 +00:00
|
|
|
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
|
2015-04-07 22:52:48 +00:00
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Errorf("Unexpected errors")
|
|
|
|
}
|
|
|
|
if len(deleted) > 0 {
|
|
|
|
t.Errorf("Files unexpectedly deleted: %s", deleted)
|
|
|
|
}
|
|
|
|
}
|