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.

88 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. assert, require := assert.New(t), 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. assert.Equal([]byte(defaultConfig(defaultMoniker)), data)
  30. ensureFiles(t, tmpDir, "data")
  31. }
  32. func TestEnsureTestRoot(t *testing.T) {
  33. assert, require := assert.New(t), require.New(t)
  34. testName := "ensureTestRoot"
  35. // create root dir
  36. cfg := ResetTestRoot(testName)
  37. rootDir := cfg.RootDir
  38. // make sure config is set properly
  39. data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath))
  40. require.Nil(err)
  41. assert.Equal([]byte(testConfig(defaultMoniker)), data)
  42. // TODO: make sure the cfg returned and testconfig are the same!
  43. baseConfig := DefaultBaseConfig()
  44. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, baseConfig.PrivValidator)
  45. }
  46. func checkConfig(configFile string) bool {
  47. var valid bool
  48. // list of words we expect in the config
  49. var elems = []string{
  50. "moniker",
  51. "seeds",
  52. "proxy_app",
  53. "fast_sync",
  54. "create_empty_blocks",
  55. "peer",
  56. "timeout",
  57. "broadcast",
  58. "send",
  59. "addr",
  60. "wal",
  61. "propose",
  62. "max",
  63. "genesis",
  64. }
  65. for _, e := range elems {
  66. if !strings.Contains(configFile, e) {
  67. valid = false
  68. } else {
  69. valid = true
  70. }
  71. }
  72. return valid
  73. }