-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
server_test.go
519 lines (466 loc) · 15.8 KB
/
server_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
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Andrew Bonventre ([email protected])
package server
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"reflect"
"testing"
"time"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/gogo/protobuf/jsonpb"
"github.com/pkg/errors"
)
var nodeTestBaseContext = testutils.NewNodeTestBaseContext()
// TestSelfBootstrap verifies operation when no bootstrap hosts have
// been specified.
func TestSelfBootstrap(t *testing.T) {
defer leaktest.AfterTest(t)()
s, err := serverutils.StartServerRaw(base.TestServerArgs{})
if err != nil {
t.Fatal(err)
}
defer s.Stopper().Stop(context.TODO())
}
// TestServerStartClock tests that a server's clock is not pushed out of thin
// air. This used to happen - the simple act of starting was causing a server's
// clock to be pushed because we were introducing bogus future timestamps into
// our system.
func TestServerStartClock(t *testing.T) {
defer leaktest.AfterTest(t)()
// Set a high max-offset so that, if the server's clock is pushed by
// MaxOffset, we don't hide that under the latency of the Start operation
// which would allow the physical clock to catch up to the pushed one.
params := base.TestServerArgs{
Knobs: base.TestingKnobs{
Store: &storage.StoreTestingKnobs{
MaxOffset: time.Second,
},
},
}
s, _, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.TODO())
// Run a command so that we are sure to touch the timestamp cache. This is
// actually not needed because other commands run during server
// initialization, but we cannot guarantee that's going to stay that way.
get := &roachpb.GetRequest{
Span: roachpb.Span{Key: roachpb.Key("a")},
}
if _, err := client.SendWrapped(
context.Background(), s.KVClient().(*client.DB).GetSender(), get,
); err != nil {
t.Fatal(err)
}
now := s.Clock().Now()
// We rely on s.Clock() having been initialized from hlc.UnixNano(), which is a
// bit fragile.
physicalNow := hlc.UnixNano()
serverClockWasPushed := (now.Logical > 0) || (now.WallTime > physicalNow)
if serverClockWasPushed {
t.Fatalf("time: server %s vs actual %d", now, physicalNow)
}
}
// TestPlainHTTPServer verifies that we can serve plain http and talk to it.
// This is controlled by -cert=""
func TestPlainHTTPServer(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{
// The default context uses embedded certs.
Insecure: true,
})
defer s.Stopper().Stop(context.TODO())
var data serverpb.JSONResponse
testutils.SucceedsSoon(t, func() error {
return getStatusJSONProto(s, "metrics/local", &data)
})
ctx := s.RPCContext()
ctx.Insecure = false
if err := getStatusJSONProto(s, "metrics/local", &data); !testutils.IsError(err, "http: server gave HTTP response to HTTPS client") {
t.Fatalf("unexpected error %v", err)
}
}
func TestSecureHTTPRedirect(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.TODO())
ts := s.(*TestServer)
httpClient, err := s.GetHTTPClient()
if err != nil {
t.Fatal(err)
}
// Avoid automatically following redirects.
httpClient.CheckRedirect = func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}
origURL := "http://" + ts.Cfg.HTTPAddr
expURL := url.URL{Scheme: "https", Host: ts.Cfg.HTTPAddr, Path: "/"}
if resp, err := httpClient.Get(origURL); err != nil {
t.Fatal(err)
} else {
resp.Body.Close()
if resp.StatusCode != http.StatusPermanentRedirect {
t.Errorf("expected status code %d; got %d", http.StatusPermanentRedirect, resp.StatusCode)
}
if redirectURL, err := resp.Location(); err != nil {
t.Error(err)
} else if a, e := redirectURL.String(), expURL.String(); a != e {
t.Errorf("expected location %s; got %s", e, a)
}
}
if resp, err := httpClient.Post(origURL, "text/plain; charset=utf-8", nil); err != nil {
t.Fatal(err)
} else {
resp.Body.Close()
if resp.StatusCode != http.StatusPermanentRedirect {
t.Errorf("expected status code %d; got %d", http.StatusPermanentRedirect, resp.StatusCode)
}
if redirectURL, err := resp.Location(); err != nil {
t.Error(err)
} else if a, e := redirectURL.String(), expURL.String(); a != e {
t.Errorf("expected location %s; got %s", e, a)
}
}
}
// TestAcceptEncoding hits the server while explicitly disabling
// decompression on a custom client's Transport and setting it
// conditionally via the request's Accept-Encoding headers.
func TestAcceptEncoding(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.TODO())
client, err := s.GetHTTPClient()
if err != nil {
t.Fatal(err)
}
testData := []struct {
acceptEncoding string
newReader func(io.Reader) io.Reader
}{
{"",
func(b io.Reader) io.Reader {
return b
},
},
{httputil.GzipEncoding,
func(b io.Reader) io.Reader {
r, err := gzip.NewReader(b)
if err != nil {
t.Fatalf("could not create new gzip reader: %s", err)
}
return r
},
},
}
for _, d := range testData {
req, err := http.NewRequest("GET", s.AdminURL()+statusPrefix+"metrics/local", nil)
if err != nil {
t.Fatalf("could not create request: %s", err)
}
if d.acceptEncoding != "" {
req.Header.Set(httputil.AcceptEncodingHeader, d.acceptEncoding)
}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("could not make request to %s: %s", req.URL, err)
}
defer resp.Body.Close()
if ce := resp.Header.Get(httputil.ContentEncodingHeader); ce != d.acceptEncoding {
t.Fatalf("unexpected content encoding: '%s' != '%s'", ce, d.acceptEncoding)
}
r := d.newReader(resp.Body)
var data serverpb.JSONResponse
if err := jsonpb.Unmarshal(r, &data); err != nil {
t.Error(err)
}
}
}
// TestMultiRangeScanDeleteRange tests that commands which access multiple
// ranges are carried out properly.
func TestMultiRangeScanDeleteRange(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, db := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.TODO())
ts := s.(*TestServer)
tds := db.GetSender()
if err := ts.node.storeCfg.DB.AdminSplit(context.TODO(), "m"); err != nil {
t.Fatal(err)
}
writes := []roachpb.Key{roachpb.Key("a"), roachpb.Key("z")}
get := &roachpb.GetRequest{
Span: roachpb.Span{Key: writes[0]},
}
get.EndKey = writes[len(writes)-1]
if _, err := client.SendWrapped(context.Background(), tds, get); err == nil {
t.Errorf("able to call Get with a key range: %v", get)
}
var delTS hlc.Timestamp
for i, k := range writes {
put := roachpb.NewPut(k, roachpb.MakeValueFromBytes(k))
if _, err := client.SendWrapped(context.Background(), tds, put); err != nil {
t.Fatal(err)
}
scan := roachpb.NewScan(writes[0], writes[len(writes)-1].Next())
reply, err := client.SendWrapped(context.Background(), tds, scan)
if err != nil {
t.Fatal(err)
}
sr := reply.(*roachpb.ScanResponse)
if sr.Txn != nil {
// This was the other way around at some point in the past.
// Same below for Delete, etc.
t.Errorf("expected no transaction in response header")
}
if rows := sr.Rows; len(rows) != i+1 {
t.Fatalf("expected %d rows, but got %d", i+1, len(rows))
}
}
del := &roachpb.DeleteRangeRequest{
Span: roachpb.Span{
Key: writes[0],
EndKey: roachpb.Key(writes[len(writes)-1]).Next(),
},
ReturnKeys: true,
}
reply, err := client.SendWrappedWith(context.Background(), tds, roachpb.Header{Timestamp: delTS}, del)
if err != nil {
t.Fatal(err)
}
dr := reply.(*roachpb.DeleteRangeResponse)
if dr.Txn != nil {
t.Errorf("expected no transaction in response header")
}
if !reflect.DeepEqual(dr.Keys, writes) {
t.Errorf("expected %d keys to be deleted, but got %d instead", writes, dr.Keys)
}
scan := roachpb.NewScan(writes[0], writes[len(writes)-1].Next())
txn := roachpb.NewTransaction("MyTxn", nil, 0, 0, s.Clock().Now(), 0)
reply, err = client.SendWrappedWith(context.Background(), tds, roachpb.Header{Txn: txn}, scan)
if err != nil {
t.Fatal(err)
}
sr := reply.(*roachpb.ScanResponse)
if txn := sr.Txn; txn == nil || txn.Name != "MyTxn" {
t.Errorf("wanted Txn to persist, but it changed to %v", txn)
}
if rows := sr.Rows; len(rows) > 0 {
t.Fatalf("scan after delete returned rows: %v", rows)
}
}
// TestMultiRangeScanWithMaxResults tests that commands which access multiple
// ranges with MaxResults parameter are carried out properly.
func TestMultiRangeScanWithMaxResults(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
splitKeys []roachpb.Key
keys []roachpb.Key
}{
{[]roachpb.Key{roachpb.Key("m")},
[]roachpb.Key{roachpb.Key("a"), roachpb.Key("z")}},
{[]roachpb.Key{roachpb.Key("h"), roachpb.Key("q")},
[]roachpb.Key{roachpb.Key("b"), roachpb.Key("f"), roachpb.Key("k"),
roachpb.Key("r"), roachpb.Key("w"), roachpb.Key("y")}},
}
for i, tc := range testCases {
s, _, db := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.TODO())
ts := s.(*TestServer)
tds := db.GetSender()
for _, sk := range tc.splitKeys {
if err := ts.node.storeCfg.DB.AdminSplit(context.TODO(), sk); err != nil {
t.Fatal(err)
}
}
for _, k := range tc.keys {
put := roachpb.NewPut(k, roachpb.MakeValueFromBytes(k))
if _, err := client.SendWrapped(context.Background(), tds, put); err != nil {
t.Fatal(err)
}
}
// Try every possible ScanRequest startKey.
for start := 0; start < len(tc.keys); start++ {
// Try every possible maxResults, from 1 to beyond the size of key array.
for maxResults := 1; maxResults <= len(tc.keys)-start+1; maxResults++ {
scan := roachpb.NewScan(tc.keys[start], tc.keys[len(tc.keys)-1].Next())
reply, err := client.SendWrappedWith(
context.Background(), tds, roachpb.Header{MaxSpanRequestKeys: int64(maxResults)}, scan,
)
if err != nil {
t.Fatal(err)
}
rows := reply.(*roachpb.ScanResponse).Rows
if start+maxResults <= len(tc.keys) && len(rows) != maxResults {
t.Errorf("%d: start=%s: expected %d rows, but got %d", i, tc.keys[start], maxResults, len(rows))
} else if start+maxResults == len(tc.keys)+1 && len(rows) != maxResults-1 {
t.Errorf("%d: expected %d rows, but got %d", i, maxResults-1, len(rows))
}
}
}
}
}
func TestSystemConfigGossip(t *testing.T) {
defer leaktest.AfterTest(t)()
t.Skip("#12351")
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.TODO())
ts := s.(*TestServer)
ctx := context.TODO()
key := sqlbase.MakeDescMetadataKey(keys.MaxReservedDescID)
valAt := func(i int) *sqlbase.DatabaseDescriptor {
return &sqlbase.DatabaseDescriptor{Name: "foo", ID: sqlbase.ID(i)}
}
// Register a callback for gossip updates.
resultChan := ts.Gossip().RegisterSystemConfigChannel()
// The span gets gossiped when it first shows up.
select {
case <-resultChan:
case <-time.After(500 * time.Millisecond):
t.Fatal("did not receive gossip message")
}
// Write a system key with the transaction marked as having a Gossip trigger.
if err := kvDB.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
return txn.Put(ctx, key, valAt(2))
}); err != nil {
t.Fatal(err)
}
// This has to be wrapped in a SucceedSoon because system migrations on the
// testserver's startup can trigger system config updates without the key we
// wrote.
testutils.SucceedsSoon(t, func() error {
// New system config received.
var systemConfig config.SystemConfig
select {
case <-resultChan:
systemConfig, _ = ts.gossip.GetSystemConfig()
case <-time.After(500 * time.Millisecond):
return errors.Errorf("did not receive gossip message")
}
// Now check the new config.
var val *roachpb.Value
for _, kv := range systemConfig.Values {
if bytes.Equal(key, kv.Key) {
val = &kv.Value
break
}
}
if val == nil {
return errors.Errorf("key not found in gossiped info")
}
// Make sure the returned value is valAt(2).
got := new(sqlbase.DatabaseDescriptor)
if err := val.GetProto(got); err != nil {
return err
}
if expected := valAt(2); !reflect.DeepEqual(got, expected) {
return errors.Errorf("mismatch: expected %+v, got %+v", *expected, *got)
}
return nil
})
}
func checkOfficialize(t *testing.T, network, oldAddrString, newAddrString, expAddrString string) {
resolvedAddr := util.NewUnresolvedAddr(network, newAddrString)
if unresolvedAddr, err := officialAddr(oldAddrString, resolvedAddr); err != nil {
t.Fatal(err)
} else if retAddrString := unresolvedAddr.String(); retAddrString != expAddrString {
t.Errorf("officialAddr(%s, %s) was %s; expected %s", oldAddrString, newAddrString, retAddrString, expAddrString)
}
}
func TestOfficializeAddr(t *testing.T) {
defer leaktest.AfterTest(t)()
hostname, err := os.Hostname()
if err != nil {
t.Fatal(err)
}
for _, network := range []string{"tcp", "tcp4", "tcp6"} {
checkOfficialize(t, network, "hellow.world:0", "127.0.0.1:1234", "hellow.world:1234")
checkOfficialize(t, network, "hellow.world:1234", "127.0.0.1:2345", "hellow.world:1234")
checkOfficialize(t, network, ":1234", "127.0.0.1:2345", net.JoinHostPort(hostname, "1234"))
checkOfficialize(t, network, ":0", "127.0.0.1:2345", net.JoinHostPort(hostname, "2345"))
}
}
func TestClusterStores(t *testing.T) {
defer leaktest.AfterTest(t)()
tc := serverutils.StartTestCluster(t, 2, base.TestClusterArgs{})
defer tc.Stopper().Stop(context.TODO())
testutils.SucceedsSoon(t, func() error {
stores := tc.Server(0).(*TestServer).ClusterStores()
if len(stores) != 2 {
return errors.Errorf("expected 2 stores, got %v", stores)
}
n1 := tc.Server(0).NodeID()
n2 := tc.Server(1).NodeID()
s1 := stores[0].NodeID
s2 := stores[1].NodeID
if !(n1 == s1 && n2 == s2) && !(n1 == s2 && n2 == s1) {
return errors.Errorf("expected stores for nodes %d, %d, got %v", n1, n2, stores)
}
return nil
})
}
func TestListenURLFileCreation(t *testing.T) {
defer leaktest.AfterTest(t)()
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
filePath := path.Join(cwd, "listening_url_file")
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{
ListeningURLFile: filePath,
})
defer s.Stopper().Stop(context.TODO())
defer func() {
os.Remove(filePath)
}()
testutils.SucceedsSoon(t, func() error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
u, err := url.Parse(string(data))
if err != nil {
return err
}
if s.ServingAddr() != u.Host {
return errors.Errorf(
"expected address in file %s, got %s, full URL in file %s",
s.ServingAddr(), u.Host, string(data))
}
return nil
})
}