Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Jul 1, 2024
1 parent d081713 commit da5032a
Show file tree
Hide file tree
Showing 10 changed files with 675 additions and 2 deletions.
2 changes: 1 addition & 1 deletion e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cosmos/cosmos-sdk v0.47.9
github.com/cosmos/gogoproto v1.4.10
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.14.4
google.golang.org/protobuf v1.32.0
mods.irisnet.org/api v1.0.0-beta
mods.irisnet.org/modules/coinswap v1.0.0-beta
Expand Down Expand Up @@ -171,7 +172,6 @@ require (
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tidwall/btree v1.6.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
Expand Down
15 changes: 15 additions & 0 deletions e2e/random/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package random

import (
"testing"

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

func TestTxTestSuite(t *testing.T) {
suite.Run(t, new(TxTestSuite))
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
197 changes: 197 additions & 0 deletions e2e/random/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package random

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/cosmos/gogoproto/proto"
"github.com/tidwall/gjson"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"

"mods.irisnet.org/e2e"
"mods.irisnet.org/e2e/service"
randomcli "mods.irisnet.org/modules/random/client/cli"
randomtypes "mods.irisnet.org/modules/random/types"
servicecli "mods.irisnet.org/modules/service/client/cli"
servicetypes "mods.irisnet.org/modules/service/types"
)

// QueryTestSuite is a suite of end-to-end tests for the nft module
type QueryTestSuite struct {
e2e.TestSuite
}

// SetupSuite sets up test suite
func (s *QueryTestSuite) SetupSuite() {
s.SetModifyConfigFn(func(cfg *network.Config) {
var serviceGenState servicetypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[servicetypes.ModuleName], &serviceGenState)

serviceGenState.Definitions = append(
serviceGenState.Definitions,
servicetypes.GenOraclePriceSvcDefinition(),
servicetypes.GetRandomSvcDefinition(),
)
serviceGenState.Bindings = append(
serviceGenState.Bindings,
servicetypes.GenOraclePriceSvcBinding(sdk.DefaultBondDenom),
)
cfg.GenesisState[servicetypes.ModuleName] = cfg.Codec.MustMarshalJSON(&serviceGenState)
})
s.TestSuite.SetupSuite()
}

// TestQueryCmd tests all query command in the nft module
func (s *QueryTestSuite) TestQueryCmd() {
val := s.Validators[0]
clientCtx := val.ClientCtx
expectedCode := uint32(0)

// ---------------------------------------------------------------------------
serviceDeposit := fmt.Sprintf("50000%s", s.BondDenom)
servicePrices := fmt.Sprintf(`{"price": "50%s"}`, s.BondDenom)
qos := int64(3)
options := "{}"
provider := val.Address
baseURL := val.APIAddress

from := val.Address
blockInterval := 4
oracle := true
serviceFeeCap := fmt.Sprintf("50%s", s.BondDenom)

respResult := `{"code":200,"message":""}`
seedStr := "ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12"
respOutput := fmt.Sprintf(`{"header":{},"body":{"seed":"%s"}}`, seedStr)

// ------bind random service-------------
args := []string{
fmt.Sprintf("--%s=%s", servicecli.FlagServiceName, randomtypes.ServiceName),
fmt.Sprintf("--%s=%s", servicecli.FlagDeposit, serviceDeposit),
fmt.Sprintf("--%s=%s", servicecli.FlagPricing, servicePrices),
fmt.Sprintf("--%s=%d", servicecli.FlagQoS, qos),
fmt.Sprintf("--%s=%s", servicecli.FlagOptions, options),
fmt.Sprintf("--%s=%s", servicecli.FlagProvider, provider),

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

txResult := service.BindServiceExec(
s.T(),
s.Network,
clientCtx,
provider.String(),
args...)
s.Require().Equal(expectedCode, txResult.Code)

// ------test GetCmdRequestRandom()-------------
args = []string{
fmt.Sprintf("--%s=%s", randomcli.FlagServiceFeeCap, serviceFeeCap),
fmt.Sprintf("--%s=%t", randomcli.FlagOracle, oracle),
fmt.Sprintf("--%s=%d", randomcli.FlagBlockInterval, blockInterval),

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

txResult = RequestRandomExec(s.T(), s.Network, clientCtx, from.String(), args...)
s.Require().Equal(expectedCode, txResult.Code)

requestID := gjson.Get(txResult.Log, "0.events.1.attributes.0.value").String()
requestHeight := gjson.Get(txResult.Log, "0.events.1.attributes.2.value").Int()

// ------test GetCmdQueryRandomRequestQueue()-------------
url := fmt.Sprintf("%s/irismod/random/queue", baseURL)
resp, err := testutil.GetRequest(url)
respType := proto.Message(&randomtypes.QueryRandomRequestQueueResponse{})
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType))
qrrResp := respType.(*randomtypes.QueryRandomRequestQueueResponse)
s.Require().NoError(err)
s.Require().Len(qrrResp.Requests, 1)

// ------get service request-------------
requestHeight = requestHeight + 1
_, err = s.Network.WaitForHeightWithTimeout(
requestHeight,
time.Duration(int64(blockInterval+2)*int64(s.Network.TimeoutCommit)),
)
if err != nil {
s.Network.WaitForNBlock(2)
}

blockResult, err := val.RPCClient.BlockResults(context.Background(), &requestHeight)
s.Require().NoError(err)
var requestId string
for _, event := range blockResult.EndBlockEvents {
if event.Type == servicetypes.EventTypeNewBatchRequestProvider {
var found bool
var requestIds []string
var requestsBz []byte
for _, attribute := range event.Attributes {
if string(attribute.Key) == servicetypes.AttributeKeyRequests {
requestsBz = []byte(attribute.Value)
found = true
}
}
s.Require().True(found)
if found {
err := json.Unmarshal(requestsBz, &requestIds)
s.Require().NoError(err)
}
s.Require().Len(requestIds, 1)
requestId = requestIds[0]
}
}
s.Require().NotEmpty(requestId)

// ------respond service request-------------
args = []string{
fmt.Sprintf("--%s=%s", servicecli.FlagRequestID, requestId),
fmt.Sprintf("--%s=%s", servicecli.FlagResult, respResult),
fmt.Sprintf("--%s=%s", servicecli.FlagData, respOutput),

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

txResult = service.RespondServiceExec(
s.T(),
s.Network,
clientCtx,
provider.String(),
args...)
s.Require().Equal(expectedCode, txResult.Code)

// ------test GetCmdQueryRandom()-------------
url = fmt.Sprintf("%s/irismod/random/randoms/%s", baseURL, requestID)
resp, err = testutil.GetRequest(url)
respType = proto.Message(&randomtypes.QueryRandomResponse{})
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType))
randomResp := respType.(*randomtypes.QueryRandomResponse)
s.Require().NoError(err)
s.Require().NotNil(randomResp.Random.Value)
}
62 changes: 62 additions & 0 deletions e2e/random/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package random

