forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
243 lines (192 loc) · 7.85 KB
/
suite.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright 2015 Gravitational, Inc.
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 test contains a backend acceptance test suite that is backend implementation independant
// each backend will use the suite to test itself
package test
import (
"sync/atomic"
"testing"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/backend"
. "gopkg.in/check.v1"
)
func TestBackend(t *testing.T) { TestingT(t) }
type BackendSuite struct {
B backend.Backend
ChangesC chan interface{}
}
func (s *BackendSuite) collectChanges(c *C, expected int) []interface{} {
changes := make([]interface{}, expected)
for i, _ := range changes {
select {
case changes[i] = <-s.ChangesC:
// successfully collected changes
case <-time.After(2 * time.Second):
c.Fatalf("Timeout occured waiting for events")
}
}
return changes
}
func (s *BackendSuite) expectChanges(c *C, expected ...interface{}) {
changes := s.collectChanges(c, len(expected))
for i, ch := range changes {
c.Assert(ch, DeepEquals, expected[i])
}
}
func (s *BackendSuite) BasicCRUD(c *C) {
keys, err := s.B.GetKeys([]string{"keys"})
c.Assert(err, IsNil)
c.Assert(keys, IsNil)
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "bkey", []byte("val1"), 0), IsNil)
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "akey", []byte("val2"), 0), IsNil)
_, err = s.B.GetVal([]string{"a"}, "b")
c.Assert(teleport.IsBadParameter(err), Equals, true, Commentf("%#v", err))
_, _, err = s.B.GetValAndTTL([]string{"a"}, "b")
c.Assert(teleport.IsBadParameter(err), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a", "b"}, "x")
c.Assert(teleport.IsNotFound(err), Equals, true, Commentf("%#v", err))
_, _, err = s.B.GetValAndTTL([]string{"a", "b"}, "x")
c.Assert(teleport.IsNotFound(err), Equals, true, Commentf("%#v", err))
keys, _ = s.B.GetKeys([]string{"a", "b", "bkey"})
c.Assert(len(keys), Equals, 0)
keys, err = s.B.GetKeys([]string{"a", "b"})
c.Assert(err, IsNil)
c.Assert(keys, DeepEquals, []string{"akey", "bkey"})
out, err := s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(out), Equals, "val1")
out, ttl, err := s.B.GetValAndTTL([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(out), Equals, "val1")
c.Assert(ttl, Equals, time.Duration(0))
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "bkey", []byte("val-updated"), 0), IsNil)
out, err = s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(out), Equals, "val-updated")
c.Assert(s.B.DeleteKey([]string{"a", "b"}, "bkey"), IsNil)
c.Assert(teleport.IsNotFound(s.B.DeleteKey([]string{"a", "b"}, "bkey")), Equals, true)
_, err = s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(teleport.IsNotFound(err), Equals, true, Commentf("%#v", err))
c.Assert(s.B.UpsertVal([]string{"a", "c"}, "xkey", []byte("val3"), 0), IsNil)
c.Assert(s.B.UpsertVal([]string{"a", "c"}, "ykey", []byte("val4"), 0), IsNil)
c.Assert(s.B.DeleteBucket([]string{"a"}, "c"), IsNil)
_, err = s.B.GetVal([]string{"a", "c"}, "xkey")
c.Assert(teleport.IsNotFound(err), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a", "c"}, "ykey")
c.Assert(teleport.IsNotFound(err), Equals, true, Commentf("%#v", err))
}
func (s *BackendSuite) CompareAndSwap(c *C) {
prev, err := s.B.CompareAndSwap([]string{"a", "b"}, "bkey", []byte("val10"), 0, []byte("1231"))
c.Assert(teleport.IsNotFound(err), Equals, true, Commentf("%#v", err))
c.Assert(len(prev), Equals, 0)
prev, err = s.B.CompareAndSwap([]string{"a", "b"}, "bkey", []byte("val1"), 0, []byte{})
c.Assert(err, IsNil)
c.Assert(len(prev), Equals, 0)
_, err = s.B.CompareAndSwap([]string{"a", "b"}, "bkey", []byte("val2"), 0, []byte{})
c.Assert(teleport.IsAlreadyExists(err), Equals, true, Commentf("%#v", err))
_, err = s.B.CompareAndSwap([]string{"a", "b"}, "bkey", []byte("val2"), 0, []byte("abcd"))
c.Assert(teleport.IsCompareFailed(err), Equals, true, Commentf("%#v", err))
out, err := s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(out), Equals, "val1")
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "anotherkey", []byte("val3"), 0), IsNil)
prev, err = s.B.CompareAndSwap([]string{"a", "b"}, "bkey", []byte("val4"), 0, []byte("val1"))
c.Assert(err, IsNil)
c.Assert(string(prev), DeepEquals, "val1")
out, err = s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(out), Equals, "val4")
}
func (s *BackendSuite) Expiration(c *C) {
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "bkey", []byte("val1"), time.Second), IsNil)
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "akey", []byte("val2"), 0), IsNil)
time.Sleep(2 * time.Second)
keys, err := s.B.GetKeys([]string{"a", "b"})
c.Assert(err, IsNil)
c.Assert(keys, DeepEquals, []string{"akey"})
}
func (s *BackendSuite) Renewal(c *C) {
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "bkey", []byte("val1"), time.Second), IsNil)
time.Sleep(time.Second)
c.Assert(s.B.TouchVal([]string{"a", "b"}, "bkey", 100*time.Second), IsNil)
val, ttl, err := s.B.GetValAndTTL([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(val), Equals, "val1")
c.Assert(ttl > time.Second, Equals, true)
err = s.B.TouchVal([]string{"a", "b"}, "non-key", 100*time.Second)
c.Assert(teleport.IsNotFound(err), Equals, true)
}
func (s *BackendSuite) Create(c *C) {
c.Assert(s.B.CreateVal([]string{"a", "b"}, "bkey", []byte("val1"), time.Second), IsNil)
err := s.B.CreateVal([]string{"a", "b"}, "bkey", []byte("val2"), 0)
c.Assert(teleport.IsAlreadyExists(err), Equals, true)
val, err := s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(val), Equals, "val1")
}
func (s *BackendSuite) ValueAndTTl(c *C) {
c.Assert(s.B.UpsertVal([]string{"a", "b"}, "bkey",
[]byte("val1"), 2000*time.Millisecond), IsNil)
time.Sleep(1000 * time.Millisecond)
value, ttl, err := s.B.GetValAndTTL([]string{"a", "b"}, "bkey")
c.Assert(err, IsNil)
c.Assert(string(value), DeepEquals, "val1")
ttlIsRight := (ttl < 1400*time.Millisecond) && (ttl > 600*time.Millisecond)
c.Assert(ttlIsRight, Equals, true)
}
func (s *BackendSuite) Locking(c *C) {
tok1 := "token1"
tok2 := "token2"
err := teleport.IsNotFound(s.B.ReleaseLock(tok1))
c.Assert(err, Equals, true, Commentf("%#v", err))
c.Assert(s.B.AcquireLock(tok1, time.Second), IsNil)
x := int32(7)
go func() {
atomic.StoreInt32(&x, 9)
c.Assert(s.B.ReleaseLock(tok1), IsNil)
}()
c.Assert(s.B.AcquireLock(tok1, 0), IsNil)
atomic.AddInt32(&x, 9)
c.Assert(atomic.LoadInt32(&x), Equals, int32(18))
c.Assert(s.B.ReleaseLock(tok1), IsNil)
c.Assert(s.B.AcquireLock(tok1, 0), IsNil)
atomic.StoreInt32(&x, 7)
go func() {
atomic.StoreInt32(&x, 9)
c.Assert(s.B.ReleaseLock(tok1), IsNil)
}()
c.Assert(s.B.AcquireLock(tok1, 0), IsNil)
atomic.AddInt32(&x, 9)
c.Assert(atomic.LoadInt32(&x), Equals, int32(18))
c.Assert(s.B.ReleaseLock(tok1), IsNil)
y := int32(0)
c.Assert(s.B.AcquireLock(tok1, 0), IsNil)
c.Assert(s.B.AcquireLock(tok2, 0), IsNil)
go func() {
atomic.StoreInt32(&y, 15)
c.Assert(s.B.ReleaseLock(tok1), IsNil)
c.Assert(s.B.ReleaseLock(tok2), IsNil)
}()
c.Assert(s.B.AcquireLock(tok1, 0), IsNil)
c.Assert(atomic.LoadInt32(&y), Equals, int32(15))
c.Assert(s.B.ReleaseLock(tok1), IsNil)
c.Assert(s.B.ReleaseLock(tok1), FitsTypeOf, &teleport.NotFoundError{})
}
func toSet(vals []string) map[string]struct{} {
out := make(map[string]struct{}, len(vals))
for _, v := range vals {
out[v] = struct{}{}
}
return out
}