/
utilities_testing.go
1430 lines (1130 loc) · 43.3 KB
/
utilities_testing.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rest
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"runtime/debug"
"strings"
"sync"
"testing"
"time"
"github.com/couchbase/go-blip"
"github.com/couchbase/sg-bucket"
"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/channels"
"github.com/couchbase/sync_gateway/db"
"github.com/couchbaselabs/go.assert"
"golang.org/x/net/websocket"
)
// Testing utilities that have been included in the rest package so that they
// are available to any package that imports rest. (if they were in a _test.go
// file, they wouldn't be publicly exported to other packages)
var gBucketCounter = 0
type RestTester struct {
RestTesterBucket base.Bucket
RestTesterServerContext *ServerContext
noAdminParty bool // Unless this is true, Admin Party is in full effect
distributedIndex bool // Test with walrus-based index bucket
SyncFn string // put the sync() function source in here (optional)
DatabaseConfig *DbConfig // Supports additional config options. BucketConfig, Name, Sync, Unsupported will be ignored (overridden)
AdminHandler http.Handler
PublicHandler http.Handler
EnableNoConflictsMode bool // Enable no-conflicts mode. By default, conflicts will be allowed, which is the default behavior
NoFlush bool // Skip bucket flush step during creation. Used by tests that need to simulate start/stop of Sync Gateway with backing bucket intact.
InitSyncSeq uint64 // If specified, initializes _sync:seq on bucket creation. Not supported when running against walrus
}
func (rt *RestTester) Bucket() base.Bucket {
if rt.RestTesterBucket != nil {
return rt.RestTesterBucket
}
// Put this in a loop in case certain operations fail, like waiting for GSI indexes to be empty.
// Limit number of attempts to 2.
for i := 0; i < 2; i++ {
// Initialize the bucket. For couchbase-backed tests, triggers with creation/flushing of the bucket
if !rt.NoFlush {
tempBucket := base.GetTestBucketOrPanic() // side effect of creating/flushing bucket
if rt.InitSyncSeq > 0 {
log.Printf("Initializing %s to %d", db.SyncSeqKey, rt.InitSyncSeq)
_, incrErr := tempBucket.Incr(db.SyncSeqKey, rt.InitSyncSeq, rt.InitSyncSeq, 0)
if incrErr != nil {
panic(fmt.Sprintf("Error initializing %s in test bucket: %v", db.SyncSeqKey, incrErr))
}
}
tempBucket.Close()
} else {
if rt.InitSyncSeq > 0 {
panic("RestTester doesn't support NoFlush and InitSyncSeq in same test")
}
}
spec := base.GetTestBucketSpec(base.DataBucket)
username, password, _ := spec.Auth.GetCredentials()
server := spec.Server
gBucketCounter++
var syncFnPtr *string
if len(rt.SyncFn) > 0 {
syncFnPtr = &rt.SyncFn
}
corsConfig := &CORSConfig{
Origin: []string{"http://example.com", "*", "http://staging.example.com"},
LoginOrigin: []string{"http://example.com"},
Headers: []string{},
MaxAge: 1728000,
}
rt.RestTesterServerContext = NewServerContext(&ServerConfig{
CORS: corsConfig,
Facebook: &FacebookConfig{},
AdminInterface: &DefaultAdminInterface,
})
useXattrs := base.TestUseXattrs()
if rt.DatabaseConfig == nil {
// If no db config was passed in, create one
rt.DatabaseConfig = &DbConfig{}
// By default, does NOT use views when running against couchbase server, since should use GSI
rt.DatabaseConfig.UseViews = base.TestUseViews()
// numReplicas set to 0 for test buckets, since it should assume that there is only one node.
numReplicas := uint(0)
rt.DatabaseConfig.NumIndexReplicas = &numReplicas
}
rt.DatabaseConfig.BucketConfig = BucketConfig{
Server: &server,
Bucket: &spec.BucketName,
Username: username,
Password: password,
}
rt.DatabaseConfig.Name = "db"
rt.DatabaseConfig.Sync = syncFnPtr
rt.DatabaseConfig.EnableXattrs = &useXattrs
if rt.EnableNoConflictsMode {
boolVal := false
rt.DatabaseConfig.AllowConflicts = &boolVal
}
_, err := rt.RestTesterServerContext.AddDatabaseFromConfig(rt.DatabaseConfig)
if err != nil {
panic(fmt.Sprintf("Error from AddDatabaseFromConfig: %v", err))
}
rt.RestTesterBucket = rt.RestTesterServerContext.Database("db").Bucket
// As long as bucket flushing wasn't disabled, wait for index to be empty (if this is a gocb bucket)
if !rt.NoFlush {
asGoCbBucket, isGoCbBucket := base.AsGoCBBucket(rt.RestTesterBucket)
if isGoCbBucket {
if err := db.WaitForIndexEmpty(asGoCbBucket, spec.UseXattrs); err != nil {
base.Infof(base.KeyAll, "WaitForIndexEmpty returned an error: %v. Dropping indexes and retrying", err)
// if WaitForIndexEmpty returns error, drop the indexes and retry
if err := base.DropAllBucketIndexes(asGoCbBucket); err != nil {
panic(fmt.Sprintf("Failed to drop bucket indexes: %v", err))
}
continue // Go to the top of the for loop to retry
}
}
}
if !rt.noAdminParty {
rt.SetAdminParty(true)
}
return rt.RestTesterBucket
}
panic(fmt.Sprintf("Failed to create a RestTesterBucket after multiple attempts"))
}
func (rt *RestTester) BucketAllowEmptyPassword() base.Bucket {
//Create test DB with "AllowEmptyPassword" true
server := "walrus:"
bucketName := fmt.Sprintf("sync_gateway_test_%d", gBucketCounter)
gBucketCounter++
rt.RestTesterServerContext = NewServerContext(&ServerConfig{
CORS: &CORSConfig{},
Facebook: &FacebookConfig{},
AdminInterface: &DefaultAdminInterface,
})
_, err := rt.RestTesterServerContext.AddDatabaseFromConfig(&DbConfig{
BucketConfig: BucketConfig{
Server: &server,
Bucket: &bucketName},
Name: "db",
AllowEmptyPassword: true,
UseViews: true, // walrus only supports views
})
if err != nil {
panic(fmt.Sprintf("Error from AddDatabaseFromConfig: %v", err))
}
rt.RestTesterBucket = rt.RestTesterServerContext.Database("db").Bucket
return rt.RestTesterBucket
}
func (rt *RestTester) ServerContext() *ServerContext {
rt.Bucket()
return rt.RestTesterServerContext
}
// Returns first database found for server context.
func (rt *RestTester) GetDatabase() *db.DatabaseContext {
for _, database := range rt.ServerContext().AllDatabases() {
return database
}
return nil
}
func (rt *RestTester) MustWaitForDoc(docid string, t testing.TB) {
err := rt.WaitForDoc(docid)
assert.True(t, err == nil)
}
func (rt *RestTester) WaitForDoc(docid string) (err error) {
seq, err := rt.SequenceForDoc(docid)
if err != nil {
return err
}
return rt.WaitForSequence(seq)
}
func (rt *RestTester) SequenceForDoc(docid string) (seq uint64, err error) {
database := rt.GetDatabase()
if database == nil {
return 0, fmt.Errorf("No database found")
}
doc, err := database.GetDocument(docid, db.DocUnmarshalAll)
if err != nil {
return 0, err
}
return doc.Sequence, nil
}
// Wait for sequence to be buffered by the channel cache
func (rt *RestTester) WaitForSequence(seq uint64) error {
database := rt.GetDatabase()
if database == nil {
return fmt.Errorf("No database found")
}
return database.WaitForSequence(seq)
}
func (rt *RestTester) WaitForPendingChanges() error {
database := rt.GetDatabase()
if database == nil {
return fmt.Errorf("No database found")
}
return database.WaitForPendingChanges()
}
func (rt *RestTester) SetAdminParty(partyTime bool) {
a := rt.ServerContext().Database("db").Authenticator()
guest, _ := a.GetUser("")
guest.SetDisabled(!partyTime)
var chans channels.TimedSet
if partyTime {
chans = channels.AtSequence(base.SetOf("*"), 1)
}
guest.SetExplicitChannels(chans)
a.Save(guest)
}
func (rt *RestTester) DisableGuestUser() {
rt.SetAdminParty(false)
}
func (rt *RestTester) Close() {
if rt.RestTesterServerContext != nil {
rt.RestTesterServerContext.Close()
}
}
func (rt *RestTester) SendRequest(method, resource string, body string) *TestResponse {
return rt.Send(request(method, resource, body))
}
func (rt *RestTester) SendRequestWithHeaders(method, resource string, body string, headers map[string]string) *TestResponse {
req := request(method, resource, body)
for k, v := range headers {
req.Header.Set(k, v)
}
return rt.Send(req)
}
func (rt *RestTester) SendUserRequestWithHeaders(method, resource string, body string, headers map[string]string, username string, password string) *TestResponse {
req := request(method, resource, body)
req.SetBasicAuth(username, password)
for k, v := range headers {
req.Header.Set(k, v)
}
return rt.Send(req)
}
func (rt *RestTester) Send(request *http.Request) *TestResponse {
response := &TestResponse{httptest.NewRecorder(), request}
response.Code = 200 // doesn't seem to be initialized by default; filed Go bug #4188
rt.TestPublicHandler().ServeHTTP(response, request)
return response
}
func (rt *RestTester) TestAdminHandlerNoConflictsMode() http.Handler {
rt.EnableNoConflictsMode = true
if rt.AdminHandler == nil {
rt.AdminHandler = CreateAdminHandler(rt.ServerContext())
}
return rt.AdminHandler
}
func (rt *RestTester) TestAdminHandler() http.Handler {
if rt.AdminHandler == nil {
rt.AdminHandler = CreateAdminHandler(rt.ServerContext())
}
return rt.AdminHandler
}
func (rt *RestTester) TestPublicHandler() http.Handler {
if rt.PublicHandler == nil {
rt.PublicHandler = CreatePublicHandler(rt.ServerContext())
}
return rt.PublicHandler
}
type changesResults struct {
Results []db.ChangeEntry
Last_Seq interface{}
}
func (rt *RestTester) CreateWaitForChangesRetryWorker(numChangesExpected int, changesUrl, username string, useAdminPort bool) (worker base.RetryWorker) {
waitForChangesWorker := func() (shouldRetry bool, err error, value interface{}) {
var changes changesResults
var response *TestResponse
if useAdminPort {
response = rt.SendAdminRequest("GET", changesUrl, "")
} else {
response = rt.Send(requestByUser("GET", changesUrl, "", username))
}
err = json.Unmarshal(response.Body.Bytes(), &changes)
if err != nil {
return false, err, nil
}
if len(changes.Results) < numChangesExpected {
// not enough results, retry
return true, nil, nil
}
// If it made it this far, there is no errors and it got enough changes
return false, nil, changes
}
return waitForChangesWorker
}
func (rt *RestTester) WaitForChanges(numChangesExpected int, changesUrl, username string, useAdminPort bool) (changes changesResults, err error) {
waitForChangesWorker := rt.CreateWaitForChangesRetryWorker(numChangesExpected, changesUrl, username, useAdminPort)
sleeper := base.CreateSleeperFunc(200, 100)
err, changesVal := base.RetryLoop("Wait for changes", waitForChangesWorker, sleeper)
if err != nil {
return changes, err
}
if changesVal == nil {
return changes, fmt.Errorf("Got nil value for changes")
}
if changesVal != nil {
changes = changesVal.(changesResults)
}
return changes, nil
}
func (rt *RestTester) SendAdminRequest(method, resource string, body string) *TestResponse {
input := bytes.NewBufferString(body)
request, _ := http.NewRequest(method, "http://localhost"+resource, input)
response := &TestResponse{httptest.NewRecorder(), request}
response.Code = 200 // doesn't seem to be initialized by default; filed Go bug #4188
rt.TestAdminHandler().ServeHTTP(response, request)
return response
}
func (rt *RestTester) WaitForNUserViewResults(numResultsExpected int, viewUrlPath string, user auth.User, password string) (viewResult sgbucket.ViewResult, err error) {
return rt.WaitForNViewResults(numResultsExpected, viewUrlPath, user, password)
}
func (rt *RestTester) WaitForNAdminViewResults(numResultsExpected int, viewUrlPath string) (viewResult sgbucket.ViewResult, err error) {
return rt.WaitForNViewResults(numResultsExpected, viewUrlPath, nil, "")
}
// Wait for a certain number of results to be returned from a view query
// viewUrlPath: is the path to the view, including the db name. Eg: "/db/_design/foo/_view/bar"
func (rt *RestTester) WaitForNViewResults(numResultsExpected int, viewUrlPath string, user auth.User, password string) (viewResult sgbucket.ViewResult, err error) {
worker := func() (shouldRetry bool, err error, value interface{}) {
var response *TestResponse
if user != nil {
request, _ := http.NewRequest("GET", viewUrlPath, nil)
request.SetBasicAuth(user.Name(), password)
response = rt.Send(request)
} else {
response = rt.SendAdminRequest("GET", viewUrlPath, ``)
}
// If the view is undefined, it might be a race condition where the view is still being created
// See https://github.com/couchbase/sync_gateway/issues/3570#issuecomment-390487982
if strings.Contains(response.Body.String(), "view_undefined") {
base.Infof(base.KeyAll, "view_undefined error: %v. Retrying", response.Body.String())
return true, nil, nil
}
if response.Code != 200 {
return false, fmt.Errorf("Got response code: %d from view call. Expected 200.", response.Code), sgbucket.ViewResult{}
}
var result sgbucket.ViewResult
json.Unmarshal(response.Body.Bytes(), &result)
if len(result.Rows) >= numResultsExpected {
// Got enough results, break out of retry loop
return false, nil, result
}
// Not enough results, retry
return true, nil, sgbucket.ViewResult{}
}
description := fmt.Sprintf("Wait for %d view results for query to %v", numResultsExpected, viewUrlPath)
sleeper := base.CreateSleeperFunc(200, 100)
err, returnVal := base.RetryLoop(description, worker, sleeper)
if err != nil {
return sgbucket.ViewResult{}, err
}
return returnVal.(sgbucket.ViewResult), nil
}
// Waits for view to be defined on the server. Used to avoid view_undefined errors.
func (rt *RestTester) WaitForViewAvailable(viewURLPath string) (err error) {
worker := func() (shouldRetry bool, err error, value interface{}) {
response := rt.SendAdminRequest("GET", viewURLPath, ``)
if response.Code == 200 {
return false, nil, nil
}
// Views unavailable, retry
if response.Code == 500 {
log.Printf("Error waiting for view to be available....will retry: %s", response.Body.Bytes())
return true, fmt.Errorf("500 error"), nil
}
// Unexpected error, return
return false, fmt.Errorf("Unexpected error response code while waiting for view available: %v", response.Code), nil
}
description := "Wait for view readiness"
sleeper := base.CreateSleeperFunc(200, 100)
err, _ = base.RetryLoop(description, worker, sleeper)
return err
}
func (rt *RestTester) WaitForDBOnline() (err error) {
maxTries := 20
for i := 0; i < maxTries; i++ {
response := rt.SendAdminRequest("GET", "/db/", "")
var body db.Body
json.Unmarshal(response.Body.Bytes(), &body)
if body["state"].(string) == "Online" {
return
}
// Otherwise, sleep and try again
time.Sleep(500 * time.Millisecond)
}
return fmt.Errorf("Give up waiting for DB to come online after %d attempts", maxTries)
}
func (rt *RestTester) SendAdminRequestWithHeaders(method, resource string, body string, headers map[string]string) *TestResponse {
input := bytes.NewBufferString(body)
request, _ := http.NewRequest(method, "http://localhost"+resource, input)
for k, v := range headers {
request.Header.Set(k, v)
}
response := &TestResponse{httptest.NewRecorder(), request}
response.Code = 200 // doesn't seem to be initialized by default; filed Go bug #4188
rt.TestAdminHandler().ServeHTTP(response, request)
return response
}
type TestResponse struct {
*httptest.ResponseRecorder
Req *http.Request
}
func (r TestResponse) DumpBody() {
log.Printf("%v", string(r.Body.Bytes()))
}
func (r TestResponse) GetRestDocument() RestDocument {
restDoc := NewRestDocument()
err := json.Unmarshal(r.Body.Bytes(), restDoc)
if err != nil {
panic(fmt.Sprintf("Error parsing body into RestDocument. Body: %s. Err: %v", string(r.Body.Bytes()), err))
}
return *restDoc
}
func request(method, resource, body string) *http.Request {
request, err := http.NewRequest(method, "http://localhost"+resource, bytes.NewBufferString(body))
request.RequestURI = resource // This doesn't get filled in by NewRequest
FixQuotedSlashes(request)
if err != nil {
panic(fmt.Sprintf("http.NewRequest failed: %v", err))
}
return request
}
func requestByUser(method, resource, body, username string) *http.Request {
r := request(method, resource, body)
r.SetBasicAuth(username, "letmein")
return r
}
func assertStatus(t *testing.T, response *TestResponse, expectedStatus int) {
if response.Code != expectedStatus {
debug.PrintStack()
t.Fatalf("Response status %d (expected %d) for %s <%s> : %s",
response.Code, expectedStatus, response.Req.Method, response.Req.URL, response.Body)
}
}
func NewSlowResponseRecorder(responseDelay time.Duration, responseRecorder *httptest.ResponseRecorder) *SlowResponseRecorder {
responseStarted := sync.WaitGroup{}
responseStarted.Add(1)
responseFinished := sync.WaitGroup{}
responseFinished.Add(1)
return &SlowResponseRecorder{
responseDelay: responseDelay,
ResponseRecorder: responseRecorder,
responseStarted: &responseStarted,
responseFinished: &responseFinished,
}
}
type SlowResponseRecorder struct {
*httptest.ResponseRecorder
responseDelay time.Duration
responseStarted *sync.WaitGroup
responseFinished *sync.WaitGroup
}
func (s *SlowResponseRecorder) WaitForResponseToStart() {
s.responseStarted.Wait()
}
func (s *SlowResponseRecorder) WaitForResponseToFinish() {
s.responseFinished.Wait()
}
func (s *SlowResponseRecorder) Write(buf []byte) (int, error) {
s.responseStarted.Done()
time.Sleep(s.responseDelay)
numBytesWritten, err := s.ResponseRecorder.Write(buf)
s.responseFinished.Done()
return numBytesWritten, err
}
// The parameters used to create a BlipTester
type BlipTesterSpec struct {
// Run Sync Gateway in "No conflicts" mode. Will be propgated to the underyling RestTester
noConflictsMode bool
// If an underlying RestTester is created, it will propagate this setting to the underlying RestTester.
noAdminParty bool
// The Sync Gateway username and password to connect with. If set, then you
// may want to disable "Admin Party" mode, which will allow guest user access.
// By default, the created user will have access to a single channel that matches their username.
// If you need to grant the user access to more channels, you can override this behavior with the
// connectingUserChannelGrants field
connectingUsername string
connectingPassword string
// By default, the created user will have access to a single channel that matches their username.
// If you need to grant the user access to more channels, you can override this behavior by specifying
// the channels the user should have access in this string slice
connectingUserChannelGrants []string
// Allow tests to further customized a RestTester or re-use it across multiple BlipTesters if needed.
// If a RestTester is passed in, certain properties of the BlipTester such as noAdminParty will be ignored, since
// those properties only affect the creation of the RestTester.
// If nil, a default restTester will be created based on the properties in this spec
restTester *RestTester
}
// State associated with a BlipTester
// Note that it's not safe to have multiple goroutines access a single BlipTester due to the
// fact that certain methods register profile handlers on the BlipContext
type BlipTester struct {
// The underlying RestTester which is used to bootstrap the initial blip websocket creation,
// as well as providing a way for tests to access Sync Gateway over REST to hit admin-only endpoints
// which are not available via blip. Since a test may need to create multiple BlipTesters for multiple
// user contexts, a single RestTester may be shared among multiple BlipTester instances.
restTester *RestTester
// The blip context which contains blip related state and the sender/reciever goroutines associated
// with this websocket connection
blipContext *blip.Context
// The blip sender that can be used for sending messages over the websocket connection
sender *blip.Sender
}
// Close the bliptester
func (bt BlipTester) Close() {
bt.restTester.Close()
}
// Create a BlipTester using the default spec
func NewBlipTester() (*BlipTester, error) {
defaultSpec := BlipTesterSpec{}
return NewBlipTesterFromSpec(defaultSpec)
}
// Create a BlipTester using the given spec
func NewBlipTesterFromSpec(spec BlipTesterSpec) (*BlipTester, error) {
bt := &BlipTester{}
if spec.restTester != nil {
bt.restTester = spec.restTester
} else {
rt := RestTester{
EnableNoConflictsMode: spec.noConflictsMode,
noAdminParty: spec.noAdminParty,
}
bt.restTester = &rt
}
// Since blip requests all go over the public handler, wrap the public handler with the httptest server
publicHandler := bt.restTester.TestPublicHandler()
if len(spec.connectingUsername) > 0 {
// By default, the user will be granted access to a single channel equal to their username
adminChannels := []string{spec.connectingUsername}
// If the caller specified a list of channels to grant the user access to, then use that instead.
if len(spec.connectingUserChannelGrants) > 0 {
adminChannels = []string{} // empty it
adminChannels = append(adminChannels, spec.connectingUserChannelGrants...)
}
// serialize admin channels to json array
adminChannelsJson, err := json.Marshal(adminChannels)
if err != nil {
return nil, err
}
adminChannelsStr := fmt.Sprintf("%s", adminChannelsJson)
userDocBody := fmt.Sprintf(`{"name":"%s", "password":"%s", "admin_channels":%s}`,
spec.connectingUsername,
spec.connectingPassword,
adminChannelsStr,
)
log.Printf("Creating user: %v", userDocBody)
// Create a user. NOTE: this must come *after* the bt.rt.TestPublicHandler() call, otherwise it will end up getting ignored
response := bt.restTester.SendAdminRequest(
"POST",
"/db/_user/",
userDocBody,
)
if response.Code != 201 {
return nil, fmt.Errorf("Expected 201 response. Got: %v", response.Code)
}
}
// Create a _temporary_ test server bound to an actual port that is used to make the blip connection.
// This is needed because the mock-based approach fails with a "Connection not hijackable" error when
// trying to do the websocket upgrade. Since it's only needed to setup the websocket, it can be closed
// as soon as the websocket is established, hence the defer srv.Close() call.
srv := httptest.NewServer(publicHandler)
defer srv.Close()
// Construct URL to connect to blipsync target endpoint
destUrl := fmt.Sprintf("%s/db/_blipsync", srv.URL)
u, err := url.Parse(destUrl)
if err != nil {
return nil, err
}
u.Scheme = "ws"
// Make BLIP/Websocket connection
bt.blipContext = blip.NewContext(BlipCBMobileReplication)
bt.blipContext.Logger = DefaultBlipLogger(bt.blipContext.ID)
bt.blipContext.LogMessages = base.LogDebugEnabled(base.KeyWebSocket)
bt.blipContext.LogFrames = base.LogDebugEnabled(base.KeyWebSocketFrame)
origin := "http://localhost" // TODO: what should be used here?
config, err := websocket.NewConfig(u.String(), origin)
if err != nil {
return nil, err
}
if len(spec.connectingUsername) > 0 {
config.Header = http.Header{
"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(spec.connectingUsername+":"+spec.connectingPassword))},
}
}
bt.sender, err = bt.blipContext.DialConfig(config)
if err != nil {
return nil, err
}
return bt, nil
}
func (bt *BlipTester) SetCheckpoint(client string, checkpointRev string, body []byte) (sent bool, req *SetCheckpointMessage, res *SetCheckpointResponse, err error) {
scm := NewSetCheckpointMessage()
scm.SetCompressed(true)
scm.setClient(client)
scm.setRev(checkpointRev)
scm.SetBody(body)
sent = bt.sender.Send(scm.Message)
if !sent {
return sent, scm, nil, fmt.Errorf("Failed to send setCheckpoint for client: %v", client)
}
scr := &SetCheckpointResponse{scm.Response()}
return true, scm, scr, nil
}
// The docHistory should be in the same format as expected by db.PutExistingRev(), or empty if this is the first revision
func (bt *BlipTester) SendRevWithHistory(docId, docRev string, revHistory []string, body []byte, properties blip.Properties) (sent bool, req, res *blip.Message, err error) {
revRequest := blip.NewRequest()
revRequest.SetCompressed(true)
revRequest.SetProfile("rev")
revRequest.Properties["id"] = docId
revRequest.Properties["rev"] = docRev
revRequest.Properties["deleted"] = "false"
if len(revHistory) > 0 {
revRequest.Properties["history"] = strings.Join(revHistory, ",")
}
// Override any properties which have been supplied explicitly
for k, v := range properties {
revRequest.Properties[k] = v
}
revRequest.SetBody(body)
sent = bt.sender.Send(revRequest)
if !sent {
return sent, revRequest, nil, fmt.Errorf("Failed to send revRequest for doc: %v", docId)
}
revResponse := revRequest.Response()
if revResponse.SerialNumber() != revRequest.SerialNumber() {
return sent, revRequest, revResponse, fmt.Errorf("revResponse.SerialNumber() != revRequest.SerialNumber(). %v != %v", revResponse.SerialNumber(), revRequest.SerialNumber())
}
// Make sure no errors. Just panic for now, but if there are tests that expect errors and want
// to use SendRev(), this could be returned.
if errorCode, ok := revResponse.Properties["Error-Code"]; ok {
body, _ := revResponse.Body()
return sent, revRequest, revResponse, fmt.Errorf("Unexpected error sending rev: %v\n%s", errorCode, body)
}
return sent, revRequest, revResponse, nil
}
func (bt *BlipTester) SendRev(docId, docRev string, body []byte, properties blip.Properties) (sent bool, req, res *blip.Message, err error) {
return bt.SendRevWithHistory(docId, docRev, []string{}, body, properties)
}
// Get a doc at a particular revision from Sync Gateway.
//
// Warning: this can only be called from a single goroutine, given the fact it registers profile handlers.
//
// If that is not found, it will return an empty resultDoc with no errors.
//
// - Call subChanges (continuous=false) endpoint to get all changes from Sync Gateway
// - Respond to each "change" request telling the other side to send the revision
// - NOTE: this could be made more efficient by only requesting the revision for the docid/revid pair
// passed in the parameter.
// - If the rev handler is called back with the desired docid/revid pair, save that into a variable that will be returned
// - Block until all pending operations are complete
// - Return the resultDoc or an empty resultDoc
//
func (bt *BlipTester) GetDocAtRev(requestedDocID, requestedDocRev string) (resultDoc RestDocument, err error) {
docs := map[string]RestDocument{}
changesFinishedWg := sync.WaitGroup{}
revsFinishedWg := sync.WaitGroup{}
defer func() {
// Clean up all profile handlers that are registered as part of this test
delete(bt.blipContext.HandlerForProfile, "changes")
delete(bt.blipContext.HandlerForProfile, "rev")
}()
// -------- Changes handler callback --------
bt.blipContext.HandlerForProfile["changes"] = func(request *blip.Message) {
// Send a response telling the other side we want ALL revisions
body, err := request.Body()
if err != nil {
panic(fmt.Sprintf("Error getting request body: %v", err))
}
if string(body) == "null" {
changesFinishedWg.Done()
return
}
if !request.NoReply() {
// unmarshal into json array
changesBatch := [][]interface{}{}
if err := json.Unmarshal(body, &changesBatch); err != nil {
panic(fmt.Sprintf("Error unmarshalling changes. Body: %vs. Error: %v", string(body), err))
}
responseVal := [][]interface{}{}
for _, change := range changesBatch {
revId := change[2].(string)
responseVal = append(responseVal, []interface{}{revId})
revsFinishedWg.Add(1)
}
response := request.Response()
responseValBytes, err := json.Marshal(responseVal)
log.Printf("responseValBytes: %s", responseValBytes)
if err != nil {
panic(fmt.Sprintf("Error marshalling response: %v", err))
}
response.SetBody(responseValBytes)
}
}
// -------- Rev handler callback --------
bt.blipContext.HandlerForProfile["rev"] = func(request *blip.Message) {
defer revsFinishedWg.Done()
body, err := request.Body()
var doc RestDocument
err = json.Unmarshal(body, &doc)
if err != nil {
panic(fmt.Sprintf("Unexpected err: %v", err))
}
docId := request.Properties["id"]
docRev := request.Properties["rev"]
doc.SetID(docId)
doc.SetRevID(docRev)
docs[docId] = doc
if docId == requestedDocID && docRev == requestedDocRev {
resultDoc = doc
}
}
// Send subChanges to subscribe to changes, which will cause the "changes" profile handler above to be called back
changesFinishedWg.Add(1)
subChangesRequest := blip.NewRequest()
subChangesRequest.SetProfile("subChanges")
subChangesRequest.Properties["continuous"] = "false"
sent := bt.sender.Send(subChangesRequest)
if !sent {
panic(fmt.Sprintf("Unable to subscribe to changes."))
}
changesFinishedWg.Wait()
revsFinishedWg.Wait()
return resultDoc, nil
}
type SendRevWithAttachmentInput struct {
docId string
revId string
attachmentName string
attachmentBody string
attachmentDigest string
}
// Warning: this can only be called from a single goroutine, given the fact it registers profile handlers.
func (bt *BlipTester) SendRevWithAttachment(input SendRevWithAttachmentInput) (sent bool, req, res *blip.Message) {
defer func() {
// Clean up all profile handlers that are registered as part of this test
delete(bt.blipContext.HandlerForProfile, "getAttachment")
}()
// Create a doc with an attachment
myAttachment := db.DocAttachment{
ContentType: "application/json",
Digest: input.attachmentDigest,
Length: 6,
Revpos: 1,
Stub: true,
}
doc := NewRestDocument()
doc.SetID(input.docId)
doc.SetRevID(input.revId)
doc.SetAttachments(db.AttachmentMap{
input.attachmentName: &myAttachment,
})
docBody, err := json.Marshal(doc)
if err != nil {
panic(fmt.Sprintf("Error marshalling doc. Error: %v", err))
}
getAttachmentWg := sync.WaitGroup{}
bt.blipContext.HandlerForProfile["getAttachment"] = func(request *blip.Message) {
defer getAttachmentWg.Done()
if request.Properties["digest"] != myAttachment.Digest {
panic(fmt.Sprintf("Unexpected digest. Got: %v, expected: %v", request.Properties["digest"], myAttachment.Digest))
}
response := request.Response()
response.SetBody([]byte(input.attachmentBody))
}
// Push a rev with an attachment.
getAttachmentWg.Add(1)
sent, req, res, err = bt.SendRev(
input.docId,
input.revId,
docBody,
blip.Properties{},
)
if err != nil {
panic(fmt.Sprintf("Error sending rev: %v", err))
}
// Expect a callback to the getAttachment endpoint
getAttachmentWg.Wait()
return sent, req, res
}
func (bt *BlipTester) WaitForNumChanges(numChangesExpected int) (changes [][]interface{}) {
retryWorker := func() (shouldRetry bool, err error, value interface{}) {
currentChanges := bt.GetChanges()
if len(currentChanges) >= numChangesExpected {
return false, nil, currentChanges
}
// haven't seen numDocsExpected yet, so wait and retry
return true, nil, nil