Skip to content

Commit

Permalink
Merge pull request #2458 from irisnet/wangdi/guardian-test
Browse files Browse the repository at this point in the history
Add guardian cli test
  • Loading branch information
secret2830 authored Nov 17, 2020
2 parents 97cb09e + 0c840e5 commit 6914ea1
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/golang/protobuf v1.4.2
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.15.2
github.com/irisnet/irismod v1.1.1-0.20201112071415-fbbf9a7e54d8
github.com/irisnet/irismod v1.1.1-0.20201113084019-760319959915
github.com/olebedev/config v0.0.0-20190528211619-364964f3a8e4
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/irisnet/cosmos-sdk v0.34.4-0.20201112062004-03fa27c79e55 h1:bOH+rEkhPPG++Z06BwMyVBtPZQvPOpJfcpYdlemmo80=
github.com/irisnet/cosmos-sdk v0.34.4-0.20201112062004-03fa27c79e55/go.mod h1:4wGruNUDrenXKRl/F7ujW29lTv3C+6/TDWs3QfZZN2Y=
github.com/irisnet/irismod v1.1.1-0.20201112071415-fbbf9a7e54d8 h1:ahRHlBUs75zvWk9s2HjfoL0+q9w1TAX4KdlJKZeRNf4=
github.com/irisnet/irismod v1.1.1-0.20201112071415-fbbf9a7e54d8/go.mod h1:ZmW1KdB25bMxBLRWMEHoy0AjuH7W4tkD4Lg//qPfD64=
github.com/irisnet/irismod v1.1.1-0.20201113084019-760319959915 h1:N7WeKz7kt/pzN+Zub7kHlGe7eNdjLUiwQCnksU5Q448=
github.com/irisnet/irismod v1.1.1-0.20201113084019-760319959915/go.mod h1:ZmW1KdB25bMxBLRWMEHoy0AjuH7W4tkD4Lg//qPfD64=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
144 changes: 144 additions & 0 deletions modules/guardian/client/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package cli_test

import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/client/flags"
cosmoscrypto "github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"

guardiancli "github.com/irisnet/irishub/modules/guardian/client/cli"
guardiantypes "github.com/irisnet/irishub/modules/guardian/types"

"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/testutil/network"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"

guardiantestutil "github.com/irisnet/irishub/modules/guardian/client/testutil"
"github.com/irisnet/irishub/simapp"
)

var privKey crypto.PrivKey
var pubKey crypto.PubKey
var addr sdk.AccAddress

type IntegrationTestSuite struct {
suite.Suite

cfg network.Config
network *network.Network
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := simapp.NewConfig()
cfg.NumValidators = 1

privKey, pubKey, addr = testdata.KeyTestPubAddr()
guardian := guardiantypes.NewSuper("test", guardiantypes.Genesis, addr, addr)

var guardianGenState guardiantypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[guardiantypes.ModuleName], &guardianGenState)
guardianGenState.Supers = append(guardianGenState.Supers, guardian)

cfg.GenesisState[guardiantypes.ModuleName] = cfg.Codec.MustMarshalJSON(&guardianGenState)

s.cfg = cfg
s.network = network.New(s.T(), cfg)

_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
}

func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

func (s *IntegrationTestSuite) TestGuardian() {
val := s.network.Validators[0]
from := val.Address
description := "test"
clientCtx := val.ClientCtx
privKeyStr := cosmoscrypto.EncryptArmorPrivKey(privKey, "", "")
clientCtx.Keyring.ImportPrivKey(addr.String(), privKeyStr, "")
pubKeyStr := cosmoscrypto.ArmorPubKeyBytes(pubKey.Bytes(), "")
clientCtx.Keyring.ImportPubKey(addr.String(), pubKeyStr)

amount := sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(100000000))
args := []string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}
_, err := banktestutil.MsgSendExec(clientCtx, from, addr, amount, args...)
s.Require().NoError(err)

