github.com/Finschia/finschia-sdk@v0.48.1/client/config/config_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/Finschia/finschia-sdk/client"
    13  	"github.com/Finschia/finschia-sdk/client/config"
    14  	"github.com/Finschia/finschia-sdk/client/flags"
    15  	clitestutil "github.com/Finschia/finschia-sdk/testutil/cli"
    16  	"github.com/Finschia/finschia-sdk/x/staking/client/cli"
    17  )
    18  
    19  const (
    20  	nodeEnv   = "NODE"
    21  	testNode1 = "http://localhost:1"
    22  	testNode2 = "http://localhost:2"
    23  )
    24  
    25  // initClientContext initiates client Context for tests
    26  func initClientContext(t *testing.T, envVar string) (client.Context, func()) {
    27  	home := t.TempDir()
    28  	clientCtx := client.Context{}.
    29  		WithHomeDir(home).
    30  		WithViper("")
    31  
    32  	clientCtx.Viper.BindEnv(nodeEnv)
    33  	if envVar != "" {
    34  		os.Setenv(nodeEnv, envVar)
    35  	}
    36  
    37  	clientCtx, err := config.ReadFromClientConfig(clientCtx)
    38  	require.NoError(t, err)
    39  
    40  	return clientCtx, func() { _ = os.RemoveAll(home) }
    41  }
    42  
    43  func TestConfigCmd(t *testing.T) {
    44  	clientCtx, cleanup := initClientContext(t, testNode1)
    45  	defer func() {
    46  		os.Unsetenv(nodeEnv)
    47  		cleanup()
    48  	}()
    49  
    50  	// NODE=http://localhost:1 ./build/simd config node http://localhost:2
    51  	cmd := config.Cmd()
    52  	args := []string{"node", testNode2}
    53  	_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
    54  	require.NoError(t, err)
    55  
    56  	//./build/simd config node //http://localhost:1
    57  	b := bytes.NewBufferString("")
    58  	cmd.SetOut(b)
    59  	cmd.SetArgs([]string{"node"})
    60  	cmd.Execute()
    61  	out, err := io.ReadAll(b)
    62  	require.NoError(t, err)
    63  	require.Equal(t, string(out), testNode1+"\n")
    64  }
    65  
    66  func TestConfigCmdEnvFlag(t *testing.T) {
    67  	const (
    68  		defaultNode = "http://localhost:26657"
    69  	)
    70  
    71  	tt := []struct {
    72  		name    string
    73  		envVar  string
    74  		args    []string
    75  		expNode string
    76  	}{
    77  		{"env var is set with no flag", testNode1, []string{"validators"}, testNode1},
    78  		{"env var is set with a flag", testNode1, []string{"validators", fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2},
    79  		{"env var is not set with no flag", "", []string{"validators"}, defaultNode},
    80  		{"env var is not set with a flag", "", []string{"validators", fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2},
    81  	}
    82  
    83  	for _, tc := range tt {
    84  		tc := tc
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			clientCtx, cleanup := initClientContext(t, tc.envVar)
    87  			defer func() {
    88  				if tc.envVar != "" {
    89  					os.Unsetenv(nodeEnv)
    90  				}
    91  				cleanup()
    92  			}()
    93  			/*
    94  				env var is set with a flag
    95  
    96  				NODE=http://localhost:1 ./build/simd q staking validators --node http://localhost:2
    97  				Error: post failed: Post "http://localhost:2": dial tcp 127.0.0.1:2: connect: connection refused
    98  
    99  				We dial http://localhost:2 cause a flag has the higher priority than env variable.
   100  			*/
   101  			cmd := cli.GetQueryCmd()
   102  			_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   103  			require.Error(t, err)
   104  			require.Contains(t, err.Error(), tc.expNode, "Output does not contain expected Node")
   105  		})
   106  	}
   107  }