github.com/soomindae/tendermint@v0.0.5-0.20210528140126-84a0c70c8162/test/e2e/tests/app_test.go (about) 1 package e2e_test 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 e2e "github.com/soomindae/tendermint/test/e2e/pkg" 13 "github.com/soomindae/tendermint/types" 14 ) 15 16 // Tests that any initial state given in genesis has made it into the app. 17 func TestApp_InitialState(t *testing.T) { 18 testNode(t, func(t *testing.T, node e2e.Node) { 19 if node.Stateless() { 20 return 21 } 22 if len(node.Testnet.InitialState) == 0 { 23 return 24 } 25 26 client, err := node.Client() 27 require.NoError(t, err) 28 for k, v := range node.Testnet.InitialState { 29 resp, err := client.ABCIQuery(ctx, "", []byte(k)) 30 require.NoError(t, err) 31 assert.Equal(t, k, string(resp.Response.Key)) 32 assert.Equal(t, v, string(resp.Response.Value)) 33 } 34 }) 35 } 36 37 // Tests that the app hash (as reported by the app) matches the last 38 // block and the node sync status. 39 func TestApp_Hash(t *testing.T) { 40 testNode(t, func(t *testing.T, node e2e.Node) { 41 if node.Mode == e2e.ModeSeed { 42 return 43 } 44 45 client, err := node.Client() 46 require.NoError(t, err) 47 info, err := client.ABCIInfo(ctx) 48 require.NoError(t, err) 49 require.NotEmpty(t, info.Response.LastBlockAppHash, "expected app to return app hash") 50 51 block, err := client.Block(ctx, nil) 52 require.NoError(t, err) 53 require.EqualValues(t, info.Response.LastBlockAppHash, block.Block.AppHash, 54 "app hash does not match last block's app hash") 55 56 status, err := client.Status(ctx) 57 require.NoError(t, err) 58 require.EqualValues(t, info.Response.LastBlockAppHash, status.SyncInfo.LatestAppHash, 59 "app hash does not match node status") 60 }) 61 } 62 63 // Tests that we can set a value and retrieve it. 64 func TestApp_Tx(t *testing.T) { 65 testNode(t, func(t *testing.T, node e2e.Node) { 66 if node.Mode == e2e.ModeSeed { 67 return 68 } 69 70 client, err := node.Client() 71 require.NoError(t, err) 72 73 // Generate a random value, to prevent duplicate tx errors when 74 // manually running the test multiple times for a testnet. 75 r := rand.New(rand.NewSource(time.Now().UnixNano())) 76 bz := make([]byte, 32) 77 _, err = r.Read(bz) 78 require.NoError(t, err) 79 80 key := fmt.Sprintf("testapp-tx-%v", node.Name) 81 value := fmt.Sprintf("%x", bz) 82 tx := types.Tx(fmt.Sprintf("%v=%v", key, value)) 83 84 resp, err := client.BroadcastTxCommit(ctx, tx) 85 require.NoError(t, err) 86 87 // wait for the tx to be persisted in the tx indexer 88 time.Sleep(500 * time.Millisecond) 89 90 hash := tx.Hash() 91 txResp, err := client.Tx(ctx, hash, false) 92 require.NoError(t, err) 93 assert.Equal(t, txResp.Tx, tx) 94 assert.Equal(t, txResp.Height, resp.Height) 95 96 // NOTE: we don't test abci query of the light client 97 if node.Mode == e2e.ModeLight { 98 return 99 } 100 101 abciResp, err := client.ABCIQuery(ctx, "", []byte(key)) 102 require.NoError(t, err) 103 assert.Equal(t, key, string(abciResp.Response.Key)) 104 assert.Equal(t, value, string(abciResp.Response.Value)) 105 106 }) 107 }