github.com/number571/tendermint@v0.34.11-gost/config/toml_test.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func ensureFiles(t *testing.T, rootDir string, files ...string) {
    15  	for _, f := range files {
    16  		p := rootify(rootDir, f)
    17  		_, err := os.Stat(p)
    18  		assert.Nil(t, err, p)
    19  	}
    20  }
    21  
    22  func TestEnsureRoot(t *testing.T) {
    23  	require := require.New(t)
    24  
    25  	// setup temp dir for test
    26  	tmpDir, err := ioutil.TempDir("", "config-test")
    27  	require.Nil(err)
    28  	defer os.RemoveAll(tmpDir)
    29  
    30  	// create root dir
    31  	EnsureRoot(tmpDir)
    32  
    33  	WriteConfigFile(tmpDir, DefaultConfig())
    34  
    35  	// make sure config is set properly
    36  	data, err := ioutil.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath))
    37  	require.Nil(err)
    38  
    39  	if !checkConfig(string(data)) {
    40  		t.Fatalf("config file missing some information")
    41  	}
    42  
    43  	ensureFiles(t, tmpDir, "data")
    44  }
    45  
    46  func TestEnsureTestRoot(t *testing.T) {
    47  	require := require.New(t)
    48  
    49  	testName := "ensureTestRoot"
    50  
    51  	// create root dir
    52  	cfg := ResetTestRoot(testName)
    53  	defer os.RemoveAll(cfg.RootDir)
    54  	rootDir := cfg.RootDir
    55  
    56  	// make sure config is set properly
    57  	data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath))
    58  	require.Nil(err)
    59  
    60  	if !checkConfig(string(data)) {
    61  		t.Fatalf("config file missing some information")
    62  	}
    63  
    64  	// TODO: make sure the cfg returned and testconfig are the same!
    65  	baseConfig := DefaultBaseConfig()
    66  	pvConfig := DefaultPrivValidatorConfig()
    67  	ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, pvConfig.Key, pvConfig.State)
    68  }
    69  
    70  func checkConfig(configFile string) bool {
    71  	var valid bool
    72  
    73  	// list of words we expect in the config
    74  	var elems = []string{
    75  		"moniker",
    76  		"seeds",
    77  		"proxy-app",
    78  		"fast_sync",
    79  		"create_empty_blocks",
    80  		"peer",
    81  		"timeout",
    82  		"broadcast",
    83  		"send",
    84  		"addr",
    85  		"wal",
    86  		"propose",
    87  		"max",
    88  		"genesis",
    89  	}
    90  	for _, e := range elems {
    91  		if !strings.Contains(configFile, e) {
    92  			valid = false
    93  		} else {
    94  			valid = true
    95  		}
    96  	}
    97  	return valid
    98  }