Skip to content

Commit ef8e2d4

Browse files
hieuvubkalpe
andauthored
test(server/v2): Add system-test for store's command (#21357)
Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>
1 parent 4666e1d commit ef8e2d4

File tree

6 files changed

+146
-5
lines changed

6 files changed

+146
-5
lines changed

store/snapshots/manager.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,11 @@ func (m *Manager) doRestoreSnapshot(snapshot types.Snapshot, chChunks <-chan io.
388388
return errorsmod.Wrapf(err, "extension %s restore", metadata.Name)
389389
}
390390

391-
if nextItem.GetExtensionPayload() != nil {
391+
payload := nextItem.GetExtensionPayload()
392+
if payload != nil && len(payload.Payload) != 0 {
392393
return fmt.Errorf("extension %s don't exhausted payload stream", metadata.Name)
394+
} else {
395+
break
393396
}
394397
}
395398
return nil

store/v2/snapshots/manager.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,11 @@ func (m *Manager) doRestoreSnapshot(snapshot types.Snapshot, chChunks <-chan io.
437437
return errorsmod.Wrapf(err, "extension %s restore", metadata.Name)
438438
}
439439

440-
if nextItem.GetExtensionPayload() != nil {
440+
payload := nextItem.GetExtensionPayload()
441+
if payload != nil && len(payload.Payload) != 0 {
441442
return fmt.Errorf("extension %s don't exhausted payload stream", metadata.Name)
443+
} else {
444+
break
442445
}
443446
}
444447

tests/systemtests/cli.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ func (c CLIWrapper) RunAndWait(args ...string) string {
179179
return txResult
180180
}
181181

182+
// RunCommandWithArgs use for run cli command, not tx
183+
func (c CLIWrapper) RunCommandWithArgs(args ...string) string {
184+
c.t.Helper()
185+
execOutput, _ := c.run(args)
186+
return execOutput
187+
}
188+
182189
// AwaitTxCommitted wait for tx committed on chain
183190
// returns the server execution result and true when found within 3 blocks.
184191
func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//go:build system_test
2+
3+
package systemtests
4+
5+
import (
6+
"fmt"
7+
"github.com/stretchr/testify/require"
8+
"os"
9+
"testing"
10+
)
11+
12+
func TestSnapshots(t *testing.T) {
13+
14+
sut.ResetChain(t)
15+
cli := NewCLIWrapper(t, sut, verbose)
16+
sut.StartChain(t)
17+
18+
// Wait for chain produce some blocks
19+
sut.AwaitNBlocks(t, 6)
20+
// Stop all nodes
21+
sut.StopChain()
22+
23+
var (
24+
command string
25+
restoreableDirs []string
26+
)
27+
node0Dir := sut.NodeDir(0)
28+
if isV2() {
29+
command = "store"
30+
restoreableDirs = []string{fmt.Sprintf("%s/data/application.db", node0Dir), fmt.Sprintf("%s/data/ss", node0Dir)}
31+
} else {
32+
command = "snapshots"
33+
restoreableDirs = []string{fmt.Sprintf("%s/data/application.db", node0Dir)}
34+
}
35+
36+
// export snapshot at height 5
37+
res := cli.RunCommandWithArgs(command, "export", "--height=5", fmt.Sprintf("--home=%s", node0Dir))
38+
require.Contains(t, res, "Snapshot created at height 5")
39+
require.DirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", node0Dir))
40+
41+
// Check snapshots list
42+
res = cli.RunCommandWithArgs(command, "list", fmt.Sprintf("--home=%s", node0Dir))
43+
require.Contains(t, res, "height: 5")
44+
45+
// Dump snapshot
46+
res = cli.RunCommandWithArgs(command, "dump", "5", "3", fmt.Sprintf("--home=%s", node0Dir), fmt.Sprintf("--output=%s/5-3.tar.gz", node0Dir))
47+
// Check if output file exist
48+
require.FileExists(t, fmt.Sprintf("%s/5-3.tar.gz", node0Dir))
49+
50+
// Delete snapshots
51+
res = cli.RunCommandWithArgs(command, "delete", "5", "3", fmt.Sprintf("--home=%s", node0Dir))
52+
require.NoDirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", node0Dir))
53+
54+
// Load snapshot from file
55+
res = cli.RunCommandWithArgs(command, "load", fmt.Sprintf("%s/5-3.tar.gz", node0Dir), fmt.Sprintf("--home=%s", node0Dir))
56+
require.DirExists(t, fmt.Sprintf("%s/data/snapshots/5/3", node0Dir))
57+
58+
// Restore from snapshots
59+
for _, dir := range restoreableDirs {
60+
require.NoError(t, os.RemoveAll(dir))
61+
}
62+
// Remove database
63+
err := os.RemoveAll(fmt.Sprintf("%s/data/application.db", node0Dir))
64+
require.NoError(t, err)
65+
if isV2() {
66+
require.NoError(t, os.RemoveAll(fmt.Sprintf("%s/data/ss", node0Dir)))
67+
}
68+
69+
res = cli.RunCommandWithArgs(command, "restore", "5", "3", fmt.Sprintf("--home=%s", node0Dir))
70+
for _, dir := range restoreableDirs {
71+
require.DirExists(t, dir)
72+
}
73+
}
74+
75+
func TestPrune(t *testing.T) {
76+
sut.ResetChain(t)
77+
cli := NewCLIWrapper(t, sut, verbose)
78+
79+
sut.StartChain(t)
80+
81+
// Wait for chain produce some blocks
82+
sut.AwaitNBlocks(t, 6)
83+
84+
// Stop all nodes
85+
sut.StopChain()
86+
87+
node0Dir := sut.NodeDir(0)
88+
89+
// prune
90+
var command []string
91+
if isV2() {
92+
command = []string{"store", "prune", "--keep-recent=1"}
93+
} else {
94+
command = []string{"prune", "everything"}
95+
}
96+
res := cli.RunCommandWithArgs(append(command, fmt.Sprintf("--home=%s", node0Dir))...)
97+
require.Contains(t, res, "successfully pruned the application root multi stores")
98+
}

