config_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. [mirrors.env]
  32. REPO = "/usr/local/bin/aosp-repo"
  33. [[mirrors]]
  34. name = "debian"
  35. provider = "two-stage-rsync"
  36. stage1_profile = "debian"
  37. upstream = "rsync://ftp.debian.org/debian/"
  38. use_ipv6 = true
  39. [[mirrors]]
  40. name = "fedora"
  41. provider = "rsync"
  42. upstream = "rsync://ftp.fedoraproject.org/fedora/"
  43. use_ipv6 = true
  44. exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
  45. `
  46. Convey("When giving invalid file", t, func() {
  47. cfg, err := loadConfig("/path/to/invalid/file")
  48. So(err, ShouldNotBeNil)
  49. So(cfg, ShouldBeNil)
  50. })
  51. Convey("Everything should work on valid config file", t, func() {
  52. tmpfile, err := ioutil.TempFile("", "tunasync")
  53. So(err, ShouldEqual, nil)
  54. defer os.Remove(tmpfile.Name())
  55. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  56. So(err, ShouldEqual, nil)
  57. defer tmpfile.Close()
  58. cfg, err := loadConfig(tmpfile.Name())
  59. So(err, ShouldBeNil)
  60. So(cfg.Global.Name, ShouldEqual, "test_worker")
  61. So(cfg.Global.Interval, ShouldEqual, 240)
  62. So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
  63. So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
  64. So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
  65. m := cfg.Mirrors[0]
  66. So(m.Name, ShouldEqual, "AOSP")
  67. So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
  68. So(m.Provider, ShouldEqual, ProvCommand)
  69. So(m.Interval, ShouldEqual, 720)
  70. So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
  71. m = cfg.Mirrors[1]
  72. So(m.Name, ShouldEqual, "debian")
  73. So(m.MirrorDir, ShouldEqual, "")
  74. So(m.Provider, ShouldEqual, ProvTwoStageRsync)
  75. m = cfg.Mirrors[2]
  76. So(m.Name, ShouldEqual, "fedora")
  77. So(m.MirrorDir, ShouldEqual, "")
  78. So(m.Provider, ShouldEqual, ProvRsync)
  79. So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  80. So(len(cfg.Mirrors), ShouldEqual, 3)
  81. })
  82. Convey("Providers can be inited from a valid config file", t, func() {
  83. tmpfile, err := ioutil.TempFile("", "tunasync")
  84. So(err, ShouldEqual, nil)
  85. defer os.Remove(tmpfile.Name())
  86. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  87. So(err, ShouldEqual, nil)
  88. defer tmpfile.Close()
  89. cfg, err := loadConfig(tmpfile.Name())
  90. So(err, ShouldBeNil)
  91. w := &Worker{
  92. cfg: cfg,
  93. providers: make(map[string]mirrorProvider),
  94. }
  95. w.initProviders()
  96. p := w.providers["AOSP"]
  97. So(p.Name(), ShouldEqual, "AOSP")
  98. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/AOSP")
  99. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/AOSP/latest.log")
  100. _, ok := p.(*cmdProvider)
  101. So(ok, ShouldBeTrue)
  102. p = w.providers["debian"]
  103. So(p.Name(), ShouldEqual, "debian")
  104. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian")
  105. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian/latest.log")
  106. r2p, ok := p.(*twoStageRsyncProvider)
  107. So(ok, ShouldBeTrue)
  108. So(r2p.stage1Profile, ShouldEqual, "debian")
  109. So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian")
  110. p = w.providers["fedora"]
  111. So(p.Name(), ShouldEqual, "fedora")
  112. So(p.LogDir(), ShouldEqual, "/var/log/tunasync/fedora")
  113. So(p.LogFile(), ShouldEqual, "/var/log/tunasync/fedora/latest.log")
  114. rp, ok := p.(*rsyncProvider)
  115. So(ok, ShouldBeTrue)
  116. So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/fedora")
  117. So(rp.excludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
  118. })
  119. }