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