exec_post_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package worker
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. . "github.com/smartystreets/goconvey/convey"
  9. . "github.com/tuna/tunasync/internal"
  10. )
  11. func TestExecPost(t *testing.T) {
  12. Convey("ExecPost should work", t, func(ctx C) {
  13. tmpDir, err := ioutil.TempDir("", "tunasync")
  14. defer os.RemoveAll(tmpDir)
  15. So(err, ShouldBeNil)
  16. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  17. c := cmdConfig{
  18. name: "tuna-exec-post",
  19. upstreamURL: "http://mirrors.tuna.moe/",
  20. command: scriptFile,
  21. workingDir: tmpDir,
  22. logDir: tmpDir,
  23. logFile: filepath.Join(tmpDir, "latest.log"),
  24. interval: 600 * time.Second,
  25. }
  26. provider, err := newCmdProvider(c)
  27. So(err, ShouldBeNil)
  28. Convey("On success", func() {
  29. hook, err := newExecPostHook(provider, execOnSuccess, "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'")
  30. So(err, ShouldBeNil)
  31. provider.AddHook(hook)
  32. managerChan := make(chan jobMessage)
  33. semaphore := make(chan empty, 1)
  34. job := newMirrorJob(provider)
  35. scriptContent := `#!/bin/bash
  36. echo $TUNASYNC_WORKING_DIR
  37. echo $TUNASYNC_MIRROR_NAME
  38. echo $TUNASYNC_UPSTREAM_URL
  39. echo $TUNASYNC_LOG_FILE
  40. `
  41. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  42. So(err, ShouldBeNil)
  43. go job.Run(managerChan, semaphore)
  44. job.ctrlChan <- jobStart
  45. msg := <-managerChan
  46. So(msg.status, ShouldEqual, PreSyncing)
  47. msg = <-managerChan
  48. So(msg.status, ShouldEqual, Syncing)
  49. msg = <-managerChan
  50. So(msg.status, ShouldEqual, Success)
  51. time.Sleep(200 * time.Millisecond)
  52. job.ctrlChan <- jobDisable
  53. <-job.disabled
  54. expectedOutput := "success\n"
  55. outputContent, err := ioutil.ReadFile(filepath.Join(provider.WorkingDir(), "exit_status"))
  56. So(err, ShouldBeNil)
  57. So(string(outputContent), ShouldEqual, expectedOutput)
  58. })
  59. Convey("On failure", func() {
  60. hook, err := newExecPostHook(provider, execOnFailure, "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'")
  61. So(err, ShouldBeNil)
  62. provider.AddHook(hook)
  63. managerChan := make(chan jobMessage)
  64. semaphore := make(chan empty, 1)
  65. job := newMirrorJob(provider)
  66. scriptContent := `#!/bin/bash
  67. echo $TUNASYNC_WORKING_DIR
  68. echo $TUNASYNC_MIRROR_NAME
  69. echo $TUNASYNC_UPSTREAM_URL
  70. echo $TUNASYNC_LOG_FILE
  71. exit 1
  72. `
  73. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  74. So(err, ShouldBeNil)
  75. go job.Run(managerChan, semaphore)
  76. job.ctrlChan <- jobStart
  77. msg := <-managerChan
  78. So(msg.status, ShouldEqual, PreSyncing)
  79. for i := 0; i < defaultMaxRetry; i++ {
  80. msg = <-managerChan
  81. So(msg.status, ShouldEqual, Syncing)
  82. msg = <-managerChan
  83. So(msg.status, ShouldEqual, Failed)
  84. }
  85. time.Sleep(200 * time.Millisecond)
  86. job.ctrlChan <- jobDisable
  87. <-job.disabled
  88. expectedOutput := "failure\n"
  89. outputContent, err := ioutil.ReadFile(filepath.Join(provider.WorkingDir(), "exit_status"))
  90. So(err, ShouldBeNil)
  91. So(string(outputContent), ShouldEqual, expectedOutput)
  92. })
  93. })
  94. }