## Description This PR introduces the `cmdtest` package, offering a lightweight wrapper around cobra commands to simplify testing CLI utilities. I backfilled tests for the `version` command, which was an example of a very simple test setup; and for the `export` command, which was more involved due to the server and client context requirements. I did notice that there are some existing tests for some utilities, but the `cmdtest` package follows a simple pattern that has been easy to use successfully in [the relayer](https://github.com/cosmos/relayer/blob/main/internal/relayertest/system.go) and in other projects outside the Cosmos ecosystem. While filling in these tests, I started removing uses of `cmd.Print`, as that is the root cause of issues like #8498, #7964, #15167, and possibly others. Internal to cobra, the print family of methods write to `cmd.OutOrStderr()` -- meaning that if the authors call `cmd.SetOutput()` before executing the command, the output will be written to stdout as expected; otherwise it will go to stderr. I don't understand why that would be the default behavior, but it is probably too late to change from cobra's side. Instead of `cmd.Print`, we prefer to `fmt.Fprint(cmd.OutOrStdout())` or `fmt.Fprint(cmd.ErrOrStderr())` as appropriate, giving an unambiguous destination for output. And the new tests collect those outputs in plain `bytes.Buffer` values so that we can assert their content appropriately. In the longer term, I would like to deprecate and eventually remove the `testutil` package's `ApplyMockIO` method and its `BufferWriter` and `BufferReader` types, as they are unnecessary indirection when a simpler solution exists. But that can wait until `cmdtest` has propagated through the codebase more. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] ~~provided a link to the relevant issue or specification~~ - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] confirmed that this PR does not change production code
122 lines
3.7 KiB
Go
122 lines
3.7 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
|
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
|
|
)
|
|
|
|
const (
|
|
FlagHeight = "height"
|
|
FlagForZeroHeight = "for-zero-height"
|
|
FlagJailAllowedAddrs = "jail-allowed-addrs"
|
|
FlagModulesToExport = "modules-to-export"
|
|
)
|
|
|
|
// ExportCmd dumps app state to JSON.
|
|
func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "export",
|
|
Short: "Export state to JSON",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
serverCtx := GetServerContextFromCmd(cmd)
|
|
config := serverCtx.Config
|
|
|
|
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
|
|
config.SetRoot(homeDir)
|
|
|
|
if _, err := os.Stat(config.GenesisFile()); os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
db, err := openDB(config.RootDir, GetAppDBBackend(serverCtx.Viper))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if appExporter == nil {
|
|
if _, err := fmt.Fprintln(cmd.ErrOrStderr(), "WARNING: App exporter not defined. Returning genesis file."); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Open file in read-only mode so we can copy it to stdout.
|
|
// It is possible that the genesis file is large,
|
|
// so we don't need to read it all into memory
|
|
// before we stream it out.
|
|
f, err := os.OpenFile(config.GenesisFile(), os.O_RDONLY, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
if _, err := io.Copy(cmd.OutOrStdout(), f); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
traceWriterFile, _ := cmd.Flags().GetString(flagTraceStore)
|
|
traceWriter, err := openTraceWriter(traceWriterFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
height, _ := cmd.Flags().GetInt64(FlagHeight)
|
|
forZeroHeight, _ := cmd.Flags().GetBool(FlagForZeroHeight)
|
|
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs)
|
|
modulesToExport, _ := cmd.Flags().GetStringSlice(FlagModulesToExport)
|
|
outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
|
|
|
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport)
|
|
if err != nil {
|
|
return fmt.Errorf("error exporting state: %w", err)
|
|
}
|
|
|
|
appGenesis, err := genutiltypes.AppGenesisFromFile(serverCtx.Config.GenesisFile())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
appGenesis.AppState = exported.AppState
|
|
appGenesis.InitialHeight = exported.Height
|
|
appGenesis.Consensus = genutiltypes.NewConsensusGenesis(exported.ConsensusParams, exported.Validators)
|
|
|
|
out, err := json.Marshal(appGenesis)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if outputDocument == "" {
|
|
// Copy the entire genesis file to stdout.
|
|
_, err := io.Copy(cmd.OutOrStdout(), bytes.NewReader(out))
|
|
return err
|
|
}
|
|
|
|
if err = appGenesis.SaveAs(outputDocument); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
|
|
cmd.Flags().Int64(FlagHeight, -1, "Export state from a particular height (-1 means latest height)")
|
|
cmd.Flags().Bool(FlagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
|
|
cmd.Flags().StringSlice(FlagJailAllowedAddrs, []string{}, "Comma-separated list of operator addresses of jailed validators to unjail")
|
|
cmd.Flags().StringSlice(FlagModulesToExport, []string{}, "Comma-separated list of modules to export. If empty, will export all modules")
|
|
cmd.Flags().String(flags.FlagOutputDocument, "", "Exported state is written to the given file instead of STDOUT")
|
|
|
|
return cmd
|
|
}
|