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.

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