forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_handler_test.go
629 lines (561 loc) · 19 KB
/
http_handler_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
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
// Copyright 2018 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"bytes"
"database/sql"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
"sync/atomic"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
log "github.com/sirupsen/logrus"
)
type HTTPHandlerTestSuite struct {
server *Server
}
var _ = Suite(new(HTTPHandlerTestSuite))
func (ts *HTTPHandlerTestSuite) TestRegionIndexRange(c *C) {
sTableID := int64(3)
sIndex := int64(11)
eTableID := int64(9)
recordID := int64(133)
indexValues := []types.Datum{
types.NewIntDatum(100),
types.NewBytesDatum([]byte("foobar")),
types.NewFloat64Datum(-100.25),
}
var expectIndexValues []string
for _, v := range indexValues {
str, err := v.ToString()
if err != nil {
str = fmt.Sprintf("%d-%v", v.Kind(), v.GetValue())
}
expectIndexValues = append(expectIndexValues, str)
}
encodedValue, err := codec.EncodeKey(&stmtctx.StatementContext{TimeZone: time.Local}, nil, indexValues...)
c.Assert(err, IsNil)
startKey := tablecodec.EncodeIndexSeekKey(sTableID, sIndex, encodedValue)
recordPrefix := tablecodec.GenTableRecordPrefix(eTableID)
endKey := tablecodec.EncodeRecordKey(recordPrefix, recordID)
region := &tikv.KeyLocation{
Region: tikv.RegionVerID{},
StartKey: startKey,
EndKey: endKey,
}
r, err := NewRegionFrameRange(region)
c.Assert(err, IsNil)
c.Assert(r.first.IndexID, Equals, sIndex)
c.Assert(r.first.IsRecord, IsFalse)
c.Assert(r.first.RecordID, Equals, int64(0))
c.Assert(r.first.IndexValues, DeepEquals, expectIndexValues)
c.Assert(r.last.IsRecord, IsTrue)
c.Assert(r.last.RecordID, Equals, recordID)
c.Assert(r.last.IndexValues, IsNil)
testCases := []struct {
tableID int64
indexID int64
isCover bool
}{
{2, 0, false},
{3, 0, true},
{9, 0, true},
{10, 0, false},
{2, 10, false},
{3, 10, false},
{3, 11, true},
{3, 20, true},
{9, 10, true},
{10, 1, false},
}
for _, t := range testCases {
var f *FrameItem
if t.indexID == 0 {
f = r.getRecordFrame(t.tableID, "", "")
} else {
f = r.getIndexFrame(t.tableID, t.indexID, "", "", "")
}
if t.isCover {
c.Assert(f, NotNil)
} else {
c.Assert(f, IsNil)
}
}
}
func (ts *HTTPHandlerTestSuite) TestRegionIndexRangeWithEndNoLimit(c *C) {
sTableID := int64(15)
startKey := tablecodec.GenTableRecordPrefix(sTableID)
endKey := []byte("z_aaaaafdfd")
region := &tikv.KeyLocation{
Region: tikv.RegionVerID{},
StartKey: startKey,
EndKey: endKey,
}
r, err := NewRegionFrameRange(region)
c.Assert(err, IsNil)
c.Assert(r.first.IsRecord, IsTrue)
c.Assert(r.last.IsRecord, IsTrue)
c.Assert(r.getRecordFrame(300, "", ""), NotNil)
c.Assert(r.getIndexFrame(200, 100, "", "", ""), NotNil)
}
func (ts *HTTPHandlerTestSuite) TestRegionIndexRangeWithStartNoLimit(c *C) {
eTableID := int64(9)
startKey := []byte("m_aaaaafdfd")
endKey := tablecodec.GenTableRecordPrefix(eTableID)
region := &tikv.KeyLocation{
Region: tikv.RegionVerID{},
StartKey: startKey,
EndKey: endKey,
}
r, err := NewRegionFrameRange(region)
c.Assert(err, IsNil)
c.Assert(r.first.IsRecord, IsFalse)
c.Assert(r.last.IsRecord, IsTrue)
c.Assert(r.getRecordFrame(3, "", ""), NotNil)
c.Assert(r.getIndexFrame(8, 1, "", "", ""), NotNil)
}
func (ts *HTTPHandlerTestSuite) TestRegionsAPI(c *C) {
ts.startServer(c)
defer ts.stopServer(c)
resp, err := http.Get("http://127.0.0.1:10090/tables/information_schema/SCHEMATA/regions")
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var data TableRegions
err = decoder.Decode(&data)
c.Assert(err, IsNil)
c.Assert(len(data.RecordRegions) > 0, IsTrue)
// list region
for _, region := range data.RecordRegions {
c.Assert(regionContainsTable(c, region.ID, data.TableID), IsTrue)
}
}
func regionContainsTable(c *C, regionID uint64, tableID int64) bool {
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/regions/%d", regionID))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var data RegionDetail
err = decoder.Decode(&data)
c.Assert(err, IsNil)
for _, index := range data.Frames {
if index.TableID == tableID {
return true
}
}
return false
}
func (ts *HTTPHandlerTestSuite) TestListTableRegionsWithError(c *C) {
ts.startServer(c)
defer ts.stopServer(c)
resp, err := http.Get("http://127.0.0.1:10090/tables/fdsfds/aaa/regions")
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)
}
func (ts *HTTPHandlerTestSuite) TestGetRegionByIDWithError(c *C) {
ts.startServer(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/regions/xxx"))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)
defer resp.Body.Close()
}
func (ts *HTTPHandlerTestSuite) TestRegionsFromMeta(c *C) {
ts.startServer(c)
defer ts.stopServer(c)
resp, err := http.Get("http://127.0.0.1:10090/regions/meta")
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
// Verify the resp body.
decoder := json.NewDecoder(resp.Body)
metas := make([]RegionMeta, 0)
err = decoder.Decode(&metas)
c.Assert(err, IsNil)
for _, meta := range metas {
c.Assert(meta.ID != 0, IsTrue)
}
}
func (ts *HTTPHandlerTestSuite) startServer(c *C) {
mvccStore := mocktikv.MustNewMVCCStore()
store, err := mockstore.NewMockTikvStore(mockstore.WithMVCCStore(mvccStore))
c.Assert(err, IsNil)
_, err = session.BootstrapSession(store)
c.Assert(err, IsNil)
tidbdrv := NewTiDBDriver(store)
cfg := config.NewConfig()
cfg.Port = 4001
cfg.Store = "tikv"
cfg.Status.StatusPort = 10090
cfg.Status.ReportStatus = true
server, err := NewServer(cfg, tidbdrv)
c.Assert(err, IsNil)
ts.server = server
go server.Run()
waitUntilServerOnline(cfg.Status.StatusPort)
}
func (ts *HTTPHandlerTestSuite) stopServer(c *C) {
if ts.server != nil {
ts.server.Close()
}
}
func (ts *HTTPHandlerTestSuite) prepareData(c *C) {
db, err := sql.Open("mysql", getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer db.Close()
dbt := &DBTest{c, db}
dbt.mustExec("create database tidb;")
dbt.mustExec("use tidb;")
dbt.mustExec("create table tidb.test (a int auto_increment primary key, b varchar(20));")
dbt.mustExec("insert tidb.test values (1, 1);")
txn1, err := dbt.db.Begin()
c.Assert(err, IsNil)
_, err = txn1.Exec("update tidb.test set b = b + 1 where a = 1;")
c.Assert(err, IsNil)
_, err = txn1.Exec("insert tidb.test values (2, 2);")
c.Assert(err, IsNil)
_, err = txn1.Exec("insert tidb.test (a) values (3);")
c.Assert(err, IsNil)
_, err = txn1.Exec("insert tidb.test values (4, '');")
c.Assert(err, IsNil)
err = txn1.Commit()
c.Assert(err, IsNil)
dbt.mustExec("alter table tidb.test add index idx1 (a, b);")
dbt.mustExec("alter table tidb.test add unique index idx2 (a, b);")
}
func decodeKeyMvcc(closer io.ReadCloser, c *C, valid bool) {
decoder := json.NewDecoder(closer)
var data kvrpcpb.MvccGetByKeyResponse
err := decoder.Decode(&data)
c.Assert(err, IsNil)
if valid {
c.Assert(data.Info, NotNil)
c.Assert(len(data.Info.Writes), Greater, 0)
} else {
c.Assert(data.Info, IsNil)
}
}
func (ts *HTTPHandlerTestSuite) TestGetTableMVCC(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
c.Skip("MVCCLevelDB doesn't implement MVCCDebugger interface.")
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/mvcc/key/tidb/test/1"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var data kvrpcpb.MvccGetByKeyResponse
err = decoder.Decode(&data)
c.Assert(err, IsNil)
c.Assert(data.Info, NotNil)
c.Assert(len(data.Info.Writes), Greater, 0)
startTs := data.Info.Writes[0].StartTs
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/mvcc/txn/%d", startTs))
c.Assert(err, IsNil)
var p1 kvrpcpb.MvccGetByStartTsResponse
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&p1)
c.Assert(err, IsNil)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/mvcc/txn/%d/tidb/test", startTs))
c.Assert(err, IsNil)
var p2 kvrpcpb.MvccGetByStartTsResponse
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&p2)
c.Assert(err, IsNil)
for id, expect := range data.Info.Values {
v1 := p1.Info.Values[id].Value
v2 := p2.Info.Values[id].Value
c.Assert(bytes.Equal(v1, expect.Value), IsTrue)
c.Assert(bytes.Equal(v2, expect.Value), IsTrue)
}
_, key, err := codec.DecodeBytes(p1.Key, nil)
c.Assert(err, IsNil)
hexKey := hex.EncodeToString(key)
resp, err = http.Get("http://127.0.0.1:10090/mvcc/hex/" + hexKey)
c.Assert(err, IsNil)
decoder = json.NewDecoder(resp.Body)
var data2 kvrpcpb.MvccGetByKeyResponse
err = decoder.Decode(&data2)
c.Assert(err, IsNil)
c.Assert(data2, DeepEquals, data)
}
func (ts *HTTPHandlerTestSuite) TestGetMVCCNotFound(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/mvcc/key/tidb/test/1234"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var data kvrpcpb.MvccGetByKeyResponse
err = decoder.Decode(&data)
c.Assert(err, IsNil)
c.Assert(data.Info, IsNil)
c.Skip("MVCCLevelDB doesn't implement MVCCDebugger interface.")
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/mvcc/txn/0"))
c.Assert(err, IsNil)
var p kvrpcpb.MvccGetByStartTsResponse
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&p)
c.Assert(err, IsNil)
c.Assert(p.Info, IsNil)
}
func (ts *HTTPHandlerTestSuite) TestDecodeColumnValue(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
// column is a structure used for test
type column struct {
id int64
tp *types.FieldType
}
// Backfill columns.
c1 := &column{id: 1, tp: types.NewFieldType(mysql.TypeLonglong)}
c2 := &column{id: 2, tp: types.NewFieldType(mysql.TypeVarchar)}
c3 := &column{id: 3, tp: types.NewFieldType(mysql.TypeNewDecimal)}
c4 := &column{id: 4, tp: types.NewFieldType(mysql.TypeTimestamp)}
cols := []*column{c1, c2, c3, c4}
row := make([]types.Datum, len(cols))
row[0] = types.NewIntDatum(100)
row[1] = types.NewBytesDatum([]byte("abc"))
row[2] = types.NewDecimalDatum(types.NewDecFromInt(1))
row[3] = types.NewTimeDatum(types.Time{Time: types.FromGoTime(time.Now()), Fsp: 6, Type: mysql.TypeTimestamp})
// Encode the row.
colIDs := make([]int64, 0, 3)
for _, col := range cols {
colIDs = append(colIDs, col.id)
}
sc := &stmtctx.StatementContext{TimeZone: time.UTC}
bs, err := tablecodec.EncodeRow(sc, row, colIDs, nil, nil)
c.Assert(err, IsNil)
c.Assert(bs, NotNil)
bin := base64.StdEncoding.EncodeToString(bs)
unitTest := func(col *column) {
url := fmt.Sprintf("http://127.0.0.1:10090/tables/%d/%v/%d/%d?rowBin=%s", col.id, col.tp.Tp, col.tp.Flag, col.tp.Flen, bin)
resp, err := http.Get(url)
c.Assert(err, IsNil, Commentf("url:%s", url))
decoder := json.NewDecoder(resp.Body)
var data interface{}
err = decoder.Decode(&data)
c.Assert(err, IsNil, Commentf("url:%v\ndata%v", url, data))
colVal, err := types.DatumsToString([]types.Datum{row[col.id-1]})
c.Assert(err, IsNil)
c.Assert(data, Equals, colVal, Commentf("url:%v", url))
}
for _, col := range cols {
unitTest(col)
}
// Test bin has `+`.
// 2018-03-08 16:01:00.315313
bin = "CAIIyAEIBAIGYWJjCAYGAQCBCAgJsZ+TgISg1M8Z"
row[3] = types.NewTimeDatum(types.Time{Time: types.FromGoTime(time.Date(2018, 3, 8, 16, 1, 0, 315313000, time.UTC)), Fsp: 6, Type: mysql.TypeTimestamp})
unitTest(cols[3])
// Test bin has `/`.
// 2018-03-08 02:44:46.409199
bin = "CAIIyAEIBAIGYWJjCAYGAQCBCAgJ7/yY8LKF1M8Z"
row[3] = types.NewTimeDatum(types.Time{Time: types.FromGoTime(time.Date(2018, 3, 8, 2, 44, 46, 409199000, time.UTC)), Fsp: 6, Type: mysql.TypeTimestamp})
unitTest(cols[3])
}
func (ts *HTTPHandlerTestSuite) TestGetIndexMVCC(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
c.Skip("MVCCLevelDB doesn't implement MVCCDebugger interface.")
// tests for normal index key
resp, err := http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx1/1?a=1&b=2")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, true)
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx2/1?a=1&b=2")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, true)
// tests for index key which includes null
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx1/3?a=3&b")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, true)
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx2/3?a=3&b")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, true)
// tests for index key which includes empty string
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx1/4?a=4&b=")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, true)
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx2/3?a=4&b=")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, true)
// tests for wrong key
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx1/5?a=5&b=1")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, false)
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx2/5?a=5&b=1")
c.Assert(err, IsNil)
decodeKeyMvcc(resp.Body, c, false)
// tests for missing column value
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx1/1?a=1")
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var data1 kvrpcpb.MvccGetByKeyResponse
err = decoder.Decode(&data1)
c.Assert(err, NotNil)
resp, err = http.Get("http://127.0.0.1:10090/mvcc/index/tidb/test/idx2/1?a=1")
c.Assert(err, IsNil)
decoder = json.NewDecoder(resp.Body)
var data2 kvrpcpb.MvccGetByKeyResponse
err = decoder.Decode(&data2)
c.Assert(err, NotNil)
}
func (ts *HTTPHandlerTestSuite) TestGetSettings(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/settings"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var settings *config.Config
err = decoder.Decode(&settings)
c.Assert(err, IsNil)
c.Assert(settings, DeepEquals, config.GetGlobalConfig())
}
func (ts *HTTPHandlerTestSuite) TestGetSchema(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var dbs []*model.DBInfo
err = decoder.Decode(&dbs)
c.Assert(err, IsNil)
expects := []string{"information_schema", "mysql", "performance_schema", "test", "tidb"}
names := make([]string, len(dbs))
for i, v := range dbs {
names[i] = v.Name.L
}
sort.Strings(names)
c.Assert(names, DeepEquals, expects)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=5"))
c.Assert(err, IsNil)
var t *model.TableInfo
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&t)
c.Assert(err, IsNil)
c.Assert(t.Name.L, Equals, "user")
_, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=a"))
c.Assert(err, IsNil)
_, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=1"))
c.Assert(err, IsNil)
_, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=-1"))
c.Assert(err, IsNil)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/tidb"))
c.Assert(err, IsNil)
var lt []*model.TableInfo
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(<)
c.Assert(err, IsNil)
c.Assert(lt[0].Name.L, Equals, "test")
_, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/abc"))
c.Assert(err, IsNil)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/tidb/test"))
c.Assert(err, IsNil)
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&t)
c.Assert(err, IsNil)
c.Assert(t.Name.L, Equals, "test")
_, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/tidb/abc"))
c.Assert(err, IsNil)
}
func (ts *HTTPHandlerTestSuite) TestAllHistory(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/ddl/history/?limit=3"))
c.Assert(err, IsNil)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/ddl/history/?limit=-1"))
c.Assert(err, IsNil)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/ddl/history"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var jobs []*model.Job
s, _ := session.CreateSession(ts.server.newTikvHandlerTool().store.(kv.Storage))
defer s.Close()
store := domain.GetDomain(s.(sessionctx.Context)).Store()
txn, _ := store.Begin()
txnMeta := meta.NewMeta(txn)
txnMeta.GetAllHistoryDDLJobs()
data, _ := txnMeta.GetAllHistoryDDLJobs()
err = decoder.Decode(&jobs)
c.Assert(err, IsNil)
c.Assert(jobs, DeepEquals, data)
}
func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
form := make(url.Values)
form.Set("log_level", "error")
form.Set("tidb_general_log", "1")
resp, err := http.PostForm("http://127.0.0.1:10090/settings", form)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(log.GetLevel(), Equals, log.ErrorLevel)
c.Assert(config.GetGlobalConfig().Log.Level, Equals, "error")
c.Assert(atomic.LoadUint32(&variable.ProcessGeneralLog), Equals, uint32(1))
form = make(url.Values)
form.Set("log_level", "info")
form.Set("tidb_general_log", "0")
resp, err = http.PostForm("http://127.0.0.1:10090/settings", form)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(atomic.LoadUint32(&variable.ProcessGeneralLog), Equals, uint32(0))
c.Assert(log.GetLevel(), Equals, log.InfoLevel)
c.Assert(config.GetGlobalConfig().Log.Level, Equals, "info")
}
func (ts *HTTPHandlerTestSuite) TestPprof(c *C) {
ts.startServer(c)
defer ts.stopServer(c)
retryTime := 100
for retry := 0; retry < retryTime; retry++ {
resp, err := http.Get("http://127.0.0.1:10090/debug/pprof/heap")
if err == nil {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
return
}
time.Sleep(time.Millisecond * 10)
}
log.Fatalf("Failed to get profile for %d retries in every 10 ms", retryTime)
}