provider_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package worker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestRsyncProvider(t *testing.T) {
  11. Convey("Rsync Provider should work", t, func() {
  12. c := rsyncConfig{
  13. name: "tuna",
  14. upstreamURL: "rsync://rsync.tuna.moe/tuna/",
  15. workingDir: "/srv/mirror/production/tuna",
  16. logDir: "/var/log/tunasync",
  17. logFile: "tuna.log",
  18. useIPv6: true,
  19. interval: 600,
  20. }
  21. provider, err := newRsyncProvider(c)
  22. So(err, ShouldBeNil)
  23. So(provider.Name(), ShouldEqual, c.name)
  24. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  25. So(provider.LogDir(), ShouldEqual, c.logDir)
  26. So(provider.LogFile(), ShouldEqual, c.logFile)
  27. So(provider.Interval(), ShouldEqual, c.interval)
  28. Convey("When entering a context (auto exit)", func() {
  29. func() {
  30. ctx := provider.EnterContext()
  31. defer provider.ExitContext()
  32. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  33. newWorkingDir := "/srv/mirror/working/tuna"
  34. ctx.Set(_WorkingDirKey, newWorkingDir)
  35. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  36. }()
  37. Convey("After context is done", func() {
  38. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  39. })
  40. })
  41. Convey("When entering a context (manually exit)", func() {
  42. ctx := provider.EnterContext()
  43. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  44. newWorkingDir := "/srv/mirror/working/tuna"
  45. ctx.Set(_WorkingDirKey, newWorkingDir)
  46. So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
  47. Convey("After context is done", func() {
  48. provider.ExitContext()
  49. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  50. })
  51. })
  52. })
  53. }
  54. func TestCmdProvider(t *testing.T) {
  55. Convey("Command Provider should work", t, func() {
  56. tmpDir, err := ioutil.TempDir("", "tunasync")
  57. defer os.RemoveAll(tmpDir)
  58. So(err, ShouldBeNil)
  59. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  60. tmpFile := filepath.Join(tmpDir, "log_file")
  61. c := cmdConfig{
  62. name: "tuna-cmd",
  63. upstreamURL: "http://mirrors.tuna.moe/",
  64. command: "bash " + scriptFile,
  65. workingDir: tmpDir,
  66. logDir: tmpDir,
  67. logFile: tmpFile,
  68. interval: 600,
  69. }
  70. provider, err := newCmdProvider(c)
  71. So(err, ShouldBeNil)
  72. So(provider.Name(), ShouldEqual, c.name)
  73. So(provider.WorkingDir(), ShouldEqual, c.workingDir)
  74. So(provider.LogDir(), ShouldEqual, c.logDir)
  75. So(provider.LogFile(), ShouldEqual, c.logFile)
  76. So(provider.Interval(), ShouldEqual, c.interval)
  77. Convey("Let's try to run a simple command", func() {
  78. scriptContent := `#!/bin/bash
  79. echo $TUNASYNC_WORKING_DIR
  80. echo $TUNASYNC_MIRROR_NAME
  81. echo $TUNASYNC_UPSTREAM_URL
  82. echo $TUNASYNC_LOG_FILE
  83. `
  84. exceptedOutput := fmt.Sprintf(
  85. "%s\n%s\n%s\n%s\n",
  86. provider.WorkingDir(),
  87. provider.Name(),
  88. provider.upstreamURL,
  89. provider.LogFile(),
  90. )
  91. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  92. So(err, ShouldBeNil)
  93. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  94. So(err, ShouldBeNil)
  95. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  96. err = provider.Run()
  97. So(err, ShouldBeNil)
  98. err = provider.cmd.Wait()
  99. So(err, ShouldBeNil)
  100. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  101. So(err, ShouldBeNil)
  102. So(string(loggedContent), ShouldEqual, exceptedOutput)
  103. })
  104. Convey("If a command fails", func() {
  105. scriptContent := `exit 1`
  106. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  107. So(err, ShouldBeNil)
  108. readedScriptContent, err := ioutil.ReadFile(scriptFile)
  109. So(err, ShouldBeNil)
  110. So(readedScriptContent, ShouldResemble, []byte(scriptContent))
  111. err = provider.Run()
  112. So(err, ShouldBeNil)
  113. err = provider.cmd.Wait()
  114. So(err, ShouldNotBeNil)
  115. })
  116. })
  117. }