2
0

provider_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. )
  11. func TestRsyncProvider(t *testing.T) {
  12. Convey("Rsync Provider should work", t, func() {
  13. tmpDir, err := ioutil.TempDir("", "tunasync")
  14. defer os.RemoveAll(tmpDir)
  15. So(err, ShouldBeNil)
  16. scriptFile := filepath.Join(tmpDir, "myrsync")
  17. tmpFile := filepath.Join(tmpDir, "log_file")
  18. c := rsyncConfig{
  19. name: "tuna",
  20. upstreamURL: "rsync://rsync.tuna.moe/tuna/",
  21. rsyncCmd: scriptFile,
  22. workingDir: tmpDir,
  23. logDir: tmpDir,
  24. logFile: tmpFile,
  25. useIPv6: true,
  26. interval: 600 * time.Second,
  27. }
  28. provider, err := newRsyncProvider(c)
  29. So(err, ShouldBeNil)
  30. So(provider.Name(), ShouldEqual, c.name)
  31. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  32. So(provider.LogDir(), ShouldEqual, c.logDir)
  33. So(provider.LogFile(), ShouldEqual, c.logFile)
  34. So(provider.Interval(), ShouldEqual, c.interval)
  35. Convey("When entering a context (auto exit)", func() {
  36. func() {
  37. ctx := provider.EnterContext()
  38. defer provider.ExitContext()
  39. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  40. newWorkingDir := "/srv/mirror/working/tuna"
  41. ctx.Set(_WorkingDirKey, newWorkingDir)
  42. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  43. }()
  44. Convey("After context is done", func() {
  45. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  46. })
  47. })
  48. Convey("When entering a context (manually exit)", func() {
  49. ctx := provider.EnterContext()
  50. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  51. newWorkingDir := "/srv/mirror/working/tuna"
  52. ctx.Set(_WorkingDirKey, newWorkingDir)
  53. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  54. Convey("After context is done", func() {
  55. provider.ExitContext()
  56. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  57. })
  58. })
  59. Convey("Let's try a run", func() {
  60. scriptContent := `#!/bin/bash
  61. echo "syncing to $(pwd)"
  62. echo $@
  63. sleep 1
  64. echo "Done"
  65. exit 0
  66. `
  67. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  68. So(err, ShouldBeNil)
  69. expectedOutput := fmt.Sprintf(
  70. "syncing to %s\n"+
  71. "%s\n"+
  72. "Done\n",
  73. provider.WorkingDir(),
  74. fmt.Sprintf(
  75. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
  76. "--delete --delete-after --delay-updates --safe-links "+
  77. "--timeout=120 --contimeout=120 -6 %s %s",
  78. provider.upstreamURL, provider.WorkingDir(),
  79. ),
  80. )
  81. err = provider.Run()
  82. So(err, ShouldBeNil)
  83. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  84. So(err, ShouldBeNil)
  85. So(string(loggedContent), ShouldEqual, expectedOutput)
  86. // fmt.Println(string(loggedContent))
  87. })
  88. })
  89. }
  90. func TestCmdProvider(t *testing.T) {
  91. Convey("Command Provider should work", t, func(ctx C) {
  92. tmpDir, err := ioutil.TempDir("", "tunasync")
  93. defer os.RemoveAll(tmpDir)
  94. So(err, ShouldBeNil)
  95. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  96. tmpFile := filepath.Join(tmpDir, "log_file")
  97. c := cmdConfig{
  98. name: "tuna-cmd",
  99. upstreamURL: "http://mirrors.tuna.moe/",
  100. command: "bash " + scriptFile,
  101. workingDir: tmpDir,
  102. logDir: tmpDir,
  103. logFile: tmpFile,
  104. interval: 600 * time.Second,
  105. env: map[string]string{
  106. "AOSP_REPO_BIN": "/usr/local/bin/repo",
  107. },
  108. }
  109. provider, err := newCmdProvider(c)
  110. So(err, ShouldBeNil)
  111. So(provider.Name(), ShouldEqual, c.name)
  112. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  113. So(provider.LogDir(), ShouldEqual, c.logDir)
  114. So(provider.LogFile(), ShouldEqual, c.logFile)
  115. So(provider.Interval(), ShouldEqual, c.interval)
  116. Convey("Let's try to run a simple command", func() {
  117. scriptContent := `#!/bin/bash
  118. echo $TUNASYNC_WORKING_DIR
  119. echo $TUNASYNC_MIRROR_NAME
  120. echo $TUNASYNC_UPSTREAM_URL
  121. echo $TUNASYNC_LOG_FILE
  122. echo $AOSP_REPO_BIN
  123. `
  124. expectedOutput := fmt.Sprintf(
  125. "%s\n%s\n%s\n%s\n%s\n",
  126. provider.WorkingDir(),
  127. provider.Name(),
  128. provider.upstreamURL,
  129. provider.LogFile(),
  130. "/usr/local/bin/repo",
  131. )
  132. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  133. So(err, ShouldBeNil)
  134. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  135. So(err, ShouldBeNil)
  136. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  137. err = provider.Run()
  138. So(err, ShouldBeNil)
  139. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  140. So(err, ShouldBeNil)
  141. So(string(loggedContent), ShouldEqual, expectedOutput)
  142. })
  143. Convey("If a command fails", func() {
  144. scriptContent := `exit 1`
  145. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  146. So(err, ShouldBeNil)
  147. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  148. So(err, ShouldBeNil)
  149. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  150. err = provider.Run()
  151. So(err, ShouldNotBeNil)
  152. })
  153. Convey("If a long job is killed", func(ctx C) {
  154. scriptContent := `#!/bin/bash
  155. sleep 5
  156. `
  157. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  158. So(err, ShouldBeNil)
  159. go func() {
  160. err = provider.Run()
  161. ctx.So(err, ShouldNotBeNil)
  162. }()
  163. time.Sleep(1 * time.Second)
  164. err = provider.Terminate()
  165. So(err, ShouldBeNil)
  166. })
  167. })
  168. }
  169. func TestTwoStageRsyncProvider(t *testing.T) {
  170. Convey("TwoStageRsync Provider should work", t, func(ctx C) {
  171. tmpDir, err := ioutil.TempDir("", "tunasync")
  172. defer os.RemoveAll(tmpDir)
  173. So(err, ShouldBeNil)
  174. scriptFile := filepath.Join(tmpDir, "myrsync")
  175. tmpFile := filepath.Join(tmpDir, "log_file")
  176. c := twoStageRsyncConfig{
  177. name: "tuna-two-stage-rsync",
  178. upstreamURL: "rsync://mirrors.tuna.moe/",
  179. stage1Profile: "debian",
  180. rsyncCmd: scriptFile,
  181. workingDir: tmpDir,
  182. logDir: tmpDir,
  183. logFile: tmpFile,
  184. useIPv6: true,
  185. excludeFile: tmpFile,
  186. }
  187. provider, err := newTwoStageRsyncProvider(c)
  188. So(err, ShouldBeNil)
  189. So(provider.Name(), ShouldEqual, c.name)
  190. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  191. So(provider.LogDir(), ShouldEqual, c.logDir)
  192. So(provider.LogFile(), ShouldEqual, c.logFile)
  193. So(provider.Interval(), ShouldEqual, c.interval)
  194. Convey("Try a command", func(ctx C) {
  195. scriptContent := `#!/bin/bash
  196. echo "syncing to $(pwd)"
  197. echo $@
  198. sleep 1
  199. echo "Done"
  200. exit 0
  201. `
  202. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  203. So(err, ShouldBeNil)
  204. err = provider.Run()
  205. So(err, ShouldBeNil)
  206. expectedOutput := fmt.Sprintf(
  207. "syncing to %s\n"+
  208. "%s\n"+
  209. "Done\n"+
  210. "syncing to %s\n"+
  211. "%s\n"+
  212. "Done\n",
  213. provider.WorkingDir(),
  214. fmt.Sprintf(
  215. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
  216. "--timeout=120 --contimeout=120 --exclude dists/ -6 "+
  217. "--exclude-from %s %s %s",
  218. provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
  219. ),
  220. provider.WorkingDir(),
  221. fmt.Sprintf(
  222. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
  223. "--delete --delete-after --delay-updates --safe-links "+
  224. "--timeout=120 --contimeout=120 -6 --exclude-from %s %s %s",
  225. provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
  226. ),
  227. )
  228. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  229. So(err, ShouldBeNil)
  230. So(string(loggedContent), ShouldEqual, expectedOutput)
  231. // fmt.Println(string(loggedContent))
  232. })
  233. Convey("Try terminating", func(ctx C) {
  234. scriptContent := `#!/bin/bash
  235. echo $@
  236. sleep 4
  237. exit 0
  238. `
  239. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  240. So(err, ShouldBeNil)
  241. go func() {
  242. err = provider.Run()
  243. ctx.So(err, ShouldNotBeNil)
  244. }()
  245. time.Sleep(1 * time.Second)
  246. err = provider.Terminate()
  247. So(err, ShouldBeNil)
  248. expectedOutput := fmt.Sprintf(
  249. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
  250. "--timeout=120 --contimeout=120 --exclude dists/ -6 "+
  251. "--exclude-from %s %s %s\n",
  252. provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
  253. )
  254. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  255. So(err, ShouldBeNil)
  256. So(string(loggedContent), ShouldEqual, expectedOutput)
  257. // fmt.Println(string(loggedContent))
  258. })
  259. })
  260. }