provider_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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.Type(), ShouldEqual, provRsync)
  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("When entering a context (auto exit)", func() {
  37. func() {
  38. ctx := provider.EnterContext()
  39. defer provider.ExitContext()
  40. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  41. newWorkingDir := "/srv/mirror/working/tuna"
  42. ctx.Set(_WorkingDirKey, newWorkingDir)
  43. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  44. }()
  45. Convey("After context is done", func() {
  46. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  47. })
  48. })
  49. Convey("When entering a context (manually exit)", func() {
  50. ctx := provider.EnterContext()
  51. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  52. newWorkingDir := "/srv/mirror/working/tuna"
  53. ctx.Set(_WorkingDirKey, newWorkingDir)
  54. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  55. Convey("After context is done", func() {
  56. provider.ExitContext()
  57. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  58. })
  59. })
  60. Convey("Let's try a run", func() {
  61. scriptContent := `#!/bin/bash
  62. echo "syncing to $(pwd)"
  63. echo $RSYNC_PASSWORD $@
  64. sleep 1
  65. echo "Total file size: 1.33T bytes"
  66. echo "Done"
  67. exit 0
  68. `
  69. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  70. So(err, ShouldBeNil)
  71. targetDir, _ := filepath.EvalSymlinks(provider.WorkingDir())
  72. expectedOutput := fmt.Sprintf(
  73. "syncing to %s\n"+
  74. "%s\n"+
  75. "Total file size: 1.33T bytes\n"+
  76. "Done\n",
  77. targetDir,
  78. fmt.Sprintf(
  79. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
  80. "--delete --delete-after --delay-updates --safe-links "+
  81. "--timeout=120 --contimeout=120 -6 %s %s",
  82. provider.upstreamURL, provider.WorkingDir(),
  83. ),
  84. )
  85. err = provider.Run()
  86. So(err, ShouldBeNil)
  87. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  88. So(err, ShouldBeNil)
  89. So(string(loggedContent), ShouldEqual, expectedOutput)
  90. // fmt.Println(string(loggedContent))
  91. So(provider.DataSize(), ShouldEqual, "1.33T")
  92. })
  93. })
  94. }
  95. func TestRsyncProviderWithAuthentication(t *testing.T) {
  96. Convey("Rsync Provider with password should work", t, func() {
  97. tmpDir, err := ioutil.TempDir("", "tunasync")
  98. defer os.RemoveAll(tmpDir)
  99. So(err, ShouldBeNil)
  100. scriptFile := filepath.Join(tmpDir, "myrsync")
  101. tmpFile := filepath.Join(tmpDir, "log_file")
  102. c := rsyncConfig{
  103. name: "tuna",
  104. upstreamURL: "rsync://rsync.tuna.moe/tuna/",
  105. rsyncCmd: scriptFile,
  106. username: "tunasync",
  107. password: "tunasyncpassword",
  108. workingDir: tmpDir,
  109. extraOptions: []string{"--delete-excluded"},
  110. logDir: tmpDir,
  111. logFile: tmpFile,
  112. useIPv4: true,
  113. interval: 600 * time.Second,
  114. }
  115. provider, err := newRsyncProvider(c)
  116. So(err, ShouldBeNil)
  117. So(provider.Name(), ShouldEqual, c.name)
  118. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  119. So(provider.LogDir(), ShouldEqual, c.logDir)
  120. So(provider.LogFile(), ShouldEqual, c.logFile)
  121. So(provider.Interval(), ShouldEqual, c.interval)
  122. Convey("Let's try a run", func() {
  123. scriptContent := `#!/bin/bash
  124. echo "syncing to $(pwd)"
  125. echo $USER $RSYNC_PASSWORD $@
  126. sleep 1
  127. echo "Done"
  128. exit 0
  129. `
  130. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  131. So(err, ShouldBeNil)
  132. targetDir, _ := filepath.EvalSymlinks(provider.WorkingDir())
  133. expectedOutput := fmt.Sprintf(
  134. "syncing to %s\n"+
  135. "%s\n"+
  136. "Done\n",
  137. targetDir,
  138. fmt.Sprintf(
  139. "%s %s -aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
  140. "--delete --delete-after --delay-updates --safe-links "+
  141. "--timeout=120 --contimeout=120 -4 --delete-excluded %s %s",
  142. provider.username, provider.password, provider.upstreamURL, provider.WorkingDir(),
  143. ),
  144. )
  145. err = provider.Run()
  146. So(err, ShouldBeNil)
  147. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  148. So(err, ShouldBeNil)
  149. So(string(loggedContent), ShouldEqual, expectedOutput)
  150. // fmt.Println(string(loggedContent))
  151. })
  152. })
  153. }
  154. func TestCmdProvider(t *testing.T) {
  155. Convey("Command Provider should work", t, func(ctx C) {
  156. tmpDir, err := ioutil.TempDir("", "tunasync")
  157. defer os.RemoveAll(tmpDir)
  158. So(err, ShouldBeNil)
  159. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  160. tmpFile := filepath.Join(tmpDir, "log_file")
  161. c := cmdConfig{
  162. name: "tuna-cmd",
  163. upstreamURL: "http://mirrors.tuna.moe/",
  164. command: "bash " + scriptFile,
  165. workingDir: tmpDir,
  166. logDir: tmpDir,
  167. logFile: tmpFile,
  168. interval: 600 * time.Second,
  169. env: map[string]string{
  170. "AOSP_REPO_BIN": "/usr/local/bin/repo",
  171. },
  172. }
  173. provider, err := newCmdProvider(c)
  174. So(err, ShouldBeNil)
  175. So(provider.Type(), ShouldEqual, provCommand)
  176. So(provider.Name(), ShouldEqual, c.name)
  177. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  178. So(provider.LogDir(), ShouldEqual, c.logDir)
  179. So(provider.LogFile(), ShouldEqual, c.logFile)
  180. So(provider.Interval(), ShouldEqual, c.interval)
  181. Convey("Let's try to run a simple command", func() {
  182. scriptContent := `#!/bin/bash
  183. echo $TUNASYNC_WORKING_DIR
  184. echo $TUNASYNC_MIRROR_NAME
  185. echo $TUNASYNC_UPSTREAM_URL
  186. echo $TUNASYNC_LOG_FILE
  187. echo $AOSP_REPO_BIN
  188. `
  189. expectedOutput := fmt.Sprintf(
  190. "%s\n%s\n%s\n%s\n%s\n",
  191. provider.WorkingDir(),
  192. provider.Name(),
  193. provider.upstreamURL,
  194. provider.LogFile(),
  195. "/usr/local/bin/repo",
  196. )
  197. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  198. So(err, ShouldBeNil)
  199. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  200. So(err, ShouldBeNil)
  201. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  202. err = provider.Run()
  203. So(err, ShouldBeNil)
  204. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  205. So(err, ShouldBeNil)
  206. So(string(loggedContent), ShouldEqual, expectedOutput)
  207. })
  208. Convey("If a command fails", func() {
  209. scriptContent := `exit 1`
  210. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  211. So(err, ShouldBeNil)
  212. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  213. So(err, ShouldBeNil)
  214. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  215. err = provider.Run()
  216. So(err, ShouldNotBeNil)
  217. })
  218. Convey("If a long job is killed", func(ctx C) {
  219. scriptContent := `#!/bin/bash
  220. sleep 5
  221. `
  222. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  223. So(err, ShouldBeNil)
  224. go func() {
  225. err = provider.Run()
  226. ctx.So(err, ShouldNotBeNil)
  227. }()
  228. time.Sleep(1 * time.Second)
  229. err = provider.Terminate()
  230. So(err, ShouldBeNil)
  231. })
  232. })
  233. Convey("Command Provider without log file should work", t, func(ctx C) {
  234. tmpDir, err := ioutil.TempDir("", "tunasync")
  235. defer os.RemoveAll(tmpDir)
  236. So(err, ShouldBeNil)
  237. c := cmdConfig{
  238. name: "run-ls",
  239. upstreamURL: "http://mirrors.tuna.moe/",
  240. command: "ls",
  241. workingDir: tmpDir,
  242. logDir: tmpDir,
  243. logFile: "/dev/null",
  244. interval: 600 * time.Second,
  245. }
  246. provider, err := newCmdProvider(c)
  247. So(err, ShouldBeNil)
  248. So(provider.IsMaster(), ShouldEqual, false)
  249. So(provider.ZFS(), ShouldBeNil)
  250. So(provider.Type(), ShouldEqual, provCommand)
  251. So(provider.Name(), ShouldEqual, c.name)
  252. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  253. So(provider.LogDir(), ShouldEqual, c.logDir)
  254. So(provider.LogFile(), ShouldEqual, c.logFile)
  255. So(provider.Interval(), ShouldEqual, c.interval)
  256. Convey("Run the command", func() {
  257. err = provider.Run()
  258. So(err, ShouldBeNil)
  259. })
  260. })
  261. }
  262. func TestTwoStageRsyncProvider(t *testing.T) {
  263. Convey("TwoStageRsync Provider should work", t, func(ctx C) {
  264. tmpDir, err := ioutil.TempDir("", "tunasync")
  265. defer os.RemoveAll(tmpDir)
  266. So(err, ShouldBeNil)
  267. scriptFile := filepath.Join(tmpDir, "myrsync")
  268. tmpFile := filepath.Join(tmpDir, "log_file")
  269. c := twoStageRsyncConfig{
  270. name: "tuna-two-stage-rsync",
  271. upstreamURL: "rsync://mirrors.tuna.moe/",
  272. stage1Profile: "debian",
  273. rsyncCmd: scriptFile,
  274. workingDir: tmpDir,
  275. logDir: tmpDir,
  276. logFile: tmpFile,
  277. useIPv6: true,
  278. excludeFile: tmpFile,
  279. extraOptions: []string{"--delete-excluded", "--cache"},
  280. username: "hello",
  281. password: "world",
  282. }
  283. provider, err := newTwoStageRsyncProvider(c)
  284. So(err, ShouldBeNil)
  285. So(provider.Type(), ShouldEqual, provTwoStageRsync)
  286. So(provider.Name(), ShouldEqual, c.name)
  287. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  288. So(provider.LogDir(), ShouldEqual, c.logDir)
  289. So(provider.LogFile(), ShouldEqual, c.logFile)
  290. So(provider.Interval(), ShouldEqual, c.interval)
  291. Convey("Try a command", func(ctx C) {
  292. scriptContent := `#!/bin/bash
  293. echo "syncing to $(pwd)"
  294. echo $@
  295. sleep 1
  296. echo "Done"
  297. exit 0
  298. `
  299. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  300. So(err, ShouldBeNil)
  301. err = provider.Run()
  302. So(err, ShouldBeNil)
  303. targetDir, _ := filepath.EvalSymlinks(provider.WorkingDir())
  304. expectedOutput := fmt.Sprintf(
  305. "syncing to %s\n"+
  306. "%s\n"+
  307. "Done\n"+
  308. "syncing to %s\n"+
  309. "%s\n"+
  310. "Done\n",
  311. targetDir,
  312. fmt.Sprintf(
  313. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
  314. "--timeout=120 --contimeout=120 --exclude dists/ -6 "+
  315. "--exclude-from %s --delete-excluded --cache %s %s",
  316. provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
  317. ),
  318. targetDir,
  319. fmt.Sprintf(
  320. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
  321. "--delete --delete-after --delay-updates --safe-links "+
  322. "--timeout=120 --contimeout=120 -6 --exclude-from %s --delete-excluded --cache %s %s",
  323. provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
  324. ),
  325. )
  326. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  327. So(err, ShouldBeNil)
  328. So(string(loggedContent), ShouldEqual, expectedOutput)
  329. // fmt.Println(string(loggedContent))
  330. })
  331. Convey("Try terminating", func(ctx C) {
  332. scriptContent := `#!/bin/bash
  333. echo $@
  334. sleep 4
  335. exit 0
  336. `
  337. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  338. So(err, ShouldBeNil)
  339. go func() {
  340. err = provider.Run()
  341. ctx.So(err, ShouldNotBeNil)
  342. }()
  343. time.Sleep(1 * time.Second)
  344. err = provider.Terminate()
  345. So(err, ShouldBeNil)
  346. expectedOutput := fmt.Sprintf(
  347. "-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
  348. "--timeout=120 --contimeout=120 --exclude dists/ -6 "+
  349. "--exclude-from %s --delete-excluded --cache %s %s\n",
  350. provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
  351. )
  352. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  353. So(err, ShouldBeNil)
  354. So(string(loggedContent), ShouldEqual, expectedOutput)
  355. // fmt.Println(string(loggedContent))
  356. })
  357. })
  358. }