forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_test.go
175 lines (153 loc) · 4.76 KB
/
lock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2017 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iptables_test
import (
"io"
"io/ioutil"
"os"
"time"
. "github.com/projectcalico/felix/iptables"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("SharedLock", func() {
var lock *SharedLock
var mockIptablesLock *mockLock
var mockErr error
mockGrabLock := func(lockFilePath, socketName string, timeout, probeInterval time.Duration) (io.Closer, error) {
return mockIptablesLock, mockErr
}
BeforeEach(func() {
mockErr = nil
mockIptablesLock = &mockLock{}
lock = NewSharedLock("/foo/bar.lock", time.Second, time.Millisecond)
lock.GrabIptablesLocks = mockGrabLock
})
It("should close the lock after final unlock call", func() {
lock.Lock()
Expect(mockIptablesLock.Closed).To(BeFalse())
lock.Unlock()
Expect(mockIptablesLock.Closed).To(BeTrue())
})
It("should allow multiple holders", func() {
lock.Lock()
lock.Lock()
Expect(mockIptablesLock.Closed).To(BeFalse())
lock.Unlock()
Expect(mockIptablesLock.Closed).To(BeFalse())
lock.Unlock()
Expect(mockIptablesLock.Closed).To(BeTrue())
})
It("should panic on misuse", func() {
Expect(lock.Unlock).To(Panic())
})
It("should panic on failure to acquire", func() {
mockErr = Err14LockTimeout
Expect(lock.Lock).To(Panic())
})
It("should panic on failure to close", func() {
lock.Lock()
mockIptablesLock.Err = Err14LockTimeout
Expect(lock.Unlock).To(Panic())
})
})
type mockLock struct {
Closed bool
Err error
}
func (l *mockLock) Close() error {
Expect(l.Closed).To(BeFalse())
l.Closed = true
return l.Err
}
var _ = Describe("GrabIptablesLocks FV", func() {
var fileName string
BeforeEach(func() {
f, err := ioutil.TempFile("", "iptlocktest")
Expect(err).NotTo(HaveOccurred())
fileName = f.Name()
f.Close()
})
AfterEach(func() {
os.Remove(fileName)
})
It("should block concurrent invocations", func() {
l, err := GrabIptablesLocks(fileName, "@dummytables", 1*time.Second, 50*time.Millisecond)
Expect(err).NotTo(HaveOccurred())
defer l.Close()
l2, err := GrabIptablesLocks(fileName, "@dummytables", 100*time.Millisecond, 10*time.Millisecond)
Expect(err).To(Equal(Err16LockTimeout))
Expect(l2).To(BeNil())
})
It("should allow access after being released", func() {
l, err := GrabIptablesLocks(fileName, "@dummytables", 1*time.Second, 50*time.Millisecond)
Expect(err).NotTo(HaveOccurred())
l.Close()
l2, err := GrabIptablesLocks(fileName, "@dummytables", 1*time.Second, 50*time.Millisecond)
Expect(err).NotTo(HaveOccurred())
l2.Close()
})
It("should block concurrent invocations using only iptables 1.4 version of lock", func() {
l, err := GrabIptablesLocks(fileName, "@dummytables", 1*time.Second, 50*time.Millisecond)
// Sneakily remove the lockfile after it's been locked so that we fall through to
// the v1.4 lock.
os.Remove(fileName)
Expect(err).NotTo(HaveOccurred())
defer l.Close()
l2, err := GrabIptablesLocks(fileName, "@dummytables", 100*time.Millisecond, 10*time.Millisecond)
Expect(err).To(Equal(Err14LockTimeout))
Expect(l2).To(BeNil())
})
It("should allow access after being released using only iptables 1.4 version of lock", func() {
l, err := GrabIptablesLocks(fileName, "@dummytables", 1*time.Second, 50*time.Millisecond)
// Sneakily remove the lockfile after it's been locked so that we fall through to
// the v1.4 lock.
os.Remove(fileName)
Expect(err).NotTo(HaveOccurred())
l.Close()
l2, err := GrabIptablesLocks(fileName, "@dummytables", 1*time.Second, 50*time.Millisecond)
Expect(err).NotTo(HaveOccurred())
l2.Close()
})
})
var _ = Describe("locker", func() {
var l *Locker
var lock14 mockCloser
var lock16 mockCloser
BeforeEach(func() {
lock14 = mockCloser{}
lock16 = mockCloser{}
l = &Locker{
Lock14: &lock14,
Lock16: &lock16,
}
})
It("should return nil with no err", func() {
Expect(l.Close()).NotTo(HaveOccurred())
})
It("should return lock16 err", func() {
lock16.Err = Err16LockTimeout
Expect(l.Close()).To(Equal(Err16LockTimeout))
})
It("should return lock14 err", func() {
lock14.Err = Err14LockTimeout
Expect(l.Close()).To(Equal(Err14LockTimeout))
})
})
type mockCloser struct {
Err error
}
func (c *mockCloser) Close() error {
return c.Err
}