(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestExecPost(t *testing.T) { |
| 14 | Convey("ExecPost should work", t, func(ctx C) { |
| 15 | tmpDir, err := os.MkdirTemp("", "tunasync") |
| 16 | defer os.RemoveAll(tmpDir) |
| 17 | So(err, ShouldBeNil) |
| 18 | scriptFile := filepath.Join(tmpDir, "cmd.sh") |
| 19 | |
| 20 | c := cmdConfig{ |
| 21 | name: "tuna-exec-post", |
| 22 | upstreamURL: "http://mirrors.tuna.moe/", |
| 23 | command: scriptFile, |
| 24 | workingDir: tmpDir, |
| 25 | logDir: tmpDir, |
| 26 | logFile: filepath.Join(tmpDir, "latest.log"), |
| 27 | interval: 600 * time.Second, |
| 28 | } |
| 29 | |
| 30 | provider, err := newCmdProvider(c) |
| 31 | So(err, ShouldBeNil) |
| 32 | |
| 33 | Convey("On success", func() { |
| 34 | hook, err := newExecPostHook(provider, execOnSuccess, "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'") |
| 35 | So(err, ShouldBeNil) |
| 36 | provider.AddHook(hook) |
| 37 | managerChan := make(chan jobMessage) |
| 38 | semaphore := make(chan empty, 1) |
| 39 | job := newMirrorJob(provider) |
| 40 | |
| 41 | scriptContent := `#!/bin/bash |
| 42 | echo $TUNASYNC_WORKING_DIR |
| 43 | echo $TUNASYNC_MIRROR_NAME |
| 44 | echo $TUNASYNC_UPSTREAM_URL |
| 45 | echo $TUNASYNC_LOG_FILE |
| 46 | ` |
| 47 | |
| 48 | err = os.WriteFile(scriptFile, []byte(scriptContent), 0755) |
| 49 | So(err, ShouldBeNil) |
| 50 | |
| 51 | go job.Run(managerChan, semaphore) |
| 52 | job.ctrlChan <- jobStart |
| 53 | msg := <-managerChan |
| 54 | So(msg.status, ShouldEqual, PreSyncing) |
| 55 | msg = <-managerChan |
| 56 | So(msg.status, ShouldEqual, Syncing) |
| 57 | msg = <-managerChan |
| 58 | So(msg.status, ShouldEqual, Success) |
| 59 | |
| 60 | time.Sleep(200 * time.Millisecond) |
| 61 | job.ctrlChan <- jobDisable |
| 62 | <-job.disabled |
| 63 | |
| 64 | expectedOutput := "success\n" |
| 65 | |
| 66 | outputContent, err := os.ReadFile(filepath.Join(provider.WorkingDir(), "exit_status")) |
| 67 | So(err, ShouldBeNil) |
| 68 | So(string(outputContent), ShouldEqual, expectedOutput) |
| 69 | }) |
| 70 |
nothing calls this directly
no test coverage detected