forked from metalogical/BigFiles
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
477 lines (397 loc) · 11.5 KB
/
Copy pathmain_test.go
File metadata and controls
477 lines (397 loc) · 11.5 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
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
package main
import (
"errors"
"flag"
"net"
"net/http"
"os"
"reflect"
"syscall"
"testing"
"time"
"bou.ke/monkey"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"github.com/metalogical/BigFiles/auth"
"github.com/metalogical/BigFiles/config"
"github.com/metalogical/BigFiles/db"
"github.com/metalogical/BigFiles/server"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func patchFatalf() {
monkey.PatchInstanceMethod(reflect.TypeOf(logrus.StandardLogger()), "Fatalf",
func(_ *logrus.Logger, format string, args ...interface{}) {
panic(format)
})
}
func Test_initConfig_serverInitError(t *testing.T) {
monkey.Patch(server.Init, func(cfg *config.Config) error {
return errors.New("server init failed")
})
defer monkey.UnpatchAll()
cfg := &config.Config{}
err := initConfig(cfg)
assert.Error(t, err)
assert.Contains(t, err.Error(), "load ValidateConfig")
}
func Test_initConfig_authInitError(t *testing.T) {
monkey.Patch(server.Init, func(cfg *config.Config) error { return nil })
monkey.Patch(auth.Init, func(cfg *config.Config) error {
return errors.New("auth init failed")
})
defer monkey.UnpatchAll()
cfg := &config.Config{}
err := initConfig(cfg)
assert.Error(t, err)
assert.Contains(t, err.Error(), "load gitee config")
}
func Test_initConfig_dbInitError(t *testing.T) {
monkey.Patch(server.Init, func(cfg *config.Config) error { return nil })
monkey.Patch(auth.Init, func(cfg *config.Config) error { return nil })
monkey.Patch(db.Init, func(cfg config.DBConfig) error {
return errors.New("db init failed")
})
defer monkey.UnpatchAll()
cfg := &config.Config{}
err := initConfig(cfg)
assert.Error(t, err)
assert.Contains(t, err.Error(), "init database config")
}
func Test_initConfig_success(t *testing.T) {
monkey.Patch(server.Init, func(cfg *config.Config) error { return nil })
monkey.Patch(auth.Init, func(cfg *config.Config) error { return nil })
monkey.Patch(db.Init, func(cfg config.DBConfig) error { return nil })
defer monkey.UnpatchAll()
cfg := &config.Config{}
err := initConfig(cfg)
assert.NoError(t, err)
}
func patchObsNew(fn func(ak, sk, endpoint string) (*obs.ObsClient, error)) {
target := obs.New
targetType := reflect.TypeOf(target)
wrapper := reflect.MakeFunc(targetType, func(args []reflect.Value) []reflect.Value {
ak := args[0].String()
sk := args[1].String()
endpoint := args[2].String()
result, err := fn(ak, sk, endpoint)
var errVal reflect.Value
if err != nil {
errVal = reflect.ValueOf(err)
} else {
errVal = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
}
return []reflect.Value{reflect.ValueOf(result), errVal}
})
monkey.Patch(target, wrapper.Interface())
}
func Test_initObsClient_error(t *testing.T) {
patchObsNew(func(ak, sk, endpoint string) (*obs.ObsClient, error) {
return nil, errors.New("obs new failed")
})
defer monkey.UnpatchAll()
cfg := &config.Config{
ObsAccessKeyId: "fake-ak",
ObsSecretAccessKey: "fake-sk",
ObsRegion: "fake-region",
LfsBucket: "fake-bucket",
Prefix: "fake-prefix",
}
err := initObsClient(cfg)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to initialize OBS client")
}
func Test_initObsClient_success(t *testing.T) {
patchObsNew(func(ak, sk, endpoint string) (*obs.ObsClient, error) {
return &obs.ObsClient{}, nil
})
defer monkey.UnpatchAll()
cfg := &config.Config{
ObsAccessKeyId: "fake-ak",
ObsSecretAccessKey: "fake-sk",
ObsRegion: "fake-region",
LfsBucket: "test-bucket",
Prefix: "test-prefix",
}
err := initObsClient(cfg)
assert.NoError(t, err)
assert.NotNil(t, server.ObsClient)
assert.Equal(t, "test-bucket", server.Bucket)
assert.Equal(t, "test-prefix", server.Prefit)
server.ObsClient = nil
server.Bucket = ""
server.Prefit = ""
}
func TestServiceOptions_Validate_EmptyConfigFile(t *testing.T) {
o := ServiceOptions{ConfigFile: ""}
err := o.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing config-file")
}
func TestServiceOptions_Validate_ValidConfigFile(t *testing.T) {
o := ServiceOptions{ConfigFile: "/some/path.yaml"}
err := o.Validate()
assert.NoError(t, err)
}
func TestOptions_Validate_DelegatesToServiceOptions(t *testing.T) {
o := options{service: ServiceOptions{ConfigFile: ""}}
err := o.Validate()
assert.Error(t, err)
o2 := options{service: ServiceOptions{ConfigFile: "/path/to/config"}}
err2 := o2.Validate()
assert.NoError(t, err2)
}
func TestServiceOptions_AddFlags(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
var o ServiceOptions
o.AddFlags(fs)
configFile := fs.Lookup("config-file")
assert.NotNil(t, configFile, "config-file flag should be registered")
rmCfg := fs.Lookup("rm-cfg")
assert.NotNil(t, rmCfg, "rm-cfg flag should be registered")
}
func TestGatherOptions_Defaults(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
o, err := gatherOptions(fs)
assert.NoError(t, err)
assert.False(t, o.enableDebug)
assert.Equal(t, "", o.service.ConfigFile)
}
func TestGatherOptions_WithConfigFile(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
o, err := gatherOptions(fs, "--config-file", "/etc/app/config.yaml")
assert.NoError(t, err)
assert.Equal(t, "/etc/app/config.yaml", o.service.ConfigFile)
}
func TestGatherOptions_EnableDebug(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
o, err := gatherOptions(fs, "--enable_debug")
assert.NoError(t, err)
assert.True(t, o.enableDebug)
}
func TestGatherOptions_RmCfg(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
o, err := gatherOptions(fs, "--rm-cfg")
assert.NoError(t, err)
assert.True(t, o.service.RemoveCfg)
}
func TestReapZombies_ExitsOnChannelClose(t *testing.T) {
sigChld := make(chan os.Signal, 1)
done := make(chan struct{})
go func() {
reapZombies(sigChld)
close(done)
}()
close(sigChld)
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("reapZombies did not exit on channel close")
}
}
func TestReapZombies_ProcessesSignal(t *testing.T) {
sigChld := make(chan os.Signal, 1)
done := make(chan struct{})
go func() {
reapZombies(sigChld)
close(done)
}()
sigChld <- syscall.SIGCHLD
close(sigChld)
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("reapZombies did not exit")
}
}
func TestSetupGracefulShutdown(t *testing.T) {
srv := &http.Server{}
quit := setupGracefulShutdown(srv)
assert.NotNil(t, quit, "should return the quit channel")
}
func TestSetupGracefulShutdown_TriggersShutdown(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
srv := &http.Server{}
go srv.Serve(ln)
time.Sleep(50 * time.Millisecond)
setupGracefulShutdown(srv)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
time.Sleep(300 * time.Millisecond)
}
func TestMain_GatherOptionsError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
defer resetWrappers()
startSchedulerFn = func() {}
startOidCheckerFn = func() {}
monkey.Patch(gatherOptions, func(fs *flag.FlagSet, args ...string) (options, error) {
return options{}, errors.New("flag error")
})
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
func TestMain_ValidateError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
defer resetWrappers()
startSchedulerFn = func() {}
startOidCheckerFn = func() {}
monkey.Patch(gatherOptions, func(fs *flag.FlagSet, args ...string) (options, error) {
return options{}, nil
})
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
// validOptions returns options that pass Validate() and enableDebug=true
// to cover the debug-level branch in main().
func validOptions() options {
return options{
service: ServiceOptions{ConfigFile: "/fake/config.yaml"},
enableDebug: true,
}
}
// patchSuccessUpTo patches all init functions before the given checkpoint
// so that main() reaches the specified line.
type mainCheckpoint string
const (
checkpointLoadConfig mainCheckpoint = "loadConfig"
checkpointInitObs mainCheckpoint = "initObs"
checkpointInitConfig mainCheckpoint = "initConfig"
checkpointRunMigration mainCheckpoint = "runMigration"
checkpointNewServer mainCheckpoint = "newServer"
checkpointListenAndServe mainCheckpoint = "listenAndServe"
)
func resetWrappers() {
runMigrationFn = db.RunMigration
createServerFn = server.New
serveFn = func(srv *http.Server) error {
return srv.ListenAndServe()
}
}
func patchSuccessUpTo(cp mainCheckpoint) {
startSchedulerFn = func() {}
startOidCheckerFn = func() {}
monkey.Patch(gatherOptions, func(fs *flag.FlagSet, args ...string) (options, error) {
return validOptions(), nil
})
if cp == checkpointLoadConfig {
return
}
monkey.Patch(config.LoadConfig, func(path string, cfg *config.Config, remove bool) error {
return nil
})
if cp == checkpointInitObs {
return
}
monkey.Patch(initObsClient, func(cfg *config.Config) error {
return nil
})
if cp == checkpointInitConfig {
return
}
monkey.Patch(initConfig, func(cfg *config.Config) error {
return nil
})
if cp == checkpointRunMigration {
return
}
runMigrationFn = func() error { return nil }
if cp == checkpointNewServer {
return
}
createServerFn = func(o server.Options) (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), nil
}
if cp == checkpointListenAndServe {
return
}
}
func TestMain_LoadConfigError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
patchSuccessUpTo(checkpointLoadConfig)
monkey.Patch(config.LoadConfig, func(path string, cfg *config.Config, remove bool) error {
return errors.New("load config failed")
})
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
func TestMain_InitObsClientError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
patchSuccessUpTo(checkpointInitObs)
monkey.Patch(initObsClient, func(cfg *config.Config) error {
return errors.New("obs client failed")
})
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
func TestMain_InitConfigError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
patchSuccessUpTo(checkpointInitConfig)
monkey.Patch(initConfig, func(cfg *config.Config) error {
return errors.New("init config failed")
})
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
func TestMain_RunMigrationError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
defer resetWrappers()
patchSuccessUpTo(checkpointRunMigration)
runMigrationFn = func() error {
return errors.New("migration failed")
}
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
func TestMain_NewServerError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
defer resetWrappers()
patchSuccessUpTo(checkpointNewServer)
createServerFn = func(o server.Options) (http.Handler, error) {
return nil, errors.New("new server failed")
}
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}
func TestMain_ListenAndServeError(t *testing.T) {
patchFatalf()
defer monkey.UnpatchAll()
defer resetWrappers()
patchSuccessUpTo(checkpointListenAndServe)
serveFn = func(srv *http.Server) error {
return errors.New("listen and serve failed")
}
defer func() {
r := recover()
assert.NotNil(t, r, "should have panicked via logrus.Fatalf")
}()
main()
}