|
| 1 | +//go:build system_test |
| 2 | + |
| 3 | +package systemtests |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/tidwall/gjson" |
| 14 | +) |
| 15 | + |
| 16 | +func TestExportCmd_WithHeight(t *testing.T) { |
| 17 | + sut.ResetChain(t) |
| 18 | + cli := NewCLIWrapper(t, sut, verbose) |
| 19 | + |
| 20 | + sut.StartChain(t) |
| 21 | + |
| 22 | + // Wait 10s for producing blocks |
| 23 | + time.Sleep(10 * time.Second) |
| 24 | + |
| 25 | + sut.StopChain() |
| 26 | + |
| 27 | + testCases := []struct { |
| 28 | + name string |
| 29 | + args []string |
| 30 | + expZeroHeight bool |
| 31 | + }{ |
| 32 | + {"should export correct height", []string{"genesis", "export", "--home", sut.nodePath(0)}, false}, |
| 33 | + {"should export correct height with --height", []string{"genesis", "export", "--height=5", "--home", sut.nodePath(0), "--log_level=disabled"}, false}, |
| 34 | + {"should export height 0 with --for-zero-height", []string{"genesis", "export", "--for-zero-height=true", "--home", sut.nodePath(0)}, true}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tc := range testCases { |
| 38 | + res := cli.RunCommandWithArgs(tc.args...) |
| 39 | + height := gjson.Get(res, "initial_height").Int() |
| 40 | + if tc.expZeroHeight { |
| 41 | + require.Equal(t, height, int64(0)) |
| 42 | + } else { |
| 43 | + require.Greater(t, height, int64(0)) |
| 44 | + } |
| 45 | + |
| 46 | + // Check consensus params of exported state |
| 47 | + maxGas := gjson.Get(res, "consensus.params.block.max_gas").Int() |
| 48 | + require.Equal(t, maxGas, int64(MaxGas)) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestExportCmd_WithFileFlag(t *testing.T) { |
| 53 | + sut.ResetChain(t) |
| 54 | + cli := NewCLIWrapper(t, sut, verbose) |
| 55 | + exportFile := "foobar.json" |
| 56 | + |
| 57 | + sut.StartChain(t) |
| 58 | + |
| 59 | + // Wait 10s for producing blocks |
| 60 | + time.Sleep(10 * time.Second) |
| 61 | + |
| 62 | + sut.StopChain() |
| 63 | + |
| 64 | + testCases := []struct { |
| 65 | + name string |
| 66 | + args []string |
| 67 | + expErr bool |
| 68 | + errMsg string |
| 69 | + }{ |
| 70 | + {"invalid home dir", []string{"genesis", "export", "--home=foo"}, true, "no such file or directory"}, |
| 71 | + {"should export state to the specified file", []string{"genesis", "export", fmt.Sprintf("--output-document=%s", exportFile), "--home", sut.nodePath(0)}, false, ""}, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tc := range testCases { |
| 75 | + if tc.expErr { |
| 76 | + assertOutput := func(_ assert.TestingT, gotErr error, gotOutputs ...interface{}) bool { |
| 77 | + require.Contains(t, gotOutputs[0], tc.errMsg) |
| 78 | + return false |
| 79 | + } |
| 80 | + cli.WithRunErrorMatcher(assertOutput).RunCommandWithArgs(tc.args...) |
| 81 | + } else { |
| 82 | + cli.RunCommandWithArgs(tc.args...) |
| 83 | + require.FileExists(t, exportFile) |
| 84 | + err := os.Remove(exportFile) |
| 85 | + require.NoError(t, err) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments