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.

90 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)
  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. 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 := ResetTestRoot(testName)
  38. defer os.RemoveAll(cfg.RootDir)
  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. checkConfig(t, string(data))
  44. // TODO: make sure the cfg returned and testconfig are the same!
  45. baseConfig := DefaultBaseConfig()
  46. pvConfig := DefaultPrivValidatorConfig()
  47. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, pvConfig.Key, pvConfig.State)
  48. }
  49. func checkConfig(t *testing.T, configFile string) {
  50. t.Helper()
  51. // list of words we expect in the config
  52. var elems = []string{
  53. "moniker",
  54. "seeds",
  55. "proxy-app",
  56. "blocksync",
  57. "create-empty-blocks",
  58. "peer",
  59. "timeout",
  60. "broadcast",
  61. "send",
  62. "addr",
  63. "wal",
  64. "propose",
  65. "max",
  66. "genesis",
  67. }
  68. for _, e := range elems {
  69. if !strings.Contains(configFile, e) {
  70. t.Errorf("config file was expected to contain %s but did not", e)
  71. }
  72. }
  73. }