-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_queryindexes_test.go
196 lines (162 loc) · 5.88 KB
/
cluster_queryindexes_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
package gocb
import (
"errors"
"time"
)
func (suite *IntegrationTestSuite) TestQueryIndexesCrud() {
suite.skipIfUnsupported(QueryIndexFeature)
bucketMgr := globalCluster.Buckets()
bucketName := "testIndexes"
err := bucketMgr.CreateBucket(CreateBucketSettings{
BucketSettings: BucketSettings{
Name: bucketName,
RAMQuotaMB: 100,
NumReplicas: 0,
BucketType: CouchbaseBucketType,
},
}, nil)
suite.Require().Nil(err, err)
defer bucketMgr.DropBucket(bucketName, nil)
mgr := globalCluster.QueryIndexes()
deadline := time.Now().Add(5 * time.Second)
for {
err = mgr.CreatePrimaryIndex(bucketName, &CreatePrimaryQueryIndexOptions{
IgnoreIfExists: true,
})
if err == nil {
break
}
suite.T().Logf("Failed to create primary index: %s", err)
sleepDeadline := time.Now().Add(500 * time.Millisecond)
if sleepDeadline.After(deadline) {
sleepDeadline = deadline
}
time.Sleep(sleepDeadline.Sub(time.Now()))
if sleepDeadline == deadline {
suite.T().Errorf("timed out waiting for create index to succeed")
return
}
}
err = mgr.CreatePrimaryIndex(bucketName, &CreatePrimaryQueryIndexOptions{
IgnoreIfExists: false,
})
suite.Require().NotNil(err, err)
if !errors.Is(err, ErrIndexExists) {
suite.T().Fatalf("Expected index exists error but was %s", err)
}
err = mgr.CreateIndex(bucketName, "testIndex", []string{"field"}, &CreateQueryIndexOptions{
IgnoreIfExists: true,
})
suite.Require().Nil(err, err)
err = mgr.CreateIndex(bucketName, "testIndex", []string{"field"}, &CreateQueryIndexOptions{
IgnoreIfExists: false,
})
suite.Require().NotNil(err, err)
if !errors.Is(err, ErrIndexExists) {
suite.T().Fatalf("Expected index exists error but was %s", err)
}
// We create this first to give it a chance to be created by the time we need it.
err = mgr.CreateIndex(bucketName, "testIndexDeferred", []string{"field"}, &CreateQueryIndexOptions{
IgnoreIfExists: false,
Deferred: true,
})
suite.Require().Nil(err, err)
indexNames, err := mgr.BuildDeferredIndexes(bucketName, nil)
suite.Require().Nil(err, err)
suite.Assert().Len(indexNames, 1)
err = mgr.WatchIndexes(bucketName, []string{"testIndexDeferred"}, 10*time.Second, nil)
suite.Require().Nil(err, err)
indexes, err := mgr.GetAllIndexes(bucketName, nil)
suite.Require().Nil(err, err)
suite.Assert().Len(indexes, 3)
var index QueryIndex
for _, idx := range indexes {
if idx.Name == "testIndex" {
index = idx
break
}
}
suite.Assert().Equal("testIndex", index.Name)
suite.Assert().False(index.IsPrimary)
suite.Assert().Equal(QueryIndexTypeGsi, index.Type)
suite.Assert().Equal("online", index.State)
suite.Assert().Equal("testIndexes", index.Keyspace)
suite.Assert().Equal("default", index.Namespace)
if suite.Assert().Len(index.IndexKey, 1) {
suite.Assert().Equal("`field`", index.IndexKey[0])
}
suite.Assert().Empty(index.Condition)
suite.Assert().Empty(index.Partition)
err = mgr.DropIndex(bucketName, "testIndex", nil)
suite.Require().Nil(err, err)
err = mgr.DropIndex(bucketName, "testIndex", nil)
suite.Require().NotNil(err, err)
if !errors.Is(err, ErrIndexNotFound) {
suite.T().Fatalf("Expected index not found error but was %s", err)
}
err = mgr.DropPrimaryIndex(bucketName, nil)
suite.Require().Nil(err, err)
err = mgr.DropPrimaryIndex(bucketName, nil)
suite.Require().NotNil(err, err)
if !errors.Is(err, ErrIndexNotFound) {
suite.T().Fatalf("Expected index not found error but was %s", err)
}
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_bucket_create_bucket"), 1, false)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_create_primary_index"), 2, true)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_create_index"), 3, false)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_build_deferred_indexes"), 1, false)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_watch_indexes"), 1, false)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_get_all_indexes"), 1, false)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_drop_primary_index"), 2, false)
suite.AssertMetrics(makeMetricsKey(meterNameCBOperations, "management", "manager_query_drop_index"), 2, false)
}
type testQueryIndexDataset struct {
Results []map[string]interface{}
jsonQueryResponse
}
type mockQueryIndexRowReader struct {
Dataset []map[string]interface{}
mockQueryRowReaderBase
}
func (arr *mockQueryIndexRowReader) NextRow() []byte {
if arr.idx == len(arr.Dataset) {
return nil
}
idx := arr.idx
arr.idx++
return arr.Suite.mustConvertToBytes(arr.Dataset[idx])
}
func (suite *UnitTestSuite) TestQueryIndexesParsing() {
var dataset testQueryIndexDataset
err := loadJSONTestDataset("query_index_response", &dataset)
suite.Require().Nil(err, err)
reader := &mockQueryIndexRowReader{
Dataset: dataset.Results,
mockQueryRowReaderBase: mockQueryRowReaderBase{
Meta: suite.mustConvertToBytes(dataset.jsonQueryResponse),
Suite: suite,
},
}
var cluster *Cluster
cluster = suite.queryCluster(false, reader, nil)
mgr := QueryIndexManager{
provider: cluster,
tracer: &NoopTracer{},
meter: &meterWrapper{meter: &NoopMeter{}},
}
res, err := mgr.GetAllIndexes("mybucket", nil)
suite.Require().Nil(err, err)
suite.Require().Len(res, 1)
index := res[0]
suite.Assert().Equal("ih", index.Name)
suite.Assert().False(index.IsPrimary)
suite.Assert().Equal(QueryIndexTypeGsi, index.Type)
suite.Assert().Equal("online", index.State)
suite.Assert().Equal("test", index.Keyspace)
suite.Assert().Equal("default", index.Namespace)
if suite.Assert().Len(index.IndexKey, 1) {
suite.Assert().Equal("`_type`", index.IndexKey[0])
}
suite.Assert().Empty(index.Condition)
suite.Assert().Equal("HASH(`_type`)", index.Partition)
}