config_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 TestConfig(t *testing.T) {
  11. var cfgBlob = `
  12. [global]
  13. name = "test_worker"
  14. log_dir = "/var/log/tunasync/{{.Name}}"
  15. mirror_dir = "/data/mirrors"
  16. concurrent = 10
  17. interval = 240
  18. retry = 3
  19. [manager]
  20. api_base = "https://127.0.0.1:5000"
  21. token = "some_token"
  22. [server]
  23. hostname = "worker1.example.com"
  24. listen_addr = "127.0.0.1"
  25. listen_port = 6000
  26. ssl_cert = "/etc/tunasync.d/worker1.cert"
  27. ssl_key = "/etc/tunasync.d/worker1.key"
  28. [[mirrors]]
  29. name = "AOSP"
  30. provider = "command"
  31. upstream = "https://aosp.google.com/"
  32. interval = 720
  33. retry = 2
  34. mirror_dir = "/data/git/AOSP"
  35. exec_on_success = [
  36. "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
  37. ]
  38. [mirrors.env]
  39. REPO = "/usr/local/bin/aosp-repo"
  40. [[mirrors]]
  41. name = "debian"
  42. provider = "two-stage-rsync"
  43. stage1_profile = "debian"
  44. upstream = "rsync://ftp.debian.org/debian/"
  45. use_ipv6 = true
  46. [[mirrors]]
  47. name = "fedora"
  48. provider = "rsync"
  49. upstream = "rsync://ftp.fedoraproject.org/fedora/"
  50. use_ipv6 = true
  51. exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
  52. exec_on_failure = [
  53. "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
  54. ]
  55. `
  56. Convey("When giving invalid file", t, func() {
  57. cfg, err := LoadConfig("/path/to/invalid/file")
  58. So(err, ShouldNotBeNil)
  59. So(cfg, ShouldBeNil)
  60. })
  61. Convey("Everything should work on valid config file", t, func() {
  62. tmpfile, err := ioutil.TempFile("", "tunasync")
  63. So(err, ShouldEqual, nil)
  64. defer os.Remove(tmpfile.Name())
  65. tmpDir, err := ioutil.TempDir("", "tunasync")
  66. So(err, ShouldBeNil)
  67. defer os.RemoveAll(tmpDir)
  68. incSection := fmt.Sprintf(
  69. "\n[include]\n"+
  70. "include_mirrors = \"%s/*.conf\"",
  71. tmpDir,
  72. )
  73. cfgBlob = cfgBlob + incSection
  74. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  75. So(err, ShouldEqual, nil)
  76. defer tmpfile.Close()
  77. incBlob1 := `
  78. [[mirrors]]
  79. name = "debian-cd"
  80. provider = "two-stage-rsync"
  81. stage1_profile = "debian"
  82. use_ipv6 = true
  83. [[mirrors]]
  84. name = "debian-security"
  85. provider = "two-stage-rsync"
  86. stage1_profile = "debian"
  87. use_ipv6 = true
  88. `
  89. incBlob2 := `
  90. [[mirrors]]
  91. name = "ubuntu"
  92. provider = "two-stage-rsync"
  93. stage1_profile = "debian"
  94. use_ipv6 = true
  95. `
  96. err = ioutil.WriteFile(filepath.Join(tmpDir, "debian.conf"), []byte(incBlob1), 0644)
  97. So(err, ShouldEqual, nil)
  98. err = ioutil.WriteFile(filepath.Join(tmpDir, "ubuntu.conf"), []byte(incBlob2), 0644)
  99. So(err, ShouldEqual, nil)
  100. cfg, err := LoadConfig(tmpfile.Name())
  101. So(err, ShouldBeNil)
  102. So(cfg.Global.Name, ShouldEqual, "test_worker")
  103. So(cfg.Global.Interval, ShouldEqual, 240)
  104. So(cfg.Global.Retry, ShouldEqual, 3)
  105. So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
  106. So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
  107. So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
  108. m := cfg.Mirrors[0]
  109. So(m.Name, ShouldEqual, "AOSP")
  110. So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
  111. So(m.Provider, ShouldEqual, provCommand)
  112. So(m.Interval, ShouldEqual, 720)
  113. So(m.Retry, ShouldEqual, 2)
  114. So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
  115. m = cfg.Mirrors[1]
  116. So(m.Name, ShouldEqual, "debian")
  117. So(m.MirrorDir, ShouldEqual, "")
  118. So(m.Provider, ShouldEqual, provTwoStageRsync)
  119. m = cfg.Mirrors[2]
  120. So(m.Name, ShouldEqual, "fedora")
  121. So(m.MirrorDir, ShouldEqual, "")
  122. So(m.Provider, ShouldEqual, provRsync)
  123. So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  124. m = cfg.Mirrors[3]
  125. So(m.Name, ShouldEqual, "debian-cd")
  126. So(m.MirrorDir, ShouldEqual, "")
  127. So(m.Provider, ShouldEqual, provTwoStageRsync)
  128. m = cfg.Mirrors[4]
  129. So(m.Name, ShouldEqual, "debian-security")
  130. m = cfg.Mirrors[5]
  131. So(m.Name, ShouldEqual, "ubuntu")
  132. So(len(cfg.Mirrors), ShouldEqual, 6)
  133. })
  134. Convey("Providers can be inited from a valid config file", t, func() {
  135. tmpfile, err := ioutil.TempFile("", "tunasync")
  136. So(err, ShouldEqual, nil)
  137. defer os.Remove(tmpfile.Name())
  138. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  139. So(err, ShouldEqual, nil)
  140. defer tmpfile.Close()
  141. cfg, err := LoadConfig(tmpfile.Name())
  142. So(err, ShouldBeNil)
  143. providers := map[string]mirrorProvider{}
  144. for _, m := range cfg.Mirrors {
  145. p := newMirrorProvider(m, cfg)
  146. providers[p.Name()] = p
  147. }
  148. p := providers["AOSP"]
  149. So(p.Name(), ShouldEqual, "AOSP")
  150. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/AOSP")
  151. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/AOSP/latest.log")
  152. _, ok := p.(*cmdProvider)
  153. So(ok, ShouldBeTrue)
  154. for _, hook := range p.Hooks() {
  155. switch h := hook.(type) {
  156. case *execPostHook:
  157. So(h.command, ShouldResemble, []string{"bash", "-c", `echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status`})
  158. }
  159. }
  160. p = providers["debian"]
  161. So(p.Name(), ShouldEqual, "debian")
  162. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian")
  163. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian/latest.log")
  164. r2p, ok := p.(*twoStageRsyncProvider)
  165. So(ok, ShouldBeTrue)
  166. So(r2p.stage1Profile, ShouldEqual, "debian")
  167. So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian")
  168. p = providers["fedora"]
  169. So(p.Name(), ShouldEqual, "fedora")
  170. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/fedora")
  171. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/fedora/latest.log")
  172. rp, ok := p.(*rsyncProvider)
  173. So(ok, ShouldBeTrue)
  174. So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/fedora")
  175. So(rp.excludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  176. })
  177. }