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.

89 lines
1.8 KiB

  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func ensureFiles(t *testing.T, rootDir string, files ...string) {
  11. for _, f := range files {
  12. p := rootify(rootDir, f)
  13. _, err := os.Stat(p)
  14. assert.Nil(t, err, p)
  15. }
  16. }
  17. func TestEnsureRoot(t *testing.T) {
  18. require := require.New(t)
  19. // setup temp dir for test
  20. tmpDir, err := os.MkdirTemp("", "config-test")
  21. require.NoError(err)
  22. defer os.RemoveAll(tmpDir)
  23. // create root dir
  24. EnsureRoot(tmpDir)
  25. require.NoError(WriteConfigFile(tmpDir, DefaultConfig()))
  26. // make sure config is set properly
  27. data, err := os.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath))
  28. require.NoError(err)
  29. checkConfig(t, string(data))
  30. ensureFiles(t, tmpDir, "data")
  31. }
  32. func TestEnsureTestRoot(t *testing.T) {
  33. require := require.New(t)
  34. testName := "ensureTestRoot"
  35. // create root dir
  36. cfg, err := ResetTestRoot(testName)
  37. require.NoError(err)
  38. defer os.RemoveAll(cfg.RootDir)
  39. rootDir := cfg.RootDir
  40. // make sure config is set properly
  41. data, err := os.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. "create-empty-blocks",
  57. "peer",
  58. "timeout",
  59. "broadcast",
  60. "send",
  61. "addr",
  62. "wal",
  63. "propose",
  64. "max",
  65. "genesis",
  66. }
  67. for _, e := range elems {
  68. if !strings.Contains(configFile, e) {
  69. t.Errorf("config file was expected to contain %s but did not", e)
  70. }
  71. }
  72. }