Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sahith/add base setup #6032

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ all: tools build lint test

build: go.sum
@go build -mod=readonly ./...
.PHONY: build

build-sim: go.sum
ifeq ($(OS),Windows_NT)
go build -mod=readonly $(BUILD_FLAGS) -o build/simd.exe ./simapp/cmd/simd
go build -mod=readonly $(BUILD_FLAGS) -o build/simcli.exe ./simapp/cmd/simcli
else
go build -mod=readonly $(BUILD_FLAGS) -o build/simd ./simapp/cmd/simd
go build -mod=readonly $(BUILD_FLAGS) -o build/simcli ./simapp/cmd/simcli
endif

.PHONY: \
build \
build-sim

mocks: $(MOCKS_DIR)
mockgen -source=x/auth/types/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
Expand Down Expand Up @@ -152,6 +164,9 @@ test-sim-benchmark-invariants:
-Enabled=true -NumBlocks=1000 -BlockSize=200 \
-Period=1 -Commit=true -Seed=57 -v -timeout 24h

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

.PHONY: \
test-sim-nondeterminism \
test-sim-custom-genesis-fast \
Expand All @@ -160,7 +175,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 \
test-cli

SIM_NUM_BLOCKS ?= 500
SIM_BLOCK_SIZE ?= 200
Expand Down
13 changes: 13 additions & 0 deletions cli_test/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"
)
47 changes: 47 additions & 0 deletions cli_test/helpers/executors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helpers

import (
"fmt"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/stretchr/testify/require"
"testing"
)

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)
}
75 changes: 75 additions & 0 deletions cli_test/helpers/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package helpers

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

// 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
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")
if buildDir == "" {
buildDir, err = filepath.Abs("../../build/")
require.NoError(t, err)
}

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,
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 {
cdc := codec.New()
genDoc, err := tmtypes.GenesisDocFromFile(f.GenesisFile())
require.NoError(f.T, err)

var appState simapp.GenesisState
require.NoError(f.T, cdc.UnmarshalJSON(genDoc.AppState, &appState))
return appState
}
69 changes: 69 additions & 0 deletions cli_test/helpers/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package helpers

import (
"fmt"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)

// InitFixtures is called at the beginning of a test and initializes a chain
// with 1 validator.
func InitFixtures(t *testing.T) (f *Fixtures) {
f = NewFixtures(t)

// reset test state
f.UnsafeResetAll()

f.CLIConfig("keyring-backend", "test")

// ensure keystore has foo and bar keys
f.KeysDelete(KeyFoo)
f.KeysDelete(KeyBar)
f.KeysDelete(KeyBar)
f.KeysDelete(KeyFooBarBaz)
f.KeysAdd(KeyFoo)
f.KeysAdd(KeyBar)
f.KeysAdd(KeyBaz)
f.KeysAdd(KeyVesting)
f.KeysAdd(KeyFooBarBaz, "--multisig-threshold=2", fmt.Sprintf(
"--multisig=%s,%s,%s", KeyFoo, KeyBar, KeyBaz))

// ensure that CLI output is in JSON format
f.CLIConfig("output", "json")

// NOTE: SDInit sets the ChainID
f.SDInit(KeyFoo)

f.CLIConfig("chain-id", f.ChainID)
f.CLIConfig("broadcast-mode", "block")
f.CLIConfig("trust-node", "true")

// start an account with tokens
f.AddGenesisAccount(f.KeyAddress(KeyFoo), startCoins)
f.AddGenesisAccount(
f.KeyAddress(KeyVesting), startCoins,
fmt.Sprintf("--vesting-amount=%s", vestingCoins),
fmt.Sprintf("--vesting-start-time=%d", time.Now().UTC().UnixNano()),
fmt.Sprintf("--vesting-end-time=%d", time.Now().Add(60*time.Second).UTC().UnixNano()),
)

f.GenTx(KeyFoo)
f.CollectGenTxs()

return f
}

// Cleanup is meant to be run at the end of a test to clean up an remaining test state
func (f *Fixtures) Cleanup(dirs ...string) {
clean := append(dirs, f.RootDir)
for _, d := range clean {
require.NoError(f.T, os.RemoveAll(d))
}
}

// Flags returns the flags necessary for making most CLI calls
func (f *Fixtures) Flags() string {
return fmt.Sprintf("--home=%s --node=%s", f.SimcliHome, f.RPCAddr)
}
Loading