2020-08-17 14:32:16 -07:00
package cli_test
2018-10-10 15:45:41 -07:00
import (
"bytes"
2020-07-07 08:40:46 -07:00
"context"
"fmt"
2018-10-10 15:45:41 -07:00
"io"
"os"
"testing"
"time"
2019-02-08 12:45:23 -08:00
"github.com/spf13/viper"
2018-10-10 15:45:41 -07:00
"github.com/stretchr/testify/require"
2020-07-07 08:40:46 -07:00
abci_server "github.com/tendermint/tendermint/abci/server"
2019-02-08 12:45:23 -08:00
"github.com/tendermint/tendermint/libs/cli"
2018-10-10 15:45:41 -07:00
"github.com/tendermint/tendermint/libs/log"
2019-04-04 07:36:39 -07:00
2020-07-07 08:40:46 -07:00
"github.com/cosmos/cosmos-sdk/client"
2019-05-16 18:34:54 -07:00
"github.com/cosmos/cosmos-sdk/codec"
2020-08-26 02:39:38 -07:00
"github.com/cosmos/cosmos-sdk/codec/types"
2020-06-04 03:38:24 -07:00
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
2019-04-04 07:36:39 -07:00
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/mock"
2020-12-08 09:22:43 -08:00
"github.com/cosmos/cosmos-sdk/testutil"
2019-05-16 08:25:32 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
2019-06-05 16:26:17 -07:00
"github.com/cosmos/cosmos-sdk/types/module"
2019-05-16 08:25:32 -07:00
"github.com/cosmos/cosmos-sdk/x/genutil"
2020-08-17 14:32:16 -07:00
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
2018-10-10 15:45:41 -07:00
)
2019-06-05 16:26:17 -07:00
var testMbm = module . NewBasicManager ( genutil . AppModuleBasic { } )
2019-05-16 08:25:32 -07:00
2018-10-10 15:45:41 -07:00
func TestInitCmd ( t * testing . T ) {
2020-07-17 07:20:31 -07:00
tests := [ ] struct {
name string
flags func ( dir string ) [ ] string
shouldErr bool
err error
} {
{
name : "happy path" ,
flags : func ( dir string ) [ ] string {
return [ ] string {
"appnode-test" ,
}
} ,
shouldErr : false ,
err : nil ,
} ,
}
2020-07-07 08:40:46 -07:00
2020-07-17 07:20:31 -07:00
for _ , tt := range tests {
tt := tt
t . Run ( tt . name , func ( t * testing . T ) {
2020-09-18 04:08:24 -07:00
home := t . TempDir ( )
2020-07-17 07:20:31 -07:00
logger := log . NewNopLogger ( )
2020-08-17 14:32:16 -07:00
cfg , err := genutiltest . CreateDefaultTendermintConfig ( home )
2020-07-17 07:20:31 -07:00
require . NoError ( t , err )
serverCtx := server . NewContext ( viper . New ( ) , cfg , logger )
2020-08-26 02:39:38 -07:00
interfaceRegistry := types . NewInterfaceRegistry ( )
marshaler := codec . NewProtoCodec ( interfaceRegistry )
clientCtx := client . Context { } .
2021-05-03 07:45:47 -07:00
WithJSONCodec ( marshaler ) .
2020-08-26 02:39:38 -07:00
WithLegacyAmino ( makeCodec ( ) ) .
WithHomeDir ( home )
2020-07-17 07:20:31 -07:00
ctx := context . Background ( )
ctx = context . WithValue ( ctx , client . ClientContextKey , & clientCtx )
ctx = context . WithValue ( ctx , server . ServerContextKey , serverCtx )
2020-08-17 14:32:16 -07:00
cmd := genutilcli . InitCmd ( testMbm , home )
2020-07-17 07:20:31 -07:00
cmd . SetArgs (
tt . flags ( home ) ,
)
if tt . shouldErr {
err := cmd . ExecuteContext ( ctx )
require . EqualError ( t , err , tt . err . Error ( ) )
} else {
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
}
} )
}
2020-07-07 08:40:46 -07:00
2018-10-10 15:45:41 -07:00
}
2020-12-08 09:22:43 -08:00
func TestInitRecover ( t * testing . T ) {
home := t . TempDir ( )
logger := log . NewNopLogger ( )
cfg , err := genutiltest . CreateDefaultTendermintConfig ( home )
require . NoError ( t , err )
serverCtx := server . NewContext ( viper . New ( ) , cfg , logger )
interfaceRegistry := types . NewInterfaceRegistry ( )
marshaler := codec . NewProtoCodec ( interfaceRegistry )
clientCtx := client . Context { } .
2021-05-03 07:45:47 -07:00
WithJSONCodec ( marshaler ) .
2020-12-08 09:22:43 -08:00
WithLegacyAmino ( makeCodec ( ) ) .
WithHomeDir ( home )
ctx := context . Background ( )
ctx = context . WithValue ( ctx , client . ClientContextKey , & clientCtx )
ctx = context . WithValue ( ctx , server . ServerContextKey , serverCtx )
cmd := genutilcli . InitCmd ( testMbm , home )
mockIn := testutil . ApplyMockIODiscardOutErr ( cmd )
cmd . SetArgs ( [ ] string {
"appnode-test" ,
fmt . Sprintf ( "--%s=true" , genutilcli . FlagRecover ) ,
} )
// use valid mnemonic and complete recovery key generation successfully
mockIn . Reset ( "decide praise business actor peasant farm drastic weather extend front hurt later song give verb rhythm worry fun pond reform school tumble august one\n" )
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
}
2018-10-10 15:45:41 -07:00
func TestEmptyState ( t * testing . T ) {
2020-09-18 04:08:24 -07:00
home := t . TempDir ( )
2018-10-10 15:45:41 -07:00
logger := log . NewNopLogger ( )
2020-08-17 14:32:16 -07:00
cfg , err := genutiltest . CreateDefaultTendermintConfig ( home )
2020-07-07 08:40:46 -07:00
require . NoError ( t , err )
serverCtx := server . NewContext ( viper . New ( ) , cfg , logger )
2020-08-26 02:39:38 -07:00
interfaceRegistry := types . NewInterfaceRegistry ( )
marshaler := codec . NewProtoCodec ( interfaceRegistry )
clientCtx := client . Context { } .
2021-05-03 07:45:47 -07:00
WithJSONCodec ( marshaler ) .
2020-08-26 02:39:38 -07:00
WithLegacyAmino ( makeCodec ( ) ) .
WithHomeDir ( home )
2018-11-12 05:45:05 -08:00
2020-07-07 08:40:46 -07:00
ctx := context . Background ( )
ctx = context . WithValue ( ctx , client . ClientContextKey , & clientCtx )
ctx = context . WithValue ( ctx , server . ServerContextKey , serverCtx )
2018-11-12 05:45:05 -08:00
2020-08-17 14:32:16 -07:00
cmd := genutilcli . InitCmd ( testMbm , home )
2020-07-07 08:40:46 -07:00
cmd . SetArgs ( [ ] string { "appnode-test" , fmt . Sprintf ( "--%s=%s" , cli . HomeFlag , home ) } )
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2018-10-10 15:45:41 -07:00
old := os . Stdout
r , w , _ := os . Pipe ( )
os . Stdout = w
2018-11-12 05:45:05 -08:00
2020-07-20 08:12:33 -07:00
cmd = server . ExportCmd ( nil , home )
2020-07-07 08:40:46 -07:00
cmd . SetArgs ( [ ] string { fmt . Sprintf ( "--%s=%s" , cli . HomeFlag , home ) } )
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2018-10-10 15:45:41 -07:00
outC := make ( chan string )
go func ( ) {
var buf bytes . Buffer
io . Copy ( & buf , r )
outC <- buf . String ( )
} ( )
w . Close ( )
os . Stdout = old
out := <- outC
2019-05-16 08:25:32 -07:00
2018-10-10 15:45:41 -07:00
require . Contains ( t , out , "genesis_time" )
require . Contains ( t , out , "chain_id" )
require . Contains ( t , out , "consensus_params" )
require . Contains ( t , out , "app_hash" )
2019-05-16 08:25:32 -07:00
require . Contains ( t , out , "app_state" )
2018-10-10 15:45:41 -07:00
}
func TestStartStandAlone ( t * testing . T ) {
2020-09-18 04:08:24 -07:00
home := t . TempDir ( )
2018-10-10 15:45:41 -07:00
logger := log . NewNopLogger ( )
2020-08-26 02:39:38 -07:00
interfaceRegistry := types . NewInterfaceRegistry ( )
marshaler := codec . NewProtoCodec ( interfaceRegistry )
err := genutiltest . ExecInitCmd ( testMbm , home , marshaler )
2020-07-07 08:40:46 -07:00
require . NoError ( t , err )
2018-10-10 15:45:41 -07:00
app , err := mock . NewApp ( home , logger )
2020-07-07 08:40:46 -07:00
require . NoError ( t , err )
2018-10-10 15:45:41 -07:00
svrAddr , _ , err := server . FreeTCPAddr ( )
2020-07-07 08:40:46 -07:00
require . NoError ( t , err )
svr , err := abci_server . NewServer ( svrAddr , "socket" , app )
require . NoError ( t , err , "error creating listener" )
2018-10-10 15:45:41 -07:00
svr . SetLogger ( logger . With ( "module" , "abci-server" ) )
2020-03-20 12:24:18 -07:00
err = svr . Start ( )
require . NoError ( t , err )
2018-10-10 15:45:41 -07:00
timer := time . NewTimer ( time . Duration ( 2 ) * time . Second )
2019-09-16 10:11:17 -07:00
for range timer . C {
2020-03-20 12:24:18 -07:00
err = svr . Stop ( )
require . NoError ( t , err )
2019-09-16 10:11:17 -07:00
break
2018-10-10 15:45:41 -07:00
}
}
2018-10-19 11:00:27 -07:00
func TestInitNodeValidatorFiles ( t * testing . T ) {
2020-09-18 04:08:24 -07:00
home := t . TempDir ( )
2020-08-17 14:32:16 -07:00
cfg , err := genutiltest . CreateDefaultTendermintConfig ( home )
2019-05-16 08:25:32 -07:00
nodeID , valPubKey , err := genutil . InitializeNodeValidatorFiles ( cfg )
2020-09-18 04:08:24 -07:00
2018-10-19 11:00:27 -07:00
require . Nil ( t , err )
require . NotEqual ( t , "" , nodeID )
require . NotEqual ( t , 0 , len ( valPubKey . Bytes ( ) ) )
}
2019-05-16 18:34:54 -07:00
// custom tx codec
2020-08-10 12:41:21 -07:00
func makeCodec ( ) * codec . LegacyAmino {
2020-09-07 07:47:12 -07:00
var cdc = codec . NewLegacyAmino ( )
sdk . RegisterLegacyAminoCodec ( cdc )
2020-06-04 03:38:24 -07:00
cryptocodec . RegisterCrypto ( cdc )
2019-05-16 18:34:54 -07:00
return cdc
}