github.com/lusis/distribution@v2.0.1+incompatible/registry/storage/purgeuploads_test.go (about)

     1  package storage
     2  
     3  import (
     4  	"path"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"code.google.com/p/go-uuid/uuid"
    10  	"github.com/docker/distribution/registry/storage/driver"
    11  	"github.com/docker/distribution/registry/storage/driver/inmemory"
    12  )
    13  
    14  var pm = defaultPathMapper
    15  
    16  func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) driver.StorageDriver {
    17  	d := inmemory.New()
    18  	for i := 0; i < numUploads; i++ {
    19  		addUploads(t, d, uuid.New(), repoName, startedAt)
    20  	}
    21  	return d
    22  }
    23  
    24  func addUploads(t *testing.T, d driver.StorageDriver, uploadID, repo string, startedAt time.Time) {
    25  	dataPath, err := pm.path(uploadDataPathSpec{name: repo, uuid: uploadID})
    26  	if err != nil {
    27  		t.Fatalf("Unable to resolve path")
    28  	}
    29  	if err := d.PutContent(dataPath, []byte("")); err != nil {
    30  		t.Fatalf("Unable to write data file")
    31  	}
    32  
    33  	startedAtPath, err := pm.path(uploadStartedAtPathSpec{name: repo, uuid: uploadID})
    34  	if err != nil {
    35  		t.Fatalf("Unable to resolve path")
    36  	}
    37  
    38  	if d.PutContent(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 := testUploadFS(t, uploadCount, "test-repo", time.Now())
    47  	uploadData, errs := getOutstandingUploads(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 := testUploadFS(t, 10, "test-repo", time.Now())
    58  	oneHourAgo := time.Now().Add(-1 * time.Hour)
    59  	deleted, errs := PurgeUploads(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 := testUploadFS(t, uploadCount, "test-repo", oneHourAgo)
    72  
    73  	// Ensure > 1 repos are purged
    74  	addUploads(t, fs, uuid.New(), "test-repo2", oneHourAgo)
    75  	uploadCount++
    76  
    77  	deleted, errs := PurgeUploads(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 := testUploadFS(t, oldUploadCount, "library/test-repo", oneHourAgo)
    92  
    93  	newUploadCount := 4
    94  
    95  	for i := 0; i < newUploadCount; i++ {
    96  		addUploads(t, fs, uuid.New(), "test-repo", time.Now().Add(1*time.Hour))
    97  	}
    98  
    99  	deleted, errs := PurgeUploads(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 := 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 := pm.path(uploadDataPathSpec{name: "test-repo", uuid: uuid.New()})
   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(nonUploadFile, []byte("")); err != nil {
   127  		t.Fatalf("Unable to write data file")
   128  	}
   129  
   130  	deleted, errs := PurgeUploads(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 := testUploadFS(t, 1, "test-repo", oneHourAgo)
   144  	err := Walk(fs, "/", func(fileInfo driver.FileInfo) error {
   145  		filePath := fileInfo.Path()
   146  		_, file := path.Split(filePath)
   147  
   148  		if file == "startedat" {
   149  			if err := fs.Delete(filePath); err != nil {
   150  				t.Fatalf("Unable to delete startedat file: %s", filePath)
   151  			}
   152  		}
   153  		return nil
   154  	})
   155  	if err != nil {
   156  		t.Fatalf("Unexpected error during Walk: %s ", err.Error())
   157  	}
   158  	deleted, errs := PurgeUploads(fs, time.Now(), true)
   159  	if len(errs) > 0 {
   160  		t.Errorf("Unexpected errors")
   161  	}
   162  	if len(deleted) > 0 {
   163  		t.Errorf("Files unexpectedly deleted: %s", deleted)
   164  	}
   165  }