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.

139 lines
3.7 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. if node.Stateless() {
  45. continue
  46. }
  47. node := *node
  48. t.Run(node.Name, func(t *testing.T) {
  49. t.Parallel()
  50. testFunc(t, node)
  51. })
  52. }
  53. }
  54. // loadTestnet loads the testnet based on the E2E_MANIFEST envvar.
  55. func loadTestnet(t *testing.T) e2e.Testnet {
  56. t.Helper()
  57. manifest := os.Getenv("E2E_MANIFEST")
  58. if manifest == "" {
  59. t.Skip("E2E_MANIFEST not set, not an end-to-end test run")
  60. }
  61. if !filepath.IsAbs(manifest) {
  62. manifest = filepath.Join("..", manifest)
  63. }
  64. testnetCacheMtx.Lock()
  65. defer testnetCacheMtx.Unlock()
  66. if testnet, ok := testnetCache[manifest]; ok {
  67. return testnet
  68. }
  69. testnet, err := e2e.LoadTestnet(manifest)
  70. require.NoError(t, err)
  71. testnetCache[manifest] = *testnet
  72. return *testnet
  73. }
  74. // fetchBlockChain fetches a complete, up-to-date block history from
  75. // the freshest testnet archive node.
  76. func fetchBlockChain(t *testing.T) []*types.Block {
  77. t.Helper()
  78. testnet := loadTestnet(t)
  79. // Find the freshest archive node
  80. var (
  81. client *rpchttp.HTTP
  82. status *rpctypes.ResultStatus
  83. )
  84. for _, node := range testnet.ArchiveNodes() {
  85. c, err := node.Client()
  86. require.NoError(t, err)
  87. s, err := c.Status(ctx)
  88. require.NoError(t, err)
  89. if status == nil || s.SyncInfo.LatestBlockHeight > status.SyncInfo.LatestBlockHeight {
  90. client = c
  91. status = s
  92. }
  93. }
  94. require.NotNil(t, client, "couldn't find an archive node")
  95. // Fetch blocks. Look for existing block history in the block cache, and
  96. // extend it with any new blocks that have been produced.
  97. blocksCacheMtx.Lock()
  98. defer blocksCacheMtx.Unlock()
  99. from := status.SyncInfo.EarliestBlockHeight
  100. to := status.SyncInfo.LatestBlockHeight
  101. blocks, ok := blocksCache[testnet.Name]
  102. if !ok {
  103. blocks = make([]*types.Block, 0, to-from+1)
  104. }
  105. if len(blocks) > 0 {
  106. from = blocks[len(blocks)-1].Height + 1
  107. }
  108. for h := from; h <= to; h++ {
  109. resp, err := client.Block(ctx, &(h))
  110. require.NoError(t, err)
  111. require.NotNil(t, resp.Block)
  112. require.Equal(t, h, resp.Block.Height, "unexpected block height %v", resp.Block.Height)
  113. blocks = append(blocks, resp.Block)
  114. }
  115. require.NotEmpty(t, blocks, "blockchain does not contain any blocks")
  116. blocksCache[testnet.Name] = blocks
  117. return blocks
  118. }