forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connspec_test.go
211 lines (179 loc) · 6.19 KB
/
connspec_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
package gocb
import (
"runtime"
"testing"
)
func (s *connSpec) findHost(hostname string, scheme connSpecScheme) (a *connSpecAddr) {
if scheme == csInvalid || scheme.IsHTTP() {
for _, host := range s.HttpHosts {
if hostname == host.Host {
return host
}
}
}
if scheme == csInvalid || scheme.IsMCD() {
for _, host := range s.MemcachedHosts {
if hostname == host.Host {
return host
}
}
}
return nil
}
// Check if the host is present in both formats in its default form
func (s *connSpec) hasBoth(hostname string) bool {
if tmphost := s.findHost(hostname, csPlainHttp); tmphost == nil || tmphost.Port != uint16(csPlainHttp.DefaultPort()) {
return false
}
if tmphost := s.findHost(hostname, csPlainMcd); tmphost == nil || tmphost.Port != uint16(csPlainMcd.DefaultPort()) {
return false
}
return true
}
func parseOrDie(connstr string, t *testing.T) (cs connSpec) {
_, _, line, _ := runtime.Caller(1)
cs, err := parseConnSpec(connstr)
if err != nil {
t.Fatalf("(called from line %d) Failed to parse %s: %v", line, connstr, err)
}
return cs
}
func TestParseBasic(t *testing.T) {
cs := parseOrDie("couchbase://1.2.3.4", t)
if len(cs.HttpHosts) != 1 || len(cs.MemcachedHosts) != 1 {
t.Fatalf("Expected 1 memcached and 1 http host!")
}
if tmphost := cs.findHost("1.2.3.4", csPlainMcd); tmphost.Port != 11210 {
t.Fatalf("Wrong memcached port found (%d)", tmphost.Port)
}
if tmphost := cs.findHost("1.2.3.4", csPlainHttp); tmphost.Port != 8091 {
t.Fatalf("Wrong HTTP port found!")
}
cs, err := parseConnSpec("blah://foo.com")
if err == nil {
t.Fatalf("Expected error for bad scheme")
}
cs = parseOrDie("couchbase://", t)
if len(cs.MemcachedHosts) < 1 {
t.Fatalf("Scheme-only spec should inject localhost by default!")
}
if tmphost := cs.findHost("127.0.0.1", csPlainMcd); tmphost.Port != uint16(csPlainMcd.DefaultPort()) {
t.Fatalf("Bad implicit parsing of hostless connspec")
}
cs = parseOrDie("couchbase://?", t)
if len(cs.Options) != 0 {
t.Fatalf("Options should be 0 with couchbase://?")
}
if cs.findHost("127.0.0.1", csPlainMcd) == nil {
t.Fatalf("Parsed couchbase://? with '?' as host!")
}
cs = parseOrDie("1.2.3.4", t)
if !cs.hasBoth("1.2.3.4") {
t.Fatalf("Expected one Mcd and one Htp entry for schemeless host!")
}
cs = parseOrDie("1.2.3.4:8091", t)
if !cs.hasBoth("1.2.3.4") {
t.Fatalf("implicit xxx:8091 should contain both host types!")
}
cs, err = parseConnSpec("1.2.3.4:999")
if err == nil {
t.Fatalf("Expected error with non-default port without scheme")
}
}
func TestParseHosts(t *testing.T) {
cs := parseOrDie("couchbase://foo.com,bar.com,baz.com", t)
if len(cs.MemcachedHosts) != 3 || len(cs.HttpHosts) != 3 {
t.Fatalf("Memcached and HTTP hosts not at expected count")
}
// Parse using legacy format
cs, err := parseConnSpec("couchbase://foo.com:8091")
if err == nil {
t.Fatalf("Expected error for couchbase://XXX:8091")
}
cs = parseOrDie("couchbase://foo.com:4444", t)
if tmphost := cs.findHost("foo.com", csPlainMcd); tmphost.Port != 4444 {
t.Fatalf("couchbase://foo.com:4444 - want explicit mcd port")
}
if tmphost := cs.findHost("foo.com", csPlainHttp); tmphost != nil {
t.Fatalf("Derived HTTP port found when none should be present")
}
cs = parseOrDie("couchbases://foo.com:4444", t)
if tmphost := cs.findHost("foo.com", csPlainMcd); tmphost.Port != 4444 {
t.Fatalf("Explicit SSL port not set!")
}
if tmphost := cs.findHost("foo.com", csPlainHttp); tmphost != nil {
t.Fatalf("HTTP Host found with ssl explicit port")
}
cs = parseOrDie("couchbases://", t)
if tmphost := cs.findHost("127.0.0.1", csPlainMcd); tmphost.Port != uint16(csSslMcd.DefaultPort()) {
t.Fatal("Implicit couchbases scheme didn't set SSL/Mcd port!")
}
if tmphost := cs.findHost("127.0.0.1", csPlainHttp); tmphost.Port != uint16(csSslHttp.DefaultPort()) {
t.Fatal("Implicit couchbases scheme didn't set SSL/HTTP port")
}
cs = parseOrDie("couchbase://foo.com,bar.com:4444", t)
if tmphost := cs.findHost("bar.com", csPlainMcd); tmphost == nil || tmphost.Port != 4444 {
t.Fatalf("Couldn't find explicit mcd host!")
}
if tmphost := cs.findHost("bar.com", csPlainHttp); tmphost != nil {
t.Fatalf("can't derive http host with non-default mcd port")
}
if !cs.hasBoth("foo.com") {
t.Fatalf("Portless host is not found in both mcd and http")
}
// Check old-style delimiters
cs = parseOrDie("couchbase://foo.com;bar.com;baz.com", t)
if !cs.hasBoth("foo.com") || !cs.hasBoth("bar.com") || !cs.hasBoth("baz.com") {
t.Fatalf("Failed to parse all hosts in the spec!")
}
}
func TestParseBucket(t *testing.T) {
cs := parseOrDie("couchbase://foo.com/user", t)
if cs.Bucket != "user" {
t.Fatalf("Wrong bucket name. Expected user. Got %s", cs.Bucket)
}
cs = parseOrDie("couchbase://foo.com/user/", t)
if cs.Bucket != "user/" {
t.Fatalf("Wrong bucket name! (exp: user/, got: %s", cs.Bucket)
}
cs = parseOrDie("couchbase:///default", t)
if cs.Bucket != "default" {
t.Fatalf("Expected 'default' bucket without host. Got %s", cs.Bucket)
}
cs = parseOrDie("couchbase:///default?", t)
if cs.Bucket != "default" {
t.Fatalf("Expected 'default' bucket wihtout host. Got %s", cs.Bucket)
}
cs = parseOrDie("couchbase:///%2FUsers%2F?", t)
if cs.Bucket != "/Users/" {
t.Fatalf("Expected URL-encoding escaping. Got %s", cs.Bucket)
}
}
func TestOptionsPassthrough(t *testing.T) {
cs := parseOrDie("couchbase:///?foo=bar", t)
if cs.Options.Get("foo") != "bar" || len(cs.Options) != 1 {
t.Fatalf("Couldn't match options (///?)")
}
cs = parseOrDie("couchbase://?foo=bar", t)
if cs.Options.Get("foo") != "bar" || len(cs.Options) != 1 {
t.Fatalf("Couldn't match options (//?)")
}
/*
cs, err := parseConnSpec("couchbase://?foo")
if err == nil {
t.Fatalf("Expected error for value-less option!")
}
*/
cs = parseOrDie("couchbase://?foo=fooval&bar=barval", t)
if cs.Options.Get("foo") != "fooval" || cs.Options.Get("bar") != "barval" {
t.Fatalf("Missing options")
}
if len(cs.Options) != 2 {
t.Fatalf("Too many options!")
}
cs = parseOrDie("couchbase://?foo=fooval&bar=barval&", t)
if cs.Options.Get("foo") != "fooval" || cs.Options.Get("bar") != "barval" || len(cs.Options) != 2 {
t.Fatal("Didn't get expected options!")
}
// TODO: bootstrap_on
}