tests/systemtests/system.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ func (s *SystemUnderTest) PrintBuffer() {
360360
})
361361
}
362362

363-
// AwaitBlockHeight blocks until te target height is reached. An optional timeout parameter can be passed to abort early
363+
// AwaitNBlocks blocks until the current height + n block is reached. An optional timeout parameter can be passed to abort early
364+
func (s *SystemUnderTest) AwaitNBlocks(t *testing.T, n int64, timeout ...time.Duration) {
365+
t.Helper()
366+
s.AwaitBlockHeight(t, s.CurrentHeight()+n, timeout...)
367+
}
368+
369+
// AwaitBlockHeight blocks until the target height is reached. An optional timeout parameter can be passed to abort early
364370
func (s *SystemUnderTest) AwaitBlockHeight(t *testing.T, targetHeight int64, timeout ...time.Duration) {
365371
t.Helper()
366372
require.Greater(t, targetHeight, s.currentHeight.Load())
@@ -577,6 +583,7 @@ func (s *SystemUnderTest) startNodesAsync(t *testing.T, xargs ...string) {
577583
})
578584
}
579585

586+
// tracks the PID in state with a go routine waiting for the shutdown completion to unregister
580587
func (s *SystemUnderTest) awaitProcessCleanup(cmd *exec.Cmd) {
581588
pid := cmd.Process.Pid
582589
s.pidsLock.Lock()
@@ -597,6 +604,11 @@ func (s *SystemUnderTest) withEachNodeHome(cb func(i int, home string)) {
597604
}
598605
}
599606

607+
// NodeDir returns the workdir and path to the node home folder.
608+
func (s *SystemUnderTest) NodeDir(i int) string {
609+
return filepath.Join(WorkDir, s.nodePath(i))
610+
}
611+
600612
// nodePath returns the path of the node within the work dir. not absolute
601613
func (s *SystemUnderTest) nodePath(i int) string {
602614
return NodePath(i, s.outputDir, s.projectName)

tests/systemtests/testnet_init.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
"github.com/creachadair/tomledit/parser"
1414
)
1515

16+
// isV2 checks if the tests run with simapp v2
17+
func isV2() bool {
18+
buildOptions := os.Getenv("COSMOS_BUILD_OPTIONS")
19+
return strings.Contains(buildOptions, "v2")
20+
}
21+
1622
// SingleHostTestnetCmdInitializer default testnet cmd that supports the --single-host param
1723
type SingleHostTestnetCmdInitializer struct {
1824
execBinary string
@@ -53,10 +59,16 @@ func (s SingleHostTestnetCmdInitializer) Initialize() {
5359
"--output-dir=" + s.outputDir,
5460
"--validator-count=" + strconv.Itoa(s.initialNodesCount),
5561
"--keyring-backend=test",
56-
"--minimum-gas-prices=" + s.minGasPrice,
5762
"--commit-timeout=" + s.commitTimeout.String(),
5863
"--single-host",
5964
}
65+
66+
if isV2() {
67+
args = append(args, "--server.minimum-gas-prices="+s.minGasPrice)
68+
} else {
69+
args = append(args, "--minimum-gas-prices="+s.minGasPrice)
70+
}
71+
6072
s.log(fmt.Sprintf("+++ %s %s\n", s.execBinary, strings.Join(args, " ")))
6173
out, err := RunShellCmd(s.execBinary, args...)
6274
if err != nil {
@@ -108,8 +120,14 @@ func (s ModifyConfigYamlInitializer) Initialize() {
108120
"--output-dir=" + s.outputDir,
109121
"--v=" + strconv.Itoa(s.initialNodesCount),
110122
"--keyring-backend=test",
111-
"--minimum-gas-prices=" + s.minGasPrice,
112123
}
124+
125+
if isV2() {
126+
args = append(args, "--server.minimum-gas-prices="+s.minGasPrice)
127+
} else {
128+
args = append(args, "--minimum-gas-prices="+s.minGasPrice)
129+
}
130+
113131
s.log(fmt.Sprintf("+++ %s %s\n", s.execBinary, strings.Join(args, " ")))
114132

115133
out, err := RunShellCmd(s.execBinary, args...)

0 commit comments

Comments
 (0)