Skip to content

Commit

Permalink
Fix relocking already-locked nodes. Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Feb 3, 2022
1 parent becd79a commit 13889fb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pkg/storage/utils/decomposedfs/node/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (n *Node) SetLock(ctx context.Context, lock *provider.Lock) error {
lockID, _ := ctxpkg.ContextGetLockID(ctx)
if l.LockId != lockID {
return errtypes.Locked(l.LockId)
} else {
err := os.Remove(n.LockFilePath())
if err != nil {
return err
}
}
}

Expand Down
45 changes: 35 additions & 10 deletions pkg/storage/utils/decomposedfs/node/locks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ var _ = Describe("Node locks", func() {
var (
env *helpers.TestEnv

lock *provider.Lock
n *node.Node
id string
name string
lock *provider.Lock
wrongLock *provider.Lock
n *node.Node
id string
name string
)

BeforeEach(func() {
Expand All @@ -53,6 +54,11 @@ var _ = Describe("Node locks", func() {
User: env.Owner.Id,
LockId: uuid.New().String(),
}
wrongLock = &provider.Lock{
Type: provider.LockType_LOCK_TYPE_EXCL,
User: env.Owner.Id,
LockId: uuid.New().String(),
}
id = "fooId"
name = "foo"
n = node.New(id, "", name, 10, "", env.Owner.Id, env.Lookup)
Expand All @@ -75,6 +81,27 @@ var _ = Describe("Node locks", func() {
_, err = os.Stat(n.LockFilePath())
Expect(err).ToNot(HaveOccurred())
})

It("refuses to lock if already locked an existing lock was not provided", func() {
err := n.SetLock(env.Ctx, lock)
Expect(err).ToNot(HaveOccurred())

err = n.SetLock(env.Ctx, lock)
Expect(err).To(HaveOccurred())

env.Ctx = ctxpkg.ContextSetLockID(env.Ctx, wrongLock.LockId)
err = n.SetLock(env.Ctx, lock)
Expect(err).To(HaveOccurred())
})

It("relocks if the existing lock was provided", func() {
err := n.SetLock(env.Ctx, lock)
Expect(err).ToNot(HaveOccurred())

env.Ctx = ctxpkg.ContextSetLockID(env.Ctx, lock.LockId)
err = n.SetLock(env.Ctx, lock)
Expect(err).ToNot(HaveOccurred())
})
})

Context("with an existing lock", func() {
Expand All @@ -90,12 +117,10 @@ var _ = Describe("Node locks", func() {
})

It("refuses to unlock without having the proper lock", func() {
wrongLock := &provider.Lock{
Type: provider.LockType_LOCK_TYPE_EXCL,
User: env.Owner.Id,
LockId: uuid.New().String(),
}
err := n.Unlock(env.Ctx, wrongLock)
err := n.Unlock(env.Ctx, nil)
Expect(err.Error()).To(ContainSubstring(lock.LockId))

err = n.Unlock(env.Ctx, wrongLock)
Expect(err.Error()).To(ContainSubstring(lock.LockId))
})

Expand Down

0 comments on commit 13889fb

Please sign in to comment.