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.

83 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.NoError(t, err, p)
  15. }
  16. }
  17. func TestEnsureRoot(t *testing.T) {
  18. // setup temp dir for test
  19. tmpDir := t.TempDir()
  20. // create root dir
  21. EnsureRoot(tmpDir)
  22. require.NoError(t, WriteConfigFile(tmpDir, DefaultConfig()))
  23. // make sure config is set properly
  24. data, err := os.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath))
  25. require.NoError(t, err)
  26. checkConfig(t, string(data))
  27. ensureFiles(t, tmpDir, "data")
  28. }
  29. func TestEnsureTestRoot(t *testing.T) {
  30. testName := "ensureTestRoot"
  31. // create root dir
  32. cfg, err := ResetTestRoot(t.TempDir(), testName)
  33. require.NoError(t, err)
  34. defer os.RemoveAll(cfg.RootDir)
  35. rootDir := cfg.RootDir
  36. // make sure config is set properly
  37. data, err := os.ReadFile(filepath.Join(rootDir, defaultConfigFilePath))
  38. require.NoError(t, err)
  39. checkConfig(t, string(data))
  40. // TODO: make sure the cfg returned and testconfig are the same!
  41. baseConfig := DefaultBaseConfig()
  42. pvConfig := DefaultPrivValidatorConfig()
  43. ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, pvConfig.Key, pvConfig.State)
  44. }
  45. func checkConfig(t *testing.T, configFile string) {
  46. t.Helper()
  47. // list of words we expect in the config
  48. var elems = []string{
  49. "moniker",
  50. "seeds",
  51. "proxy-app",
  52. "create-empty-blocks",
  53. "peer",
  54. "timeout",
  55. "broadcast",
  56. "send",
  57. "addr",
  58. "wal",
  59. "propose",
  60. "max",
  61. "genesis",
  62. }
  63. for _, e := range elems {
  64. if !strings.Contains(configFile, e) {
  65. t.Errorf("config file was expected to contain %s but did not", e)
  66. }
  67. }
  68. }