cosmos-sdk/x/genutil/client/cli/init_test.go
frog power 4000 3fe5869148
Merge PR #4159: Module/Genesis Generalization
* first commit

* gaia cleanup

* ...

* staking multihooks

* missing module function return args

* bank module name constant

* working, module interface for x/

* got this thing compiling

* make test compiles and passes

* remove expanded simulation invariants

* genesis issue

* continued

* continued

* register crisis routes thought mm

* begin blocker to mm

* end blocker to mm

* empty routes not initialized

* move gaia initChainer sanity check to baseapp

* remove codecs from module manager

* reorging genesis stuff

* module manager passed by reference/bugfixes from working last commit

int

int

* move invariant checks from gaia to crisis

* typo

* basic refactors cmd/gaia/init

* working

* MultiStakingHooks from types to x/staking/types

int

* default module manager order of operations from input modules

* working

* typo

* add AppModuleBasic

* moduleBasicManager / non-test code compiles

* working attempting to get tests passing

* make test passes

* sim random genesis fix

* export bug

* ...

* genutil module

* genutil working

* refactored - happy with non-testing code in cmd/

* ...

* lint fixes

* comment improvement

* cli test fix

* compile housing

* working through compile errors

* working gettin' compilin'

* non-test code compiles

* move testnet to its own module

* reworking tests

int

* bez staging PR 1 comments

* concise module function-of names

* moved all tests from genesis_test.go to other genutil tests

* genaccounts package, add genutil and genaccounts to app.go

* docs for genutil genaccounts

* genaccounts iterate fn

* non-test code with genaccounts/ now compiles

* working test compiling

* debugging tests

* resolved all make test compile errors

* test debuggin

* resolved all unit tests, introduced param module

* cli-test compile fixes

* staking initialization bug

* code comment improvements, changelog entries

* BasicGaiaApp -> ModuleBasics

* highlevel explanation in types/module.go

* @alexanderbez comment revisions

* @fedekunze PR comments

* @alexanderbez PR comments (x2)

* @cwgoes comments (minor updates)

* @fedekunze suggestions

* panic on init with multiple validator updates from different modules

* initchain panic makes validate genesis fail

int

* AppModuleGenesis seperation

int

* test

* remove init panic logic in validate genesis replaced with TODO

* set maxprocs to match system's GOMAXPROCS

* Update circleci

* Cap maxprocs in CI to 4

* @alexanderbez recent comments addressed

* less blocks in twouble sims

int

* runsim error output flag

* -e on import_export as well

* error out

int

* Try to fix failures

* runsim
2019-05-16 11:25:32 -04:00

143 lines
3.4 KiB
Go

package cli
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
"time"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
abciServer "github.com/tendermint/tendermint/abci/server"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
)
var testMbm = sdk.NewModuleBasicManager(genutil.AppModuleBasic{})
func TestInitCmd(t *testing.T) {
defer server.SetupViper(t)()
defer setupClientHome(t)()
logger := log.NewNopLogger()
cfg, err := tcmd.ParseConfig()
require.Nil(t, err)
ctx := server.NewContext(cfg, logger)
cdc := app.MakeCodec()
cmd := InitCmd(ctx, cdc, testMbm)
require.NoError(t, cmd.RunE(nil, []string{"gaianode-test"}))
}
func setupClientHome(t *testing.T) func() {
clientDir, err := ioutil.TempDir("", "mock-sdk-cmd")
require.Nil(t, err)
viper.Set(flagClientHome, clientDir)
return func() {
if err := os.RemoveAll(clientDir); err != nil {
// TODO: Handle with #870
panic(err)
}
}
}
func TestEmptyState(t *testing.T) {
defer server.SetupViper(t)()
defer setupClientHome(t)()
logger := log.NewNopLogger()
cfg, err := tcmd.ParseConfig()
require.Nil(t, err)
ctx := server.NewContext(cfg, logger)
cdc := app.MakeCodec()
cmd := InitCmd(ctx, cdc, testMbm)
require.NoError(t, cmd.RunE(nil, []string{"gaianode-test"}))
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
cmd = server.ExportCmd(ctx, cdc, nil)
err = cmd.RunE(nil, nil)
require.NoError(t, err)
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = old
out := <-outC
require.Contains(t, out, "genesis_time")
require.Contains(t, out, "chain_id")
require.Contains(t, out, "consensus_params")
require.Contains(t, out, "app_hash")
require.Contains(t, out, "app_state")
}
func TestStartStandAlone(t *testing.T) {
home, err := ioutil.TempDir("", "mock-sdk-cmd")
require.Nil(t, err)
defer func() {
os.RemoveAll(home)
}()
viper.Set(cli.HomeFlag, home)
defer setupClientHome(t)()
logger := log.NewNopLogger()
cfg, err := tcmd.ParseConfig()
require.Nil(t, err)
ctx := server.NewContext(cfg, logger)
cdc := app.MakeCodec()
initCmd := InitCmd(ctx, cdc, testMbm)
require.NoError(t, initCmd.RunE(nil, []string{"gaianode-test"}))
app, err := mock.NewApp(home, logger)
require.Nil(t, err)
svrAddr, _, err := server.FreeTCPAddr()
require.Nil(t, err)
svr, err := abciServer.NewServer(svrAddr, "socket", app)
require.Nil(t, err, "error creating listener")
svr.SetLogger(logger.With("module", "abci-server"))
svr.Start()
timer := time.NewTimer(time.Duration(2) * time.Second)
select {
case <-timer.C:
svr.Stop()
}
}
func TestInitNodeValidatorFiles(t *testing.T) {
home, err := ioutil.TempDir("", "mock-sdk-cmd")
require.Nil(t, err)
defer func() {
os.RemoveAll(home)
}()
viper.Set(cli.HomeFlag, home)
viper.Set(client.FlagName, "moniker")
cfg, err := tcmd.ParseConfig()
require.Nil(t, err)
nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(cfg)
require.Nil(t, err)
require.NotEqual(t, "", nodeID)
require.NotEqual(t, 0, len(valPubKey.Bytes()))
}