2
0

config_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package worker
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestConfig(t *testing.T) {
  9. var cfgBlob = `
  10. [global]
  11. name = "test_worker"
  12. log_dir = "/var/log/tunasync/{{.Name}}"
  13. mirror_dir = "/data/mirrors"
  14. concurrent = 10
  15. interval = 240
  16. [manager]
  17. api_base = "https://127.0.0.1:5000"
  18. token = "some_token"
  19. [server]
  20. hostname = "worker1.example.com"
  21. listen_addr = "127.0.0.1"
  22. listen_port = 6000
  23. ssl_cert = "/etc/tunasync.d/worker1.cert"
  24. ssl_key = "/etc/tunasync.d/worker1.key"
  25. [[mirrors]]
  26. name = "AOSP"
  27. provider = "command"
  28. upstream = "https://aosp.google.com/"
  29. interval = 720
  30. mirror_dir = "/data/git/AOSP"
  31. exec_on_success = "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
  32. [mirrors.env]
  33. REPO = "/usr/local/bin/aosp-repo"
  34. [[mirrors]]
  35. name = "debian"
  36. provider = "two-stage-rsync"
  37. stage1_profile = "debian"
  38. upstream = "rsync://ftp.debian.org/debian/"
  39. use_ipv6 = true
  40. [[mirrors]]
  41. name = "fedora"
  42. provider = "rsync"
  43. upstream = "rsync://ftp.fedoraproject.org/fedora/"
  44. use_ipv6 = true
  45. exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
  46. exec_on_failure = "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
  47. `
  48. Convey("When giving invalid file", t, func() {
  49. cfg, err := LoadConfig("/path/to/invalid/file")
  50. So(err, ShouldNotBeNil)
  51. So(cfg, ShouldBeNil)
  52. })
  53. Convey("Everything should work on valid config file", t, func() {
  54. tmpfile, err := ioutil.TempFile("", "tunasync")
  55. So(err, ShouldEqual, nil)
  56. defer os.Remove(tmpfile.Name())
  57. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  58. So(err, ShouldEqual, nil)
  59. defer tmpfile.Close()
  60. cfg, err := LoadConfig(tmpfile.Name())
  61. So(err, ShouldBeNil)
  62. So(cfg.Global.Name, ShouldEqual, "test_worker")
  63. So(cfg.Global.Interval, ShouldEqual, 240)
  64. So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
  65. So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
  66. So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
  67. m := cfg.Mirrors[0]
  68. So(m.Name, ShouldEqual, "AOSP")
  69. So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
  70. So(m.Provider, ShouldEqual, ProvCommand)
  71. So(m.Interval, ShouldEqual, 720)
  72. So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
  73. m = cfg.Mirrors[1]
  74. So(m.Name, ShouldEqual, "debian")
  75. So(m.MirrorDir, ShouldEqual, "")
  76. So(m.Provider, ShouldEqual, ProvTwoStageRsync)
  77. m = cfg.Mirrors[2]
  78. So(m.Name, ShouldEqual, "fedora")
  79. So(m.MirrorDir, ShouldEqual, "")
  80. So(m.Provider, ShouldEqual, ProvRsync)
  81. So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  82. So(len(cfg.Mirrors), ShouldEqual, 3)
  83. })
  84. Convey("Providers can be inited from a valid config file", t, func() {
  85. tmpfile, err := ioutil.TempFile("", "tunasync")
  86. So(err, ShouldEqual, nil)
  87. defer os.Remove(tmpfile.Name())
  88. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  89. So(err, ShouldEqual, nil)
  90. defer tmpfile.Close()
  91. cfg, err := LoadConfig(tmpfile.Name())
  92. So(err, ShouldBeNil)
  93. w := &Worker{
  94. cfg: cfg,
  95. providers: make(map[string]mirrorProvider),
  96. }
  97. w.initProviders()
  98. p := w.providers["AOSP"]
  99. So(p.Name(), ShouldEqual, "AOSP")
  100. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/AOSP")
  101. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/AOSP/latest.log")
  102. _, ok := p.(*cmdProvider)
  103. So(ok, ShouldBeTrue)
  104. for _, hook := range p.Hooks() {
  105. switch h := hook.(type) {
  106. case *execPostHook:
  107. So(h.command, ShouldResemble, []string{"bash", "-c", `echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status`})
  108. }
  109. }
  110. p = w.providers["debian"]
  111. So(p.Name(), ShouldEqual, "debian")
  112. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian")
  113. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian/latest.log")
  114. r2p, ok := p.(*twoStageRsyncProvider)
  115. So(ok, ShouldBeTrue)
  116. So(r2p.stage1Profile, ShouldEqual, "debian")
  117. So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian")
  118. p = w.providers["fedora"]
  119. So(p.Name(), ShouldEqual, "fedora")
  120. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/fedora")
  121. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/fedora/latest.log")
  122. rp, ok := p.(*rsyncProvider)
  123. So(ok, ShouldBeTrue)
  124. So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/fedora")
  125. So(rp.excludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  126. })
  127. }