restic/internal/lock_test.go

251 lines
6.1 KiB
Go
Raw Normal View History

2015-06-24 16:17:01 +00:00
package restic_test
import (
2017-06-05 21:56:59 +00:00
"context"
2015-06-24 16:17:01 +00:00
"os"
"testing"
"time"
"restic"
2016-09-04 11:24:51 +00:00
"restic/repository"
. "restic/test"
2015-06-24 16:17:01 +00:00
)
func TestLock(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
2017-06-05 21:56:59 +00:00
lock, err := restic.NewLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
OK(t, lock.Unlock())
}
func TestDoubleUnlock(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
2017-06-05 21:56:59 +00:00
lock, err := restic.NewLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
OK(t, lock.Unlock())
err = lock.Unlock()
Assert(t, err != nil,
"double unlock didn't return an error, got %v", err)
}
func TestMultipleLock(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
2017-06-05 21:56:59 +00:00
lock1, err := restic.NewLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
2017-06-05 21:56:59 +00:00
lock2, err := restic.NewLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
OK(t, lock1.Unlock())
OK(t, lock2.Unlock())
}
func TestLockExclusive(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
2017-06-05 21:56:59 +00:00
elock, err := restic.NewExclusiveLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
OK(t, elock.Unlock())
}
func TestLockOnExclusiveLockedRepo(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
2017-06-05 21:56:59 +00:00
elock, err := restic.NewExclusiveLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
2017-06-05 21:56:59 +00:00
lock, err := restic.NewLock(context.TODO(), repo)
Assert(t, err != nil,
2015-06-24 16:17:01 +00:00
"create normal lock with exclusively locked repo didn't return an error")
Assert(t, restic.IsAlreadyLocked(err),
"create normal lock with exclusively locked repo didn't return the correct error")
2015-06-24 16:17:01 +00:00
OK(t, lock.Unlock())
OK(t, elock.Unlock())
}
func TestExclusiveLockOnLockedRepo(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
2017-06-05 21:56:59 +00:00
elock, err := restic.NewLock(context.TODO(), repo)
2015-06-24 16:17:01 +00:00
OK(t, err)
2017-06-05 21:56:59 +00:00
lock, err := restic.NewExclusiveLock(context.TODO(), repo)
Assert(t, err != nil,
"create normal lock with exclusively locked repo didn't return an error")
Assert(t, restic.IsAlreadyLocked(err),
"create normal lock with exclusively locked repo didn't return the correct error")
2015-06-24 16:17:01 +00:00
OK(t, lock.Unlock())
OK(t, elock.Unlock())
}
2016-08-31 18:29:54 +00:00
func createFakeLock(repo restic.Repository, t time.Time, pid int) (restic.ID, error) {
hostname, err := os.Hostname()
if err != nil {
2016-08-31 18:29:54 +00:00
return restic.ID{}, err
}
newLock := &restic.Lock{Time: t, PID: pid, Hostname: hostname}
2017-06-05 21:56:59 +00:00
return repo.SaveJSONUnpacked(context.TODO(), restic.LockFile, &newLock)
2015-06-24 16:17:01 +00:00
}
2016-08-31 18:29:54 +00:00
func removeLock(repo restic.Repository, id restic.ID) error {
h := restic.Handle{Type: restic.LockFile, Name: id.String()}
2017-06-05 21:56:59 +00:00
return repo.Backend().Remove(context.TODO(), h)
2015-06-24 16:17:01 +00:00
}
var staleLockTests = []struct {
timestamp time.Time
stale bool
staleOnOtherHost bool
pid int
2015-06-24 16:17:01 +00:00
}{
{
timestamp: time.Now(),
stale: false,
staleOnOtherHost: false,
pid: os.Getpid(),
2015-06-24 16:17:01 +00:00
},
{
timestamp: time.Now().Add(-time.Hour),
stale: true,
staleOnOtherHost: true,
pid: os.Getpid(),
2015-06-24 16:17:01 +00:00
},
{
timestamp: time.Now().Add(3 * time.Minute),
stale: false,
staleOnOtherHost: false,
pid: os.Getpid(),
2015-06-24 16:17:01 +00:00
},
{
timestamp: time.Now(),
stale: true,
staleOnOtherHost: false,
pid: os.Getpid() + 500000,
2015-06-24 16:17:01 +00:00
},
}
func TestLockStale(t *testing.T) {
hostname, err := os.Hostname()
OK(t, err)
otherHostname := "other-" + hostname
2015-06-24 16:17:01 +00:00
for i, test := range staleLockTests {
lock := restic.Lock{
Time: test.timestamp,
PID: test.pid,
Hostname: hostname,
2015-06-24 16:17:01 +00:00
}
Assert(t, lock.Stale() == test.stale,
"TestStaleLock: test %d failed: expected stale: %v, got %v",
i, test.stale, !test.stale)
lock.Hostname = otherHostname
Assert(t, lock.Stale() == test.staleOnOtherHost,
"TestStaleLock: test %d failed: expected staleOnOtherHost: %v, got %v",
i, test.staleOnOtherHost, !test.staleOnOtherHost)
2015-06-24 16:17:01 +00:00
}
}
2016-08-31 18:29:54 +00:00
func lockExists(repo restic.Repository, t testing.TB, id restic.ID) bool {
h := restic.Handle{Type: restic.LockFile, Name: id.String()}
2017-06-05 21:56:59 +00:00
exists, err := repo.Backend().Test(context.TODO(), h)
2015-06-24 16:17:01 +00:00
OK(t, err)
return exists
}
func TestLockWithStaleLock(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-06-24 16:17:01 +00:00
id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid())
OK(t, err)
id2, err := createFakeLock(repo, time.Now().Add(-time.Minute), os.Getpid())
OK(t, err)
id3, err := createFakeLock(repo, time.Now().Add(-time.Minute), os.Getpid()+500000)
2015-06-24 16:17:01 +00:00
OK(t, err)
2017-06-05 21:56:59 +00:00
OK(t, restic.RemoveStaleLocks(context.TODO(), repo))
2015-06-24 16:17:01 +00:00
Assert(t, lockExists(repo, t, id1) == false,
"stale lock still exists after RemoveStaleLocks was called")
Assert(t, lockExists(repo, t, id2) == true,
"non-stale lock was removed by RemoveStaleLocks")
Assert(t, lockExists(repo, t, id3) == false,
"stale lock still exists after RemoveStaleLocks was called")
OK(t, removeLock(repo, id2))
}
2015-06-27 12:26:33 +00:00
func TestRemoveAllLocks(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid())
OK(t, err)
id2, err := createFakeLock(repo, time.Now().Add(-time.Minute), os.Getpid())
OK(t, err)
id3, err := createFakeLock(repo, time.Now().Add(-time.Minute), os.Getpid()+500000)
OK(t, err)
2017-06-05 21:56:59 +00:00
OK(t, restic.RemoveAllLocks(context.TODO(), repo))
Assert(t, lockExists(repo, t, id1) == false,
"lock still exists after RemoveAllLocks was called")
Assert(t, lockExists(repo, t, id2) == false,
"lock still exists after RemoveAllLocks was called")
Assert(t, lockExists(repo, t, id3) == false,
"lock still exists after RemoveAllLocks was called")
}
2015-07-12 19:02:00 +00:00
func TestLockRefresh(t *testing.T) {
2016-09-04 11:24:51 +00:00
repo, cleanup := repository.TestRepository(t)
2016-09-04 10:52:43 +00:00
defer cleanup()
2015-07-12 19:02:00 +00:00
2017-06-05 21:56:59 +00:00
lock, err := restic.NewLock(context.TODO(), repo)
2015-07-12 19:02:00 +00:00
OK(t, err)
2016-08-31 18:29:54 +00:00
var lockID *restic.ID
2017-06-05 21:56:59 +00:00
for id := range repo.List(context.TODO(), restic.LockFile) {
2015-07-12 19:02:00 +00:00
if lockID != nil {
t.Error("more than one lock found")
}
lockID = &id
2015-07-12 19:02:00 +00:00
}
2017-06-05 21:56:59 +00:00
OK(t, lock.Refresh(context.TODO()))
2015-07-12 19:02:00 +00:00
2016-08-31 18:29:54 +00:00
var lockID2 *restic.ID
2017-06-05 21:56:59 +00:00
for id := range repo.List(context.TODO(), restic.LockFile) {
2015-07-12 19:02:00 +00:00
if lockID2 != nil {
t.Error("more than one lock found")
}
lockID2 = &id
2015-07-12 19:02:00 +00:00
}
Assert(t, !lockID.Equal(*lockID2),
2015-07-12 19:02:00 +00:00
"expected a new ID after lock refresh, got the same")
OK(t, lock.Unlock())
}