forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_test.go
More file actions
142 lines (121 loc) · 4.05 KB
/
global_test.go
File metadata and controls
142 lines (121 loc) · 4.05 KB
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
// Copyright 2021 TiKV Project 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.
package tests
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/tests"
cmd "github.com/tikv/pd/tools/pd-ctl/pdctl"
"github.com/tikv/pd/tools/pd-ctl/pdctl/command"
)
func TestSendAndGetComponent(t *testing.T) {
re := require.New(t)
handler := func(context.Context, *server.Server) (http.Handler, apiutil.APIServiceGroup, error) {
mux := http.NewServeMux()
// check pd http sdk api
mux.HandleFunc("/pd/api/v1/cluster", func(w http.ResponseWriter, r *http.Request) {
callerID := apiutil.GetCallerIDOnHTTP(r)
re.Equal(command.PDControlCallerID, callerID)
cluster := &metapb.Cluster{Id: 1}
clusterBytes, err := json.Marshal(cluster)
re.NoError(err)
w.Write(clusterBytes)
})
// check http client api
// TODO: remove this comment after replacing dialClient with the PD HTTP client completely.
mux.HandleFunc("/pd/api/v1/stores", func(w http.ResponseWriter, r *http.Request) {
callerID := apiutil.GetCallerIDOnHTTP(r)
re.Equal(command.PDControlCallerID, callerID)
fmt.Fprint(w, callerID)
})
info := apiutil.APIServiceGroup{
IsCore: true,
}
return mux, info, nil
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestClusterWithHandlers(ctx, 1, []server.HandlerBuilder{handler})
re.NoError(err)
defer cluster.Destroy()
err = cluster.RunInitialServers()
re.NoError(err)
leaderName := cluster.WaitLeader()
re.NotEmpty(leaderName)
pdAddr := cluster.GetLeaderServer().GetAddr()
cmd := cmd.GetRootCmd()
args := []string{"-u", pdAddr, "cluster"}
output, err := ExecuteCommand(cmd, args...)
re.NoError(err)
re.Equal(fmt.Sprintf("%s\n", `{
"id": 1
}`), string(output))
args = []string{"-u", pdAddr, "store"}
output, err = ExecuteCommand(cmd, args...)
re.NoError(err)
re.Equal(fmt.Sprintf("%s\n", command.PDControlCallerID), string(output))
}
func TestRegionDirectHeader(t *testing.T) {
re := require.New(t)
var (
mu sync.Mutex
count = 0
)
handler := func(context.Context, *server.Server) (http.Handler, apiutil.APIServiceGroup, error) {
mux := http.NewServeMux()
mux.HandleFunc("/pd/api/v1/regions", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
if vals := r.Header.Values(apiutil.PDAllowFollowerHandleHeader); len(vals) > 0 {
count++
}
mu.Unlock()
fmt.Fprint(w, `{}`)
})
info := apiutil.APIServiceGroup{IsCore: true}
return mux, info, nil
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestClusterWithHandlers(ctx, 1, []server.HandlerBuilder{handler})
re.NoError(err)
defer cluster.Destroy()
err = cluster.RunInitialServers()
re.NoError(err)
leaderName := cluster.WaitLeader()
re.NotEmpty(leaderName)
pdAddr := cluster.GetLeaderServer().GetAddr()
cmd := cmd.GetRootCmd()
_, err = ExecuteCommand(cmd, "-u", pdAddr, "region")
re.NoError(err)
re.Equal(0, count)
// PD-Allow-follower-handle is only added when --direct=true.
_, err = ExecuteCommand(cmd, "-u", pdAddr, "region", "--direct")
re.NoError(err)
re.Equal(1, count)
// --direct=false should not add PD-Allow-follower-handle.
_, err = ExecuteCommand(cmd, "-u", pdAddr, "region", "--direct=false")
re.NoError(err)
re.Equal(1, count)
_, err = ExecuteCommand(cmd, "-u", pdAddr, "region", "--direct=true")
re.NoError(err)
re.Equal(2, count)
}