Skip to content

Commit 04bedcd

Browse files
alpejulienrbrttac0turtle
authored
chore(systemtests): better support for app v2 (#21128)
Co-authored-by: Julien Robert <julien@rbrt.fr> Co-authored-by: Marko <marko@baricevic.me>
1 parent bc72e34 commit 04bedcd

File tree

7 files changed

+53
-39
lines changed

7 files changed

+53
-39
lines changed

scripts/build/testing.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test-all: test-unit test-e2e test-integration test-ledger-mock test-race
2929
.PHONY: test-system
3030
test-system: build
3131
mkdir -p ./tests/systemtests/binaries/
32-
cp $(BUILDDIR)/simd ./tests/systemtests/binaries/
32+
cp $(BUILDDIR)/simd$(if $(findstring v2,$(COSMOS_BUILD_OPTIONS)),v2) ./tests/systemtests/binaries/
3333
$(MAKE) -C tests/systemtests test
3434

3535

tests/systemtests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ WAIT_TIME ?= 45s
55
all: test
66

77
test:
8-
go test -mod=readonly -failfast -tags='system_test' ./... --wait-time=$(WAIT_TIME) --verbose
8+
go test -mod=readonly -failfast -tags='system_test' ./... --wait-time=$(WAIT_TIME) --verbose $(if $(findstring v2,$(COSMOS_BUILD_OPTIONS)),--binary=simdv2)
99

1010
format:
1111
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs gofumpt -w

tests/systemtests/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (c CLIWrapper) runWithInput(args []string, input io.Reader) (output string,
223223
cmd := exec.Command(locateExecutable(c.execBinary), args...) //nolint:gosec // test code only
224224
cmd.Dir = WorkDir
225225
cmd.Stdin = input
226-
return cmd.CombinedOutput()
226+
return cmd.Output()
227227
}()
228228
gotOut = filterProtoNoise(gotOut)
229229
ok = c.assertErrorFn(c.t, gotErr, string(gotOut))

tests/systemtests/fraud_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ func TestValidatorDoubleSign(t *testing.T) {
2929
t.Log(rsp)
3030
nodePowerBefore := QueryCometValidatorPowerForNode(t, sut, 0)
3131
require.NotEmpty(t, nodePowerBefore)
32+
t.Logf("nodePowerBefore: %v", nodePowerBefore)
3233

3334
var validatorPubKey cryptotypes.PubKey
34-
newNode := sut.AddFullnode(t, "0.001stake", func(nodeNumber int, nodePath string) {
35+
newNode := sut.AddFullnode(t, func(nodeNumber int, nodePath string) {
3536
valKeyFile := filepath.Join(WorkDir, nodePath, "config", "priv_validator_key.json")
3637
_ = os.Remove(valKeyFile)
3738
_, err := copyFile(filepath.Join(WorkDir, sut.nodePath(0), "config", "priv_validator_key.json"), valKeyFile)
@@ -54,9 +55,15 @@ func TestValidatorDoubleSign(t *testing.T) {
5455
// then comet status updated
5556
nodePowerAfter := QueryCometValidatorPowerForNode(t, sut, 0)
5657
require.Empty(t, nodePowerAfter)
58+
t.Logf("nodePowerAfter: %v", nodePowerAfter)
5759

5860
// and sdk status updated
5961
byzantineOperatorAddr := cli.GetKeyAddrPrefix("node0", "val")
6062
rsp = cli.CustomQuery("q", "staking", "validator", byzantineOperatorAddr)
6163
assert.True(t, gjson.Get(rsp, "validator.jailed").Bool(), rsp)
64+
65+
t.Log("let's run for some blocks to confirm all good")
66+
for i := 0; i < 10; i++ {
67+
sut.AwaitNextBlock(t)
68+
}
6269
}

tests/systemtests/system.go

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
tmtypes "github.com/cometbft/cometbft/types"
2424
"github.com/stretchr/testify/require"
2525
"github.com/tidwall/sjson"
26+
"golang.org/x/exp/maps"
2627

2728
"github.com/cosmos/cosmos-sdk/server"
2829
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -154,7 +155,7 @@ func (s *SystemUnderTest) StartChain(t *testing.T, xargs ...string) {
154155
t.Helper()
155156
s.Log("Start chain\n")
156157
s.ChainStarted = true
157-
s.startNodesAsync(t, append([]string{"start", "--trace", "--log_level=info", "--log_no_color"}, xargs...)...)
158+
s.startNodesAsync(t, append([]string{"start", "--log_level=info", "--log_no_color"}, xargs...)...)
158159

159160
s.AwaitNodeUp(t, s.rpcAddr)
160161

@@ -332,10 +333,10 @@ func (s *SystemUnderTest) StopChain() {
332333

333334
func (s *SystemUnderTest) withEachPid(cb func(p *os.Process)) {
334335
s.pidsLock.RLock()
335-
pids := s.pids
336+
pids := maps.Keys(s.pids)
336337
s.pidsLock.RUnlock()
337338

338-
for pid := range pids {
339+
for _, pid := range pids {
339340
p, err := os.FindProcess(pid)
340341
if err != nil {
341342
continue
@@ -528,28 +529,28 @@ func (s *SystemUnderTest) ForEachNodeExecAndWait(t *testing.T, cmds ...[]string)
528529
for j, xargs := range cmds {
529530
xargs = append(xargs, "--home", home)
530531
s.Logf("Execute `%s %s`\n", s.execBinary, strings.Join(xargs, " "))
531-
out := runShellCmd(t, s.execBinary, xargs...)
532+
out := MustRunShellCmd(t, s.execBinary, xargs...)
532533
s.Logf("Result: %s\n", out)
533534
result[i][j] = out
534535
}
535536
})
536537
return result
537538
}
538539

539-
func runShellCmd(t *testing.T, cmd string, args ...string) string {
540+
func MustRunShellCmd(t *testing.T, cmd string, args ...string) string {
540541
t.Helper()
541-
out, err := runShellCmdX(cmd, args...)
542+
out, err := RunShellCmd(cmd, args...)
542543
require.NoError(t, err)
543544
return out
544545
}
545546

546-
func runShellCmdX(cmd string, args ...string) (string, error) {
547+
func RunShellCmd(cmd string, args ...string) (string, error) {
547548
c := exec.Command( //nolint:gosec // used by tests only
548549
locateExecutable(cmd),
549550
args...,
550551
)
551552
c.Dir = WorkDir
552-
out, err := c.CombinedOutput()
553+
out, err := c.Output()
553554
if err != nil {
554555
return string(out), fmt.Errorf("run `%s %s`: out: %s: %w", cmd, strings.Join(args, " "), string(out), err)
555556
}
@@ -560,7 +561,7 @@ func runShellCmdX(cmd string, args ...string) (string, error) {
560561
func (s *SystemUnderTest) startNodesAsync(t *testing.T, xargs ...string) {
561562
t.Helper()
562563
s.withEachNodeHome(func(i int, home string) {
563-
args := append(xargs, "--home", home)
564+
args := append(xargs, "--home="+home)
564565
s.Logf("Execute `%s %s`\n", s.execBinary, strings.Join(args, " "))
565566
cmd := exec.Command( //nolint:gosec // used by tests only
566567
locateExecutable(s.execBinary),
@@ -569,24 +570,27 @@ func (s *SystemUnderTest) startNodesAsync(t *testing.T, xargs ...string) {
569570
cmd.Dir = WorkDir
570571
s.watchLogs(i, cmd)
571572
require.NoError(t, cmd.Start(), "node %d", i)
572-
573-
pid := cmd.Process.Pid
574-
s.pidsLock.Lock()
575-
s.pids[pid] = struct{}{}
576-
s.pidsLock.Unlock()
577-
s.Logf("Node started: %d\n", pid)
573+
s.Logf("Node started: %d\n", cmd.Process.Pid)
578574

579575
// cleanup when stopped
580-
go func(pid int) {
581-
_ = cmd.Wait() // blocks until shutdown
582-
s.pidsLock.Lock()
583-
delete(s.pids, pid)
584-
s.pidsLock.Unlock()
585-
s.Logf("Node stopped: %d\n", pid)
586-
}(pid)
576+
s.awaitProcessCleanup(cmd)
587577
})
588578
}
589579

580+
func (s *SystemUnderTest) awaitProcessCleanup(cmd *exec.Cmd) {
581+
pid := cmd.Process.Pid
582+
s.pidsLock.Lock()
583+
s.pids[pid] = struct{}{}
584+
s.pidsLock.Unlock()
585+
go func() {
586+
_ = cmd.Wait() // blocks until shutdown
587+
s.Logf("Node stopped: %d\n", pid)
588+
s.pidsLock.Lock()
589+
delete(s.pids, pid)
590+
s.pidsLock.Unlock()
591+
}()
592+
}
593+
590594
func (s *SystemUnderTest) withEachNodeHome(cb func(i int, home string)) {
591595
for i := 0; i < s.nodesCount; i++ {
592596
cb(i, s.nodePath(i))
@@ -658,7 +662,7 @@ func (s *SystemUnderTest) resetBuffers() {
658662
}
659663

660664
// AddFullnode starts a new fullnode that connects to the existing chain but is not a validator.
661-
func (s *SystemUnderTest) AddFullnode(t *testing.T, minGasPrices string, beforeStart ...func(nodeNumber int, nodePath string)) Node {
665+
func (s *SystemUnderTest) AddFullnode(t *testing.T, beforeStart ...func(nodeNumber int, nodePath string)) Node {
662666
t.Helper()
663667
s.MarkDirty()
664668
s.nodesCount++
@@ -668,7 +672,7 @@ func (s *SystemUnderTest) AddFullnode(t *testing.T, minGasPrices string, beforeS
668672

669673
// prepare new node
670674
moniker := fmt.Sprintf("node%d", nodeNumber)
671-
args := []string{"init", moniker, "--home", nodePath, "--overwrite"}
675+
args := []string{"init", moniker, "--home=" + nodePath, "--overwrite"}
672676
s.Logf("Execute `%s %s`\n", s.execBinary, strings.Join(args, " "))
673677
cmd := exec.Command( //nolint:gosec // used by tests only
674678
locateExecutable(s.execBinary),
@@ -679,12 +683,15 @@ func (s *SystemUnderTest) AddFullnode(t *testing.T, minGasPrices string, beforeS
679683
require.NoError(t, cmd.Run(), "failed to start node with id %d", nodeNumber)
680684
require.NoError(t, saveGenesis(nodePath, []byte(s.ReadGenesisJSON(t))))
681685

682-
// quick hack: copy config and overwrite by start params
683-
configFile := filepath.Join(WorkDir, nodePath, "config", "config.toml")
684-
_ = os.Remove(configFile)
685-
_, err := copyFile(filepath.Join(WorkDir, s.nodePath(0), "config", "config.toml"), configFile)
686-
require.NoError(t, err)
686+
configPath := filepath.Join(WorkDir, nodePath, "config")
687687

688+
// quick hack: copy config and overwrite by start params
689+
for _, tomlFile := range []string{"config.toml", "app.toml"} {
690+
configFile := filepath.Join(configPath, tomlFile)
691+
_ = os.Remove(configFile)
692+
_, err := copyFile(filepath.Join(WorkDir, s.nodePath(0), "config", tomlFile), configFile)
693+
require.NoError(t, err)
694+
}
688695
// start node
689696
allNodes := s.AllNodes(t)
690697
node := allNodes[len(allNodes)-1]
@@ -701,7 +708,6 @@ func (s *SystemUnderTest) AddFullnode(t *testing.T, minGasPrices string, beforeS
701708
fmt.Sprintf("--p2p.laddr=tcp://localhost:%d", node.P2PPort),
702709
fmt.Sprintf("--rpc.laddr=tcp://localhost:%d", node.RPCPort),
703710
fmt.Sprintf("--grpc.address=localhost:%d", 9090+nodeNumber),
704-
fmt.Sprintf("--minimum-gas-prices=%s", minGasPrices),
705711
"--p2p.pex=false",
706712
"--moniker=" + moniker,
707713
"--log_level=info",
@@ -716,6 +722,7 @@ func (s *SystemUnderTest) AddFullnode(t *testing.T, minGasPrices string, beforeS
716722
cmd.Dir = WorkDir
717723
s.watchLogs(nodeNumber, cmd)
718724
require.NoError(t, cmd.Start(), "node %d", nodeNumber)
725+
s.awaitProcessCleanup(cmd)
719726
return node
720727
}
721728

tests/systemtests/testnet_init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s SingleHostTestnetCmdInitializer) Initialize() {
5858
"--single-host",
5959
}
6060
s.log(fmt.Sprintf("+++ %s %s\n", s.execBinary, strings.Join(args, " ")))
61-
out, err := runShellCmdX(s.execBinary, args...)
61+
out, err := RunShellCmd(s.execBinary, args...)
6262
if err != nil {
6363
panic(err)
6464
}
@@ -112,7 +112,7 @@ func (s ModifyConfigYamlInitializer) Initialize() {
112112
}
113113
s.log(fmt.Sprintf("+++ %s %s\n", s.execBinary, strings.Join(args, " ")))
114114

115-
out, err := runShellCmdX(s.execBinary, args...)
115+
out, err := RunShellCmd(s.execBinary, args...)
116116
if err != nil {
117117
panic(err)
118118
}

tests/systemtests/upgrade_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func FetchExecutable(t *testing.T, version string) string {
106106
}
107107
destFile := cacheFile
108108
t.Log("+++ version not in cache, downloading from docker image")
109-
runShellCmd(t, "docker", "pull", "ghcr.io/cosmos/simapp:"+version)
110-
runShellCmd(t, "docker", "create", "--name=ci_temp", "ghcr.io/cosmos/simapp:"+version)
111-
runShellCmd(t, "docker", "cp", "ci_temp:/usr/bin/simd", destFile)
109+
MustRunShellCmd(t, "docker", "pull", "ghcr.io/cosmos/simapp:"+version)
110+
MustRunShellCmd(t, "docker", "create", "--name=ci_temp", "ghcr.io/cosmos/simapp:"+version)
111+
MustRunShellCmd(t, "docker", "cp", "ci_temp:/usr/bin/simd", destFile)
112112
return destFile
113113
}

0 commit comments

Comments
 (0)