-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
287 lines (240 loc) · 7.15 KB
/
mock.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
/*
* NeoDB
*
* Copyright 2018 The Radon Authors.
* Copyright 2021-2030 The NeoDB Authors.
* Code is licensed under the GPLv3.
*
*/
package proxy
import (
"fmt"
"math/rand"
"os"
"path"
"time"
"github.com/sealdb/neodb/config"
"github.com/sealdb/neodb/fakedb"
"github.com/sealdb/neodb/plugins/privilege"
querypb "github.com/sealdb/mysqlstack/sqlparser/depends/query"
"github.com/sealdb/mysqlstack/sqlparser/depends/sqltypes"
"github.com/sealdb/mysqlstack/xlog"
)
var (
result1 = &sqltypes.Result{
RowsAffected: 2,
Fields: []*querypb.Field{
{
Name: "id",
Type: querypb.Type_INT32,
},
{
Name: "name",
Type: querypb.Type_VARCHAR,
},
},
Rows: [][]sqltypes.Value{
{
sqltypes.MakeTrusted(querypb.Type_INT32, []byte("11")),
sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("1nice name")),
},
{
sqltypes.MakeTrusted(querypb.Type_INT32, []byte("12")),
sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("12nice name")),
},
},
}
autocommitResult1 = &sqltypes.Result{
RowsAffected: 5,
Fields: []*querypb.Field{
{
Name: "@@autocommit",
Type: querypb.Type_INT64,
},
},
Rows: [][]sqltypes.Value{
{
sqltypes.MakeTrusted(querypb.Type_INT64, []byte("1")),
},
},
}
)
func randomPort(min int, max int) int {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
d, delta := min, (max - min)
if delta > 0 {
d += rand.Intn(int(delta))
}
return d
}
// MockDefaultConfig mocks the default config.
func MockDefaultConfig() *config.Config {
conf := &config.Config{
Proxy: config.DefaultProxyConfig(),
Audit: config.DefaultAuditConfig(),
Router: config.DefaultRouterConfig(),
Log: config.DefaultLogConfig(),
Scatter: config.DefaultScatterConfig(),
}
return conf
}
// MockConfigMax16 mocks the config with MaxConnections=16.
func MockConfigMax16() *config.Config {
conf := MockDefaultConfig()
conf.Proxy.IPS = []string{"127.0.0.2"}
conf.Proxy.MetaDir = "/tmp/test_neodbmeta"
conf.Proxy.TwopcEnable = false
conf.Proxy.Endpoint = "127.0.0.1:3306"
conf.Proxy.MaxConnections = 16
conf.Proxy.MaxResultSize = 1024 * 1024 * 1024 // 1GB
conf.Proxy.MaxJoinRows = 32768
conf.Proxy.DDLTimeout = 10 * 3600 * 1000 // 10 hours
conf.Proxy.QueryTimeout = 5 * 60 * 1000 // 5 minutes
conf.Log = &config.LogConfig{
Level: "ERROR",
}
return conf
}
// MockProxy mocks a proxy.
func MockProxy(log *xlog.Log) (*fakedb.DB, *Proxy, func()) {
return MockProxy1(log, MockDefaultConfig())
}
// MockProxy1 mocks the proxy with config.
func MockProxy1(log *xlog.Log, conf *config.Config) (*fakedb.DB, *Proxy, func()) {
tmpDir := fakedb.GetTmpDir("", "neodb_mock_", log)
// set Blocks 128
conf.Router.Blocks = 128
// Fake backends.
fakedbs := fakedb.New(log, 5)
port := randomPort(15000, 20000)
addr := fmt.Sprintf(":%d", port)
conf.Proxy.Endpoint = addr
fileFormat := "20060102150405.000"
t := time.Now().UTC()
timestamp := t.Format(fileFormat)
metaDir := tmpDir + "/test_neodbmeta_" + timestamp
conf.Proxy.MetaDir = metaDir
if x := os.MkdirAll(metaDir, 0777); x != nil {
log.Panic("%+v", x)
}
backendsConf := &config.BackendsConfig{Backends: fakedbs.BackendConfs()}
if err := config.WriteConfig(path.Join(conf.Proxy.MetaDir, "backend.json"), backendsConf); err != nil {
log.Panic("mock.proxy.write.backends.config.error:%+v", err)
}
// the user with super privilege.
privilege.MockInitPrivilegeY(fakedbs)
// Proxy.
mockJSON := tmpDir + "/neodb_mock.json"
proxy := NewProxy(log, mockJSON, "", conf)
proxy.Start()
return fakedbs, proxy, func() {
proxy.Stop()
fakedbs.Close()
os.RemoveAll(tmpDir)
}
}
// MockProxyPrivilegeN mocks the proxy with Privilege N.
func MockProxyPrivilegeN(log *xlog.Log, conf *config.Config) (*fakedb.DB, *Proxy, func()) {
tmpDir := fakedb.GetTmpDir("", "neodb_mock_", log)
// set Blocks 128
conf.Router.Blocks = 128
// Fake backends.
fakedbs := fakedb.New(log, 5)
port := randomPort(15000, 20000)
addr := fmt.Sprintf(":%d", port)
conf.Proxy.Endpoint = addr
fileFormat := "20060102150405.000"
t := time.Now().UTC()
timestamp := t.Format(fileFormat)
metaDir := tmpDir + "/test_neodbmeta_" + timestamp
conf.Proxy.MetaDir = metaDir
if x := os.MkdirAll(metaDir, 0777); x != nil {
log.Panic("%+v", x)
}
backendsConf := &config.BackendsConfig{Backends: fakedbs.BackendConfs()}
if err := config.WriteConfig(path.Join(conf.Proxy.MetaDir, "backend.json"), backendsConf); err != nil {
log.Panic("mock.proxy.write.backends.config.error:%+v", err)
}
privilege.MockInitPrivilegeN(fakedbs)
// Proxy.
mockJSON := tmpDir + "/neodb_mock.json"
proxy := NewProxy(log, mockJSON, "", conf)
proxy.Start()
return fakedbs, proxy, func() {
proxy.Stop()
fakedbs.Close()
os.RemoveAll(tmpDir)
}
}
// MockProxyPrivilegeNotSuper mocks the proxy Not Super Privilege.
func MockProxyPrivilegeNotSuper(log *xlog.Log, conf *config.Config) (*fakedb.DB, *Proxy, func()) {
tmpDir := fakedb.GetTmpDir("", "neodb_mock_", log)
// set Blocks 128
conf.Router.Blocks = 128
// Fake backends.
fakedbs := fakedb.New(log, 5)
port := randomPort(15000, 20000)
addr := fmt.Sprintf(":%d", port)
conf.Proxy.Endpoint = addr
fileFormat := "20060102150405.000"
t := time.Now().UTC()
timestamp := t.Format(fileFormat)
metaDir := tmpDir + "/test_neodbmeta_" + timestamp
conf.Proxy.MetaDir = metaDir
if x := os.MkdirAll(metaDir, 0777); x != nil {
log.Panic("%+v", x)
}
backendsConf := &config.BackendsConfig{Backends: fakedbs.BackendConfs()}
if err := config.WriteConfig(path.Join(conf.Proxy.MetaDir, "backend.json"), backendsConf); err != nil {
log.Panic("mock.proxy.write.backends.config.error:%+v", err)
}
privilege.MockInitPrivilegeNotSuper(fakedbs)
// Proxy.
mockJSON := tmpDir + "/neodb_mock.json"
proxy := NewProxy(log, mockJSON, "", conf)
proxy.Start()
return fakedbs, proxy, func() {
proxy.Stop()
fakedbs.Close()
os.RemoveAll(tmpDir)
}
}
// MockProxyPrivilegeUsers mocks the proxy with multipe users.
func MockProxyPrivilegeUsers(log *xlog.Log, conf *config.Config) (*fakedb.DB, *Proxy, func()) {
tmpDir := fakedb.GetTmpDir("", "neodb_mock_", log)
// set Blocks 128
conf.Router.Blocks = 128
// Fake backends.
fakedbs := fakedb.New(log, 5)
port := randomPort(15000, 20000)
addr := fmt.Sprintf(":%d", port)
conf.Proxy.Endpoint = addr
fileFormat := "20060102150405.000"
t := time.Now().UTC()
timestamp := t.Format(fileFormat)
metaDir := tmpDir + "/test_neodbmeta_" + timestamp
conf.Proxy.MetaDir = metaDir
if x := os.MkdirAll(metaDir, 0777); x != nil {
log.Panic("%+v", x)
}
backendsConf := &config.BackendsConfig{Backends: fakedbs.BackendConfs()}
if err := config.WriteConfig(path.Join(conf.Proxy.MetaDir, "backend.json"), backendsConf); err != nil {
log.Panic("mock.proxy.write.backends.config.error:%+v", err)
}
privilege.MockInitPrivilegeUsers(fakedbs)
// Proxy.
mockJSON := tmpDir + "/neodb_mock.json"
proxy := NewProxy(log, mockJSON, "", conf)
proxy.Start()
return fakedbs, proxy, func() {
proxy.Stop()
fakedbs.Close()
os.RemoveAll(tmpDir)
}
}
// MockConfigIdleTxnTimeout1 mocks the config with IdleTxnTimeout=1.
func MockConfigIdleTxnTimeout1() *config.Config {
conf := MockDefaultConfig()
conf.Proxy.IdleTxnTimeout = 1 // 1s
return conf
}