2021-11-14 16:38:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-04-07 17:51:31 +00:00
|
|
|
"runtime"
|
2023-02-16 15:58:36 +00:00
|
|
|
"strings"
|
2021-11-14 16:38:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2023-02-16 15:58:36 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2021-11-14 16:38:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func openTestRepo(t *testing.T, wrapper backendWrapper) (*repository.Repository, func(), *testEnvironment) {
|
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
if wrapper != nil {
|
|
|
|
env.gopts.backendTestHook = wrapper
|
|
|
|
}
|
|
|
|
testRunInit(t, env.gopts)
|
|
|
|
|
|
|
|
repo, err := OpenRepository(context.TODO(), env.gopts)
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
2021-11-14 16:38:56 +00:00
|
|
|
return repo, cleanup, env
|
|
|
|
}
|
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
func checkedLockRepo(ctx context.Context, t *testing.T, repo restic.Repository, env *testEnvironment) (*restic.Lock, context.Context) {
|
|
|
|
lock, wrappedCtx, err := lockRepo(ctx, repo, env.gopts.RetryLock, env.gopts.JSON)
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.OK(t, wrappedCtx.Err())
|
2021-11-14 16:38:56 +00:00
|
|
|
if lock.Stale() {
|
|
|
|
t.Fatal("lock returned stale lock")
|
|
|
|
}
|
|
|
|
return lock, wrappedCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLock(t *testing.T) {
|
2023-02-16 15:58:36 +00:00
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
2021-11-14 16:38:56 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, env)
|
2021-11-14 16:38:56 +00:00
|
|
|
unlockRepo(lock)
|
|
|
|
if wrappedCtx.Err() == nil {
|
|
|
|
t.Fatal("unlock did not cancel context")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCancel(t *testing.T) {
|
2023-02-16 15:58:36 +00:00
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
2021-11-14 16:38:56 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(ctx, t, repo, env)
|
2021-11-14 16:38:56 +00:00
|
|
|
cancel()
|
|
|
|
if wrappedCtx.Err() == nil {
|
|
|
|
t.Fatal("canceled parent context did not cancel context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlockRepo should not crash
|
|
|
|
unlockRepo(lock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockUnlockAll(t *testing.T) {
|
2023-02-16 15:58:36 +00:00
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
2021-11-14 16:38:56 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, env)
|
2021-11-14 16:38:56 +00:00
|
|
|
_, err := unlockAll(0)
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
2021-11-14 16:38:56 +00:00
|
|
|
if wrappedCtx.Err() == nil {
|
|
|
|
t.Fatal("canceled parent context did not cancel context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlockRepo should not crash
|
|
|
|
unlockRepo(lock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockConflict(t *testing.T) {
|
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
repo2, err := OpenRepository(context.TODO(), env.gopts)
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
2021-11-14 16:38:56 +00:00
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, _, err := lockRepoExclusive(context.Background(), repo, env.gopts.RetryLock, env.gopts.JSON)
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
2021-11-14 16:38:56 +00:00
|
|
|
defer unlockRepo(lock)
|
2023-02-16 15:58:36 +00:00
|
|
|
_, _, err = lockRepo(context.Background(), repo2, env.gopts.RetryLock, env.gopts.JSON)
|
2021-11-14 16:38:56 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("second lock should have failed")
|
|
|
|
}
|
2023-06-02 21:16:49 +00:00
|
|
|
test.Assert(t, restic.IsAlreadyLocked(err), "unexpected error %v", err)
|
2021-11-14 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type writeOnceBackend struct {
|
|
|
|
restic.Backend
|
|
|
|
written bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *writeOnceBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
|
|
|
if b.written {
|
|
|
|
return fmt.Errorf("fail after first write")
|
|
|
|
}
|
|
|
|
b.written = true
|
|
|
|
return b.Backend.Save(ctx, h, rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockFailedRefresh(t *testing.T) {
|
2023-02-16 15:58:36 +00:00
|
|
|
repo, cleanup, env := openTestRepo(t, func(r restic.Backend) (restic.Backend, error) {
|
2021-11-14 16:38:56 +00:00
|
|
|
return &writeOnceBackend{Backend: r}, nil
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// reduce locking intervals to be suitable for testing
|
|
|
|
ri, rt := refreshInterval, refreshabilityTimeout
|
|
|
|
refreshInterval = 20 * time.Millisecond
|
|
|
|
refreshabilityTimeout = 100 * time.Millisecond
|
|
|
|
defer func() {
|
|
|
|
refreshInterval, refreshabilityTimeout = ri, rt
|
|
|
|
}()
|
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, env)
|
2021-11-14 16:38:56 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wrappedCtx.Done():
|
|
|
|
// expected lock refresh failure
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("failed lock refresh did not cause context cancellation")
|
|
|
|
}
|
|
|
|
// unlockRepo should not crash
|
|
|
|
unlockRepo(lock)
|
|
|
|
}
|
2022-10-29 09:26:00 +00:00
|
|
|
|
2022-12-03 11:05:38 +00:00
|
|
|
type loggingBackend struct {
|
|
|
|
restic.Backend
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *loggingBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
|
|
|
b.t.Logf("save %v @ %v", h, time.Now())
|
2023-04-07 17:43:00 +00:00
|
|
|
err := b.Backend.Save(ctx, h, rd)
|
|
|
|
b.t.Logf("save finished %v @ %v", h, time.Now())
|
|
|
|
return err
|
2022-12-03 11:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 09:26:00 +00:00
|
|
|
func TestLockSuccessfulRefresh(t *testing.T) {
|
2023-02-16 15:58:36 +00:00
|
|
|
repo, cleanup, env := openTestRepo(t, func(r restic.Backend) (restic.Backend, error) {
|
2022-12-03 11:05:38 +00:00
|
|
|
return &loggingBackend{
|
|
|
|
Backend: r,
|
|
|
|
t: t,
|
|
|
|
}, nil
|
|
|
|
})
|
2022-10-29 09:26:00 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2022-12-03 11:05:38 +00:00
|
|
|
t.Logf("test for successful lock refresh %v", time.Now())
|
2022-10-29 09:26:00 +00:00
|
|
|
// reduce locking intervals to be suitable for testing
|
|
|
|
ri, rt := refreshInterval, refreshabilityTimeout
|
2023-04-22 10:45:59 +00:00
|
|
|
refreshInterval = 60 * time.Millisecond
|
|
|
|
refreshabilityTimeout = 500 * time.Millisecond
|
2022-10-29 09:26:00 +00:00
|
|
|
defer func() {
|
|
|
|
refreshInterval, refreshabilityTimeout = ri, rt
|
|
|
|
}()
|
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, env)
|
2022-10-29 09:26:00 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wrappedCtx.Done():
|
2023-04-07 17:43:00 +00:00
|
|
|
// don't call t.Fatal to allow the lock to be properly cleaned up
|
|
|
|
t.Error("lock refresh failed", time.Now())
|
2023-04-07 17:51:31 +00:00
|
|
|
|
|
|
|
// Dump full stacktrace
|
|
|
|
buf := make([]byte, 1024*1024)
|
|
|
|
n := runtime.Stack(buf, true)
|
|
|
|
buf = buf[:n]
|
|
|
|
t.Log(string(buf))
|
|
|
|
|
2022-10-29 09:26:00 +00:00
|
|
|
case <-time.After(2 * refreshabilityTimeout):
|
|
|
|
// expected lock refresh to work
|
|
|
|
}
|
|
|
|
// unlockRepo should not crash
|
|
|
|
unlockRepo(lock)
|
|
|
|
}
|
2023-02-16 15:58:36 +00:00
|
|
|
|
|
|
|
func TestLockWaitTimeout(t *testing.T) {
|
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
elock, _, err := lockRepoExclusive(context.TODO(), repo, env.gopts.RetryLock, env.gopts.JSON)
|
|
|
|
test.OK(t, err)
|
|
|
|
|
2023-04-22 10:45:59 +00:00
|
|
|
retryLock := 200 * time.Millisecond
|
2023-02-16 15:58:36 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
lock, _, err := lockRepo(context.TODO(), repo, retryLock, env.gopts.JSON)
|
|
|
|
duration := time.Since(start)
|
|
|
|
|
|
|
|
test.Assert(t, err != nil,
|
|
|
|
"create normal lock with exclusively locked repo didn't return an error")
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), "repository is already locked exclusively"),
|
|
|
|
"create normal lock with exclusively locked repo didn't return the correct error")
|
2023-04-22 10:45:59 +00:00
|
|
|
test.Assert(t, retryLock <= duration && duration < retryLock*3/2,
|
2023-02-16 15:58:36 +00:00
|
|
|
"create normal lock with exclusively locked repo didn't wait for the specified timeout")
|
|
|
|
|
|
|
|
test.OK(t, lock.Unlock())
|
|
|
|
test.OK(t, elock.Unlock())
|
|
|
|
}
|
|
|
|
func TestLockWaitCancel(t *testing.T) {
|
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
elock, _, err := lockRepoExclusive(context.TODO(), repo, env.gopts.RetryLock, env.gopts.JSON)
|
|
|
|
test.OK(t, err)
|
|
|
|
|
2023-04-22 10:45:59 +00:00
|
|
|
retryLock := 200 * time.Millisecond
|
2023-02-16 15:58:36 +00:00
|
|
|
cancelAfter := 40 * time.Millisecond
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
time.AfterFunc(cancelAfter, cancel)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
lock, _, err := lockRepo(ctx, repo, retryLock, env.gopts.JSON)
|
|
|
|
duration := time.Since(start)
|
|
|
|
|
|
|
|
test.Assert(t, err != nil,
|
|
|
|
"create normal lock with exclusively locked repo didn't return an error")
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), "context canceled"),
|
|
|
|
"create normal lock with exclusively locked repo didn't return the correct error")
|
2023-04-22 10:45:59 +00:00
|
|
|
test.Assert(t, cancelAfter <= duration && duration < retryLock-10*time.Millisecond,
|
2023-02-16 15:58:36 +00:00
|
|
|
"create normal lock with exclusively locked repo didn't return in time")
|
|
|
|
|
|
|
|
test.OK(t, lock.Unlock())
|
|
|
|
test.OK(t, elock.Unlock())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockWaitSuccess(t *testing.T) {
|
|
|
|
repo, cleanup, env := openTestRepo(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
elock, _, err := lockRepoExclusive(context.TODO(), repo, env.gopts.RetryLock, env.gopts.JSON)
|
|
|
|
test.OK(t, err)
|
|
|
|
|
2023-04-22 10:45:59 +00:00
|
|
|
retryLock := 200 * time.Millisecond
|
2023-02-16 15:58:36 +00:00
|
|
|
unlockAfter := 40 * time.Millisecond
|
|
|
|
|
|
|
|
time.AfterFunc(unlockAfter, func() {
|
|
|
|
test.OK(t, elock.Unlock())
|
|
|
|
})
|
|
|
|
|
|
|
|
lock, _, err := lockRepo(context.TODO(), repo, retryLock, env.gopts.JSON)
|
|
|
|
test.OK(t, err)
|
|
|
|
|
|
|
|
test.OK(t, lock.Unlock())
|
|
|
|
}
|