github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/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/lazyledger/lazyledger-core/test/e2e/pkg" 13 "github.com/lazyledger/lazyledger-core/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.Mode == e2e.ModeSeed { 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 _, err = client.BroadcastTxCommit(ctx, tx) 85 require.NoError(t, err) 86 87 resp, err := client.ABCIQuery(ctx, "", []byte(key)) 88 require.NoError(t, err) 89 assert.Equal(t, key, string(resp.Response.Key)) 90 assert.Equal(t, value, string(resp.Response.Value)) 91 }) 92 }