config_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }