Skip to content

Commit

Permalink
logger
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 25, 2023
1 parent bd1c8d7 commit 7b21f7e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
3 changes: 1 addition & 2 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/math/unsafe"
"cosmossdk.io/simapp"
Expand Down Expand Up @@ -516,7 +515,7 @@ func startTestnet(cmd *cobra.Command, args startArgs) error {
networkConfig.APIAddress = args.apiAddress
networkConfig.GRPCAddress = args.grpcAddress
networkConfig.PrintMnemonic = args.printMnemonic
networkLogger := log.NewLogger(cmd.OutOrStdout()).With(log.ModuleKey, "network")
networkLogger := network.NewCLILogger(cmd)

baseDir := fmt.Sprintf("%s/%s", args.outputDir, networkConfig.ChainID)
if _, err := os.Stat(baseDir); !os.IsNotExist(err) {
Expand Down
4 changes: 1 addition & 3 deletions simapp/testutil_network_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package simapp_test

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/suite"

"cosmossdk.io/log"
"cosmossdk.io/simapp"

"github.com/cosmos/cosmos-sdk/testutil/network"
Expand All @@ -23,7 +21,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

var err error
s.network, err = network.New(log.NewLogger(os.Stdout).With(log.ModuleKey, "network"), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.Require().NoError(err)

h, err := s.network.WaitForHeight(1)
Expand Down
66 changes: 46 additions & 20 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"strings"
"sync"
"syscall"
"testing"
"time"

"github.com/cometbft/cometbft/node"
cmtclient "github.com/cometbft/cometbft/rpc/client"
dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"

Expand Down Expand Up @@ -265,7 +267,7 @@ type (
// to create networks. In addition, only the first validator will have a valid
// RPC and API server/client.
Network struct {
Logger log.Logger
Logger Logger
BaseDir string
Validators []*Validator

Expand Down Expand Up @@ -313,7 +315,11 @@ type (
}
)

var _ ValidatorI = Validator{}
var (
_ Logger = (*testing.T)(nil)
_ Logger = (*CLILogger)(nil)
_ ValidatorI = Validator{}
)

func (v Validator) GetCtx() *server.Context {
return v.Ctx
Expand All @@ -323,10 +329,30 @@ func (v Validator) GetAppConfig() *srvconfig.Config {
return v.AppConfig
}

// CLILogger wraps a cobra.Command and provides command logging methods.
type CLILogger struct {
cmd *cobra.Command
}

// Log logs given args.
func (s CLILogger) Log(args ...interface{}) {
s.cmd.Println(args...)
}

// Logf logs given args according to a format specifier.
func (s CLILogger) Logf(format string, args ...interface{}) {
s.cmd.Printf(format, args...)
}

// NewCLILogger creates a new CLILogger.
func NewCLILogger(cmd *cobra.Command) CLILogger {
return CLILogger{cmd}
}

// New creates a new Network for integration tests or in-process testnets run via the CLI
func New(l log.Logger, baseDir string, cfg Config) (*Network, error) {
func New(l Logger, baseDir string, cfg Config) (*Network, error) {
// only one caller/test can create and use a network at a time
l.Info("acquiring test network lock")
l.Log("acquiring test network lock")
lock.Lock()

network := &Network{
Expand All @@ -336,7 +362,7 @@ func New(l log.Logger, baseDir string, cfg Config) (*Network, error) {
Config: cfg,
}

l.Info(fmt.Sprintf("preparing test network with chain-id \"%s\"\n", cfg.ChainID))
l.Logf("preparing test network with chain-id \"%s\"\n", cfg.ChainID)

monikers := make([]string, cfg.NumValidators)
nodeIDs := make([]string, cfg.NumValidators)
Expand Down Expand Up @@ -609,20 +635,20 @@ func New(l log.Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}

l.Info("starting test network...")
l.Log("starting test network...")
for idx, v := range network.Validators {
if err := startInProcess(cfg, v); err != nil {
return nil, err
}
l.Info("started validator", "validator", idx)
l.Log("started validator", "validator", idx)
}

height, err := network.LatestHeight()
if err != nil {
return nil, err
}

l.Info(fmt.Sprintf("started test network at height: %d", height))
l.Log(fmt.Sprintf("started test network at height: %d", height))

// Ensure we cleanup incase any test was abruptly halted (e.g. SIGINT) as any
// defer in a test would not be called.
Expand Down Expand Up @@ -779,23 +805,23 @@ func (n *Network) WaitForNextBlock() error {
func (n *Network) Cleanup() {
defer func() {
lock.Unlock()
n.Logger.Info("released test network lock")
n.Logger.Log("released test network lock")
}()

n.Logger.Info("cleaning up test network...")
n.Logger.Log("cleaning up test network...")

for _, v := range n.Validators {
// cancel the validator's context which will signal to the gRPC and API
// goroutines that they should gracefully exit.
v.cancelFn()

if err := v.errGroup.Wait(); err != nil {
n.Logger.Info("unexpected error waiting for validator gRPC and API processes to exit", "err", err)
n.Logger.Log("unexpected error waiting for validator gRPC and API processes to exit", "err", err)
}

if v.tmNode != nil && v.tmNode.IsRunning() {
if err := v.tmNode.Stop(); err != nil {
n.Logger.Info("failed to stop validator CometBFT node", "err", err)
n.Logger.Log("failed to stop validator CometBFT node", "err", err)
}
}

Expand All @@ -805,7 +831,7 @@ func (n *Network) Cleanup() {

if v.app != nil {
if err := v.app.Close(); err != nil {
n.Logger.Info("failed to stop validator ABCI application", "err", err)
n.Logger.Log("failed to stop validator ABCI application", "err", err)
}
}
}
Expand All @@ -816,12 +842,12 @@ func (n *Network) Cleanup() {
_ = os.RemoveAll(n.BaseDir)
}

n.Logger.Info("finished cleaning up test network")
n.Logger.Log("finished cleaning up test network")
}

// printMnemonic prints a provided mnemonic seed phrase on a network logger
// for debugging and manual testing
func printMnemonic(l log.Logger, secret string) {
func printMnemonic(l Logger, secret string) {
lines := []string{
"THIS MNEMONIC IS FOR TESTING PURPOSES ONLY",
"DO NOT USE IN PRODUCTION",
Expand All @@ -843,13 +869,13 @@ func printMnemonic(l log.Logger, secret string) {
}
}

l.Info("\n")
l.Info(strings.Repeat("+", maxLineLength+8))
l.Log("\n")
l.Log(strings.Repeat("+", maxLineLength+8))
for _, line := range lines {
l.Info(fmt.Sprintf("++ %s ++\n", centerText(line, maxLineLength)))
l.Log(fmt.Sprintf("++ %s ++\n", centerText(line, maxLineLength)))
}
l.Info(strings.Repeat("+", maxLineLength+8))
l.Info("\n")
l.Log(strings.Repeat("+", maxLineLength+8))
l.Log("\n")
}

// centerText centers text across a fixed width, filling either side with whitespace buffers
Expand Down

0 comments on commit 7b21f7e

Please sign in to comment.