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