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.

95 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. // 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. defer os.RemoveAll(cfg.RootDir)
  40. rootDir := cfg.RootDir
  41. // make sure config is set properly
  42. data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath))
  43. require.Nil(err)
  44. if !checkConfig(string(data)) {
  45. t.Fatalf("config file missing some information")
  46. }
  47. // TODO: make sure the cfg returned and testconfig are the same!
  48. baseConfig := DefaultBaseConfig()
  49. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, baseConfig.PrivValidatorKey, baseConfig.PrivValidatorState)
  50. }
  51. func checkConfig(configFile string) bool {
  52. var valid bool
  53. // list of words we expect in the config
  54. var elems = []string{
  55. "moniker",
  56. "seeds",
  57. "proxy-app",
  58. "fast_sync",
  59. "create_empty_blocks",
  60. "peer",
  61. "timeout",
  62. "broadcast",
  63. "send",
  64. "addr",
  65. "wal",
  66. "propose",
  67. "max",
  68. "genesis",
  69. }
  70. for _, e := range elems {
  71. if !strings.Contains(configFile, e) {
  72. valid = false
  73. } else {
  74. valid = true
  75. }
  76. }
  77. return valid
  78. }