2
0

config_test.go 5.4 KB

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