exec_post_test.go 3.0 KB

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