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.

98 lines
1.9 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)
  24. // create root dir
  25. EnsureRoot(tmpDir)
  26. WriteConfigFile(tmpDir, DefaultConfig())
  27. // make sure config is set properly
  28. data, err := ioutil.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath))
  29. require.Nil(err)
  30. if !checkConfig(string(data)) {
  31. t.Fatalf("config file missing some information")
  32. }
  33. ensureFiles(t, tmpDir, "data")
  34. }
  35. func TestEnsureTestRoot(t *testing.T) {
  36. require := require.New(t)
  37. testName := "ensureTestRoot"
  38. // create root dir
  39. cfg := ResetTestRoot(testName)
  40. defer os.RemoveAll(cfg.RootDir)
  41. rootDir := cfg.RootDir
  42. // make sure config is set properly
  43. data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath))
  44. require.Nil(err)
  45. if !checkConfig(string(data)) {
  46. t.Fatalf("config file missing some information")
  47. }
  48. // TODO: make sure the cfg returned and testconfig are the same!
  49. baseConfig := DefaultBaseConfig()
  50. pvConfig := DefaultPrivValidatorConfig()
  51. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, pvConfig.Key, pvConfig.State)
  52. }
  53. func checkConfig(configFile string) bool {
  54. var valid bool
  55. // list of words we expect in the config
  56. var elems = []string{
  57. "moniker",
  58. "seeds",
  59. "proxy-app",
  60. "fast_sync",
  61. "create_empty_blocks",
  62. "peer",
  63. "timeout",
  64. "broadcast",
  65. "send",
  66. "addr",
  67. "wal",
  68. "propose",
  69. "max",
  70. "genesis",
  71. }
  72. for _, e := range elems {
  73. if !strings.Contains(configFile, e) {
  74. valid = false
  75. } else {
  76. valid = true
  77. }
  78. }
  79. return valid
  80. }