Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp committed Jan 8, 2022
1 parent 410800e commit bf2d705
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
13 changes: 2 additions & 11 deletions x/distribution/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
//go:build norace
// +build norace

package testutil

import (
"testing"

"github.com/cosmos/cosmos-sdk/testutil/network"

"github.com/stretchr/testify/suite"
)

func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
suite.Run(t, NewIntegrationTestSuite(cfg))
suite.Run(t, new(IntegrationTestSuite))
}

func TestGRPCQueryTestSuite(t *testing.T) {
suite.Run(t, new(GRPCQueryTestSuite))
}

func TestWithdrawAllSuite(t *testing.T) {
cfg1 := network.DefaultConfig()
cfg1.NumValidators = 2
suite.Run(t, NewWithdrawAllTestSuite(cfg1))
suite.Run(t, new(WithdrawAllTestSuite))
}
6 changes: 6 additions & 0 deletions x/distribution/client/testutil/grpc_query_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (s *GRPCQueryTestSuite) SetupSuite() {
s.Require().NoError(err)
}

// TearDownSuite cleans up the curret test network after _each_ test.
func (s *GRPCQueryTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite1")
s.network.Cleanup()
}

func (s *GRPCQueryTestSuite) TestQueryParamsGRPC() {
val := s.network.Validators[0]
baseURL := val.APIAddress
Expand Down
51 changes: 34 additions & 17 deletions x/distribution/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
return &IntegrationTestSuite{cfg: cfg}
}

// SetupTest creates a new network for _each_ integration test. We create a new
// SetupSuite creates a new network for _each_ integration test. We create a new
// network for each test because there are some state modifications that are
// needed to be made in order to make useful queries. However, we don't want
// these state changes to be present in other tests.
func (s *IntegrationTestSuite) SetupTest() {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := network.DefaultConfig()
cfg.NumValidators = 1
s.cfg = cfg

genesisState := s.cfg.GenesisState
var mintData minttypes.GenesisState
s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[minttypes.ModuleName], &mintData))
Expand All @@ -57,9 +61,9 @@ func (s *IntegrationTestSuite) SetupTest() {
s.Require().NoError(err)
}

// TearDownTest cleans up the curret test network after _each_ test.
func (s *IntegrationTestSuite) TearDownTest() {
s.T().Log("tearing down integration test suite")
// TearDownSuite cleans up the curret test network after _each_ test.
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite1")
s.network.Cleanup()
}

Expand Down Expand Up @@ -129,7 +133,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorOutstandingRewards() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`{"rewards":[{"denom":"stake","amount":"1164.240000000000000000"}]}`,
`{"rewards":[{"denom":"stake","amount":"232.260000000000000000"}]}`,
},
{
"text output",
Expand All @@ -140,7 +144,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorOutstandingRewards() {
},
false,
`rewards:
- amount: "1164.240000000000000000"
- amount: "232.260000000000000000"
denom: stake`,
},
}
Expand Down Expand Up @@ -192,7 +196,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorCommission() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`{"commission":[{"denom":"stake","amount":"464.520000000000000000"}]}`,
`{"commission":[{"denom":"stake","amount":"116.130000000000000000"}]}`,
},
{
"text output",
Expand All @@ -203,7 +207,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorCommission() {
},
false,
`commission:
- amount: "464.520000000000000000"
- amount: "116.130000000000000000"
denom: stake`,
},
}
Expand Down Expand Up @@ -345,7 +349,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegatorRewards() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
fmt.Sprintf(`{"rewards":[{"validator_address":"%s","reward":[{"denom":"stake","amount":"387.100000000000000000"}]}],"total":[{"denom":"stake","amount":"387.100000000000000000"}]}`, valAddr.String()),
fmt.Sprintf(`{"rewards":[{"validator_address":"%s","reward":[{"denom":"stake","amount":"193.550000000000000000"}]}],"total":[{"denom":"stake","amount":"193.550000000000000000"}]}`, valAddr.String()),
},
{
"json output (specific validator)",
Expand All @@ -355,7 +359,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegatorRewards() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`{"rewards":[{"denom":"stake","amount":"387.100000000000000000"}]}`,
`{"rewards":[{"denom":"stake","amount":"193.550000000000000000"}]}`,
},
{
"text output",
Expand All @@ -367,11 +371,11 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegatorRewards() {
false,
fmt.Sprintf(`rewards:
- reward:
- amount: "387.100000000000000000"
- amount: "193.550000000000000000"
denom: stake
validator_address: %s
total:
- amount: "387.100000000000000000"
- amount: "193.550000000000000000"
denom: stake`, valAddr.String()),
},
{
Expand All @@ -383,7 +387,7 @@ total:
},
false,
`rewards:
- amount: "387.100000000000000000"
- amount: "193.550000000000000000"
denom: stake`,
},
}
Expand All @@ -407,7 +411,6 @@ total:
}

