github.com/number571/tendermint@v0.34.11-gost/cmd/tendermint/commands/root_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strconv"
     9  	"testing"
    10  
    11  	"github.com/spf13/cobra"
    12  	"github.com/spf13/viper"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	cfg "github.com/number571/tendermint/config"
    17  	"github.com/number571/tendermint/libs/cli"
    18  	tmos "github.com/number571/tendermint/libs/os"
    19  )
    20  
    21  // clearConfig clears env vars, the given root dir, and resets viper.
    22  func clearConfig(dir string) {
    23  	if err := os.Unsetenv("TMHOME"); err != nil {
    24  		panic(err)
    25  	}
    26  	if err := os.Unsetenv("TM_HOME"); err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	if err := os.RemoveAll(dir); err != nil {
    31  		panic(err)
    32  	}
    33  	viper.Reset()
    34  	config = cfg.DefaultConfig()
    35  }
    36  
    37  // prepare new rootCmd
    38  func testRootCmd() *cobra.Command {
    39  	rootCmd := &cobra.Command{
    40  		Use:               RootCmd.Use,
    41  		PersistentPreRunE: RootCmd.PersistentPreRunE,
    42  		Run:               func(cmd *cobra.Command, args []string) {},
    43  	}
    44  	registerFlagsRootCmd(rootCmd)
    45  	var l string
    46  	rootCmd.PersistentFlags().String("log", l, "Log")
    47  	return rootCmd
    48  }
    49  
    50  func testSetup(rootDir string, args []string, env map[string]string) error {
    51  	clearConfig(rootDir)
    52  
    53  	rootCmd := testRootCmd()
    54  	cmd := cli.PrepareBaseCmd(rootCmd, "TM", rootDir)
    55  
    56  	// run with the args and env
    57  	args = append([]string{rootCmd.Use}, args...)
    58  	return cli.RunWithArgs(cmd, args, env)
    59  }
    60  
    61  func TestRootHome(t *testing.T) {
    62  	defaultRoot := t.TempDir()
    63  	newRoot := filepath.Join(defaultRoot, "something-else")
    64  	cases := []struct {
    65  		args []string
    66  		env  map[string]string
    67  		root string
    68  	}{
    69  		{nil, nil, defaultRoot},
    70  		{[]string{"--home", newRoot}, nil, newRoot},
    71  		{nil, map[string]string{"TMHOME": newRoot}, newRoot},
    72  	}
    73  
    74  	for i, tc := range cases {
    75  		idxString := strconv.Itoa(i)
    76  
    77  		err := testSetup(defaultRoot, tc.args, tc.env)
    78  		require.Nil(t, err, idxString)
    79  
    80  		assert.Equal(t, tc.root, config.RootDir, idxString)
    81  		assert.Equal(t, tc.root, config.P2P.RootDir, idxString)
    82  		assert.Equal(t, tc.root, config.Consensus.RootDir, idxString)
    83  		assert.Equal(t, tc.root, config.Mempool.RootDir, idxString)
    84  	}
    85  }
    86  
    87  func TestRootFlagsEnv(t *testing.T) {
    88  
    89  	// defaults
    90  	defaults := cfg.DefaultConfig()
    91  	defaultLogLvl := defaults.LogLevel
    92  
    93  	cases := []struct {
    94  		args     []string
    95  		env      map[string]string
    96  		logLevel string
    97  	}{
    98  		{[]string{"--log", "debug"}, nil, defaultLogLvl},                 // wrong flag
    99  		{[]string{"--log-level", "debug"}, nil, "debug"},                 // right flag
   100  		{nil, map[string]string{"TM_LOW": "debug"}, defaultLogLvl},       // wrong env flag
   101  		{nil, map[string]string{"MT_LOG_LEVEL": "debug"}, defaultLogLvl}, // wrong env prefix
   102  		{nil, map[string]string{"TM_LOG_LEVEL": "debug"}, "debug"},       // right env
   103  	}
   104  
   105  	defaultRoot := t.TempDir()
   106  	for i, tc := range cases {
   107  		idxString := strconv.Itoa(i)
   108  
   109  		err := testSetup(defaultRoot, tc.args, tc.env)
   110  		require.Nil(t, err, idxString)
   111  
   112  		assert.Equal(t, tc.logLevel, config.LogLevel, idxString)
   113  	}
   114  }
   115  
   116  func TestRootConfig(t *testing.T) {
   117  
   118  	// write non-default config
   119  	nonDefaultLogLvl := "debug"
   120  	cvals := map[string]string{
   121  		"log-level": nonDefaultLogLvl,
   122  	}
   123  
   124  	cases := []struct {
   125  		args []string
   126  		env  map[string]string
   127  
   128  		logLvl string
   129  	}{
   130  		{nil, nil, nonDefaultLogLvl},                             // should load config
   131  		{[]string{"--log-level=info"}, nil, "info"},              // flag over rides
   132  		{nil, map[string]string{"TM_LOG_LEVEL": "info"}, "info"}, // env over rides
   133  	}
   134  
   135  	for i, tc := range cases {
   136  		defaultRoot := t.TempDir()
   137  		idxString := strconv.Itoa(i)
   138  		clearConfig(defaultRoot)
   139  
   140  		// XXX: path must match cfg.defaultConfigPath
   141  		configFilePath := filepath.Join(defaultRoot, "config")
   142  		err := tmos.EnsureDir(configFilePath, 0700)
   143  		require.Nil(t, err)
   144  
   145  		// write the non-defaults to a different path
   146  		// TODO: support writing sub configs so we can test that too
   147  		err = WriteConfigVals(configFilePath, cvals)
   148  		require.Nil(t, err)
   149  
   150  		rootCmd := testRootCmd()
   151  		cmd := cli.PrepareBaseCmd(rootCmd, "TM", defaultRoot)
   152  
   153  		// run with the args and env
   154  		tc.args = append([]string{rootCmd.Use}, tc.args...)
   155  		err = cli.RunWithArgs(cmd, tc.args, tc.env)
   156  		require.Nil(t, err, idxString)
   157  
   158  		assert.Equal(t, tc.logLvl, config.LogLevel, idxString)
   159  	}
   160  }
   161  
   162  // WriteConfigVals writes a toml file with the given values.
   163  // It returns an error if writing was impossible.
   164  func WriteConfigVals(dir string, vals map[string]string) error {
   165  	data := ""
   166  	for k, v := range vals {
   167  		data += fmt.Sprintf("%s = \"%s\"\n", k, v)
   168  	}
   169  	cfile := filepath.Join(dir, "config.toml")
   170  	return ioutil.WriteFile(cfile, []byte(data), 0600)
   171  }