bitbucket.org/number571/tendermint@v0.8.14/test/e2e/tests/app_test.go (about)

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