From 5b8f187d75267934207cbabe7555b7a047cd6cd6 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Mon, 21 Oct 2024 21:21:18 +0700 Subject: [PATCH] genutil tests --- tests/systemtests/export_test.go | 67 ++++++++++++++++++++++++++++++++ tests/systemtests/system.go | 4 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/systemtests/export_test.go diff --git a/tests/systemtests/export_test.go b/tests/systemtests/export_test.go new file mode 100644 index 000000000000..2c9d2851821e --- /dev/null +++ b/tests/systemtests/export_test.go @@ -0,0 +1,67 @@ +//go:build system_test + +package systemtests + +import ( + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tidwall/gjson" +) + +func TestExportCmd(t *testing.T) { + // scenario: test bank send command + // given a running chain + + sut.ResetChain(t) + cli := NewCLIWrapper(t, sut, verbose) + exportFile := "foobar.json" + + sut.StartChain(t) + + testCases := []struct { + name string + args []string + expErr bool + errMsg string + expZeroHeight bool + }{ + {"invalid home dir", []string{"genesis", "export", "--home=foo"}, true, "no such file or directory", false}, + {"should export correct height", []string{"genesis", "export"}, false, "", false}, + {"should export correct height with --height", []string{"genesis", "export", "--height=5"}, false, "", false}, + {"should export height 0 with --for-zero-height", []string{"genesis", "export", "--for-zero-height=true"}, false, "", true}, + {"should export state to the specified file", []string{"genesis", "export", fmt.Sprintf("--output-document=%s", exportFile)}, false, "", false}, + } + + for _, tc := range testCases { + + // fmt.Println(tc.name, res) + if tc.expErr { + assertOutput := func(_ assert.TestingT, gotErr error, gotOutputs ...interface{}) bool { + require.Contains(t, gotOutputs[0], tc.errMsg) + return false + } + cli.WithRunErrorMatcher(assertOutput).RunCommandWithArgs(tc.args...) + } else { + res := cli.RunCommandWithArgs(tc.args...) + if res == "" { + require.FileExists(t, exportFile) + os.Remove(exportFile) + } else { + height := gjson.Get(res, "initial_height").Int() + if tc.expZeroHeight { + require.Equal(t, height, int64(0)) + } else { + require.Greater(t, height, int64(0)) + } + + // Check consensus params of exported state + maxGas := gjson.Get(res, "consensus.params.block.max_gas").Int() + require.Equal(t, maxGas, int64(MaxGas)) + } + } + } +} diff --git a/tests/systemtests/system.go b/tests/systemtests/system.go index adaa6da91d77..7105ff400ed1 100644 --- a/tests/systemtests/system.go +++ b/tests/systemtests/system.go @@ -35,6 +35,8 @@ var ( // ExecBinaryUnversionedRegExp regular expression to extract the unversioned binary name ExecBinaryUnversionedRegExp = regexp.MustCompile(`^(\w+)-?.*$`) + + MaxGas = 10_000_000 ) type TestnetInitializer interface { @@ -130,7 +132,7 @@ func (s *SystemUnderTest) SetupChain() { panic(fmt.Sprintf("failed to load genesis: %s", err)) } - genesisBz, err = sjson.SetRawBytes(genesisBz, "consensus.params.block.max_gas", []byte(fmt.Sprintf(`"%d"`, 10_000_000))) + genesisBz, err = sjson.SetRawBytes(genesisBz, "consensus.params.block.max_gas", []byte(fmt.Sprintf(`"%d"`, MaxGas))) if err != nil { panic(fmt.Sprintf("failed to set block max gas: %s", err)) }