2
0

config_test.go 3.8 KB

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