config_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. timeout = 86400
  20. [manager]
  21. api_base = "https://127.0.0.1:5000"
  22. token = "some_token"
  23. [server]
  24. hostname = "worker1.example.com"
  25. listen_addr = "127.0.0.1"
  26. listen_port = 6000
  27. ssl_cert = "/etc/tunasync.d/worker1.cert"
  28. ssl_key = "/etc/tunasync.d/worker1.key"
  29. [[mirrors]]
  30. name = "AOSP"
  31. provider = "command"
  32. upstream = "https://aosp.google.com/"
  33. interval = 720
  34. retry = 2
  35. timeout = 3600
  36. mirror_dir = "/data/git/AOSP"
  37. exec_on_success = [
  38. "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
  39. ]
  40. [mirrors.env]
  41. REPO = "/usr/local/bin/aosp-repo"
  42. [[mirrors]]
  43. name = "debian"
  44. provider = "two-stage-rsync"
  45. stage1_profile = "debian"
  46. upstream = "rsync://ftp.debian.org/debian/"
  47. use_ipv6 = true
  48. [[mirrors]]
  49. name = "fedora"
  50. provider = "rsync"
  51. upstream = "rsync://ftp.fedoraproject.org/fedora/"
  52. use_ipv6 = true
  53. exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
  54. exec_on_failure = [
  55. "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
  56. ]
  57. `
  58. Convey("When giving invalid file", t, func() {
  59. cfg, err := LoadConfig("/path/to/invalid/file")
  60. So(err, ShouldNotBeNil)
  61. So(cfg, ShouldBeNil)
  62. })
  63. Convey("Everything should work on valid config file", t, func() {
  64. tmpfile, err := ioutil.TempFile("", "tunasync")
  65. So(err, ShouldEqual, nil)
  66. defer os.Remove(tmpfile.Name())
  67. tmpDir, err := ioutil.TempDir("", "tunasync")
  68. So(err, ShouldBeNil)
  69. defer os.RemoveAll(tmpDir)
  70. incSection := fmt.Sprintf(
  71. "\n[include]\n"+
  72. "include_mirrors = \"%s/*.conf\"",
  73. tmpDir,
  74. )
  75. curCfgBlob := cfgBlob + incSection
  76. err = ioutil.WriteFile(tmpfile.Name(), []byte(curCfgBlob), 0644)
  77. So(err, ShouldEqual, nil)
  78. defer tmpfile.Close()
  79. incBlob1 := `
  80. [[mirrors]]
  81. name = "debian-cd"
  82. provider = "two-stage-rsync"
  83. stage1_profile = "debian"
  84. use_ipv6 = true
  85. [[mirrors]]
  86. name = "debian-security"
  87. provider = "two-stage-rsync"
  88. stage1_profile = "debian"
  89. use_ipv6 = true
  90. `
  91. incBlob2 := `
  92. [[mirrors]]
  93. name = "ubuntu"
  94. provider = "two-stage-rsync"
  95. stage1_profile = "debian"
  96. use_ipv6 = true
  97. `
  98. err = ioutil.WriteFile(filepath.Join(tmpDir, "debian.conf"), []byte(incBlob1), 0644)
  99. So(err, ShouldEqual, nil)
  100. err = ioutil.WriteFile(filepath.Join(tmpDir, "ubuntu.conf"), []byte(incBlob2), 0644)
  101. So(err, ShouldEqual, nil)
  102. cfg, err := LoadConfig(tmpfile.Name())
  103. So(err, ShouldBeNil)
  104. So(cfg.Global.Name, ShouldEqual, "test_worker")
  105. So(cfg.Global.Interval, ShouldEqual, 240)
  106. So(cfg.Global.Retry, ShouldEqual, 3)
  107. So(cfg.Global.Timeout, ShouldEqual, 86400)
  108. So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
  109. So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
  110. So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
  111. m := cfg.Mirrors[0]
  112. So(m.Name, ShouldEqual, "AOSP")
  113. So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
  114. So(m.Provider, ShouldEqual, provCommand)
  115. So(m.Interval, ShouldEqual, 720)
  116. So(m.Retry, ShouldEqual, 2)
  117. So(m.Timeout, ShouldEqual, 3600)
  118. So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
  119. m = cfg.Mirrors[1]
  120. So(m.Name, ShouldEqual, "debian")
  121. So(m.MirrorDir, ShouldEqual, "")
  122. So(m.Provider, ShouldEqual, provTwoStageRsync)
  123. So(m.Timeout, ShouldEqual, 86400)
  124. m = cfg.Mirrors[2]
  125. So(m.Name, ShouldEqual, "fedora")
  126. So(m.MirrorDir, ShouldEqual, "")
  127. So(m.Provider, ShouldEqual, provRsync)
  128. So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  129. m = cfg.Mirrors[3]
  130. So(m.Name, ShouldEqual, "debian-cd")
  131. So(m.MirrorDir, ShouldEqual, "")
  132. So(m.Provider, ShouldEqual, provTwoStageRsync)
  133. m = cfg.Mirrors[4]
  134. So(m.Name, ShouldEqual, "debian-security")
  135. m = cfg.Mirrors[5]
  136. So(m.Name, ShouldEqual, "ubuntu")
  137. So(len(cfg.Mirrors), ShouldEqual, 6)
  138. })
  139. Convey("Everything should work on nested config file", t, func() {
  140. tmpfile, err := ioutil.TempFile("", "tunasync")
  141. So(err, ShouldEqual, nil)
  142. defer os.Remove(tmpfile.Name())
  143. tmpDir, err := ioutil.TempDir("", "tunasync")
  144. So(err, ShouldBeNil)
  145. defer os.RemoveAll(tmpDir)
  146. incSection := fmt.Sprintf(
  147. "\n[include]\n"+
  148. "include_mirrors = \"%s/*.conf\"",
  149. tmpDir,
  150. )
  151. curCfgBlob := cfgBlob + incSection
  152. err = ioutil.WriteFile(tmpfile.Name(), []byte(curCfgBlob), 0644)
  153. So(err, ShouldEqual, nil)
  154. defer tmpfile.Close()
  155. incBlob1 := `
  156. [[mirrors]]
  157. name = "ipv6s"
  158. use_ipv6 = true
  159. [[mirrors.mirrors]]
  160. name = "debians"
  161. mirror_subdir = "debian"
  162. provider = "two-stage-rsync"
  163. stage1_profile = "debian"
  164. [[mirrors.mirrors.mirrors]]
  165. name = "debian-security"
  166. upstream = "rsync://test.host/debian-security/"
  167. [[mirrors.mirrors.mirrors]]
  168. name = "ubuntu"
  169. stage1_profile = "ubuntu"
  170. upstream = "rsync://test.host2/ubuntu/"
  171. [[mirrors.mirrors]]
  172. name = "debian-cd"
  173. provider = "rsync"
  174. upstream = "rsync://test.host3/debian-cd/"
  175. `
  176. err = ioutil.WriteFile(filepath.Join(tmpDir, "nest.conf"), []byte(incBlob1), 0644)
  177. So(err, ShouldEqual, nil)
  178. cfg, err := LoadConfig(tmpfile.Name())
  179. So(err, ShouldBeNil)
  180. So(cfg.Global.Name, ShouldEqual, "test_worker")
  181. So(cfg.Global.Interval, ShouldEqual, 240)
  182. So(cfg.Global.Retry, ShouldEqual, 3)
  183. So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
  184. So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
  185. So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
  186. m := cfg.Mirrors[0]
  187. So(m.Name, ShouldEqual, "AOSP")
  188. So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
  189. So(m.Provider, ShouldEqual, provCommand)
  190. So(m.Interval, ShouldEqual, 720)
  191. So(m.Retry, ShouldEqual, 2)
  192. So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
  193. m = cfg.Mirrors[1]
  194. So(m.Name, ShouldEqual, "debian")
  195. So(m.MirrorDir, ShouldEqual, "")
  196. So(m.Provider, ShouldEqual, provTwoStageRsync)
  197. m = cfg.Mirrors[2]
  198. So(m.Name, ShouldEqual, "fedora")
  199. So(m.MirrorDir, ShouldEqual, "")
  200. So(m.Provider, ShouldEqual, provRsync)
  201. So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  202. m = cfg.Mirrors[3]
  203. So(m.Name, ShouldEqual, "debian-security")
  204. So(m.MirrorDir, ShouldEqual, "")
  205. So(m.Provider, ShouldEqual, provTwoStageRsync)
  206. So(m.UseIPv6, ShouldEqual, true)
  207. So(m.Stage1Profile, ShouldEqual, "debian")
  208. m = cfg.Mirrors[4]
  209. So(m.Name, ShouldEqual, "ubuntu")
  210. So(m.MirrorDir, ShouldEqual, "")
  211. So(m.Provider, ShouldEqual, provTwoStageRsync)
  212. So(m.UseIPv6, ShouldEqual, true)
  213. So(m.Stage1Profile, ShouldEqual, "ubuntu")
  214. m = cfg.Mirrors[5]
  215. So(m.Name, ShouldEqual, "debian-cd")
  216. So(m.UseIPv6, ShouldEqual, true)
  217. So(m.Provider, ShouldEqual, provRsync)
  218. So(len(cfg.Mirrors), ShouldEqual, 6)
  219. })
  220. Convey("Providers can be inited from a valid config file", t, func() {
  221. tmpfile, err := ioutil.TempFile("", "tunasync")
  222. So(err, ShouldEqual, nil)
  223. defer os.Remove(tmpfile.Name())
  224. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  225. So(err, ShouldEqual, nil)
  226. defer tmpfile.Close()
  227. cfg, err := LoadConfig(tmpfile.Name())
  228. So(err, ShouldBeNil)
  229. providers := map[string]mirrorProvider{}
  230. for _, m := range cfg.Mirrors {
  231. p := newMirrorProvider(m, cfg)
  232. providers[p.Name()] = p
  233. }
  234. p := providers["AOSP"]
  235. So(p.Name(), ShouldEqual, "AOSP")
  236. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/AOSP")
  237. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/AOSP/latest.log")
  238. _, ok := p.(*cmdProvider)
  239. So(ok, ShouldBeTrue)
  240. for _, hook := range p.Hooks() {
  241. switch h := hook.(type) {
  242. case *execPostHook:
  243. So(h.command, ShouldResemble, []string{"bash", "-c", `echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status`})
  244. }
  245. }
  246. p = providers["debian"]
  247. So(p.Name(), ShouldEqual, "debian")
  248. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian")
  249. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian/latest.log")
  250. r2p, ok := p.(*twoStageRsyncProvider)
  251. So(ok, ShouldBeTrue)
  252. So(r2p.stage1Profile, ShouldEqual, "debian")
  253. So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian")
  254. p = providers["fedora"]
  255. So(p.Name(), ShouldEqual, "fedora")
  256. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/fedora")
  257. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/fedora/latest.log")
  258. rp, ok := p.(*rsyncProvider)
  259. So(ok, ShouldBeTrue)
  260. So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/fedora")
  261. So(rp.excludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  262. })
  263. Convey("MirrorSubdir should work", t, func() {
  264. tmpfile, err := ioutil.TempFile("", "tunasync")
  265. So(err, ShouldEqual, nil)
  266. defer os.Remove(tmpfile.Name())
  267. cfgBlob1 := `
  268. [global]
  269. name = "test_worker"
  270. log_dir = "/var/log/tunasync/{{.Name}}"
  271. mirror_dir = "/data/mirrors"
  272. concurrent = 10
  273. interval = 240
  274. retry = 3
  275. [manager]
  276. api_base = "https://127.0.0.1:5000"
  277. token = "some_token"
  278. [server]
  279. hostname = "worker1.example.com"
  280. listen_addr = "127.0.0.1"
  281. listen_port = 6000
  282. ssl_cert = "/etc/tunasync.d/worker1.cert"
  283. ssl_key = "/etc/tunasync.d/worker1.key"
  284. [[mirrors]]
  285. name = "ipv6s"
  286. use_ipv6 = true
  287. [[mirrors.mirrors]]
  288. name = "debians"
  289. mirror_subdir = "debian"
  290. provider = "two-stage-rsync"
  291. stage1_profile = "debian"
  292. [[mirrors.mirrors.mirrors]]
  293. name = "debian-security"
  294. upstream = "rsync://test.host/debian-security/"
  295. [[mirrors.mirrors.mirrors]]
  296. name = "ubuntu"
  297. stage1_profile = "ubuntu"
  298. upstream = "rsync://test.host2/ubuntu/"
  299. [[mirrors.mirrors]]
  300. name = "debian-cd"
  301. provider = "rsync"
  302. upstream = "rsync://test.host3/debian-cd/"
  303. `
  304. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob1), 0644)
  305. So(err, ShouldEqual, nil)
  306. defer tmpfile.Close()
  307. cfg, err := LoadConfig(tmpfile.Name())
  308. So(err, ShouldBeNil)
  309. providers := map[string]mirrorProvider{}
  310. for _, m := range cfg.Mirrors {
  311. p := newMirrorProvider(m, cfg)
  312. providers[p.Name()] = p
  313. }
  314. p := providers["debian-security"]
  315. So(p.Name(), ShouldEqual, "debian-security")
  316. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian-security")
  317. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian-security/latest.log")
  318. r2p, ok := p.(*twoStageRsyncProvider)
  319. So(ok, ShouldBeTrue)
  320. So(r2p.stage1Profile, ShouldEqual, "debian")
  321. So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian/debian-security")
  322. p = providers["ubuntu"]
  323. So(p.Name(), ShouldEqual, "ubuntu")
  324. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/ubuntu")
  325. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/ubuntu/latest.log")
  326. r2p, ok = p.(*twoStageRsyncProvider)
  327. So(ok, ShouldBeTrue)
  328. So(r2p.stage1Profile, ShouldEqual, "ubuntu")
  329. So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian/ubuntu")
  330. p = providers["debian-cd"]
  331. So(p.Name(), ShouldEqual, "debian-cd")
  332. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian-cd")
  333. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian-cd/latest.log")
  334. rp, ok := p.(*rsyncProvider)
  335. So(ok, ShouldBeTrue)
  336. So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/debian-cd")
  337. })
  338. }