forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
260 lines (208 loc) · 7.99 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
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 independent
// each backend will use the suite to test itself
package test
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/fixtures"
"github.com/gravitational/trace"
. "gopkg.in/check.v1"
)
var _ = fmt.Printf
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 occurred 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) {
bucket := []string{"test", "create"}
c.Assert(s.B.CreateVal(bucket, "one", []byte("1"), backend.Forever), IsNil)
err := s.B.CreateVal(bucket, "one", []byte("2"), backend.Forever)
c.Assert(trace.IsAlreadyExists(err), Equals, true)
val, err := s.B.GetVal(bucket, "one")
c.Assert(err, IsNil)
c.Assert(string(val), Equals, "1")
keys, err := s.B.GetKeys([]string{"keys"})
c.Assert(err, IsNil)
c.Assert(keys, HasLen, 0)
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(trace.IsBadParameter(err), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a"}, "b")
c.Assert(trace.IsBadParameter(err), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a", "b"}, "x")
c.Assert(trace.IsNotFound(err), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a", "b"}, "x")
c.Assert(trace.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, 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"}, "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(trace.IsNotFound(s.B.DeleteKey([]string{"a", "b"}, "bkey")), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a", "b"}, "bkey")
c.Assert(trace.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)
c.Assert(s.B.DeleteBucket([]string{"a"}, "c"), NotNil)
_, err = s.B.GetVal([]string{"a", "c"}, "xkey")
c.Assert(trace.IsNotFound(err), Equals, true, Commentf("%#v", err))
_, err = s.B.GetVal([]string{"a", "c"}, "ykey")
c.Assert(trace.IsNotFound(err), Equals, true, Commentf("%#v", err))
}
// CompareAndSwap tests compare and swap functionality
func (s *BackendSuite) CompareAndSwap(c *C) {
bucket := []string{"test", "cas"}
// compare and swap on non existing operation will fail
err := s.B.CompareAndSwapVal(bucket, "one", []byte("1"), []byte("2"), backend.Forever)
fixtures.ExpectCompareFailed(c, err)
err = s.B.CreateVal(bucket, "one", []byte("1"), backend.Forever)
c.Assert(err, IsNil)
// success CAS!
err = s.B.CompareAndSwapVal(bucket, "one", []byte("2"), []byte("1"), backend.Forever)
c.Assert(err, IsNil)
val, err := s.B.GetVal(bucket, "one")
c.Assert(err, IsNil)
c.Assert(string(val), Equals, "2")
// value has been updated - not '1' any more
err = s.B.CompareAndSwapVal(bucket, "one", []byte("3"), []byte("1"), backend.Forever)
fixtures.ExpectCompareFailed(c, err)
// existing value has not been changed by the failed CAS operation
val, err = s.B.GetVal(bucket, "one")
c.Assert(err, IsNil)
c.Assert(string(val), Equals, "2")
}
// BatchCRUD tests batch CRUD operations.
func (s *BackendSuite) BatchCRUD(c *C) {
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)
items, err := s.B.GetItems([]string{"a", "b"})
c.Assert(err, IsNil)
c.Assert(len(items), Equals, 2)
c.Assert(string(items[0].Value), Equals, "val2")
c.Assert(items[0].Key, Equals, "akey")
c.Assert(string(items[1].Value), Equals, "val1")
c.Assert(items[1].Key, Equals, "bkey")
}
// Directories checks directories access
func (s *BackendSuite) Directories(c *C) {
bucket := []string{"level1", "level2", "level3"}
c.Assert(s.B.UpsertVal(bucket, "key", []byte("val"), 0), IsNil)
keys, err := s.B.GetKeys(bucket[:2])
c.Assert(err, IsNil)
c.Assert(keys, DeepEquals, []string{"level3"})
keys, err = s.B.GetKeys(bucket[:1])
c.Assert(err, IsNil)
c.Assert(keys, DeepEquals, []string{"level2"})
}
func (s *BackendSuite) Expiration(c *C) {
bucket := []string{"one", "two"}
c.Assert(s.B.UpsertVal(bucket, "bkey", []byte("val1"), time.Second), IsNil)
c.Assert(s.B.UpsertVal(bucket, "akey", []byte("val2"), 0), IsNil)
time.Sleep(2 * time.Second)
keys, err := s.B.GetKeys(bucket)
c.Assert(err, IsNil)
c.Assert(keys, DeepEquals, []string{"akey"})
}
func (s *BackendSuite) ValueAndTTL(c *C) {
bucket := []string{"test", "ttl"}
c.Assert(s.B.UpsertVal(bucket, "bkey",
[]byte("val1"), 2*time.Second), IsNil)
time.Sleep(time.Second)
value, err := s.B.GetVal(bucket, "bkey")
c.Assert(err, IsNil)
c.Assert(string(value), DeepEquals, "val1")
}
func (s *BackendSuite) Locking(c *C) {
tok1 := "token1"
tok2 := "token2"
ttl := time.Second * 5
err := s.B.ReleaseLock(tok1)
c.Assert(trace.IsNotFound(err), Equals, true, Commentf("%#v", err))
c.Assert(s.B.AcquireLock(tok1, ttl), IsNil)
x := int32(7)
go func() {
atomic.StoreInt32(&x, 9)
c.Assert(s.B.ReleaseLock(tok1), IsNil)
}()
c.Assert(s.B.AcquireLock(tok1, ttl), 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, ttl), IsNil)
atomic.StoreInt32(&x, 7)
go func() {
atomic.StoreInt32(&x, 9)
c.Assert(s.B.ReleaseLock(tok1), IsNil)
}()
c.Assert(s.B.AcquireLock(tok1, ttl), 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, ttl), IsNil)
c.Assert(s.B.AcquireLock(tok2, ttl), 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, ttl), IsNil)
c.Assert(atomic.LoadInt32(&y), Equals, int32(15))
c.Assert(s.B.ReleaseLock(tok1), IsNil)
err = s.B.ReleaseLock(tok1)
c.Assert(trace.IsNotFound(err), Equals, true, Commentf("%T", err))
}
func toSet(vals []string) map[string]struct{} {
out := make(map[string]struct{}, len(vals))
for _, v := range vals {
out[v] = struct{}{}
}
return out
}