-
Notifications
You must be signed in to change notification settings - Fork 104
/
base_test.go
136 lines (107 loc) · 3.38 KB
/
base_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
package gocb
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/couchbaselabs/gojcbmock"
)
const (
defaultServerVersion = "5.1.0"
)
var globalBucket *Bucket
var globalTravelBucket *Bucket
var globalCollection *Collection
var globalCluster *testCluster
func TestMain(m *testing.M) {
// SetLogger(VerboseStdioLogger())
server := flag.String("server", "", "The connection string to connect to for a real server")
user := flag.String("user", "", "The username to use to authenticate when using a real server")
password := flag.String("pass", "", "The password to use to authenticate when using a real server")
bucketName := flag.String("bucket", "default", "The bucket to use to test against")
// bucketPassword := flag.String("bucket-pass", "", "The bucket password to use when connecting to the bucket")
version := flag.String("version", "", "The server or mock version being tested against (major.minor.patch.build_edition)")
collectionName := flag.String("collection-name", "", "The name of the collection to use")
flag.Parse()
var err error
var connStr string
var mock *gojcbmock.Mock
if *server == "" {
if *version != "" {
panic("version cannot be specified with mock")
}
mpath, err := gojcbmock.GetMockPath()
if err != nil {
panic(err.Error())
}
mock, err = gojcbmock.NewMock(mpath, 4, 1, 64, []gojcbmock.BucketSpec{
{Name: "default", Type: gojcbmock.BCouchbase},
{Name: "memd", Type: gojcbmock.BCouchbase},
}...)
if err != nil {
panic(err.Error())
}
*version = mock.Version()
connStr = fmt.Sprintf("http://127.0.0.1:%d", mock.EntryPort)
} else {
connStr = *server
if *version == "" {
*version = defaultServerVersion
}
}
cluster, err := NewCluster(connStr, ClusterOptions{Authenticator: PasswordAuthenticator{Username: *user, Password: *password}})
if err != nil {
panic(err.Error())
}
nodeVersion, err := newNodeVersion(*version, mock != nil)
if err != nil {
panic(err.Error())
}
globalCluster = &testCluster{Cluster: cluster, Mock: mock, Version: nodeVersion}
globalBucket = globalCluster.Bucket(*bucketName, &BucketOptions{UseMutationTokens: true})
if *collectionName != "" {
globalCollection = globalBucket.Collection("_default", *collectionName, nil)
} else {
globalCollection = globalBucket.DefaultCollection(nil)
}
globalTravelBucket = globalCluster.Bucket("travel-sample", &BucketOptions{UseMutationTokens: true})
_, err = globalTravelBucket.DefaultCollection(nil).Get("invalid", nil)
if !(err == nil || IsKeyNotFoundError(err)) {
globalTravelBucket = nil
}
os.Exit(m.Run())
}
func loadTestDataset(dataset string, valuePtr interface{}) error {
bytes, err := ioutil.ReadFile("testdata/" + dataset + ".json")
if err != nil {
return err
}
err = json.Unmarshal(bytes, &valuePtr)
if err != nil {
return err
}
return nil
}
type mockAuthenticator struct {
username string
password string
}
func newMockAuthenticator(username string, password string) *mockAuthenticator {
return &mockAuthenticator{
username: username,
password: password,
}
}
func (ma *mockAuthenticator) Credentials(req AuthCredsRequest) ([]UserPassPair, error) {
return []UserPassPair{{
Username: ma.username,
Password: ma.password,
}}, nil
}
type roundTripFunc func(req *http.Request) *http.Response
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}