/internal/config/config_test.go

https://github.com/zaquestion/lab · Go · 295 lines · 240 code · 48 blank · 7 comment · 34 complexity · 6e9339a4052924d46fe5950b646c317c MD5 · raw file

  1. package config
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/spf13/viper"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func initMainConfig(testconf string) {
  18. MainConfig = viper.New()
  19. MainConfig.SetConfigName("lab")
  20. MainConfig.SetConfigType("toml")
  21. MainConfig.AddConfigPath(testconf)
  22. }
  23. func resetMainConfig() {
  24. // *viper.Viper.Reset() does not exist so just set MainConfig to nil for testing
  25. MainConfig = nil
  26. }
  27. func TestNewConfig(t *testing.T) {
  28. testconf := t.TempDir()
  29. t.Run("create config", func(t *testing.T) {
  30. old := os.Stdout // keep backup of the real stdout
  31. r, w, _ := os.Pipe()
  32. os.Stdout = w
  33. var buf bytes.Buffer
  34. fmt.Fprintln(&buf, "https://gitlab.zaquestion.io")
  35. oldreadPassword := readPassword
  36. readPassword = func(bufio.Reader) (string, string, error) {
  37. return "abcde12345", "", nil
  38. }
  39. defer func() {
  40. readPassword = oldreadPassword
  41. }()
  42. initMainConfig(testconf)
  43. err := New(testconf, &buf)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. outC := make(chan string)
  48. // copy the output in a separate goroutine so printing can't block indefinitely
  49. go func() {
  50. var buf bytes.Buffer
  51. io.Copy(&buf, r)
  52. outC <- buf.String()
  53. }()
  54. // back to normal state
  55. w.Close()
  56. os.Stdout = old // restoring the real stdout
  57. out := <-outC
  58. assert.Contains(t, out, "Enter GitLab host (default: https://gitlab.com): ")
  59. cfg, err := os.Open(path.Join(testconf, "lab.toml"))
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. cfgData, err := ioutil.ReadAll(cfg)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. assert.Equal(t, `
  68. [core]
  69. host = "https://gitlab.zaquestion.io"
  70. token = "abcde12345"
  71. `, string(cfgData))
  72. })
  73. resetMainConfig()
  74. }
  75. func TestNewConfigHostOverride(t *testing.T) {
  76. testconf := t.TempDir()
  77. os.Setenv("LAB_CORE_HOST", "https://gitlab2.zaquestion.io")
  78. t.Run("create config", func(t *testing.T) {
  79. initMainConfig(testconf)
  80. MainConfig.SetEnvPrefix("LAB")
  81. MainConfig.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  82. MainConfig.AutomaticEnv()
  83. require.Equal(t, "https://gitlab2.zaquestion.io", MainConfig.GetString("core.host"))
  84. old := os.Stdout // keep backup of the real stdout
  85. r, w, _ := os.Pipe()
  86. os.Stdout = w
  87. oldreadPassword := readPassword
  88. readPassword = func(bufio.Reader) (string, string, error) {
  89. return "abcde12345", "", nil
  90. }
  91. defer func() {
  92. readPassword = oldreadPassword
  93. }()
  94. var buf bytes.Buffer
  95. err := New(testconf, &buf)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. outC := make(chan string)
  100. // copy the output in a separate goroutine so printing can't block indefinitely
  101. go func() {
  102. var buf bytes.Buffer
  103. io.Copy(&buf, r)
  104. outC <- buf.String()
  105. }()
  106. // back to normal state
  107. w.Close()
  108. os.Stdout = old // restoring the real stdout
  109. out := <-outC
  110. assert.NotContains(t, out, "Enter GitLab host")
  111. cfg, err := os.Open(path.Join(testconf, "lab.toml"))
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. cfgData, err := ioutil.ReadAll(cfg)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. assert.Equal(t, `
  120. [core]
  121. host = "https://gitlab2.zaquestion.io"
  122. token = "abcde12345"
  123. `, string(cfgData))
  124. })
  125. resetMainConfig()
  126. }
  127. func TestNewLoadTokenConfig(t *testing.T) {
  128. testconf := t.TempDir()
  129. t.Run("create load_token config", func(t *testing.T) {
  130. old := os.Stdout // keep backup of the real stdout
  131. r, w, _ := os.Pipe()
  132. os.Stdout = w
  133. var buf bytes.Buffer
  134. fmt.Fprintln(&buf, "https://gitlab.zaquestion.io")
  135. oldreadPassword := readPassword
  136. readPassword = func(bufio.Reader) (string, string, error) {
  137. return "", "bash echo abcde12345", nil
  138. }
  139. defer func() {
  140. readPassword = oldreadPassword
  141. }()
  142. initMainConfig(testconf)
  143. err := New(testconf, &buf)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. outC := make(chan string)
  148. // copy the output in a separate goroutine so printing can't block indefinitely
  149. go func() {
  150. var buf bytes.Buffer
  151. io.Copy(&buf, r)
  152. outC <- buf.String()
  153. }()
  154. // back to normal state
  155. w.Close()
  156. os.Stdout = old // restoring the real stdout
  157. out := <-outC
  158. assert.Contains(t, out, "Enter GitLab host (default: https://gitlab.com): ")
  159. cfg, err := os.Open(path.Join(testconf, "lab.toml"))
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. cfgData, err := ioutil.ReadAll(cfg)
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. assert.Equal(t, `
  168. [core]
  169. host = "https://gitlab.zaquestion.io"
  170. load_token = "bash echo abcde12345"
  171. `, string(cfgData))
  172. })
  173. resetMainConfig()
  174. }
  175. func TestConvertHCLtoTOML(t *testing.T) {
  176. tmpDir := t.TempDir()
  177. oldCnfPath := filepath.Join(tmpDir, "lab.hcl")
  178. newCnfPath := filepath.Join(tmpDir, "lab.toml")
  179. oldCnf, err := os.Create(oldCnfPath)
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. oldCnf.WriteString(`"core" = {
  184. "host" = "https://gitlab.com"
  185. "token" = "foobar"
  186. "user" = "lab-testing"
  187. }`)
  188. ConvertHCLtoTOML(tmpDir, tmpDir, "lab")
  189. _, err = os.Stat(oldCnfPath)
  190. assert.True(t, os.IsNotExist(err))
  191. _, err = os.Stat(newCnfPath)
  192. assert.NoError(t, err)
  193. newCnf, err := os.Open(newCnfPath)
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. cfgData, err := ioutil.ReadAll(newCnf)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. assert.Equal(t, `
  202. [core]
  203. host = "https://gitlab.com"
  204. token = "foobar"
  205. user = "lab-testing"
  206. `, string(cfgData))
  207. }
  208. func TestTokenTest(t *testing.T) {
  209. tmpDir := t.TempDir()
  210. configPath := filepath.Join(tmpDir, "lab.toml")
  211. config, err := os.Create(configPath)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. config.WriteString(`
  216. [core]
  217. host = "https://gitlab.com"
  218. token = "foobar"
  219. user = "lab-testing"
  220. `)
  221. initMainConfig(tmpDir)
  222. MainConfig.ReadInConfig()
  223. token := GetToken()
  224. os.Remove(configPath + "/lab.toml")
  225. resetMainConfig()
  226. assert.Equal(t, "foobar", token)
  227. }
  228. func TestLoadTokenTest(t *testing.T) {
  229. tmpDir := t.TempDir()
  230. configPath := filepath.Join(tmpDir, "lab.toml")
  231. config, err := os.Create(configPath)
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. config.WriteString(`
  236. [core]
  237. host = "https://gitlab.com"
  238. load_token = "echo foobar"
  239. user = "lab-testing"
  240. `)
  241. initMainConfig(tmpDir)
  242. MainConfig.ReadInConfig()
  243. token := GetToken()
  244. os.Remove(configPath + "/lab.toml")
  245. resetMainConfig()
  246. assert.Equal(t, "foobar", token)
  247. }