Skip to content

Commit

Permalink
Merge PR #6033: Add setup for cli_test
Browse files Browse the repository at this point in the history
  • Loading branch information
sahith-narahari authored Apr 29, 2020
1 parent 5a9d3cf commit 3b71198
Show file tree
Hide file tree
Showing 15 changed files with 778 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ jobs:
file: ./coverage.txt # optional
fail_ci_if_error: true
if: "env.GIT_DIFF != ''"
cli-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: cli-test
run: |
make cli-test
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
COMMIT := $(shell git log -1 --format='%H')
LEDGER_ENABLED ?= true
BINDIR ?= $(GOPATH)/bin
BUILDDIR ?= $(CURDIR)/build
SIMAPP = ./simapp
MOCKS_DIR = $(CURDIR)/tests/mocks
HTTPS_GIT := https://github.com/cosmos/cosmos-sdk.git
DOCKER_BUF := docker run -v $(shell pwd):/workspace --workdir /workspace bufbuild/buf

export GO111MODULE = on
export BUILDDIR

# The below include contains the tools and runsim targets.
include contrib/devtools/Makefile
Expand Down Expand Up @@ -166,6 +168,10 @@ test-sim-benchmark-invariants:
-Enabled=true -NumBlocks=1000 -BlockSize=200 \
-Period=1 -Commit=true -Seed=57 -v -timeout 24h

cli-test: build-sim
@go test -mod=readonly -p 4 `go list ./tests/cli/tests/...` -tags=cli_test -v
@go test -mod=readonly -p 4 `go list ./x/.../client/cli_test/...` -tags=cli_test -v

.PHONY: \
test-sim-nondeterminism \
test-sim-custom-genesis-fast \
Expand All @@ -174,7 +180,8 @@ test-sim-after-import \
test-sim-custom-genesis-multi-seed \
test-sim-multi-seed-short \
test-sim-multi-seed-long \
test-sim-benchmark-invariants
test-sim-benchmark-invariants \
cli-test

SIM_NUM_BLOCKS ?= 500
SIM_BLOCK_SIZE ?= 200
Expand All @@ -193,7 +200,7 @@ test-sim-profile:
.PHONY: test-sim-profile test-sim-benchmark

test-cover:
@export VERSION=$(VERSION); bash -x tests/test_cover.sh
@export VERSION=$(VERSION); bash -x contrib/test_cover.sh
.PHONY: test-cover

benchmark:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cover.sh → contrib/test_cover.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -e

PKGS=$(go list ./... | grep -v '/simapp')
PKGS=$(go list ./... | grep -v '/simapp' | grep -v '/cli_test')

set -e
echo "mode: atomic" > coverage.txt
Expand Down
13 changes: 13 additions & 0 deletions tests/cli/helpers/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package helpers

const (
Denom = "stake"
KeyFoo = "foo"
KeyBar = "bar"
FooDenom = "footoken"
FeeDenom = "feetoken"
Fee2Denom = "fee2token"
KeyBaz = "baz"
KeyVesting = "vesting"
KeyFooBarBaz = "foobarbaz"
)
49 changes: 49 additions & 0 deletions tests/cli/helpers/executors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package helpers

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/tests"
)

func ExecuteWriteCheckErr(t *testing.T, cmdStr string, writes ...string) {
require.True(t, ExecuteWrite(t, cmdStr, writes...))
}

func ExecuteWrite(t *testing.T, cmdStr string, writes ...string) (exitSuccess bool) {
exitSuccess, _, _ = ExecuteWriteRetStdStreams(t, cmdStr, writes...)
return
}

func ExecuteWriteRetStdStreams(t *testing.T, cmdStr string, writes ...string) (bool, string, string) {
proc := tests.GoExecuteT(t, cmdStr)

// Enables use of interactive commands
for _, write := range writes {
_, err := proc.StdinPipe.Write([]byte(write + "\n"))
require.NoError(t, err)
}

// Read both stdout and stderr from the process
stdout, stderr, err := proc.ReadAll()
if err != nil {
fmt.Println("Err on proc.ReadAll()", err, cmdStr)
}

// Log output.
if len(stdout) > 0 {
t.Log("Stdout:", string(stdout))
}
if len(stderr) > 0 {
t.Log("Stderr:", string(stderr))
}

// Wait for process to exit
proc.Wait()

// Return succes, stdout, stderr
return proc.ExitState.Success(), string(stdout), string(stderr)
}
78 changes: 78 additions & 0 deletions tests/cli/helpers/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package helpers

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/cosmos/cosmos-sdk/std"
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp"
)

var cdc = std.MakeCodec(simapp.ModuleBasics)

// Fixtures is used to setup the testing environment
type Fixtures struct {
BuildDir string
RootDir string
SimdBinary string
SimcliBinary string
ChainID string
RPCAddr string
Port string
SimdHome string
SimcliHome string
P2PAddr string
Cdc *codec.Codec
T *testing.T
}

// NewFixtures creates a new instance of Fixtures with many vars set
func NewFixtures(t *testing.T) *Fixtures {
tmpDir, err := ioutil.TempDir("", "sdk_integration_"+t.Name()+"_")
require.NoError(t, err)

servAddr, port, err := server.FreeTCPAddr()
require.NoError(t, err)

p2pAddr, _, err := server.FreeTCPAddr()
require.NoError(t, err)

buildDir := os.Getenv("BUILDDIR")
require.NotNil(t, buildDir)

return &Fixtures{
T: t,
BuildDir: buildDir,
RootDir: tmpDir,
SimdBinary: filepath.Join(buildDir, "simd"),
SimcliBinary: filepath.Join(buildDir, "simcli"),
SimdHome: filepath.Join(tmpDir, ".simd"),
SimcliHome: filepath.Join(tmpDir, ".simcli"),
RPCAddr: servAddr,
P2PAddr: p2pAddr,
Cdc: cdc,
Port: port,
}
}

// GenesisFile returns the path of the genesis file
func (f Fixtures) GenesisFile() string {
return filepath.Join(f.SimdHome, "config", "genesis.json")
}

// GenesisFile returns the application's genesis state
func (f Fixtures) GenesisState() simapp.GenesisState {
genDoc, err := tmtypes.GenesisDocFromFile(f.GenesisFile())
require.NoError(f.T, err)

var appState simapp.GenesisState
require.NoError(f.T, f.Cdc.UnmarshalJSON(genDoc.AppState, &appState))
return appState
}
Loading

0 comments on commit 3b71198

Please sign in to comment.