Skip to content

Commit a5b72b8

Browse files
committed
Add tests for success_exit_codes in config and provider
Signed-off-by: Shengqi Chen <[email protected]>
1 parent 033aa60 commit a5b72b8

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

worker/config_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,60 @@ rsync_options = ["--local"]
521521
"--local", // from mirror.rsync_options
522522
})
523523
})
524+
525+
Convey("success_exit_codes should work globally and per mirror", t, func() {
526+
tmpfile, err := os.CreateTemp("", "tunasync")
527+
So(err, ShouldEqual, nil)
528+
defer os.Remove(tmpfile.Name())
529+
530+
cfgBlob1 := `
531+
[global]
532+
name = "test_worker"
533+
log_dir = "/var/log/tunasync/{{.Name}}"
534+
mirror_dir = "/data/mirrors"
535+
concurrent = 10
536+
interval = 240
537+
retry = 3
538+
timeout = 86400
539+
dangerous_global_success_exit_codes = [10, 20]
540+
541+
[manager]
542+
api_base = "https://127.0.0.1:5000"
543+
token = "some_token"
544+
545+
[server]
546+
hostname = "worker1.example.com"
547+
listen_addr = "127.0.0.1"
548+
listen_port = 6000
549+
ssl_cert = "/etc/tunasync.d/worker1.cert"
550+
ssl_key = "/etc/tunasync.d/worker1.key"
551+
552+
[[mirrors]]
553+
name = "foo"
554+
provider = "rsync"
555+
upstream = "rsync://foo.bar/"
556+
interval = 720
557+
retry = 2
558+
timeout = 3600
559+
mirror_dir = "/data/foo"
560+
success_exit_codes = [30, 40]
561+
`
562+
563+
err = os.WriteFile(tmpfile.Name(), []byte(cfgBlob1), 0644)
564+
So(err, ShouldEqual, nil)
565+
defer tmpfile.Close()
566+
567+
cfg, err := LoadConfig(tmpfile.Name())
568+
So(err, ShouldBeNil)
569+
570+
providers := map[string]mirrorProvider{}
571+
for _, m := range cfg.Mirrors {
572+
p := newMirrorProvider(m, cfg)
573+
providers[p.Name()] = p
574+
}
575+
576+
p, ok := providers["foo"].(*rsyncProvider)
577+
So(ok, ShouldBeTrue)
578+
So(p.successExitCodes, ShouldResemble, []int{10, 20, 30, 40})
579+
})
524580
}

worker/provider_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,59 @@ sleep 10
552552
So(provider.DataSize(), ShouldBeEmpty)
553553
})
554554
})
555+
Convey("Command Provider with successExitCodes should work", t, func(ctx C) {
556+
tmpDir, err := os.MkdirTemp("", "tunasync")
557+
defer os.RemoveAll(tmpDir)
558+
So(err, ShouldBeNil)
559+
scriptFile := filepath.Join(tmpDir, "cmd.sh")
560+
tmpFile := filepath.Join(tmpDir, "log_file")
561+
562+
c := cmdConfig{
563+
name: "tuna-cmd",
564+
upstreamURL: "http://mirrors.tuna.moe/",
565+
command: "bash " + scriptFile,
566+
workingDir: tmpDir,
567+
logDir: tmpDir,
568+
logFile: tmpFile,
569+
interval: 600 * time.Second,
570+
}
571+
572+
provider, err := newCmdProvider(c)
573+
provider.SetSuccessExitCodes([]int{199, 200})
574+
So(err, ShouldBeNil)
575+
576+
So(provider.Type(), ShouldEqual, provCommand)
577+
So(provider.Name(), ShouldEqual, c.name)
578+
So(provider.WorkingDir(), ShouldEqual, c.workingDir)
579+
So(provider.LogDir(), ShouldEqual, c.logDir)
580+
So(provider.LogFile(), ShouldEqual, c.logFile)
581+
So(provider.Interval(), ShouldEqual, c.interval)
582+
So(provider.GetSuccessExitCodes(), ShouldResemble, []int{199, 200})
583+
584+
Convey("Command exits with configured successExitCodes", func() {
585+
scriptContent := `exit 199`
586+
err = os.WriteFile(scriptFile, []byte(scriptContent), 0755)
587+
So(err, ShouldBeNil)
588+
readedScriptContent, err := os.ReadFile(scriptFile)
589+
So(err, ShouldBeNil)
590+
So(readedScriptContent, ShouldResemble, []byte(scriptContent))
591+
592+
err = provider.Run(make(chan empty, 1))
593+
So(err, ShouldBeNil)
594+
})
595+
596+
Convey("Command exits with unknown exit code", func() {
597+
scriptContent := `exit 201`
598+
err = os.WriteFile(scriptFile, []byte(scriptContent), 0755)
599+
So(err, ShouldBeNil)
600+
readedScriptContent, err := os.ReadFile(scriptFile)
601+
So(err, ShouldBeNil)
602+
So(readedScriptContent, ShouldResemble, []byte(scriptContent))
603+
604+
err = provider.Run(make(chan empty, 1))
605+
So(err, ShouldNotBeNil)
606+
})
607+
})
555608
}
556609

557610
func TestTwoStageRsyncProvider(t *testing.T) {

0 commit comments

Comments
 (0)