Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env variable to cmd flags #9040

Merged
merged 12 commits into from
Apr 13, 2021
61 changes: 61 additions & 0 deletions client/config/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package config
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/flags"
)

var ErrWrongNumberOfArgs = fmt.Errorf("wrong number of arguments")

func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) {
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
// Prepare environment
t.Parallel()
configHome, cleanup := tmpDir(t)
defer cleanup()
_ = os.RemoveAll(filepath.Join(configHome, "config"))
viper.Set(flags.FlagHome, configHome)
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

// Init command config
cmd := Cmd()
assert.NotNil(t, cmd)
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

err := cmd.RunE(cmd, []string{"node", "tcp://localhost:26657"})
assert.Nil(t, err)

err = cmd.RunE(cmd, []string{"node", "--get"})
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
assert.Nil(t, err)

err = cmd.RunE(cmd, []string{"node", "tcp://local:26657"})
assert.Nil(t, err)

err = cmd.RunE(cmd, []string{"node", "--get"})
assert.Nil(t, err)

err = cmd.RunE(cmd, nil)
assert.Nil(t, err)

//err = cmd.RunE(cmd, []string{"invalidKey", "--get"})
//require.Equal(t, err, errUnknownConfigKey("invalidKey"))

err = cmd.RunE(cmd, []string{"invalidArg1"})
require.Equal(t, err, ErrWrongNumberOfArgs)

//err = cmd.RunE(cmd, []string{"invalidKey", "invalidValue"})
//require.Equal(t, err, errUnknownConfigKey("invalidKey"))

}

func tmpDir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", t.Name()+"_")
require.NoError(t, err)
return dir, func() { _ = os.RemoveAll(dir) }
}
119 changes: 119 additions & 0 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package config_test

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
)

const (
nodeEnv = "NODE"
)

func initContext(t *testing.T, testNode string) context.Context {
home := t.TempDir()

clientCtx := client.Context{}.
WithHomeDir(home).
WithViper()

clientCtx.Viper.BindEnv(nodeEnv)
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
os.Setenv(nodeEnv, testNode)

clientCtx, err := config.ReadFromClientConfig(clientCtx)
require.NoError(t, err)

ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
return ctx
}

/*
First
env var > config
NODE=tcp://localhost:127 ./build/simd config node tcp://localhost:128
./build/simd config node //tcp://localhost:127
*/
func TestConfigCmdFirst(t *testing.T) {

const (
testNode1 = "tcp://localhost:127"
testNode2 = "tcp://localhost:128"
)

ctx := initContext(t, testNode1)

cmd := config.Cmd()

// NODE=tcp://localhost:127 ./build/simd config node tcp://localhost:128
cmd.SetArgs([]string{"node", testNode2})
require.NoError(t, cmd.ExecuteContext(ctx))
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

//./build/simd config node //tcp://localhost:127
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"node"})
cmd.Execute()
out, err := ioutil.ReadAll(b)
require.NoError(t, err)
require.Equal(t, string(out), testNode1+"\n")

}

/*
Second
env var > config WORKS
./build/simd config node // tcp://localhost:127 //done already
NODE=tcp://localhost:1 ./build/simd q staking validators
Error: post failed: Post "http://localhost:1": dial tcp 127.0.0.1:1: connect: connection refused

Third
flags > env var > config WORKS
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
./build/simd config node // tcp://localhost:127
NODE=tcp://localhost:1 ./build/simd q staking validators --node tcp://localhost:2
Error: post failed: Post "http://localhost:2": dial tcp 127.0.0.1:2: connect: connection refused
*/
func TestConfigCmdSecondThird(t *testing.T) {

const (
testNode1 = "http://localhost:1"
testNode2 = "http://localhost:2"
)

ctx := initContext(t, testNode1)

/*
"no flag" Error: post failed: Post "http://localhost:1": dial tcp 127.0.0.1:1: connect: connection refused
"flag" Error: post failed: Post "http://localhost:2": dial tcp 127.0.0.1:2: connect: connection refused
*/

tt := []struct {
name string
args []string
expNode string
}{
{"no flag", []string{"validators"}, testNode1},
{"flag", []string{"validators", fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2},
}

for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cmd := cli.GetQueryCmd()
cmd.SetArgs(tc.args)
err := cmd.ExecuteContext(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), tc.expNode)
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
})
}

}
1 change: 1 addition & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceR
// client-side config from the config file.
func (ctx Context) WithViper() Context {
v := viper.New()
v.AutomaticEnv()
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
ctx.Viper = v
return ctx
}
Expand Down