config_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package manager
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. "testing"
  8. "github.com/BurntSushi/toml"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "gopkg.in/urfave/cli.v1"
  11. )
  12. func TestConfig(t *testing.T) {
  13. var cfgBlob = `
  14. debug = true
  15. [server]
  16. addr = "0.0.0.0"
  17. port = 5000
  18. [files]
  19. status_file = "/tmp/tunasync.json"
  20. db_file = "/var/lib/tunasync/tunasync.db"
  21. `
  22. Convey("toml decoding should work", t, func() {
  23. var conf Config
  24. _, err := toml.Decode(cfgBlob, &conf)
  25. ShouldEqual(err, nil)
  26. ShouldEqual(conf.Server.Addr, "0.0.0.0")
  27. ShouldEqual(conf.Server.Port, 5000)
  28. ShouldEqual(conf.Files.StatusFile, "/tmp/tunasync.json")
  29. ShouldEqual(conf.Files.DBFile, "/var/lib/tunasync/tunasync.db")
  30. })
  31. Convey("load Config should work", t, func() {
  32. Convey("create config file & cli context", func() {
  33. tmpfile, err := ioutil.TempFile("", "tunasync")
  34. So(err, ShouldEqual, nil)
  35. defer os.Remove(tmpfile.Name())
  36. err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
  37. So(err, ShouldEqual, nil)
  38. defer tmpfile.Close()
  39. app := cli.NewApp()
  40. app.Flags = []cli.Flag{
  41. cli.StringFlag{
  42. Name: "config, c",
  43. },
  44. cli.StringFlag{
  45. Name: "addr",
  46. },
  47. cli.IntFlag{
  48. Name: "port",
  49. },
  50. cli.StringFlag{
  51. Name: "cert",
  52. },
  53. cli.StringFlag{
  54. Name: "key",
  55. },
  56. cli.StringFlag{
  57. Name: "status-file",
  58. },
  59. cli.StringFlag{
  60. Name: "db-file",
  61. },
  62. }
  63. Convey("when giving no config options", func() {
  64. app.Action = func(c *cli.Context) {
  65. cfgFile := c.String("config")
  66. cfg, err := LoadConfig(cfgFile, c)
  67. So(err, ShouldEqual, nil)
  68. So(cfg.Server.Addr, ShouldEqual, "127.0.0.1")
  69. }
  70. args := strings.Split("cmd", " ")
  71. app.Run(args)
  72. })
  73. Convey("when giving config options", func() {
  74. app.Action = func(c *cli.Context) {
  75. cfgFile := c.String("config")
  76. So(cfgFile, ShouldEqual, tmpfile.Name())
  77. conf, err := LoadConfig(cfgFile, c)
  78. So(err, ShouldEqual, nil)
  79. So(conf.Server.Addr, ShouldEqual, "0.0.0.0")
  80. So(conf.Server.Port, ShouldEqual, 5000)
  81. So(conf.Files.StatusFile, ShouldEqual, "/tmp/tunasync.json")
  82. So(conf.Files.DBFile, ShouldEqual, "/var/lib/tunasync/tunasync.db")
  83. }
  84. cmd := fmt.Sprintf("cmd -c %s", tmpfile.Name())
  85. args := strings.Split(cmd, " ")
  86. app.Run(args)
  87. })
  88. Convey("when giving cli options", func() {
  89. app.Action = func(c *cli.Context) {
  90. cfgFile := c.String("config")
  91. So(cfgFile, ShouldEqual, "")
  92. conf, err := LoadConfig(cfgFile, c)
  93. So(err, ShouldEqual, nil)
  94. So(conf.Server.Addr, ShouldEqual, "0.0.0.0")
  95. So(conf.Server.Port, ShouldEqual, 5001)
  96. So(conf.Server.SSLCert, ShouldEqual, "/ssl.cert")
  97. So(conf.Server.SSLKey, ShouldEqual, "/ssl.key")
  98. So(conf.Files.StatusFile, ShouldEqual, "/tunasync.json")
  99. So(conf.Files.DBFile, ShouldEqual, "/tunasync.db")
  100. }
  101. args := strings.Split(
  102. "cmd --addr=0.0.0.0 --port=5001 --cert=/ssl.cert --key /ssl.key --status-file=/tunasync.json --db-file=/tunasync.db",
  103. " ",
  104. )
  105. app.Run(args)
  106. })
  107. Convey("when giving both config and cli options", func() {
  108. app.Action = func(c *cli.Context) {
  109. cfgFile := c.String("config")
  110. So(cfgFile, ShouldEqual, tmpfile.Name())
  111. conf, err := LoadConfig(cfgFile, c)
  112. So(err, ShouldEqual, nil)
  113. So(conf.Server.Addr, ShouldEqual, "0.0.0.0")
  114. So(conf.Server.Port, ShouldEqual, 5000)
  115. So(conf.Server.SSLCert, ShouldEqual, "/ssl.cert")
  116. So(conf.Server.SSLKey, ShouldEqual, "/ssl.key")
  117. So(conf.Files.StatusFile, ShouldEqual, "/tunasync.json")
  118. So(conf.Files.DBFile, ShouldEqual, "/tunasync.db")
  119. }
  120. cmd := fmt.Sprintf(
  121. "cmd -c %s --cert=/ssl.cert --key /ssl.key --status-file=/tunasync.json --db-file=/tunasync.db",
  122. tmpfile.Name(),
  123. )
  124. args := strings.Split(cmd, " ")
  125. app.Run(args)
  126. })
  127. })
  128. })
  129. }