Skip to content

Commit

Permalink
test: add e2e testing for get_seed
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Dec 14, 2023
1 parent b194fe6 commit 1efc565
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ test-unit-cover: ARGS=-timeout=10m -tags='$(UNIT_TEST_TAGS)' -coverprofile=$(TES
test-unit-cover: TEST_PACKAGES=$(PACKAGES_UNIT)
test-unit-race: ARGS=-timeout=10m -race -tags='$(TEST_RACE_TAGS)'
test-unit-race: TEST_PACKAGES=$(PACKAGES_UNIT)
test-e2e: docker-build-e2e
# test-e2e: docker-build-e2e
test-e2e: ARGS=-timeout=10m -v
test-e2e: TEST_PACKAGES=$(PACKAGES_E2E)
$(TEST_TARGETS): run-tests
Expand Down
57 changes: 55 additions & 2 deletions e2e/e2e_gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"time"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand Down Expand Up @@ -75,7 +77,7 @@ func (s *IntegrationTestSuite) execWasmStorageStoreOverlay(
opt = append(opt, withKeyValue(flagSummary, summary))
opt = append(opt, withKeyValue(flagDeposit, "10000000aseda"))
opt = append(opt, withKeyValue(flagAuthority, authtypes.NewModuleAddress("gov").String()))
opts := applyOptions(c.id, opt)
opts := applyTxOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down Expand Up @@ -122,7 +124,7 @@ func (s *IntegrationTestSuite) execGovVoteYes(
) {
opt = append(opt, withKeyValue(flagFees, fees))
opt = append(opt, withKeyValue(flagFrom, from))
opts := applyOptions(c.id, opt)
opts := applyTxOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down Expand Up @@ -155,3 +157,54 @@ func (s *IntegrationTestSuite) execGovVoteYes(
5*time.Second,
)
}

func (s *IntegrationTestSuite) execGetSeedQuery(
c *chain,
valIdx int,
proxyContractAddr string,
expectErr bool,
opt ...flagOption,
) {
opts := applyQueryOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// seda-chaind query wasm contract-state smart $PROXY_CONTRACT_ADDRESS '{"query_seed_request":{}}' --node $RPC_URL --output json
command := []string{
binary,
queryCommand,
wasmtypes.ModuleName,
"contract-state",
"smart",
proxyContractAddr,
`{"query_seed_request":{}}`,
}
for flag, value := range opts {
command = append(command, fmt.Sprintf("--%s=%v", flag, value))
}

s.T().Logf("getting seed from contract %s on chain %s", proxyContractAddr, c.id)

s.executeQuery(ctx, c, command, valIdx, s.validateGetSeedResponse(false))
}

func (s *IntegrationTestSuite) validateGetSeedResponse(expectEmpty bool) func([]byte, []byte) bool {
return func(stdOut []byte, stdErr []byte) bool {
var getSeedResponse struct {
Data struct {
BlockHeight int `json:"block_height"`
Seed string `json:"seed"`
} `json:"data"`
}

err := json.Unmarshal(stdOut, &getSeedResponse)
s.Require().NoError(err)
if expectEmpty {
s.Require().Empty(getSeedResponse.Data.Seed)
} else {
s.Require().NotEmpty(getSeedResponse.Data.Seed) // TO-DO better seed check
}
return true
}
}
10 changes: 7 additions & 3 deletions e2e/e2e_register_proxy_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func (s *IntegrationTestSuite) testInstantiateAndRegisterProxyContract() {

_, err = sdktypes.AccAddressFromBech32(res.Address)
s.Require().NoError(err)
s.Require().NotEmpty(res.Address)

return res.Address != ""
s.execGetSeedQuery(s.chain, 0, res.Address, false)
s.Require().NoError(err)

return true
},
30*time.Second,
5*time.Second,
Expand Down Expand Up @@ -70,7 +74,7 @@ func (s *IntegrationTestSuite) execInstantiateAndRegisterProxyContract(

opt = append(opt, withKeyValue(flagAuthority, authtypes.NewModuleAddress("gov").String()))

opts := applyOptions(c.id, opt)
opts := applyTxOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down Expand Up @@ -118,7 +122,7 @@ func (s *IntegrationTestSuite) execWasmStore(
) {
opt = append(opt, withKeyValue(flagFees, fees))
opt = append(opt, withKeyValue(flagFrom, from))
opts := applyOptions(c.id, opt)
opts := applyTxOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down
2 changes: 2 additions & 0 deletions e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type IntegrationTestSuite struct {
dkrNet *dockertest.Network
valResources map[string][]*dockertest.Resource
endpoint string
grpcEndpoint string
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down Expand Up @@ -331,6 +332,7 @@ func (s *IntegrationTestSuite) runValidators(c *chain, portOffset int) {
}

s.endpoint = fmt.Sprintf("http://%s", s.valResources[s.chain.id][0].GetHostPort("1317/tcp"))
s.grpcEndpoint = s.valResources[s.chain.id][0].GetHostPort("9090/tcp")

rpcClient, err := rpchttp.New("tcp://localhost:26657", "/websocket")
s.Require().NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e_wasm_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *IntegrationTestSuite) execWasmStorageStoreDataRequest(
opt = append(opt, withKeyValue(flagFrom, from))
opt = append(opt, withKeyValue(flagWasmType, wasmType))

opts := applyOptions(c.id, opt)
opts := applyTxOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down
50 changes: 48 additions & 2 deletions e2e/e2e_exec_test.go → e2e/exec_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func withKeyValue(key string, value interface{}) flagOption {
}
}

func applyOptions(chainID string, options []flagOption) map[string]interface{} {
func applyTxOptions(chainID string, options []flagOption) map[string]interface{} {
opts := map[string]interface{}{
flagKeyringBackend: "test",
flagOutput: "json",
Expand All @@ -65,6 +65,53 @@ func applyOptions(chainID string, options []flagOption) map[string]interface{} {
return opts
}

func applyQueryOptions(chainID string, options []flagOption) map[string]interface{} {
opts := map[string]interface{}{
flagOutput: "json",
}
for _, apply := range options {
apply(opts)
}
return opts
}

func (s *IntegrationTestSuite) executeQuery(ctx context.Context, c *chain, command []string, valIdx int, validation func([]byte, []byte) bool) {
s.T().Logf("executing command %s", command)

if validation == nil {
validation = s.defaultExecValidation(s.chain, 0)
}
var (
outBuf bytes.Buffer
errBuf bytes.Buffer
)
exec, err := s.dkrPool.Client.CreateExec(docker.CreateExecOptions{
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: s.valResources[c.id][valIdx].Container.ID,
User: "root",
Cmd: command,
})
s.Require().NoError(err)

err = s.dkrPool.Client.StartExec(exec.ID, docker.StartExecOptions{
Context: ctx,
Detach: false,
OutputStream: &outBuf,
ErrorStream: &errBuf,
})
s.Require().NoError(err)

stdOut := outBuf.Bytes()
stdErr := errBuf.Bytes()

if !validation(stdOut, stdErr) {
s.Require().FailNowf("Query execution validation failed", "stdout: %s, stderr: %s",
string(stdOut), string(stdErr))
}
}

func (s *IntegrationTestSuite) executeTx(ctx context.Context, c *chain, command []string, valIdx int, validation func([]byte, []byte) bool) {
s.T().Logf("executing command %s", command)

Expand Down Expand Up @@ -102,7 +149,6 @@ func (s *IntegrationTestSuite) executeTx(ctx context.Context, c *chain, command
}
}

// TO-DO refactor
func (s *IntegrationTestSuite) expectErrExecValidation(chain *chain, valIdx int, expectErr bool) func([]byte, []byte) bool {
return func(stdOut []byte, stdErr []byte) bool {
var txResp sdk.TxResponse
Expand Down
Binary file added testutil/testwasms/data_requests.wasm
Binary file not shown.
Binary file added testutil/testwasms/integration_tests.wasm
Binary file not shown.
Binary file modified testutil/testwasms/proxy_contract.wasm
Binary file not shown.
Binary file modified testutil/testwasms/staking.wasm
Binary file not shown.

0 comments on commit 1efc565

Please sign in to comment.