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.

137 lines
3.6 KiB

  1. package e2e_test
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. rpchttp "github.com/tendermint/tendermint/rpc/client/http"
  9. rpctypes "github.com/tendermint/tendermint/rpc/coretypes"
  10. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func init() {
  14. // This can be used to manually specify a testnet manifest and/or node to
  15. // run tests against. The testnet must have been started by the runner first.
  16. // os.Setenv("E2E_MANIFEST", "networks/ci.toml")
  17. // os.Setenv("E2E_NODE", "validator01")
  18. }
  19. var (
  20. ctx = context.Background()
  21. testnetCache = map[string]e2e.Testnet{}
  22. testnetCacheMtx = sync.Mutex{}
  23. blocksCache = map[string][]*types.Block{}
  24. blocksCacheMtx = sync.Mutex{}
  25. )
  26. // testNode runs tests for testnet nodes. The callback function is
  27. // given a single stateful node to test, running as a subtest in
  28. // 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. if node.Stateless() {
  46. continue
  47. }
  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. 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. }