import (
"fmt"
"testing"

"github.com/cometbft/cometbft/libs/cli"

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

randomcli "mods.irisnet.org/modules/random/client/cli"
randomtypes "mods.irisnet.org/modules/random/types"
"mods.irisnet.org/simapp"
)

// MsgRedelegateExec creates a redelegate message.
func RequestRandomExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
extraArgs ...string,
) *simapp.ResponseTx {
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, randomcli.GetCmdRequestRandom(), args)
}

func QueryRandomExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
requestID string,
extraArgs ...string) *randomtypes.Random {
args := []string{
requestID,
fmt.Sprintf("--%s=json", cli.OutputFlag),
}
args = append(args, extraArgs...)

response := &randomtypes.Random{}
network.ExecQueryCmd(t, clientCtx, randomcli.GetCmdQueryRandom(), args, response)
return response
}

func QueryRandomRequestQueueExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
genHeight string,
extraArgs ...string) *randomtypes.QueryRandomRequestQueueResponse {
args := []string{
genHeight,
fmt.Sprintf("--%s=json", cli.OutputFlag),
}
args = append(args, extraArgs...)

response := &randomtypes.QueryRandomRequestQueueResponse{}
network.ExecQueryCmd(t, clientCtx, randomcli.GetCmdQueryRandomRequestQueue(), args, response)
return response
}
Loading

0 comments on commit da5032a

Please sign in to comment.