job_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package worker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. . "github.com/smartystreets/goconvey/convey"
  10. . "github.com/tuna/tunasync/internal"
  11. )
  12. func TestMirrorJob(t *testing.T) {
  13. InitLogger(true, true, false)
  14. Convey("MirrorJob should work", t, func(ctx C) {
  15. tmpDir, err := ioutil.TempDir("", "tunasync")
  16. defer os.RemoveAll(tmpDir)
  17. So(err, ShouldBeNil)
  18. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  19. tmpFile := filepath.Join(tmpDir, "log_file")
  20. c := cmdConfig{
  21. name: "tuna-cmd-jobtest",
  22. upstreamURL: "http://mirrors.tuna.moe/",
  23. command: "bash " + scriptFile,
  24. workingDir: tmpDir,
  25. logDir: tmpDir,
  26. logFile: tmpFile,
  27. interval: 1 * time.Second,
  28. }
  29. provider, err := newCmdProvider(c)
  30. So(err, ShouldBeNil)
  31. So(provider.Name(), ShouldEqual, c.name)
  32. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  33. So(provider.LogDir(), ShouldEqual, c.logDir)
  34. So(provider.LogFile(), ShouldEqual, c.logFile)
  35. So(provider.Interval(), ShouldEqual, c.interval)
  36. Convey("For a normal mirror job", func(ctx C) {
  37. scriptContent := `#!/bin/bash
  38. echo $TUNASYNC_WORKING_DIR
  39. echo $TUNASYNC_MIRROR_NAME
  40. echo $TUNASYNC_UPSTREAM_URL
  41. echo $TUNASYNC_LOG_FILE
  42. `
  43. exceptedOutput := fmt.Sprintf(
  44. "%s\n%s\n%s\n%s\n",
  45. provider.WorkingDir(),
  46. provider.Name(),
  47. provider.upstreamURL,
  48. provider.LogFile(),
  49. )
  50. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  51. So(err, ShouldBeNil)
  52. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  53. So(err, ShouldBeNil)
  54. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  55. Convey("If we let it run several times", func(ctx C) {
  56. ctrlChan := make(chan ctrlAction)
  57. managerChan := make(chan jobMessage, 10)
  58. semaphore := make(chan empty, 1)
  59. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  60. for i := 0; i < 2; i++ {
  61. msg := <-managerChan
  62. So(msg.status, ShouldEqual, PreSyncing)
  63. msg = <-managerChan
  64. So(msg.status, ShouldEqual, Syncing)
  65. msg = <-managerChan
  66. So(msg.status, ShouldEqual, Success)
  67. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  68. So(err, ShouldBeNil)
  69. So(string(loggedContent), ShouldEqual, exceptedOutput)
  70. ctrlChan <- jobStart
  71. }
  72. select {
  73. case msg := <-managerChan:
  74. So(msg.status, ShouldEqual, PreSyncing)
  75. msg = <-managerChan
  76. So(msg.status, ShouldEqual, Syncing)
  77. msg = <-managerChan
  78. So(msg.status, ShouldEqual, Success)
  79. case <-time.After(2 * time.Second):
  80. So(0, ShouldEqual, 1)
  81. }
  82. ctrlChan <- jobDisable
  83. select {
  84. case <-managerChan:
  85. So(0, ShouldEqual, 1) // made this fail
  86. case <-time.After(2 * time.Second):
  87. So(0, ShouldEqual, 0)
  88. }
  89. })
  90. })
  91. Convey("When running long jobs", func(ctx C) {
  92. scriptContent := `#!/bin/bash
  93. echo $TUNASYNC_WORKING_DIR
  94. sleep 5
  95. echo $TUNASYNC_WORKING_DIR
  96. `
  97. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  98. So(err, ShouldBeNil)
  99. ctrlChan := make(chan ctrlAction)
  100. managerChan := make(chan jobMessage, 10)
  101. semaphore := make(chan empty, 1)
  102. Convey("If we kill it", func(ctx C) {
  103. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  104. time.Sleep(1 * time.Second)
  105. msg := <-managerChan
  106. So(msg.status, ShouldEqual, PreSyncing)
  107. msg = <-managerChan
  108. So(msg.status, ShouldEqual, Syncing)
  109. ctrlChan <- jobStop
  110. msg = <-managerChan
  111. So(msg.status, ShouldEqual, Failed)
  112. exceptedOutput := fmt.Sprintf("%s\n", provider.WorkingDir())
  113. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  114. So(err, ShouldBeNil)
  115. So(string(loggedContent), ShouldEqual, exceptedOutput)
  116. ctrlChan <- jobDisable
  117. })
  118. Convey("If we don't kill it", func(ctx C) {
  119. go runMirrorJob(provider, ctrlChan, managerChan, semaphore)
  120. msg := <-managerChan
  121. So(msg.status, ShouldEqual, PreSyncing)
  122. msg = <-managerChan
  123. So(msg.status, ShouldEqual, Syncing)
  124. msg = <-managerChan
  125. So(msg.status, ShouldEqual, Success)
  126. exceptedOutput := fmt.Sprintf(
  127. "%s\n%s\n",
  128. provider.WorkingDir(), provider.WorkingDir(),
  129. )
  130. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  131. So(err, ShouldBeNil)
  132. So(string(loggedContent), ShouldEqual, exceptedOutput)
  133. ctrlChan <- jobDisable
  134. })
  135. })
  136. })
  137. }