(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestMirrorJob(t *testing.T) { |
| 15 | |
| 16 | InitLogger(true, true, false) |
| 17 | |
| 18 | Convey("MirrorJob should work", t, func(ctx C) { |
| 19 | tmpDir, err := os.MkdirTemp("", "tunasync") |
| 20 | defer os.RemoveAll(tmpDir) |
| 21 | So(err, ShouldBeNil) |
| 22 | scriptFile := filepath.Join(tmpDir, "cmd.sh") |
| 23 | tmpFile := filepath.Join(tmpDir, "log_file") |
| 24 | |
| 25 | c := cmdConfig{ |
| 26 | name: "tuna-cmd-jobtest", |
| 27 | upstreamURL: "http://mirrors.tuna.moe/", |
| 28 | command: "bash " + scriptFile, |
| 29 | workingDir: tmpDir, |
| 30 | logDir: tmpDir, |
| 31 | logFile: tmpFile, |
| 32 | interval: 1 * time.Second, |
| 33 | timeout: 7 * time.Second, |
| 34 | } |
| 35 | |
| 36 | provider, err := newCmdProvider(c) |
| 37 | So(err, ShouldBeNil) |
| 38 | |
| 39 | So(provider.Name(), ShouldEqual, c.name) |
| 40 | So(provider.WorkingDir(), ShouldEqual, c.workingDir) |
| 41 | So(provider.LogDir(), ShouldEqual, c.logDir) |
| 42 | So(provider.LogFile(), ShouldEqual, c.logFile) |
| 43 | So(provider.Interval(), ShouldEqual, c.interval) |
| 44 | So(provider.Timeout(), ShouldEqual, c.timeout) |
| 45 | |
| 46 | Convey("For a normal mirror job", func(ctx C) { |
| 47 | scriptContent := `#!/bin/bash |
| 48 | echo $TUNASYNC_WORKING_DIR |
| 49 | echo $TUNASYNC_MIRROR_NAME |
| 50 | echo $TUNASYNC_UPSTREAM_URL |
| 51 | echo $TUNASYNC_LOG_FILE |
| 52 | ` |
| 53 | expectedOutput := fmt.Sprintf( |
| 54 | "%s\n%s\n%s\n%s\n", |
| 55 | provider.WorkingDir(), |
| 56 | provider.Name(), |
| 57 | provider.upstreamURL, |
| 58 | provider.LogFile(), |
| 59 | ) |
| 60 | err = os.WriteFile(scriptFile, []byte(scriptContent), 0755) |
| 61 | So(err, ShouldBeNil) |
| 62 | readedScriptContent, err := os.ReadFile(scriptFile) |
| 63 | So(err, ShouldBeNil) |
| 64 | So(readedScriptContent, ShouldResemble, []byte(scriptContent)) |
| 65 | |
| 66 | Convey("If we let it run several times", func(ctx C) { |
| 67 | managerChan := make(chan jobMessage, 10) |
| 68 | semaphore := make(chan empty, 1) |
| 69 | job := newMirrorJob(provider) |
| 70 | |
| 71 | go job.Run(managerChan, semaphore) |
nothing calls this directly
no test coverage detected