forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcluster_test.go
255 lines (232 loc) · 7.29 KB
/
testcluster_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
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
package gocb
import (
"errors"
"math"
"time"
"github.com/couchbaselabs/gojcbmock"
)
type FeatureCode int
var (
srvVer180 = NodeVersion{1, 8, 0, 0, "", false}
srvVer200 = NodeVersion{2, 0, 0, 0, "", false}
srvVer250 = NodeVersion{2, 5, 0, 0, "", false}
srvVer300 = NodeVersion{3, 0, 0, 0, "", false}
srvVer400 = NodeVersion{4, 0, 0, 0, "", false}
srvVer450 = NodeVersion{4, 5, 0, 0, "", false}
srvVer500 = NodeVersion{5, 0, 0, 0, "", false}
srvVer550 = NodeVersion{5, 5, 0, 0, "", false}
srvVer551 = NodeVersion{5, 5, 1, 0, "", false}
srvVer552 = NodeVersion{5, 5, 2, 0, "", false}
srvVer553 = NodeVersion{5, 5, 3, 0, "", false}
srvVer600 = NodeVersion{6, 0, 0, 0, "", false}
srvVer650 = NodeVersion{6, 5, 0, 0, "", false}
mockVer156 = NodeVersion{1, 5, 6, 0, "", true}
mockVer1513 = NodeVersion{1, 5, 13, 0, "", true}
mockVer1515 = NodeVersion{1, 5, 15, 0, "", true}
)
var (
KeyValueFeature = FeatureCode(1)
ViewFeature = FeatureCode(2)
CccpFeature = FeatureCode(3)
SslFeature = FeatureCode(4)
DcpFeature = FeatureCode(5)
SpatialViewFeature = FeatureCode(6)
N1qlFeature = FeatureCode(7)
SubdocFeature = FeatureCode(8)
KvErrorMapFeature = FeatureCode(9)
RbacFeature = FeatureCode(10)
FtsFeature = FeatureCode(11)
EnhancedErrorsFeature = FeatureCode(12)
FtsIndexFeature = FeatureCode(13)
CompressionFeature = FeatureCode(14)
ServerSideTracingFeature = FeatureCode(15)
AnalyticsFeature = FeatureCode(16)
XattrFeature = FeatureCode(17)
CollectionsFeature = FeatureCode(18)
SubdocMockBugFeature = FeatureCode(19)
AdjoinFeature = FeatureCode(20)
ExpandMacrosFeature = FeatureCode(21)
DurabilityFeature = FeatureCode(22)
UserGroupFeature = FeatureCode(23)
UserManagerFeature = FeatureCode(24)
AnalyticsIndexFeature = FeatureCode(25)
BucketMgrFeature = FeatureCode(26)
FtsAnalyzeFeature = FeatureCode(27)
AnalyticsIndexPendingMutationsFeature = FeatureCode(28)
)
type testClusterErrorWrap struct {
InnerError error
Message string
}
func (e testClusterErrorWrap) Error() string {
return e.Message + ": " + e.InnerError.Error()
}
func (e testClusterErrorWrap) Unwrap() error {
return e.InnerError
}
type testCluster struct {
*Cluster
Mock *gojcbmock.Mock
Version *NodeVersion
}
func (c *testCluster) isMock() bool {
return c.Mock != nil
}
func (c *testCluster) SupportsFeature(feature FeatureCode) bool {
supported := false
if c.Version.IsMock {
supported = true
switch feature {
case RbacFeature:
supported = !c.Version.Lower(mockVer156)
case FtsIndexFeature:
supported = false
case CompressionFeature:
supported = !c.Version.Lower(mockVer1513)
case ServerSideTracingFeature:
supported = !c.Version.Lower(mockVer1515)
case AnalyticsFeature:
supported = false
case N1qlFeature:
supported = false
case XattrFeature:
supported = false
case CollectionsFeature:
supported = false
case SubdocMockBugFeature:
supported = false
case ExpandMacrosFeature:
supported = false
case DurabilityFeature:
supported = false
case UserGroupFeature:
supported = false
case UserManagerFeature:
supported = false
case AnalyticsIndexFeature:
supported = false
case BucketMgrFeature:
supported = false
case FtsAnalyzeFeature:
supported = false
case AnalyticsIndexPendingMutationsFeature:
supported = false
}
} else {
switch feature {
case KeyValueFeature:
supported = !c.Version.Lower(srvVer180)
case ViewFeature:
supported = !c.Version.Lower(srvVer200)
case CccpFeature:
supported = !c.Version.Lower(srvVer250)
case SslFeature:
supported = !c.Version.Lower(srvVer300)
case DcpFeature:
supported = !c.Version.Lower(srvVer400)
case SpatialViewFeature:
supported = !c.Version.Lower(srvVer400)
case N1qlFeature:
supported = !c.Version.Lower(srvVer400)
case SubdocFeature:
supported = !c.Version.Lower(srvVer450)
case XattrFeature:
supported = !c.Version.Lower(srvVer450)
case KvErrorMapFeature:
supported = !c.Version.Lower(srvVer500)
case RbacFeature:
supported = !c.Version.Lower(srvVer500)
case FtsFeature:
supported = !c.Version.Lower(srvVer500)
case EnhancedErrorsFeature:
supported = !c.Version.Lower(srvVer500)
case FtsIndexFeature:
supported = !c.Version.Lower(srvVer500)
case CompressionFeature:
supported = !c.Version.Lower(srvVer550)
case ServerSideTracingFeature:
supported = !c.Version.Lower(srvVer550)
case AnalyticsFeature:
supported = !c.Version.Lower(srvVer600)
case CollectionsFeature:
supported = !c.Version.Lower(srvVer650)
case SubdocMockBugFeature:
supported = true
case ExpandMacrosFeature:
supported = !c.Version.Lower(srvVer450)
case AdjoinFeature:
supported = !c.Version.Equal(srvVer551) && !c.Version.Equal(srvVer552) && !c.Version.Equal(srvVer553)
case DurabilityFeature:
supported = !c.Version.Lower(srvVer650)
case UserGroupFeature:
supported = !c.Version.Lower(srvVer650)
case UserManagerFeature:
supported = !c.Version.Lower(srvVer500)
case AnalyticsIndexFeature:
supported = !c.Version.Lower(srvVer600)
case BucketMgrFeature:
supported = true
case FtsAnalyzeFeature:
supported = !c.Version.Lower(srvVer650)
case AnalyticsIndexPendingMutationsFeature:
supported = !c.Version.Lower(srvVer650)
}
}
return supported
}
func (c *testCluster) NotSupportsFeature(feature FeatureCode) bool {
return !c.SupportsFeature(feature)
}
func (c *testCluster) TimeTravel(waitDura time.Duration) {
if c.isMock() {
waitSecs := int(math.Ceil(float64(waitDura) / float64(time.Second)))
c.Mock.Control(gojcbmock.NewCommand(gojcbmock.CTimeTravel, map[string]interface{}{
"Offset": waitSecs,
}))
} else {
time.Sleep(waitDura)
}
}
func (c *testCluster) DefaultCollection(bucket *Bucket) *Collection {
return bucket.DefaultCollection()
}
func (c *testCluster) CreateBreweryDataset(col *Collection) error {
var dataset []testBreweryDocument
err := loadJSONTestDataset("beer_sample_brewery_five", &dataset)
if err != nil {
return testClusterErrorWrap{
InnerError: err,
Message: "could not read test dataset"}
}
for _, doc := range dataset {
_, err = col.Upsert(doc.Name, doc, nil)
if err != nil {
return testClusterErrorWrap{
InnerError: err,
Message: "could not create dataset"}
}
}
return nil
}
func waitForCollection(bucket *Bucket, name string) error {
timer := time.NewTimer(1 * time.Second)
for {
select {
case <-timer.C:
return errors.New("wait time for collection to become available expired")
default:
col := bucket.Collection(name)
_, err := col.Get("test", nil)
if err != nil {
if errors.Is(err, ErrCollectionNotFound) {
time.Sleep(100 * time.Millisecond)
continue
}
if !errors.Is(err, ErrDocumentNotFound) {
return err
}
}
return nil
}
}
}