From 582359d1c15b30ee9815d2ac7ae0d029ed66ce0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=AA?= Date: Tue, 17 Nov 2020 10:38:12 +0800 Subject: [PATCH 1/3] add guardian cli test and fix a bug --- modules/guardian/client/cli/cli_test.go | 110 ++++++++++++++++++ modules/guardian/client/cli/tx.go | 4 +- .../guardian/client/testutil/test_helpers.go | 42 +++++++ 3 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 modules/guardian/client/cli/cli_test.go create mode 100644 modules/guardian/client/testutil/test_helpers.go diff --git a/modules/guardian/client/cli/cli_test.go b/modules/guardian/client/cli/cli_test.go new file mode 100644 index 000000000..4c8f47807 --- /dev/null +++ b/modules/guardian/client/cli/cli_test.go @@ -0,0 +1,110 @@ +package cli_test + +import ( + "fmt" + "testing" + + "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/suite" + + "github.com/tendermint/tendermint/crypto" + + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/testutil/network" + + sdk "github.com/cosmos/cosmos-sdk/types" + + + guardiancli "github.com/irisnet/irishub/modules/guardian/client/cli" + guardiantestutil "github.com/irisnet/irishub/modules/guardian/client/testutil" + guardiantypes "github.com/irisnet/irishub/modules/guardian/types" + "github.com/irisnet/irismod/simapp" +) + +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 = 2 + + 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 + address := sdk.AccAddress(crypto.AddressHash([]byte("dgsbl"))) + description := "test" + clientCtx := val.ClientCtx + + //------test GetCmdCreateSuper()------------- + args := []string{ + fmt.Sprintf("--%s=%s", guardiancli.FlagAddress, address.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, from.String(), args...) + s.Require().NoError(err) + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType), bz.String()) + txResp := respType.(*sdk.TxResponse) + println(bz.String()) + s.Require().Equal(expectedCode, txResp.Code) + + + //------test GetCmdDeleteSuper()------------- + args = []string{ + fmt.Sprintf("--%s=%s", guardiancli.FlagAddress, address.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, from.String(), args...) + s.Require().NoError(err) + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType), bz.String()) + txResp = respType.(*sdk.TxResponse) + println(bz.String()) + s.Require().Equal(expectedCode, txResp.Code) + + + + //------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) + println(supersResp.String()) + s.Require().NoError(err) +} \ No newline at end of file diff --git a/modules/guardian/client/cli/tx.go b/modules/guardian/client/cli/tx.go index c8985114f..7cb29ceb5 100644 --- a/modules/guardian/client/cli/tx.go +++ b/modules/guardian/client/cli/tx.go @@ -49,7 +49,7 @@ 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") } @@ -57,7 +57,7 @@ func GetCmdCreateSuper() *cobra.Command { 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 diff --git a/modules/guardian/client/testutil/test_helpers.go b/modules/guardian/client/testutil/test_helpers.go new file mode 100644 index 000000000..54b91a6ee --- /dev/null +++ b/modules/guardian/client/testutil/test_helpers.go @@ -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) +} \ No newline at end of file From d8dfb14664c7aa51fd7a3c8d83835a9a2a95d394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=AA?= Date: Tue, 17 Nov 2020 16:07:46 +0800 Subject: [PATCH 2/3] update guardian cli test --- go.mod | 2 +- go.sum | 4 +- modules/guardian/client/cli/cli_test.go | 91 +++++++++++++------ modules/guardian/client/cli/tx.go | 6 +- .../guardian/client/testutil/test_helpers.go | 2 +- 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index f0b90e4eb..457328298 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 594ec7dda..9f13178e6 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/modules/guardian/client/cli/cli_test.go b/modules/guardian/client/cli/cli_test.go index 4c8f47807..b485138ea 100644 --- a/modules/guardian/client/cli/cli_test.go +++ b/modules/guardian/client/cli/cli_test.go @@ -4,23 +4,29 @@ import ( "fmt" "testing" - "github.com/gogo/protobuf/proto" - "github.com/stretchr/testify/suite" - + "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" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil/network" + guardiancli "github.com/irisnet/irishub/modules/guardian/client/cli" + guardiantypes "github.com/irisnet/irishub/modules/guardian/types" - sdk "github.com/cosmos/cosmos-sdk/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" - guardiancli "github.com/irisnet/irishub/modules/guardian/client/cli" guardiantestutil "github.com/irisnet/irishub/modules/guardian/client/testutil" - guardiantypes "github.com/irisnet/irishub/modules/guardian/types" - "github.com/irisnet/irismod/simapp" + "github.com/irisnet/irishub/simapp" ) +var privKey crypto.PrivKey +var pubKey crypto.PubKey +var addr sdk.AccAddress + type IntegrationTestSuite struct { suite.Suite @@ -32,7 +38,16 @@ func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") cfg := simapp.NewConfig() - cfg.NumValidators = 2 + 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) @@ -53,13 +68,33 @@ func TestIntegrationTestSuite(t *testing.T) { func (s *IntegrationTestSuite) TestGuardian() { val := s.network.Validators[0] from := val.Address - address := sdk.AccAddress(crypto.AddressHash([]byte("dgsbl"))) 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) - //------test GetCmdCreateSuper()------------- + amount := sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(100000000)) args := []string{ - fmt.Sprintf("--%s=%s", guardiancli.FlagAddress, address.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), @@ -67,44 +102,46 @@ func (s *IntegrationTestSuite) TestGuardian() { fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), } - respType := proto.Message(&sdk.TxResponse{}) + respType = proto.Message(&sdk.TxResponse{}) expectedCode := uint32(0) - bz, err := guardiantestutil.CreateSuperExec(val.ClientCtx, from.String(), args...) + 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) - println(bz.String()) 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)) + s.Require().Equal(addr.String(), supersResp.Supers[0].AddedBy) + s.Require().Equal(from.String(), supersResp.Supers[0].Address) + s.Require().Equal(description, supersResp.Supers[0].Description) //------test GetCmdDeleteSuper()------------- args = []string{ - fmt.Sprintf("--%s=%s", guardiancli.FlagAddress, address.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, from.String(), args...) + 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) - println(bz.String()) s.Require().Equal(expectedCode, txResp.Code) - - - //------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) - println(supersResp.String()) - s.Require().NoError(err) -} \ No newline at end of file + supersResp = respType.(*guardiantypes.QuerySupersResponse) + s.Require().Equal(1, len(supersResp.Supers)) +} diff --git a/modules/guardian/client/cli/tx.go b/modules/guardian/client/cli/tx.go index 7cb29ceb5..f5b49e64f 100644 --- a/modules/guardian/client/cli/tx.go +++ b/modules/guardian/client/cli/tx.go @@ -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" ) @@ -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 diff --git a/modules/guardian/client/testutil/test_helpers.go b/modules/guardian/client/testutil/test_helpers.go index 54b91a6ee..6a41b3f71 100644 --- a/modules/guardian/client/testutil/test_helpers.go +++ b/modules/guardian/client/testutil/test_helpers.go @@ -39,4 +39,4 @@ func QuerySupersExec(clientCtx client.Context, extraArgs ...string) (testutil.Bu args = append(args, extraArgs...) return clitestutil.ExecTestCLICmd(clientCtx, guardiancli.GetCmdQuerySupers(), args) -} \ No newline at end of file +} From 0c840e5ac4bacc706cdb0ceabe87c87fa91c6f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=AA?= Date: Tue, 17 Nov 2020 16:16:43 +0800 Subject: [PATCH 3/3] fix a bug --- modules/guardian/client/cli/cli_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/guardian/client/cli/cli_test.go b/modules/guardian/client/cli/cli_test.go index b485138ea..cb3b309e6 100644 --- a/modules/guardian/client/cli/cli_test.go +++ b/modules/guardian/client/cli/cli_test.go @@ -117,9 +117,6 @@ func (s *IntegrationTestSuite) TestGuardian() { s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz.Bytes(), respType)) supersResp = respType.(*guardiantypes.QuerySupersResponse) s.Require().Equal(2, len(supersResp.Supers)) - s.Require().Equal(addr.String(), supersResp.Supers[0].AddedBy) - s.Require().Equal(from.String(), supersResp.Supers[0].Address) - s.Require().Equal(description, supersResp.Supers[0].Description) //------test GetCmdDeleteSuper()------------- args = []string{