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 5 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
7 changes: 7 additions & 0 deletions tests/systemtests/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ func (c CLIWrapper) Run(args ...string) string {
return rsp
}

// 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
func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
c.t.Helper()
Expand Down
121 changes: 121 additions & 0 deletions tests/systemtests/snapshots_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//go:build system_test
Copy link
Member

Choose a reason for hiding this comment

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

As the commands are really different between simapp and simapp v2, shall we not split the test in two tests?
The multiple if conditions will make it harder to maintain.

We can then just skip the tests when we run with COSMOS_BUILD_OPTIONS v2 and vice versa

Wdyt? cc @tac0turtle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should we update the prefix, most diff is that v2 has store prefix and v1 has snapshots?


package systemtests

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

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

func getBinaryNameAndPrefix(isSnapshot bool) (string, string) {
if sut.execBinary == filepath.Join(WorkDir, "binaries", "simd") {
if isSnapshot {
return "simd", "snapshots"
}
return "simd", ""
}
return "simdv2", "store"
}

func TestSnapshots(t *testing.T) {
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)

binary, snapshotPrefix := getBinaryNameAndPrefix(true)
nodeDir := fmt.Sprintf("./testnet/node0/%s", binary)

// 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)

// export snapshot at height 5
res := cli.RunCommandWithArgs(snapshotPrefix, "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(snapshotPrefix, "list", fmt.Sprintf("--home=%s", nodeDir))
require.Contains(t, res, "height: 5")

// Dump snapshot
res = cli.RunCommandWithArgs(snapshotPrefix, "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(snapshotPrefix, "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(snapshotPrefix, "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)

// Only v2 have ss database
if binary == "simdv2" {
err = os.RemoveAll(fmt.Sprintf("%s/data/ss", nodeDir))
require.NoError(t, err)
}

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

// Start the node
sut.StartSingleNode(t, nodeDir)
}

func TestPrune(t *testing.T) {
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)

binary, prefix := getBinaryNameAndPrefix(false)
nodeDir := fmt.Sprintf("./testnet/node0/%s", binary)

// 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
var res string
if binary == "simdv2" {
res = cli.RunCommandWithArgs(prefix, "prune", "--keep-recent=1", fmt.Sprintf("--home=%s", nodeDir))
} else {
res = cli.RunCommandWithArgs("prune", "everything", fmt.Sprintf("--home=%s", nodeDir))
}
require.Contains(t, res, "successfully pruned the application root multi stores")
// Start the node
sut.StartSingleNode(t, nodeDir)
}
31 changes: 31 additions & 0 deletions tests/systemtests/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,37 @@
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()

p, err := os.FindProcess(pids[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
Loading