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.

91 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.NoError(err)
  23. defer os.RemoveAll(tmpDir)
  24. // create root dir
  25. EnsureRoot(tmpDir)
  26. require.NoError(WriteConfigFile(tmpDir, DefaultConfig()))
  27. // make sure config is set properly
  28. data, err := ioutil.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath))
  29. require.NoError(err)
  30. checkConfig(t, string(data))
  31. ensureFiles(t, tmpDir, "data")
  32. }
  33. func TestEnsureTestRoot(t *testing.T) {
  34. require := require.New(t)
  35. testName := "ensureTestRoot"
  36. // create root dir
  37. cfg, err := ResetTestRoot(testName)
  38. require.NoError(err)
  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. checkConfig(t, string(data))
  45. // TODO: make sure the cfg returned and testconfig are the same!
  46. baseConfig := DefaultBaseConfig()
  47. pvConfig := DefaultPrivValidatorConfig()
  48. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, pvConfig.Key, pvConfig.State)
  49. }
  50. func checkConfig(t *testing.T, configFile string) {
  51. t.Helper()
  52. // list of words we expect in the config
  53. var elems = []string{
  54. "moniker",
  55. "seeds",
  56. "proxy-app",
  57. "blocksync",
  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. t.Errorf("config file was expected to contain %s but did not", e)
  72. }
  73. }
  74. }