func (s *IntegrationTestSuite) TestGetCmdQueryCommunityPool() {
s.SetupTest()
val := s.network.Validators[0]

_, err := s.network.WaitForHeight(4)
Expand Down Expand Up @@ -686,7 +689,21 @@ func (s *IntegrationTestSuite) TestGetCmdSubmitProposal() {
"deposit": -324foocoin
}`

// fund some tokens to the community pool
args := []string{sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(5431))).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.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())}

invalidPropFile := testutil.WriteToNewTempFile(s.T(), invalidProp)
cmd := cli.NewFundCommunityPoolCmd()
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
s.Require().NoError(err)

var txResp sdk.TxResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txResp), out.String())
s.Require().Equal(uint32(0), txResp.Code)

validProp := fmt.Sprintf(`{
"title": "Community Pool Spend",
Expand All @@ -710,7 +727,7 @@ func (s *IntegrationTestSuite) TestGetCmdSubmitProposal() {
invalidPropFile.Name(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
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()),
},
true, 0, nil,
Expand All @@ -721,7 +738,7 @@ func (s *IntegrationTestSuite) TestGetCmdSubmitProposal() {
validPropFile.Name(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), // sync mode as there are no funds yet
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()),
},
false, 0, &sdk.TxResponse{},
Expand Down
20 changes: 14 additions & 6 deletions x/distribution/client/testutil/withdraw_all_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type WithdrawAllTestSuite struct {
network *network.Network
}

func NewWithdrawAllTestSuite(cfg network.Config) *IntegrationTestSuite {
return &IntegrationTestSuite{cfg: cfg}
}
func (s *WithdrawAllTestSuite) SetupSuite() {
cfg := network.DefaultConfig()
cfg.NumValidators = 2
s.cfg = cfg

func (s *WithdrawAllTestSuite) SetupTest() {
s.T().Log("setting up integration test suite")
network, err := network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
Expand All @@ -37,6 +37,14 @@ func (s *WithdrawAllTestSuite) SetupTest() {
s.Require().NoError(err)
}

// TearDownSuite cleans up the curret test network after _each_ test.
func (s *WithdrawAllTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

// This test requires multiple validators, if I add this test to `IntegrationTestSuite` by increasing
// `NumValidators` the existing tests are leading to non-determnism so created new suite for this test.
func (s *WithdrawAllTestSuite) TestNewWithdrawAllRewardsGenerateOnly() {
require := s.Require()
val := s.network.Validators[0]
Expand Down Expand Up @@ -90,7 +98,7 @@ func (s *WithdrawAllTestSuite) TestNewWithdrawAllRewardsGenerateOnly() {
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=true", flags.FlagGenerateOnly),
fmt.Sprintf("--%s=1", cli.FlagMaxMessagesPerTx),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
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()),
}
cmd = cli.NewWithdrawAllRewardsCmd()
Expand All @@ -104,7 +112,7 @@ func (s *WithdrawAllTestSuite) TestNewWithdrawAllRewardsGenerateOnly() {
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=true", flags.FlagGenerateOnly),
fmt.Sprintf("--%s=2", cli.FlagMaxMessagesPerTx),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastAsync),
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()),
}
cmd = cli.NewWithdrawAllRewardsCmd()
Expand Down

0 comments on commit bf2d705

Please sign in to comment.