Skip to content

Commit

Permalink
add cligen test
Browse files Browse the repository at this point in the history
  • Loading branch information
ikehara committed Oct 21, 2019
1 parent fbf66a0 commit cda42a8
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ integration-test:
e2e-test:
$(MAKE) -C ./tests e2e-test

cligen-test:
$(MAKE) -C ./tests cligen-test

build-image:
docker build . -t bluele/hypermint:${VERSION}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ require (
github.com/perlin-network/life v0.0.0-20181106205055-98065d82a6ee
github.com/pkg/errors v0.8.1
github.com/rjeczalik/notify v0.9.2 // indirect
github.com/spf13/cobra v0.0.1
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.0.0
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
github.com/tendermint/go-amino v0.14.1
github.com/tendermint/iavl v0.12.4
Expand Down
86 changes: 86 additions & 0 deletions go.sum

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions pkg/account/cli/bind/template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
build/
cligen/contract_test*
7 changes: 7 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ GO_BIN?=go
GO_TEST_FLAGS?=-v -count=1
GO_TEST_CMD=$(GO_BIN) test $(GO_TEST_FLAGS)
VERSION ?= v0.2.0
TOP_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/..

build-image:
docker build . -t hypermint/go-rust:$(VERSION)
Expand All @@ -19,3 +20,9 @@ integration-test: contract_test.wasm

e2e-test: contract_test.wasm external_contract_test.wasm
@$(GO_TEST_CMD) ./e2e/...

cligen-test: contract_test.wasm
make -C .. abigen cligen
(cd $(TOP_DIR)/hmcdk/genabi && cargo run $(TOP_DIR)/tests/contracts/contract_test/src/lib.rs) > cligen/contract_test.json
$(TOP_DIR)/build/cligen --outdir cligen --name contract_test --package cligen --abi cligen/contract_test.json
@$(GO_TEST_CMD) ./cligen/...
106 changes: 106 additions & 0 deletions tests/cligen/cligen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cligen

import (
"bytes"
"context"
"fmt"
ecommon "github.com/bluele/hypermint/tests/e2e/common"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
"path/filepath"
"regexp"
"testing"
"time"

cmn "github.com/tendermint/tendermint/libs/common"
)

const (
testContractPath = "../build/contract_test.wasm"
)

func TestCligenTestSuite(t *testing.T) {
suite.Run(t, new(CligenTestSuite))
}

type CligenTestSuite struct {
ecommon.NodeTestSuite
}

func (ts *CligenTestSuite) SetupTest() {
pjRoot, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
ts.FailNow("failed to call Abs()", err.Error())
}
// TODO these values should be configurable?
ts.Setup(
filepath.Join(pjRoot, "build"),
filepath.Join(filepath.Join(pjRoot, ".hm"), cmn.RandStr(8)),
)
}

func (ts *CligenTestSuite) TearDownTest() {
ts.TearDown()
}

func (ts *CligenTestSuite) TestCLI() {
ctx := context.Background()
c, err := ts.DeployContract(ctx, ts.Account(1), testContractPath)
if !ts.NoError(err) {
return
}

caller := ts.Account(0)

var rootCmd = &cobra.Command{
Use: "test",
Short: "Test CLI",
}

for _, tc := range []struct{
suc bool
args []string
outregex string
}{
{ true, []string{"help"}, `.*$`},
{ true, []string{"test-get-sender"}, caller.Hex()+`\n$`},
{ true, []string{"test-get-contract-address"}, c.Hex()+`\n$`},
} {
args := append([]string{"contract_test", "--passphrase", "password", "--ksdir", ts.GetCLIHomeDir()}, tc.args...)
buf := new(bytes.Buffer)
rootCmd.SetOut(buf)
rootCmd.SetArgs(args)
rootCmd.AddCommand(ContractTestCmd(c.Hex(), caller.Hex()))
if err := rootCmd.Execute(); tc.suc != (err == nil) {
if err != nil {
ts.T().Error(err)
} else {
ts.T().Fail()
}
}
output := buf.String()
r := regexp.MustCompile(tc.outregex)
if !r.MatchString(output) {
ts.T().Error(output, "does not match with the regexp", "'"+tc.outregex+"'", args)
}
}
}

func (ts *CligenTestSuite) DeployContract(ctx context.Context, from common.Address, path string) (common.Address, error) {
cmd := fmt.Sprintf("contract deploy --address=%v --path=%v --gas=1 --password=password", from.Hex(), path)
address, err := ts.sendTxCMD(ctx, cmd)
if err != nil {
return common.Address{}, err
}
return common.HexToAddress(string(address)), nil
}

func (ts *CligenTestSuite) sendTxCMD(ctx context.Context, cmd string) ([]byte, error) {
if out, e, err := ts.ExecCLICommand(ctx, cmd); err != nil {
return nil, fmt.Errorf("%v:%v:%v", string(out), string(e), err)
} else {
time.Sleep(2 * ts.TimeoutCommit)
return out, nil
}
}
44 changes: 42 additions & 2 deletions tests/contracts/contract_test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions tests/contracts/contract_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ fn call_check_signature() -> Result<i32, Error> {
}
}

#[contract]
#[contract(readonly)]
pub fn test_get_sender() -> R<Address> {
Ok(Some(get_sender()?))
}

#[contract]
#[contract(readonly)]
pub fn test_get_contract_address() -> R<Address> {
Ok(Some(get_contract_address()?))
}

#[contract]
#[contract(readonly)]
pub fn test_get_arguments() -> R<Vec<u8>> {
let argIdx: i32 = get_arg(0)?;
let arg: Vec<u8> = get_arg(argIdx as usize)?;
Ok(Some(arg))
}

#[contract]
#[contract(readonly)]
pub fn check_signature() -> R<i32> {
Ok(Some(call_check_signature()?))
}
Expand All @@ -59,7 +59,7 @@ pub fn test_write_state() -> R<i32> {
Ok(None)
}

#[contract]
#[contract(readonly)]
pub fn test_read_state() -> R<Vec<u8>> {
let key: Vec<u8> = get_arg(0)?;
let value: Vec<u8> = match read_state(&key) {
Expand Down Expand Up @@ -113,13 +113,13 @@ pub fn test_write_to_multiple_key() -> R<i32> {
Ok(None)
}

#[contract]
#[contract(readonly)]
pub fn test_keccak256() -> R<Vec<u8>> {
let msg: Vec<u8> = get_arg(0)?;
Ok(Some(keccak256(&msg)?.to_vec()))
}

#[contract]
#[contract(readonly)]
pub fn test_sha256() -> R<Vec<u8>> {
let msg: Vec<u8> = get_arg(0)?;
Ok(Some(sha256(&msg)?.to_vec()))
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/common/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (ts *NodeTestSuite) RPCClient() rpclient.Client {
return rpclient.NewHTTP(c.RPC.ListenAddress, "/websocket")
}

func (ts *NodeTestSuite) GetCLIHomeDir() string {
return ts.hmcliHomeDir
}

type Env interface {
String() string
}
Expand Down

0 comments on commit cda42a8

Please sign in to comment.