github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/test/e2e/tests/e2e_test.go (about)

     1  package e2e_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	rpchttp "github.com/lazyledger/lazyledger-core/rpc/client/http"
    13  	rpctypes "github.com/lazyledger/lazyledger-core/rpc/core/types"
    14  	e2e "github.com/lazyledger/lazyledger-core/test/e2e/pkg"
    15  	"github.com/lazyledger/lazyledger-core/types"
    16  )
    17  
    18  func init() {
    19  	// This can be used to manually specify a testnet manifest and/or node to
    20  	// run tests against. The testnet must have been started by the runner first.
    21  	// os.Setenv("E2E_MANIFEST", "networks/ci.toml")
    22  	// os.Setenv("E2E_NODE", "validator01")
    23  }
    24  
    25  var (
    26  	ctx             = context.Background()
    27  	testnetCache    = map[string]e2e.Testnet{}
    28  	testnetCacheMtx = sync.Mutex{}
    29  	blocksCache     = map[string][]*types.Block{}
    30  	blocksCacheMtx  = sync.Mutex{}
    31  )
    32  
    33  // testNode runs tests for testnet nodes. The callback function is given a
    34  // single node to test, running as a subtest in parallel with other subtests.
    35  //
    36  // The testnet manifest must be given as the envvar E2E_MANIFEST. If not set,
    37  // these tests are skipped so that they're not picked up during normal unit
    38  // test runs. If E2E_NODE is also set, only the specified node is tested,
    39  // otherwise all nodes are tested.
    40  func testNode(t *testing.T, testFunc func(*testing.T, e2e.Node)) {
    41  	t.Helper()
    42  
    43  	testnet := loadTestnet(t)
    44  	nodes := testnet.Nodes
    45  
    46  	if name := os.Getenv("E2E_NODE"); name != "" {
    47  		node := testnet.LookupNode(name)
    48  		require.NotNil(t, node, "node %q not found in testnet %q", name, testnet.Name)
    49  		nodes = []*e2e.Node{node}
    50  	}
    51  
    52  	for _, node := range nodes {
    53  		node := *node
    54  		t.Run(node.Name, func(t *testing.T) {
    55  			t.Parallel()
    56  			testFunc(t, node)
    57  		})
    58  	}
    59  }
    60  
    61  // loadTestnet loads the testnet based on the E2E_MANIFEST envvar.
    62  func loadTestnet(t *testing.T) e2e.Testnet {
    63  	t.Helper()
    64  
    65  	manifest := os.Getenv("E2E_MANIFEST")
    66  	if manifest == "" {
    67  		t.Skip("E2E_MANIFEST not set, not an end-to-end test run")
    68  	}
    69  	if !filepath.IsAbs(manifest) {
    70  		manifest = filepath.Join("..", manifest)
    71  	}
    72  
    73  	testnetCacheMtx.Lock()
    74  	defer testnetCacheMtx.Unlock()
    75  	if testnet, ok := testnetCache[manifest]; ok {
    76  		return testnet
    77  	}
    78  
    79  	testnet, err := e2e.LoadTestnet(manifest)
    80  	require.NoError(t, err)
    81  	testnetCache[manifest] = *testnet
    82  	return *testnet
    83  }
    84  
    85  // fetchBlockChain fetches a complete, up-to-date block history from
    86  // the freshest testnet archive node.
    87  func fetchBlockChain(t *testing.T) []*types.Block {
    88  	t.Helper()
    89  
    90  	testnet := loadTestnet(t)
    91  
    92  	// Find the freshest archive node
    93  	var (
    94  		client *rpchttp.HTTP
    95  		status *rpctypes.ResultStatus
    96  	)
    97  	for _, node := range testnet.ArchiveNodes() {
    98  		c, err := node.Client()
    99  		require.NoError(t, err)
   100  		s, err := c.Status(ctx)
   101  		require.NoError(t, err)
   102  		if status == nil || s.SyncInfo.LatestBlockHeight > status.SyncInfo.LatestBlockHeight {
   103  			client = c
   104  			status = s
   105  		}
   106  	}
   107  	require.NotNil(t, client, "couldn't find an archive node")
   108  
   109  	// Fetch blocks. Look for existing block history in the block cache, and
   110  	// extend it with any new blocks that have been produced.
   111  	blocksCacheMtx.Lock()
   112  	defer blocksCacheMtx.Unlock()
   113  
   114  	from := status.SyncInfo.EarliestBlockHeight
   115  	to := status.SyncInfo.LatestBlockHeight
   116  	blocks, ok := blocksCache[testnet.Name]
   117  	if !ok {
   118  		blocks = make([]*types.Block, 0, to-from+1)
   119  	}
   120  	if len(blocks) > 0 {
   121  		from = blocks[len(blocks)-1].Height + 1
   122  	}
   123  
   124  	for h := from; h <= to; h++ {
   125  		resp, err := client.Block(ctx, &(h))
   126  		require.NoError(t, err)
   127  		require.NotNil(t, resp.Block)
   128  		require.Equal(t, h, resp.Block.Height, "unexpected block height %v", resp.Block.Height)
   129  		blocks = append(blocks, resp.Block)
   130  	}
   131  	require.NotEmpty(t, blocks, "blockchain does not contain any blocks")
   132  	blocksCache[testnet.Name] = blocks
   133  
   134  	return blocks
   135  }