provider_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. c := rsyncConfig{
  14. name: "tuna",
  15. upstreamURL: "rsync://rsync.tuna.moe/tuna/",
  16. workingDir: "/srv/mirror/production/tuna",
  17. logDir: "/var/log/tunasync",
  18. logFile: "tuna.log",
  19. useIPv6: true,
  20. interval: 600,
  21. }
  22. provider, err := newRsyncProvider(c)
  23. So(err, ShouldBeNil)
  24. So(provider.Name(), ShouldEqual, c.name)
  25. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  26. So(provider.LogDir(), ShouldEqual, c.logDir)
  27. So(provider.LogFile(), ShouldEqual, c.logFile)
  28. So(provider.Interval(), ShouldEqual, c.interval)
  29. Convey("When entering a context (auto exit)", func() {
  30. func() {
  31. ctx := provider.EnterContext()
  32. defer provider.ExitContext()
  33. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  34. newWorkingDir := "/srv/mirror/working/tuna"
  35. ctx.Set(_WorkingDirKey, newWorkingDir)
  36. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  37. }()
  38. Convey("After context is done", func() {
  39. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  40. })
  41. })
  42. Convey("When entering a context (manually exit)", func() {
  43. ctx := provider.EnterContext()
  44. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  45. newWorkingDir := "/srv/mirror/working/tuna"
  46. ctx.Set(_WorkingDirKey, newWorkingDir)
  47. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  48. Convey("After context is done", func() {
  49. provider.ExitContext()
  50. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  51. })
  52. })
  53. })
  54. }
  55. func TestCmdProvider(t *testing.T) {
  56. Convey("Command Provider should work", t, func(ctx C) {
  57. tmpDir, err := ioutil.TempDir("", "tunasync")
  58. defer os.RemoveAll(tmpDir)
  59. So(err, ShouldBeNil)
  60. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  61. tmpFile := filepath.Join(tmpDir, "log_file")
  62. c := cmdConfig{
  63. name: "tuna-cmd",
  64. upstreamURL: "http://mirrors.tuna.moe/",
  65. command: "bash " + scriptFile,
  66. workingDir: tmpDir,
  67. logDir: tmpDir,
  68. logFile: tmpFile,
  69. interval: 600,
  70. env: map[string]string{
  71. "AOSP_REPO_BIN": "/usr/local/bin/repo",
  72. },
  73. }
  74. provider, err := newCmdProvider(c)
  75. So(err, ShouldBeNil)
  76. So(provider.Name(), ShouldEqual, c.name)
  77. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  78. So(provider.LogDir(), ShouldEqual, c.logDir)
  79. So(provider.LogFile(), ShouldEqual, c.logFile)
  80. So(provider.Interval(), ShouldEqual, c.interval)
  81. Convey("Let's try to run a simple command", func() {
  82. scriptContent := `#!/bin/bash
  83. echo $TUNASYNC_WORKING_DIR
  84. echo $TUNASYNC_MIRROR_NAME
  85. echo $TUNASYNC_UPSTREAM_URL
  86. echo $TUNASYNC_LOG_FILE
  87. echo $AOSP_REPO_BIN
  88. `
  89. exceptedOutput := fmt.Sprintf(
  90. "%s\n%s\n%s\n%s\n%s\n",
  91. provider.WorkingDir(),
  92. provider.Name(),
  93. provider.upstreamURL,
  94. provider.LogFile(),
  95. "/usr/local/bin/repo",
  96. )
  97. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  98. So(err, ShouldBeNil)
  99. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  100. So(err, ShouldBeNil)
  101. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  102. err = provider.Start()
  103. So(err, ShouldBeNil)
  104. err = provider.Wait()
  105. So(err, ShouldBeNil)
  106. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  107. So(err, ShouldBeNil)
  108. So(string(loggedContent), ShouldEqual, exceptedOutput)
  109. })
  110. Convey("If a command fails", func() {
  111. scriptContent := `exit 1`
  112. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  113. So(err, ShouldBeNil)
  114. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  115. So(err, ShouldBeNil)
  116. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  117. err = provider.Start()
  118. So(err, ShouldBeNil)
  119. err = provider.Wait()
  120. So(err, ShouldNotBeNil)
  121. })
  122. Convey("If a long job is killed", func(ctx C) {
  123. scriptContent := `#!/bin/bash
  124. sleep 5
  125. `
  126. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  127. So(err, ShouldBeNil)
  128. err = provider.Start()
  129. So(err, ShouldBeNil)
  130. go func() {
  131. err = provider.Wait()
  132. ctx.So(err, ShouldNotBeNil)
  133. }()
  134. time.Sleep(2)
  135. err = provider.Terminate()
  136. So(err, ShouldBeNil)
  137. })
  138. })
  139. }