-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathconfig_test.go
More file actions
320 lines (291 loc) · 9.51 KB
/
config_test.go
File metadata and controls
320 lines (291 loc) · 9.51 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
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
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/pingcap/tiproxy/lib/config"
"github.com/stretchr/testify/require"
)
func TestConfigReload(t *testing.T) {
tmpdir := t.TempDir()
tmpcfg := filepath.Join(tmpdir, "cfg")
f, err := os.Create(tmpcfg)
require.NoError(t, err)
require.NoError(t, f.Close())
cfgmgr1, _, _ := testConfigManager(t, tmpcfg, "addr")
cfgmgr2, _, _ := testConfigManager(t, "", "addr")
cases := []struct {
name string
precfg string
precheck func(*config.Config) bool
postcfg string
postcheck func(*config.Config) bool
}{
{
name: "pd override",
precfg: `proxy.pd-addrs = "127.0.0.1:2379"`,
precheck: func(c *config.Config) bool {
return c.Proxy.PDAddrs == "127.0.0.1:2379"
},
postcfg: `proxy.pd-addrs = ""`,
postcheck: func(c *config.Config) bool {
return c.Proxy.PDAddrs == "" && c.Proxy.AdvertiseAddr == "addr"
},
},
{
name: "proxy-protocol override",
precfg: `proxy.proxy-protocol = "v2"`,
precheck: func(c *config.Config) bool {
return c.Proxy.ProxyProtocol == "v2"
},
postcfg: `proxy.proxy-protocol = ""`,
postcheck: func(c *config.Config) bool {
return c.Proxy.ProxyProtocol == ""
},
},
{
name: "logfile name override",
precfg: `log.log-file.filename = "gdfg"`,
precheck: func(c *config.Config) bool {
return c.Log.LogFile.Filename == "gdfg"
},
postcfg: `log.log-file.filename = ""`,
postcheck: func(c *config.Config) bool {
return c.Log.LogFile.Filename == ""
},
},
{
name: "override empty fields",
precfg: `api.addr = ""`,
precheck: func(c *config.Config) bool {
return c.API.Addr == ""
},
postcfg: `api.addr = "0.0.0.0:3080"`,
postcheck: func(c *config.Config) bool {
return c.API.Addr == "0.0.0.0:3080"
},
},
{
name: "override non-empty fields",
precfg: `api.addr = "0.0.0.0:3080"`,
precheck: func(c *config.Config) bool {
return c.API.Addr == "0.0.0.0:3080"
},
postcfg: `api.addr = "0.0.0.0:3081"`,
postcheck: func(c *config.Config) bool {
return c.API.Addr == "0.0.0.0:3081"
},
},
{
name: "non empty fields should not be override by empty fields",
precfg: `proxy.addr = "gg"`,
precheck: func(c *config.Config) bool {
return c.Proxy.Addr == "gg"
},
postcfg: ``,
postcheck: func(c *config.Config) bool {
return c.Proxy.Addr == "gg"
},
},
}
for i, tc := range cases {
msg := fmt.Sprintf("%s[%d]", tc.name, i)
// normal path and HTTP API
require.NoError(t, cfgmgr2.SetTOMLConfig([]byte(tc.precfg)), msg)
if tc.precheck != nil {
require.True(t, tc.precheck(cfgmgr2.GetConfig()), msg)
}
require.NoError(t, cfgmgr2.SetTOMLConfig([]byte(tc.postcfg)), msg)
if tc.postcheck != nil {
require.True(t, tc.postcheck(cfgmgr2.GetConfig()), msg)
}
// config file auto reload
require.NoError(t, cfgmgr1.SetTOMLConfig([]byte(tc.precfg)), msg)
if tc.precheck != nil {
require.True(t, tc.precheck(cfgmgr1.GetConfig()), msg)
}
require.NoError(t, os.WriteFile(tmpcfg, []byte(tc.postcfg), 0644), msg)
if tc.postcheck != nil {
require.Eventually(t, func() bool { return tc.postcheck(cfgmgr1.GetConfig()) }, 3*time.Second, 100*time.Millisecond, msg)
}
}
}
func TestConfigRemove(t *testing.T) {
tmpdir := t.TempDir()
tmpcfg := filepath.Join(tmpdir, "cfg")
f, err := os.Create(tmpcfg)
require.NoError(t, err)
require.NoError(t, f.Close())
cfgmgr, _, _ := testConfigManager(t, tmpcfg, "")
// remove and recreate the file in a very short time
require.NoError(t, os.Remove(tmpcfg))
require.NoError(t, os.WriteFile(tmpcfg, []byte(`proxy.addr = "gg"`), 0644))
// check that re-watch still works
require.Eventually(t, func() bool { return cfgmgr.GetConfig() != nil && cfgmgr.GetConfig().Proxy.Addr == "gg" }, 3*time.Second, 100*time.Millisecond)
// remove again but with a long sleep
require.NoError(t, os.Remove(tmpcfg))
time.Sleep(200 * time.Millisecond)
// but eventually reload the file again
require.NoError(t, os.WriteFile(tmpcfg, []byte(`proxy.addr = "vv"`), 0644))
require.Eventually(t, func() bool { return cfgmgr.GetConfig().Proxy.Addr == "vv" }, 3*time.Second, 100*time.Millisecond)
}
func TestFilePath(t *testing.T) {
for i := 0; i < 10; i++ {
var cfgmgr *ConfigManager
tmpdir := t.TempDir()
pdAddr1, pdAddr2, pdAddr3 := "127.0.0.1:1000", "127.0.0.1:2000", "127.0.0.1:3000"
tests := []struct {
filename string
createFile func()
cleanFile func()
checker func(filename string)
}{
{
// Test updating another file in the same directory.
filename: filepath.Join(tmpdir, "cfg"),
checker: func(filename string) {
tmplog := filepath.Join(tmpdir, "log")
f, err := os.Create(tmplog)
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.WriteFile(tmplog, []byte("hello"), 0644))
newlog := filepath.Join(tmpdir, "log1")
require.NoError(t, os.Rename(tmplog, newlog))
require.NoError(t, os.Remove(newlog))
require.Equal(t, pdAddr2, cfgmgr.GetConfig().Proxy.PDAddrs)
},
},
{
// Test case-insensitive.
filename: filepath.Join(tmpdir, "cfg"),
createFile: func() {
f, err := os.Create(filepath.Join(tmpdir, "CFG"))
require.NoError(t, err)
require.NoError(t, f.Close())
// Linux is case-sensitive but macOS is case-insensitive.
// For linux, it creates another file. For macOS, it doesn't touch the file.
f, err = os.Create(filepath.Join(tmpdir, "cfg"))
require.NoError(t, err)
_, err = fmt.Fprintf(f, "proxy.pd-addrs = \"%s\"", pdAddr1)
require.NoError(t, err)
require.NoError(t, f.Close())
},
},
{
// Test relative path.
// `event.Name` is `cfg` on MacOS, but it's `./cfg` on Linux.
filename: "cfg",
},
{
// Test relative path.
filename: "./cfg",
},
{
// Test uncleaned path.
filename: fmt.Sprintf("%s%c%ccfg", tmpdir, filepath.Separator, filepath.Separator),
},
{
// Test removing and recreating the directory.
filename: "_tmp/cfg",
createFile: func() {
if err := os.Mkdir("_tmp", 0755); err != nil {
require.ErrorIs(t, err, os.ErrExist)
}
f, err := os.Create("_tmp/cfg")
require.NoError(t, err)
_, err = fmt.Fprintf(f, "proxy.pd-addrs = \"%s\"", pdAddr1)
require.NoError(t, err)
require.NoError(t, f.Close())
},
cleanFile: func() {
require.NoError(t, os.RemoveAll("_tmp"))
},
checker: func(filename string) {
require.NoError(t, os.RemoveAll("_tmp"))
t.Log("remove _tmp")
require.NoError(t, os.Mkdir("_tmp", 0755))
f, err := os.Create("_tmp/cfg")
require.NoError(t, err)
_, err = fmt.Fprintf(f, "proxy.pd-addrs = \"%s\"", pdAddr3)
require.NoError(t, err)
require.NoError(t, f.Close())
t.Log("write _tmp")
require.Eventually(t, func() bool {
return pdAddr3 == cfgmgr.GetConfig().Proxy.PDAddrs
}, 3*time.Second, 10*time.Millisecond, cfgmgr.GetConfig().Proxy.PDAddrs)
},
},
{
// Test removing and recreating the file.
filename: "cfg",
checker: func(filename string) {
require.NoError(t, os.Remove(filename))
f, err := os.Create(filename)
require.NoError(t, err)
_, err = fmt.Fprintf(f, "proxy.pd-addrs = \"%s\"", pdAddr3)
require.NoError(t, err)
require.NoError(t, f.Close())
require.Eventually(t, func() bool {
return pdAddr3 == cfgmgr.GetConfig().Proxy.PDAddrs
}, 3*time.Second, 10*time.Millisecond, cfgmgr.GetConfig().Proxy.PDAddrs)
},
},
}
for i, test := range tests {
t.Logf("%dth test", i+1)
if test.createFile != nil {
test.createFile()
} else {
f, err := os.Create(test.filename)
require.NoError(t, err)
_, err = fmt.Fprintf(f, "proxy.pd-addrs = \"%s\"", pdAddr1)
require.NoError(t, err)
require.NoError(t, f.Close())
}
cfgmgr, _, _ = testConfigManager(t, test.filename, "")
require.Equal(t, pdAddr1, cfgmgr.GetConfig().Proxy.PDAddrs)
// Test write.
require.NoError(t, os.WriteFile(test.filename, []byte(fmt.Sprintf("proxy.pd-addrs = \"%s\"", pdAddr2)), 0644))
require.Eventually(t, func() bool {
return pdAddr2 == cfgmgr.GetConfig().Proxy.PDAddrs
}, 3*time.Second, 10*time.Millisecond, cfgmgr.GetConfig().Proxy.PDAddrs)
// Test other.
if test.checker != nil {
test.checker(test.filename)
}
// Test remove.
if test.cleanFile != nil {
test.cleanFile()
} else {
// It doesn't matter whether it triggers reload or not.
require.NoError(t, os.Remove(test.filename))
}
require.NoError(t, cfgmgr.Close())
}
}
}
func TestChecksum(t *testing.T) {
cfgmgr, text, _ := testConfigManager(t, "", "")
require.Equal(t, 1, strings.Count(text.String(), "current config"))
c1 := cfgmgr.GetConfigChecksum()
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "gg"`)))
require.Equal(t, 2, strings.Count(text.String(), "current config"))
// same config, shouldn't log it again
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "gg"`)))
require.Equal(t, 2, strings.Count(text.String(), "current config"))
c2 := cfgmgr.GetConfigChecksum()
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "vv"`)))
c3 := cfgmgr.GetConfigChecksum()
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr="gg"`)))
require.Equal(t, 4, strings.Count(text.String(), "current config"))
c4 := cfgmgr.GetConfigChecksum()
require.Equal(t, c2, c4)
require.NotEqual(t, c1, c2)
require.NotEqual(t, c1, c3)
require.NotEqual(t, c2, c3)
}