You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.6 KiB

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