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