You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
1.8 KiB

  1. package config
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func ensureFiles(t *testing.T, rootDir string, files ...string) {
  12. for _, f := range files {
  13. p := rootify(rootDir, f)
  14. _, err := os.Stat(p)
  15. assert.Nil(t, err, p)
  16. }
  17. }
  18. func TestEnsureRoot(t *testing.T) {
  19. require := require.New(t)
  20. // setup temp dir for test
  21. tmpDir, err := ioutil.TempDir("", "config-test")
  22. require.Nil(err)
  23. defer os.RemoveAll(tmpDir) // nolint: errcheck
  24. // create root dir
  25. EnsureRoot(tmpDir)
  26. // make sure config is set properly
  27. data, err := ioutil.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath))
  28. require.Nil(err)
  29. if !checkConfig(string(data)) {
  30. t.Fatalf("config file missing some information")
  31. }
  32. ensureFiles(t, tmpDir, "data")
  33. }
  34. func TestEnsureTestRoot(t *testing.T) {
  35. require := require.New(t)
  36. testName := "ensureTestRoot"
  37. // create root dir
  38. cfg := ResetTestRoot(testName)
  39. rootDir := cfg.RootDir
  40. // make sure config is set properly
  41. data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath))
  42. require.Nil(err)
  43. if !checkConfig(string(data)) {
  44. t.Fatalf("config file missing some information")
  45. }
  46. // TODO: make sure the cfg returned and testconfig are the same!
  47. baseConfig := DefaultBaseConfig()
  48. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, baseConfig.PrivValidator)
  49. }
  50. func checkConfig(configFile string) bool {
  51. var valid bool
  52. // list of words we expect in the config
  53. var elems = []string{
  54. "moniker",
  55. "seeds",
  56. "proxy_app",
  57. "fast_sync",
  58. "create_empty_blocks",
  59. "peer",
  60. "timeout",
  61. "broadcast",
  62. "send",
  63. "addr",
  64. "wal",
  65. "propose",
  66. "max",
  67. "genesis",
  68. }
  69. for _, e := range elems {
  70. if !strings.Contains(configFile, e) {
  71. valid = false
  72. } else {
  73. valid = true
  74. }
  75. }
  76. return valid
  77. }