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

test(server/v2): Add system-test for store's command #21357

Merged
merged 19 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 18 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
5 changes: 4 additions & 1 deletion store/snapshots/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,11 @@ func (m *Manager) doRestoreSnapshot(snapshot types.Snapshot, chChunks <-chan io.
return errorsmod.Wrapf(err, "extension %s restore", metadata.Name)
}

if nextItem.GetExtensionPayload() != nil {
payload := nextItem.GetExtensionPayload()
if payload != nil && len(payload.Payload) != 0 {
return fmt.Errorf("extension %s don't exhausted payload stream", metadata.Name)
} else {
break
}
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion store/v2/snapshots/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,11 @@ func (m *Manager) doRestoreSnapshot(snapshot types.Snapshot, chChunks <-chan io.
return errorsmod.Wrapf(err, "extension %s restore", metadata.Name)
}

if nextItem.GetExtensionPayload() != nil {
payload := nextItem.GetExtensionPayload()
if payload != nil && len(payload.Payload) != 0 {
return fmt.Errorf("extension %s don't exhausted payload stream", metadata.Name)
} else {
break
Comment on lines +440 to +444
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar correction needed in error message.

The error message in the condition check uses incorrect grammar. It should be corrected to maintain professionalism and clarity in the codebase. Here's the suggested correction:

- return fmt.Errorf("extension %s don't exhausted payload stream", metadata.Name)
+ return fmt.Errorf("extension %s has not exhausted the payload stream", metadata.Name)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
payload := nextItem.GetExtensionPayload()
if payload != nil && len(payload.Payload) != 0 {
return fmt.Errorf("extension %s don't exhausted payload stream", metadata.Name)
} else {
break
payload := nextItem.GetExtensionPayload()
if payload != nil && len(payload.Payload) != 0 {
return fmt.Errorf("extension %s has not exhausted the payload stream", metadata.Name)
} else {
break

}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/systemtests/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ func (c CLIWrapper) RunAndWait(args ...string) string {
return txResult
}

// RunCommandWithArgs use for run cli command, not tx
func (c CLIWrapper) RunCommandWithArgs(args ...string) string {
c.t.Helper()
execOutput, _ := c.run(args)
return execOutput
}

// AwaitTxCommitted wait for tx committed on chain
// returns the server execution result and true when found within 3 blocks.
func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
Expand Down
96 changes: 96 additions & 0 deletions tests/systemtests/snapshots_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build system_test

package systemtests

import (
"fmt"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestSnapshotsV1(t *testing.T) {
if isV2() {
t.Skip()
}

sut.ResetChain(t)
cli := NewCLIWrapper(t, sut, verbose)
// add genesis account with some tokens
account1Addr := cli.AddKey("account1")
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", account1Addr, "10000000stake"},
)

sut.StartChain(t)
nodeDir := filepath.Join(WorkDir, "testnet", "node0", "simd")

// Wait for chain produce some blocks
time.Sleep(time.Second * 10)
// Stop 1 node
err := sut.StopSingleNode()
require.NoError(t, err)
time.Sleep(time.Second * 5)

// export snapshot at height 5
res := cli.RunCommandWithArgs("snapshots", "export", "--height=5", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "Snapshot created at height 5")
require.DirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", nodeDir))

// Check snapshots list
res = cli.RunCommandWithArgs("snapshots", "list", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "height: 5")

// Dump snapshot
res = cli.RunCommandWithArgs("snapshots", "dump", "5", "3", fmt.Sprintf("--home=%s", nodeDir), fmt.Sprintf("--output=%s/5-3.tar.gz", nodeDir))
// Check if output file exist
require.FileExists(t, fmt.Sprintf("%s/5-3.tar.gz", nodeDir))

// Delete snapshots
res = cli.RunCommandWithArgs("snapshots", "delete", "5", "3", fmt.Sprintf("--home=%s", nodeDir))
require.NoDirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", nodeDir))

// Load snapshot from file
res = cli.RunCommandWithArgs("snapshots", "load", fmt.Sprintf("%s/5-3.tar.gz", nodeDir), fmt.Sprintf("--home=%s", nodeDir))
require.DirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", nodeDir))

// Restore from snapshots

// Remove database
err = os.RemoveAll(fmt.Sprintf("%s/data/application.db", nodeDir))
require.NoError(t, err)

res = cli.RunCommandWithArgs("snapshots", "restore", "5", "3", fmt.Sprintf("--home=%s", nodeDir))
require.DirExists(t, fmt.Sprintf("%s/data/application.db", nodeDir))
}

func TestPruneV1(t *testing.T) {
if isV2() {
t.Skip()
}

sut.ResetChain(t)
cli := NewCLIWrapper(t, sut, verbose)
// add genesis account with some tokens
account1Addr := cli.AddKey("account1")
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", account1Addr, "10000000stake"},
)

sut.StartChain(t)
nodeDir := filepath.Join(WorkDir, "testnet", "node0", "simd")

// Wait for chain produce some blocks
time.Sleep(time.Second * 10)
// Stop 1 node
err := sut.StopSingleNode()
require.NoError(t, err)
time.Sleep(time.Second)

// prune
res := cli.RunCommandWithArgs("prune", "everything", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "successfully pruned the application root multi stores")
}
98 changes: 98 additions & 0 deletions tests/systemtests/snapshots_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//go:build system_test

package systemtests

import (
"fmt"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestSnapshotsV2(t *testing.T) {
if !isV2() {
t.Skip()
}

sut.ResetChain(t)
cli := NewCLIWrapper(t, sut, verbose)
// add genesis account with some tokens
account1Addr := cli.AddKey("account1")
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", account1Addr, "10000000stake"},
)

sut.StartChain(t)
nodeDir := filepath.Join(WorkDir, "testnet", "node0", "simdv2")

// Wait for chain produce some blocks
time.Sleep(time.Second * 10)
// Stop 1 node
err := sut.StopSingleNode()
require.NoError(t, err)
time.Sleep(time.Second * 5)

// export snapshot at height 5
res := cli.RunCommandWithArgs("store", "export", "--height=5", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "Snapshot created at height 5")
require.DirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", nodeDir))

// Check snapshots list
res = cli.RunCommandWithArgs("store", "list", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "height: 5")

// Dump snapshot
res = cli.RunCommandWithArgs("store", "dump", "5", "3", fmt.Sprintf("--home=%s", nodeDir), fmt.Sprintf("--output=%s/5-3.tar.gz", nodeDir))
// Check if output file exist
require.FileExists(t, fmt.Sprintf("%s/5-3.tar.gz", nodeDir))

// Delete snapshots
res = cli.RunCommandWithArgs("store", "delete", "5", "3", fmt.Sprintf("--home=%s", nodeDir))
require.NoDirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", nodeDir))

// Load snapshot from file
res = cli.RunCommandWithArgs("store", "load", fmt.Sprintf("%s/5-3.tar.gz", nodeDir), fmt.Sprintf("--home=%s", nodeDir))
require.DirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", nodeDir))

// Restore from snapshots

// Remove database
err = os.RemoveAll(fmt.Sprintf("%s/data/application.db", nodeDir))
require.NoError(t, err)
err = os.RemoveAll(fmt.Sprintf("%s/data/ss", nodeDir))
require.NoError(t, err)

res = cli.RunCommandWithArgs("store", "restore", "5", "3", fmt.Sprintf("--home=%s", nodeDir))
require.DirExists(t, fmt.Sprintf("%s/data/application.db", nodeDir))
require.DirExists(t, fmt.Sprintf("%s/data/ss", nodeDir))
}

func TestPruneV2(t *testing.T) {
if !isV2() {
t.Skip()
}
sut.ResetChain(t)
cli := NewCLIWrapper(t, sut, verbose)
// add genesis account with some tokens
account1Addr := cli.AddKey("account1")
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", account1Addr, "10000000stake"},
)

sut.StartChain(t)
nodeDir := filepath.Join(WorkDir, "testnet", "node0", "simdv2")

// Wait for chain produce some blocks
time.Sleep(time.Second * 10)
// Stop 1 node
err := sut.StopSingleNode()
require.NoError(t, err)
time.Sleep(time.Second)

// prune
res := cli.RunCommandWithArgs("store", "prune", "--keep-recent=1", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "successfully pruned the application root multi stores")
}
38 changes: 38 additions & 0 deletions tests/systemtests/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"sync/atomic"
"syscall"
Expand Down Expand Up @@ -331,6 +332,43 @@ func (s *SystemUnderTest) StopChain() {
s.ChainStarted = false
}

// StopSingleNode stops a validator node without stop the chain running
func (s *SystemUnderTest) StopSingleNode() error {
if !s.ChainStarted {
return nil
}

s.pidsLock.RLock()
pids := maps.Keys(s.pids)
Fixed Show fixed Hide fixed
s.pidsLock.RUnlock()

var intPids []int
for pid := range pids {
intPids = append(intPids, pid)
}

sort.Ints(intPids)
p, err := os.FindProcess(intPids[0])
if err != nil {
return err
}

// Stop the 1st node
return p.Signal(syscall.SIGTERM)
}

// StartSingleNode start running a validator node with dir input
func (s *SystemUnderTest) StartSingleNode(t *testing.T, dir string) {
t.Helper()
cmd := exec.Command( //nolint:gosec // used by tests only
locateExecutable(s.execBinary),
[]string{"start", "--log_level=info", "--log_no_color"}...,
)
cmd.Dir = WorkDir
err := cmd.Start()
require.NoError(t, err)
}

func (s *SystemUnderTest) withEachPid(cb func(p *os.Process)) {
s.pidsLock.RLock()
pids := maps.Keys(s.pids)
Expand Down
22 changes: 20 additions & 2 deletions tests/systemtests/testnet_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/creachadair/tomledit/parser"
)

// isV2 checks if the tests run with simapp v2
func isV2() bool {
buildOptions := os.Getenv("COSMOS_BUILD_OPTIONS")
return strings.Contains(buildOptions, "v2")
}

// SingleHostTestnetCmdInitializer default testnet cmd that supports the --single-host param
type SingleHostTestnetCmdInitializer struct {
execBinary string
Expand Down Expand Up @@ -53,10 +59,16 @@ func (s SingleHostTestnetCmdInitializer) Initialize() {
"--output-dir=" + s.outputDir,
"--validator-count=" + strconv.Itoa(s.initialNodesCount),
"--keyring-backend=test",
"--minimum-gas-prices=" + s.minGasPrice,
"--commit-timeout=" + s.commitTimeout.String(),
"--single-host",
}

if isV2() {
args = append(args, "--server.minimum-gas-prices="+s.minGasPrice)
} else {
args = append(args, "--minimum-gas-prices="+s.minGasPrice)
}

s.log(fmt.Sprintf("+++ %s %s\n", s.execBinary, strings.Join(args, " ")))
out, err := RunShellCmd(s.execBinary, args...)
if err != nil {
Expand Down Expand Up @@ -108,8 +120,14 @@ func (s ModifyConfigYamlInitializer) Initialize() {
"--output-dir=" + s.outputDir,
"--v=" + strconv.Itoa(s.initialNodesCount),
"--keyring-backend=test",
"--minimum-gas-prices=" + s.minGasPrice,
}

if isV2() {
args = append(args, "--server.minimum-gas-prices="+s.minGasPrice)
} else {
args = append(args, "--minimum-gas-prices="+s.minGasPrice)
}

s.log(fmt.Sprintf("+++ %s %s\n", s.execBinary, strings.Join(args, " ")))

out, err := RunShellCmd(s.execBinary, args...)
Expand Down
Loading