2015-07-29 18:12:01 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-08-29 20:39:24 +00:00
|
|
|
"sync"
|
2015-07-29 18:12:01 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/context"
|
2016-01-27 00:42:10 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-07-29 18:12:01 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
|
|
|
)
|
|
|
|
|
2016-01-27 00:42:10 +00:00
|
|
|
func testRefs(t *testing.T) (reference.Reference, reference.Reference, reference.Reference) {
|
|
|
|
ref1, err := reference.Parse("testrepo@sha256:aaaaeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not parse reference: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ref2, err := reference.Parse("testrepo@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not parse reference: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ref3, err := reference.Parse("testrepo@sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not parse reference: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref1, ref2, ref3
|
|
|
|
}
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
func TestSchedule(t *testing.T) {
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1, ref2, ref3 := testRefs(t)
|
2015-07-29 18:12:01 +00:00
|
|
|
timeUnit := time.Millisecond
|
|
|
|
remainingRepos := map[string]bool{
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1.String(): true,
|
|
|
|
ref2.String(): true,
|
|
|
|
ref3.String(): true,
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 20:39:24 +00:00
|
|
|
var mu sync.Mutex
|
2015-07-29 18:12:01 +00:00
|
|
|
s := New(context.Background(), inmemory.New(), "/ttl")
|
2016-01-27 00:42:10 +00:00
|
|
|
deleteFunc := func(repoName reference.Reference) error {
|
2015-07-29 18:12:01 +00:00
|
|
|
if len(remainingRepos) == 0 {
|
|
|
|
t.Fatalf("Incorrect expiry count")
|
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
_, ok := remainingRepos[repoName.String()]
|
2015-07-29 18:12:01 +00:00
|
|
|
if !ok {
|
2016-02-23 21:33:38 +00:00
|
|
|
t.Fatalf("Trying to remove nonexistent repo: %s", repoName)
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
2015-10-15 00:22:52 +00:00
|
|
|
t.Log("removing", repoName)
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Lock()
|
2016-01-27 00:42:10 +00:00
|
|
|
delete(remainingRepos, repoName.String())
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.onBlobExpire = deleteFunc
|
2015-10-15 00:22:52 +00:00
|
|
|
err := s.Start()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error starting ttlExpirationScheduler: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:42:10 +00:00
|
|
|
s.add(ref1, 3*timeUnit, entryTypeBlob)
|
|
|
|
s.add(ref2, 1*timeUnit, entryTypeBlob)
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
func() {
|
2016-08-29 20:39:24 +00:00
|
|
|
s.Lock()
|
2016-01-27 00:42:10 +00:00
|
|
|
s.add(ref3, 1*timeUnit, entryTypeBlob)
|
2016-08-29 20:39:24 +00:00
|
|
|
s.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Ensure all repos are deleted
|
|
|
|
<-time.After(50 * timeUnit)
|
2016-08-29 20:39:24 +00:00
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
if len(remainingRepos) != 0 {
|
|
|
|
t.Fatalf("Repositories remaining: %#v", remainingRepos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestoreOld(t *testing.T) {
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1, ref2, _ := testRefs(t)
|
2015-07-29 18:12:01 +00:00
|
|
|
remainingRepos := map[string]bool{
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1.String(): true,
|
|
|
|
ref2.String(): true,
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 20:39:24 +00:00
|
|
|
var mu sync.Mutex
|
2016-01-27 00:42:10 +00:00
|
|
|
deleteFunc := func(r reference.Reference) error {
|
|
|
|
if r.String() == ref1.String() && len(remainingRepos) == 2 {
|
|
|
|
t.Errorf("ref1 should be removed first")
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
_, ok := remainingRepos[r.String()]
|
2015-07-29 18:12:01 +00:00
|
|
|
if !ok {
|
2016-02-23 21:33:38 +00:00
|
|
|
t.Fatalf("Trying to remove nonexistent repo: %s", r)
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Lock()
|
2016-01-27 00:42:10 +00:00
|
|
|
delete(remainingRepos, r.String())
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
timeUnit := time.Millisecond
|
|
|
|
serialized, err := json.Marshal(&map[string]schedulerEntry{
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1.String(): {
|
2015-07-29 18:12:01 +00:00
|
|
|
Expiry: time.Now().Add(1 * timeUnit),
|
2016-01-27 00:42:10 +00:00
|
|
|
Key: ref1.String(),
|
2015-07-29 18:12:01 +00:00
|
|
|
EntryType: 0,
|
|
|
|
},
|
2016-01-27 00:42:10 +00:00
|
|
|
ref2.String(): {
|
2015-07-29 18:12:01 +00:00
|
|
|
Expiry: time.Now().Add(-3 * timeUnit), // TTL passed, should be removed first
|
2016-01-27 00:42:10 +00:00
|
|
|
Key: ref2.String(),
|
2015-07-29 18:12:01 +00:00
|
|
|
EntryType: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error serializing test data: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
pathToStatFile := "/ttl"
|
|
|
|
fs := inmemory.New()
|
|
|
|
err = fs.PutContent(ctx, pathToStatFile, serialized)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to write serialized data to fs")
|
|
|
|
}
|
|
|
|
s := New(context.Background(), fs, "/ttl")
|
|
|
|
s.onBlobExpire = deleteFunc
|
2015-10-15 00:22:52 +00:00
|
|
|
err = s.Start()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error starting ttlExpirationScheduler: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(50 * timeUnit)
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
if len(remainingRepos) != 0 {
|
|
|
|
t.Fatalf("Repositories remaining: %#v", remainingRepos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopRestore(t *testing.T) {
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1, ref2, _ := testRefs(t)
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
timeUnit := time.Millisecond
|
|
|
|
remainingRepos := map[string]bool{
|
2016-01-27 00:42:10 +00:00
|
|
|
ref1.String(): true,
|
|
|
|
ref2.String(): true,
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
|
2016-08-29 20:39:24 +00:00
|
|
|
var mu sync.Mutex
|
2016-01-27 00:42:10 +00:00
|
|
|
deleteFunc := func(r reference.Reference) error {
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Lock()
|
2016-01-27 00:42:10 +00:00
|
|
|
delete(remainingRepos, r.String())
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := inmemory.New()
|
|
|
|
pathToStateFile := "/ttl"
|
|
|
|
s := New(context.Background(), fs, pathToStateFile)
|
|
|
|
s.onBlobExpire = deleteFunc
|
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
err := s.Start()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
s.add(ref1, 300*timeUnit, entryTypeBlob)
|
|
|
|
s.add(ref2, 100*timeUnit, entryTypeBlob)
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
// Start and stop before all operations complete
|
|
|
|
// state will be written to fs
|
2015-10-15 00:22:52 +00:00
|
|
|
s.Stop()
|
2015-07-29 18:12:01 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
// v2 will restore state from fs
|
|
|
|
s2 := New(context.Background(), fs, pathToStateFile)
|
|
|
|
s2.onBlobExpire = deleteFunc
|
2015-10-15 00:22:52 +00:00
|
|
|
err = s2.Start()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error starting v2: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(500 * timeUnit)
|
2016-08-29 20:39:24 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
if len(remainingRepos) != 0 {
|
|
|
|
t.Fatalf("Repositories remaining: %#v", remainingRepos)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoubleStart(t *testing.T) {
|
|
|
|
s := New(context.Background(), inmemory.New(), "/ttl")
|
2015-10-15 00:22:52 +00:00
|
|
|
err := s.Start()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to start scheduler")
|
|
|
|
}
|
2015-10-15 00:22:52 +00:00
|
|
|
err = s.Start()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Scheduler started twice without error")
|
|
|
|
}
|
|
|
|
}
|