//------test GetCmdQuerySupers()-------------
respType := proto.Message(&guardiantypes.QuerySupersResponse{})
bz, err := guardiantestutil.QuerySupersExec(clientCtx)
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType))
supersResp := respType.(*guardiantypes.QuerySupersResponse)
s.Require().Equal(1, len(supersResp.Supers))

//------test GetCmdCreateSuper()-------------
args = []string{
fmt.Sprintf("--%s=%s", guardiancli.FlagAddress, from.String()),
fmt.Sprintf("--%s=%s", guardiancli.FlagDescription, description),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}

respType = proto.Message(&sdk.TxResponse{})
expectedCode := uint32(0)

bz, err = guardiantestutil.CreateSuperExec(val.ClientCtx, addr.String(), args...)
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp := respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

respType = proto.Message(&guardiantypes.QuerySupersResponse{})
bz, err = guardiantestutil.QuerySupersExec(clientCtx)
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType))
supersResp = respType.(*guardiantypes.QuerySupersResponse)
s.Require().Equal(2, len(supersResp.Supers))

//------test GetCmdDeleteSuper()-------------
args = []string{
fmt.Sprintf("--%s=%s", guardiancli.FlagAddress, from.String()),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}

respType = proto.Message(&sdk.TxResponse{})

bz, err = guardiantestutil.DeleteSuperExec(val.ClientCtx, addr.String(), args...)
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp = respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

respType = proto.Message(&guardiantypes.QuerySupersResponse{})
bz, err = guardiantestutil.QuerySupersExec(clientCtx)
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType))
supersResp = respType.(*guardiantypes.QuerySupersResponse)
s.Require().Equal(1, len(supersResp.Supers))
}
10 changes: 4 additions & 6 deletions modules/guardian/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package cli
import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"

"github.com/irisnet/irishub/modules/guardian/types"
)
Expand Down Expand Up @@ -49,15 +47,15 @@ func GetCmdCreateSuper() *cobra.Command {

fromAddr := clientCtx.GetFromAddress()

paStr := viper.GetString(FlagAddress)
paStr, _ := cmd.Flags().GetString(FlagAddress)
if len(paStr) == 0 {
return fmt.Errorf("must use --address flag")
}
pAddr, err := sdk.AccAddressFromBech32(paStr)
if err != nil {
return err
}
description := viper.GetString(FlagDescription)
description, _ := cmd.Flags().GetString(FlagDescription)
msg := types.NewMsgAddSuper(description, pAddr, fromAddr)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down Expand Up @@ -90,7 +88,7 @@ func GetCmdDeleteSuper() *cobra.Command {
}

fromAddr := clientCtx.GetFromAddress()
paStr := viper.GetString(FlagAddress)
paStr, _ := cmd.Flags().GetString(FlagAddress)
pAddr, err := sdk.AccAddressFromBech32(paStr)
if err != nil {
return err
Expand Down
42 changes: 42 additions & 0 deletions modules/guardian/client/testutil/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package testutil

import (
"fmt"

"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"

guardiancli "github.com/irisnet/irishub/modules/guardian/client/cli"
)

// MsgRedelegateExec creates a redelegate message.
func CreateSuperExec(clientCtx client.Context, from string, extraArgs ...string) (testutil.BufferWriter, error) {
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return clitestutil.ExecTestCLICmd(clientCtx, guardiancli.GetCmdCreateSuper(), args)
}

func DeleteSuperExec(clientCtx client.Context, from string, extraArgs ...string) (testutil.BufferWriter, error) {
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return clitestutil.ExecTestCLICmd(clientCtx, guardiancli.GetCmdDeleteSuper(), args)
}

func QuerySupersExec(clientCtx client.Context, extraArgs ...string) (testutil.BufferWriter, error) {
args := []string{
fmt.Sprintf("--%s=json", cli.OutputFlag),
}
args = append(args, extraArgs...)

return clitestutil.ExecTestCLICmd(clientCtx, guardiancli.GetCmdQuerySupers(), args)
}

0 comments on commit 6914ea1

Please # to comment.