Skip to content

Commit

Permalink
refactor: remove simapp from client/server tests (#12676)
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski authored Jul 21, 2022
1 parent 71dc827 commit 5e1ff06
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 170 deletions.
20 changes: 2 additions & 18 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client_test

import (
"bytes"
"context"
"encoding/json"
"os"
"strings"
Expand All @@ -17,9 +16,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -145,22 +143,8 @@ x: "10"
`, buf.String())
}

func TestCLIQueryConn(t *testing.T) {
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 1

n, err := network.New(t, t.TempDir(), cfg)
require.NoError(t, err)
defer n.Cleanup()

testClient := testdata.NewQueryClient(n.Validators[0].ClientCtx)
res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
require.NoError(t, err)
require.Equal(t, "hello", res.Message)
}

func TestGetFromFields(t *testing.T) {
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg := testutil.MakeTestEncodingConfig()
path := hd.CreateHDPath(118, 0, 0).String()

testCases := []struct {
Expand Down
124 changes: 86 additions & 38 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,131 @@
//go:build norace
// +build norace

package client_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/x/auth/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type IntegrationTestSuite struct {
suite.Suite

network *network.Network
ctx sdk.Context
genesisAccount *authtypes.BaseAccount
bankClient types.QueryClient
testClient testdata.QueryClient
genesisAccountBalance int64
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
var (
interfaceRegistry codectypes.InterfaceRegistry
bankKeeper bankkeeper.BaseKeeper
appBuilder *runtime.AppBuilder
cdc codec.Codec
)

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.Require().NoError(err)
// TODO duplicated from testutils/sims/app_helpers.go
// need more composable startup options for simapp, this test needed a handle to the closed over genesis account
// to query balances
err := depinject.Inject(testutil.AppConfig, &interfaceRegistry, &bankKeeper, &appBuilder, &cdc)
s.NoError(err)

_, err = s.network.WaitForHeight(2)
s.Require().NoError(err)
app := appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
err = app.Load(true)
s.NoError(err)

valSet, err := sims.CreateRandomValidatorSet()
s.NoError(err)

// generate genesis account
s.genesisAccountBalance = 100000000000000
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(s.genesisAccountBalance))),
}

genesisState, err := sims.GenesisStateWithValSet(cdc, appBuilder.DefaultGenesis(), valSet, []authtypes.GenesisAccount{acc}, balance)
s.NoError(err)

stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
s.NoError(err)

// init chain will set the validator set and initialize the genesis accounts
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: sims.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)

app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
Height: app.LastBlockHeight() + 1,
AppHash: app.LastCommitID().Hash,
ValidatorsHash: valSet.Hash(),
NextValidatorsHash: valSet.Hash(),
}})

// end of app init

s.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, interfaceRegistry)
types.RegisterQueryServer(queryHelper, bankKeeper)
testdata.RegisterQueryServer(queryHelper, testdata.QueryImpl{})
s.bankClient = types.NewQueryClient(queryHelper)
s.testClient = testdata.NewQueryClient(queryHelper)
s.genesisAccount = acc
}

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

func (s *IntegrationTestSuite) TestGRPCQuery() {
val0 := s.network.Validators[0]
denom := sdk.DefaultBondDenom

// gRPC query to test service should work
testClient := testdata.NewQueryClient(val0.ClientCtx)
testRes, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
testRes, err := s.testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
s.Require().NoError(err)
s.Require().Equal("hello", testRes.Message)

// gRPC query to bank service should work
denom := fmt.Sprintf("%stoken", val0.Moniker)
bankClient := banktypes.NewQueryClient(val0.ClientCtx)
var header metadata.MD
bankRes, err := bankClient.Balance(
res, err := s.bankClient.Balance(
context.Background(),
&banktypes.QueryBalanceRequest{Address: val0.Address.String(), Denom: denom},
&banktypes.QueryBalanceRequest{Address: s.genesisAccount.GetAddress().String(), Denom: denom},
grpc.Header(&header), // Also fetch grpc header
)
s.Require().NoError(err)
s.Require().Equal(
sdk.NewCoin(denom, s.network.Config.AccountTokens),
*bankRes.GetBalance(),
)
blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
s.Require().NotEmpty(blockHeight[0]) // Should contain the block height

// Request metadata should work
val0.ClientCtx = val0.ClientCtx.WithHeight(1) // We set clientCtx to height 1
bankClient = banktypes.NewQueryClient(val0.ClientCtx)
bankRes, err = bankClient.Balance(
context.Background(),
&banktypes.QueryBalanceRequest{Address: val0.Address.String(), Denom: denom},
grpc.Header(&header),
)
blockHeight = header.Get(grpctypes.GRPCBlockHeightHeader)
s.Require().Equal([]string{"1"}, blockHeight)
bal := res.GetBalance()
s.Equal(sdk.NewCoin(denom, sdk.NewInt(s.genesisAccountBalance)), *bal)
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down
63 changes: 0 additions & 63 deletions client/query_test.go

This file was deleted.

82 changes: 79 additions & 3 deletions client/rpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
//go:build norace
// +build norace

package rpc_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/simapp"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type IntegrationTestSuite struct {
Expand All @@ -21,8 +30,11 @@ type IntegrationTestSuite struct {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
cfg, err := network.DefaultConfigWithAppConfig(network.MinimumAppConfig())

s.NoError(err)

s.network, err = network.New(s.T(), s.T().TempDir(), cfg)
s.Require().NoError(err)

s.Require().NoError(s.network.WaitForNextBlock())
Expand All @@ -44,6 +56,70 @@ func (s *IntegrationTestSuite) TestStatusCommand() {
s.Require().Contains(out.String(), fmt.Sprintf("\"moniker\":\"%s\"", val0.Moniker))
}

func (s *IntegrationTestSuite) TestCLIQueryConn() {
var header metadata.MD

testClient := testdata.NewQueryClient(s.network.Validators[0].ClientCtx)
res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"}, grpc.Header(&header))
s.NoError(err)

blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
s.Require().Equal([]string{"1"}, blockHeight)

s.Equal("hello", res.Message)
}

func (s *IntegrationTestSuite) TestQueryABCIHeight() {
testCases := []struct {
name string
reqHeight int64
ctxHeight int64
expHeight int64
}{
{
name: "non zero request height",
reqHeight: 3,
ctxHeight: 1, // query at height 1 or 2 would cause an error
expHeight: 3,
},
{
name: "empty request height - use context height",
reqHeight: 0,
ctxHeight: 3,
expHeight: 3,
},
{
name: "empty request height and context height - use latest height",
reqHeight: 0,
ctxHeight: 0,
expHeight: 4,
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
s.network.WaitForHeight(tc.expHeight)

val := s.network.Validators[0]

clientCtx := val.ClientCtx
clientCtx = clientCtx.WithHeight(tc.ctxHeight)

req := abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", banktypes.StoreKey),
Height: tc.reqHeight,
Data: banktypes.CreateAccountBalancesPrefix(val.Address),
Prove: true,
}

res, err := clientCtx.QueryABCI(req)
s.Require().NoError(err)

s.Require().Equal(tc.expHeight, res.Height)
})
}
}

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

0 comments on commit 5e1ff06

Please sign in to comment.