-
Notifications
You must be signed in to change notification settings - Fork 28
/
rest_backup_restore_test.go
602 lines (564 loc) · 26.8 KB
/
rest_backup_restore_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright 2020-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"testing"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/mapping"
"github.com/couchbase/cbgt"
)
func TestCheckSourceNameMatchesFilters(t *testing.T) {
tests := []struct {
indexDef *cbgt.IndexDef
bucketLevel bool
filterRules string
expBucketFilters []string
expScopeFilters []string
expColFilters []string
expSourceNames []string
match bool
}{
// matching empty filter for index definition.
{indexDef: testIndexDefn(t, "beer-sample", "", false, nil, []string{"beer", "brewery"}),
filterRules: "",
expSourceNames: []string{"beer-sample._default._default"},
expBucketFilters: nil, expScopeFilters: nil,
expColFilters: nil, match: true},
// non matching $bucket filter for index definition.
{indexDef: testIndexDefn(t, "beer-sample", "", false, nil, []string{"beer", "brewery"}),
filterRules: "travel-sample",
expSourceNames: []string{"beer-sample._default._default"},
expBucketFilters: []string{"travel-sample"}, expScopeFilters: nil,
expColFilters: nil, match: false},
// matching $bucket filter for index definition.
{indexDef: testIndexDefn(t, "beer-sample", "", false, nil, []string{"beer", "brewery"}),
filterRules: "beer-sample",
expSourceNames: []string{"beer-sample._default._default"},
expBucketFilters: []string{"beer-sample"}, expScopeFilters: nil,
expColFilters: nil, match: true},
// matching $bucket.$scope filter for index definition.
{indexDef: testIndexDefn(t, "beer-sample", "", false, nil, []string{"beer", "brewery"}),
filterRules: "beer-sample._default",
expSourceNames: []string{"beer-sample._default._default"},
expBucketFilters: nil, expScopeFilters: []string{"beer-sample._default"},
expColFilters: nil, match: true},
// non-matching $bucket.$scope.$collection filter for index definition.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1"}, nil),
bucketLevel: true,
filterRules: "scope1.collection2",
expSourceNames: []string{"beer-sample.scope1.collection1"},
expBucketFilters: nil, expScopeFilters: nil,
expColFilters: []string{"beer-sample.scope1.collection2"}, match: false},
// matching $bucket filter for multi collection index definition.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection2"}, []string{"beer", "brewery"}),
filterRules: "beer-sample",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"},
expBucketFilters: []string{"beer-sample"}, expScopeFilters: nil,
expColFilters: nil, match: true},
// matching $bucket,$bucket.$scope filters for multi collection index definition.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection2"}, []string{"beer", "brewery"}),
filterRules: "travel-sample,beer-sample.scope1",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"},
expBucketFilters: []string{"travel-sample"}, expScopeFilters: []string{"beer-sample.scope1"},
expColFilters: nil, match: true},
// non matching $bucket,$bucket.$scope filters for multi collection index definition.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection2"}, []string{"beer", "brewery"}),
filterRules: "travel-sample,beer-sample.scope2",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"},
expBucketFilters: []string{"travel-sample"}, expScopeFilters: []string{"beer-sample.scope2"},
expColFilters: nil, match: false},
// non matching $bucket,$bucket.$scope.$collection filters for multi collection index definition.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection200"}, []string{"beer", "brewery"}),
filterRules: "travel-sample,beer-sample.scope1.collection1",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection200"},
expBucketFilters: []string{"travel-sample"}, expScopeFilters: nil,
expColFilters: []string{"beer-sample.scope1.collection1"}, match: false},
// matching multiple $bucket.$scope.$collection filters for multi collection index definition.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection200"}, []string{"beer", "brewery"}),
filterRules: "beer-sample.scope1.collection1,beer-sample.scope1.collection200",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection200"},
expBucketFilters: nil, expScopeFilters: nil,
expColFilters: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection200"}, match: true},
// test bucket level filters
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection200"}, []string{"beer", "brewery"}),
filterRules: "scope1",
bucketLevel: true,
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection200"},
expBucketFilters: nil, expScopeFilters: []string{"beer-sample.scope1"},
expColFilters: nil, match: true},
}
for i, test := range tests {
sns := parseSourceNamesFromIndexDefs(test.indexDef)
if !reflect.DeepEqual(sns, test.expSourceNames) {
t.Errorf("test: %d, expected sourceNames: %v got %v", i, test.expSourceNames, sns)
}
var bFilters, sFilters, cFilters []string
if !test.bucketLevel {
bFilters, sFilters, cFilters = parseBackupFilters(test.filterRules, "")
} else {
_, sFilters, cFilters = parseBackupFilters(test.filterRules, test.indexDef.SourceName)
}
if !reflect.DeepEqual(bFilters, test.expBucketFilters) {
t.Errorf("test: %d, expected bucketFilters: %v got %v", i, test.expBucketFilters, bFilters)
}
if !reflect.DeepEqual(sFilters, test.expScopeFilters) {
t.Errorf("test: %d, expected scopeFilters: %v got %v", i, test.expScopeFilters, sFilters)
}
if !reflect.DeepEqual(cFilters, test.expColFilters) {
t.Errorf("test: %d, expected collectionFilters: %v got %v", i, test.expColFilters, cFilters)
}
actual := checkSourceNameMatchesFilters(sns, bFilters, sFilters, cFilters)
if actual != test.match {
t.Errorf("test: %d, expected %t got %t", i, test.match, actual)
}
}
}
func TestCheckSourceNameMatchesFiltersBucketLevel(t *testing.T) {
tests := []struct {
indexDef *cbgt.IndexDef
filterRules string
bucketName string
expScopeFilters []string
expColFilters []string
expSourceNames []string
match bool
}{
// empty filters for index definition for bucketlevel filtering.
{indexDef: testIndexDefn(t, "beer-sample", "", false, nil, []string{"beer", "brewery"}),
filterRules: "",
bucketName: "beer-sample",
expSourceNames: []string{"beer-sample._default._default"},
expScopeFilters: nil,
expColFilters: nil, match: true},
// matching $scope filter for index definition for bucketlevel filtering.
{indexDef: testIndexDefn(t, "beer-sample", "", false, nil, []string{"beer", "brewery"}),
filterRules: "_default",
bucketName: "beer-sample",
expSourceNames: []string{"beer-sample._default._default"},
expScopeFilters: []string{"beer-sample._default"},
expColFilters: nil, match: true},
// non matching $scope.$collection filter for index definition for bucketlevel filtering.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection2"}, []string{"beer", "brewery"}),
filterRules: "_scope1.collection5",
bucketName: "beer-sample",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"},
expScopeFilters: nil,
expColFilters: []string{"beer-sample._scope1.collection5"}, match: false},
// matching $scope filter for multi collection index definition for bucketlevel filtering.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection2"}, []string{"beer", "brewery"}),
filterRules: "scope1",
bucketName: "beer-sample",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"},
expScopeFilters: []string{"beer-sample.scope1"},
expColFilters: nil, match: true},
// matching mulit $scope.$collection filters for multi collection index definition for bucketlevel filtering.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection2"}, []string{"beer", "brewery"}),
filterRules: "scope1.collection1,scope1.collection2",
bucketName: "beer-sample",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"},
expScopeFilters: nil,
expColFilters: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection2"}, match: true},
// non matching $scope.$collection filter for multi collection index definition for bucketlevel filtering.
{indexDef: testIndexDefn(t, "beer-sample", "scope1", true, []string{"collection1", "collection200"}, []string{"beer", "brewery"}),
filterRules: "scope1.collection1",
bucketName: "beer-sample",
expSourceNames: []string{"beer-sample.scope1.collection1", "beer-sample.scope1.collection200"},
expScopeFilters: nil,
expColFilters: []string{"beer-sample.scope1.collection1"}, match: false},
}
for i, test := range tests {
sns := parseSourceNamesFromIndexDefs(test.indexDef)
if !reflect.DeepEqual(sns, test.expSourceNames) {
t.Errorf("test: %d, expected sourceNames: %v got %v", i, test.expSourceNames, sns)
}
_, sFilters, cFilters := parseBackupFilters(test.filterRules, test.bucketName)
if !reflect.DeepEqual(sFilters, test.expScopeFilters) {
t.Errorf("test: %d, expected scopeFilters: %v got %v", i, test.expScopeFilters, sFilters)
}
if !reflect.DeepEqual(cFilters, test.expColFilters) {
t.Errorf("test: %d, expected collectionFilters: %v got %v", i, test.expColFilters, cFilters)
}
actual := checkSourceNameMatchesFilters(sns, nil, sFilters, cFilters)
if actual != test.match {
t.Errorf("test: %d, expected %t got %t", i, test.match, actual)
}
}
}
func testIndexDefn(t *testing.T, bucketName, scopeName string, colMode bool,
collName, typs []string) *cbgt.IndexDef {
indexDefn := &cbgt.IndexDef{
SourceName: bucketName,
}
bp := NewBleveParams()
var im *mapping.IndexMappingImpl
var ok bool
if im, ok = bp.Mapping.(*mapping.IndexMappingImpl); ok {
im.TypeMapping = make(map[string]*mapping.DocumentMapping, 1)
im.DefaultMapping.Enabled = false
}
if colMode {
bp.DocConfig.Mode = "scope.collection.type_field"
} else {
bp.DocConfig.Mode = "type_field"
}
for i, cn := range collName {
typ := scopeName + "." + cn
if i < len(typs) {
typ = typ + "." + typs[i]
}
im.TypeMapping[typ] = bleve.NewDocumentMapping()
}
pBytes, err := json.Marshal(bp)
if err != nil {
t.Errorf("testIndexDefn, json err: %v", err)
}
indexDefn.Params = string(pBytes)
return indexDefn
}
func TestRemapTypeMappings(t *testing.T) {
tests := []struct {
typMappings map[string]*mapping.DocumentMapping
remapRules string
bucketName string
bucketLevel bool
expTypNames []string
expBucketName string
}{
// remap scopes in a single collection type mapping
{
typMappings: createTypeMappings([]string{"scope1.collection1.brewery"}),
remapRules: "beer-sample.scope1:beer-sample.scope2",
bucketName: "beer-sample",
bucketLevel: false,
expTypNames: []string{"scope2.collection1.brewery"},
expBucketName: "beer-sample",
},
// remap scopes in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"scope1.collection1.brewery", "scope1.collection2.beer"}),
remapRules: "beer-sample.scope1:beer-sample.scope2",
bucketName: "beer-sample",
bucketLevel: false,
expTypNames: []string{"scope2.collection1.brewery", "scope2.collection2.beer"},
expBucketName: "beer-sample",
},
// remap scopes in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"scope1.collection1.brewery", "scope1.collection2.beer"}),
remapRules: "scope1:scope2",
bucketName: "beer-sample",
bucketLevel: true,
expTypNames: []string{"scope2.collection1.brewery", "scope2.collection2.beer"},
expBucketName: "beer-sample",
},
// remap collections in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"scope1.collection1.brewery", "scope1.collection2.beer"}),
remapRules: "scope1.collection1:scope1.collection3",
bucketName: "beer-sample",
bucketLevel: true,
expTypNames: []string{"scope1.collection2.beer", "scope1.collection3.brewery"},
expBucketName: "beer-sample",
},
// remap scope and collections in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"scope1.collection1.brewery", "scope1.collection2.beer"}),
remapRules: "scope1.collection1:scope2.collection2,scope1.collection2:scope2.collection1",
bucketName: "beer-sample",
bucketLevel: true,
expTypNames: []string{"scope2.collection1.beer", "scope2.collection2.brewery"},
expBucketName: "beer-sample",
},
// remap bucket, scope and collections in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"scope1.collection1.brewery", "scope1.collection2.beer"}),
remapRules: "beer-sample.scope1.collection1:travel-sample.scope2.collection2,beer-sample.scope1.collection2:travel-sample.scope2.collection1",
bucketName: "beer-sample",
bucketLevel: false,
expTypNames: []string{"scope2.collection1.beer", "scope2.collection2.brewery"},
expBucketName: "travel-sample",
},
// remap bucket and the default scope/collections in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"brewery", "beer"}),
remapRules: "beer-sample._default._default:travel-sample.scope1.collection1",
bucketName: "beer-sample",
bucketLevel: false,
expTypNames: []string{"scope1.collection1.beer", "scope1.collection1.brewery"},
expBucketName: "travel-sample",
},
// remap bucket and the explicit default scope/collections in a multi collection type mappings
{
typMappings: createTypeMappings([]string{"_default._default.brewery", "_default._default.beer"}),
remapRules: "beer-sample._default._default:travel-sample.scope1.collection1",
bucketName: "beer-sample",
bucketLevel: false,
expTypNames: []string{"scope1.collection1.beer", "scope1.collection1.brewery"},
expBucketName: "travel-sample",
},
// allow remap even with a conflict with an existing mapping (overwrite).
{
typMappings: createTypeMappings([]string{"scope1.collection1.beer", "scope1.collection2.beer"}),
remapRules: "beer-sample.scope1.collection1:beer-sample.scope1.collection2",
bucketName: "beer-sample",
bucketLevel: false,
expTypNames: []string{"scope1.collection2.beer"},
expBucketName: "beer-sample",
},
}
for i, test := range tests {
rmr, err := parseMappingParams(test.remapRules)
if err != nil {
t.Errorf("test %d parseMappingParams failed, err: %v", i, err)
}
typMappings, bucketName, err := remapTypeMappings(test.typMappings, rmr, "testIndex", test.bucketName, test.bucketLevel)
if err != nil {
t.Errorf("test %d remapTypeMappings failed, err: %v", i, err)
}
actualTypNames := getTypeNames(typMappings)
if !reflect.DeepEqual(actualTypNames, test.expTypNames) {
t.Errorf("test %d remapTypeMappings failed, expected remapped types: %v, got: %v", i, test.expTypNames, actualTypNames)
}
if test.expBucketName != bucketName {
t.Errorf("test %d remapTypeMappings failed, expected bucketName: %s, got: %s", i, test.expBucketName, bucketName)
}
}
}
func TestRemapTypeMappingsErrCases(t *testing.T) {
tests := []struct {
typMappings map[string]*mapping.DocumentMapping
remapRules string
bucketName string
bucketLevel bool
errDescription string
}{
// remap fails upon remap bucket.scope.collection rules within bucket scope. (it is supported for cluster level)
{
typMappings: createTypeMappings([]string{"scope1.collection1.beer", "scope1.collection2.beer"}),
remapRules: "beer-sample.scope1.collection1:beer-sample.scope1.collection2",
bucketName: "beer-sample",
bucketLevel: true,
errDescription: "Invalid remap rules:",
},
}
for i, test := range tests {
rmr, err := parseMappingParams(test.remapRules)
if err != nil {
t.Errorf("test %d parseMappingParams failed, err: %v", i, err)
}
typMappings, bucketName, err := remapTypeMappings(test.typMappings, rmr, "testIndex", test.bucketName, test.bucketLevel)
if err == nil || !strings.Contains(err.Error(), test.errDescription) {
t.Errorf("test %d remapTypeMappings expected to fail, but got :%v %s", i, typMappings, bucketName)
}
}
}
func createTypeMappings(typNames []string) map[string]*mapping.DocumentMapping {
tm := make(map[string]*mapping.DocumentMapping, len(typNames))
for _, tname := range typNames {
tm[tname] = mapping.NewDocumentMapping()
}
return tm
}
func getTypeNames(tm map[string]*mapping.DocumentMapping) []string {
var rv []string
for tname, _ := range tm {
rv = append(rv, tname)
}
sort.Strings(rv)
return rv
}
func TestRemapIndexDefinions(t *testing.T) {
requiredVersion, _ := cbgt.CompatibilityVersion(FeatureScopedIndexNamesVersion)
tests := []struct {
indexDefBytes []byte
mappingRules map[string]string
resIndexName string
resMappings []string
testDescription string
version uint64
}{
// verify an index name and mappings remap for the keyspaced index.
{
indexDefBytes: []byte(`{"uuid":"34483a9bd6044df7","indexDefs":{"travel-sample.inventory.DemoIndex":
{"type":"fulltext-index","name":"travel-sample.inventory.DemoIndex","uuid":"","sourceType":
"gocbcore","sourceName":"travel-sample","planParams":{"maxPartitionsPerPIndex":1024,"indexPartitions":1},
"params":{"doc_config":{"docid_prefix_delim":"","docid_regexp":"","mode":"scope.collection.type_field",
"type_field":"type"},"mapping":{"analysis":{},"default_analyzer":"standard","default_datetime_parser":
"dateTimeOptional","default_field":"_all","default_mapping":{"dynamic":true,"enabled":false},
"default_type":"_default","docvalues_dynamic":false,"index_dynamic":true,"store_dynamic":false,
"type_field":"_type","types":{"inventory.hotel":{"dynamic":true,"enabled":true},"inventory.landmark":
{"dynamic":true,"enabled":true}}},"store":{"indexType":"scorch","segmentVersion":15}},"sourceParams":{}}},
"implVersion":"5.6.0"}`),
mappingRules: map[string]string{
"travel-sample.inventory": "beer-sample.brewery",
},
resIndexName: "beer-sample.brewery.DemoIndex",
resMappings: []string{"brewery.hotel", "brewery.landmark"},
testDescription: "remap an index defn with both bucket and scope name remapped",
version: requiredVersion,
},
// verify an index name and mappings remap for the restored index.
// The index name isn't decorated, though the cluster is compatible since
// it's originally undecorated.
{
indexDefBytes: []byte(`{"uuid":"34483a9bd6044df7","indexDefs":{"DemoIndex":
{"type":"fulltext-index","name":"DemoIndex","uuid":"","sourceType":
"gocbcore","sourceName":"travel-sample","planParams":{"maxPartitionsPerPIndex":1024,"indexPartitions":1},
"params":{"doc_config":{"docid_prefix_delim":"","docid_regexp":"","mode":"scope.collection.type_field",
"type_field":"type"},"mapping":{"analysis":{},"default_analyzer":"standard","default_datetime_parser":
"dateTimeOptional","default_field":"_all","default_mapping":{"dynamic":true,"enabled":false},
"default_type":"_default","docvalues_dynamic":false,"index_dynamic":true,"store_dynamic":false,
"type_field":"_type","types":{"inventory.hotel":{"dynamic":true,"enabled":true},"inventory.landmark":
{"dynamic":true,"enabled":true}}},"store":{"indexType":"scorch","segmentVersion":15}},"sourceParams":{}}},
"implVersion":"5.6.0"}`),
mappingRules: map[string]string{
"travel-sample.inventory": "beer-sample.brewery",
},
resIndexName: "DemoIndex",
resMappings: []string{"brewery.hotel", "brewery.landmark"},
testDescription: "remap an undecorated index defn without the bucket " +
"and scope name",
version: requiredVersion,
},
// verify an index name and mappings remap to the default index.
{
indexDefBytes: []byte(`{"uuid":"34483a9bd6044df7","indexDefs":{"travel-sample.inventory.DemoIndex":
{"type":"fulltext-index","name":"travel-sample.inventory.DemoIndex","uuid":"","sourceType":
"gocbcore","sourceName":"travel-sample","planParams":{"maxPartitionsPerPIndex":1024,"indexPartitions":1},
"params":{"doc_config":{"docid_prefix_delim":"","docid_regexp":"","mode":"scope.collection.type_field",
"type_field":"type"},"mapping":{"analysis":{},"default_analyzer":"standard","default_datetime_parser":
"dateTimeOptional","default_field":"_all","default_mapping":{"dynamic":true,"enabled":false},
"default_type":"_default","docvalues_dynamic":false,"index_dynamic":true,"store_dynamic":false,
"type_field":"_type","types":{"inventory.hotel":{"dynamic":true,"enabled":true},"inventory.landmark":
{"dynamic":true,"enabled":true}}},"store":{"indexType":"scorch","segmentVersion":15}},"sourceParams":{}}},
"implVersion":"5.6.0"}`),
mappingRules: map[string]string{
"travel-sample.inventory": "beer-sample._default",
},
resIndexName: "beer-sample._default.DemoIndex",
resMappings: []string{"_default.hotel", "_default.landmark"},
testDescription: "remap an index defn with both bucket and scope name remapped",
version: requiredVersion,
},
// verify if decoration of the remapped name is not done if the cluster
// isn't compatible
{
indexDefBytes: []byte(`{"uuid":"34483a9bd6044df7","indexDefs":{"DemoIndex":
{"type":"fulltext-index","name":"DemoIndex","uuid":"","sourceType":
"gocbcore","sourceName":"travel-sample","planParams":{"maxPartitionsPerPIndex":1024,"indexPartitions":1},
"params":{"doc_config":{"docid_prefix_delim":"","docid_regexp":"","mode":"scope.collection.type_field",
"type_field":"type"},"mapping":{"analysis":{},"default_analyzer":"standard","default_datetime_parser":
"dateTimeOptional","default_field":"_all","default_mapping":{"dynamic":true,"enabled":false},
"default_type":"_default","docvalues_dynamic":false,"index_dynamic":true,"store_dynamic":false,
"type_field":"_type","types":{"inventory.hotel":{"dynamic":true,"enabled":true},"inventory.landmark":
{"dynamic":true,"enabled":true}}},"store":{"indexType":"scorch","segmentVersion":15}},"sourceParams":{}}},
"implVersion":"5.6.0"}`),
mappingRules: map[string]string{
"travel-sample.inventory": "beer-sample._default",
},
resIndexName: "DemoIndex",
resMappings: []string{"_default.hotel", "_default.landmark"},
testDescription: "remap an index defn with both bucket and scope name remapped",
},
}
versionTracker = &clusterVersionTracker{}
versionTracker.compatibleFeatures = make(map[string]struct{})
for i, test := range tests {
var indexDefs cbgt.IndexDefs
err := json.Unmarshal(test.indexDefBytes, &indexDefs)
if err != nil {
t.Fatalf("test %d, json err: %v", i, err)
}
versionTracker.version = test.version
versionTracker.clusterVersion = versionTracker.version
resIndexDefs, err := remapIndexDefinitions(&indexDefs, test.mappingRules, "", true)
if err != nil {
t.Errorf("test %d, remapIndexDefinitions failed, err: %v", i, err)
continue
}
if len(resIndexDefs.IndexDefs) != 1 {
t.Errorf("test %d remapIndexDefinitions, multiple index defns found: %+v",
i, resIndexDefs.IndexDefs)
continue
}
var res *cbgt.IndexDef
var found bool
if res, found = resIndexDefs.IndexDefs[test.resIndexName]; !found {
t.Errorf("test %d remapIndexDefinitions, no index defn found for: %s, %+v",
i, test.resIndexName, resIndexDefs.IndexDefs)
continue
}
for _, mappingName := range test.resMappings {
if !strings.Contains(res.Params, mappingName) {
t.Errorf("test %d remapIndexDefinitions, no mapping found for: %s",
i, mappingName)
}
}
}
versionTracker = nil
}
func TestRemapIndexDefinionsErrors(t *testing.T) {
// Starting with a decorated index name
x := "travel-sample.inventory."
for i := 0; i < cbgt.MaxIndexNameLength+10; i++ {
x += strconv.Itoa(i)
}
tests := []struct {
indexDefBytes []byte
mappingRules map[string]string
resIndexName string
resMappings []string
testDescription string
errDescription string
}{
// verify an index throwing an error after the remapped-restored name is
// longer than permissible.
{
indexDefBytes: []byte(fmt.Sprintf(`{"uuid":"34483a9bd6044df7","indexDefs":{"%s":
{"type":"fulltext-index","name":"%s","uuid":"","sourceType":
"gocbcore","sourceName":"travel-sample","planParams":{"maxPartitionsPerPIndex":1024,"indexPartitions":1},
"params":{"doc_config":{"docid_prefix_delim":"","docid_regexp":"","mode":"scope.collection.type_field",
"type_field":"type"},"mapping":{"analysis":{},"default_analyzer":"standard","default_datetime_parser":
"dateTimeOptional","default_field":"_all","default_mapping":{"dynamic":true,"enabled":false},
"default_type":"_default","docvalues_dynamic":false,"index_dynamic":true,"store_dynamic":false,
"type_field":"_type","types":{"inventory.hotel":{"dynamic":true,"enabled":true},"inventory.landmark":
{"dynamic":true,"enabled":true}}},"store":{"indexType":"scorch","segmentVersion":15}},"sourceParams":{}}},
"implVersion":"5.6.0"}`, x, x)),
mappingRules: map[string]string{
"travel-sample.inventory": "beer-sample.brewery",
},
resIndexName: "beer-sample.brewery." + x,
resMappings: []string{"brewery.hotel", "brewery.landmark"},
testDescription: "remap an index defn with both bucket and scope name remapped",
errDescription: "longer than the maximum permissible length",
},
}
versionTracker = &clusterVersionTracker{}
versionTracker.version, _ = cbgt.CompatibilityVersion(FeatureScopedIndexNamesVersion)
versionTracker.clusterVersion = versionTracker.version
versionTracker.compatibleFeatures = make(map[string]struct{})
for i, test := range tests {
var indexDefs cbgt.IndexDefs
err := json.Unmarshal(test.indexDefBytes, &indexDefs)
if err != nil {
t.Fatalf("test %d, json err: %v", i, err)
}
_, err = remapIndexDefinitions(&indexDefs, test.mappingRules, "", true)
if err == nil || !strings.Contains(err.Error(), test.errDescription) {
t.Fatalf("test %d: expected err: %s, got: %v", i, test.errDescription,
err)
}
}
versionTracker = nil
}