config_test.go 5.4 KB

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