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.

100 lines
2.9 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/tendermint/tendermint/libs/log"
  7. rpctypes "github.com/tendermint/tendermint/rpc/coretypes"
  8. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  9. )
  10. // Perturbs a running testnet.
  11. func Perturb(ctx context.Context, logger log.Logger, testnet *e2e.Testnet) error {
  12. timer := time.NewTimer(0) // first tick fires immediately; reset below
  13. defer timer.Stop()
  14. for _, node := range testnet.Nodes {
  15. for _, perturbation := range node.Perturbations {
  16. select {
  17. case <-ctx.Done():
  18. return ctx.Err()
  19. case <-timer.C:
  20. _, err := PerturbNode(ctx, logger, node, perturbation)
  21. if err != nil {
  22. return err
  23. }
  24. // give network some time to recover between each
  25. timer.Reset(20 * time.Second)
  26. }
  27. }
  28. }
  29. return nil
  30. }
  31. // PerturbNode perturbs a node with a given perturbation, returning its status
  32. // after recovering.
  33. func PerturbNode(ctx context.Context, logger log.Logger, node *e2e.Node, perturbation e2e.Perturbation) (*rpctypes.ResultStatus, error) {
  34. testnet := node.Testnet
  35. switch perturbation {
  36. case e2e.PerturbationDisconnect:
  37. logger.Info(fmt.Sprintf("Disconnecting node %v...", node.Name))
  38. if err := execDocker("network", "disconnect", testnet.Name+"_"+testnet.Name, node.Name); err != nil {
  39. return nil, err
  40. }
  41. time.Sleep(10 * time.Second)
  42. if err := execDocker("network", "connect", testnet.Name+"_"+testnet.Name, node.Name); err != nil {
  43. return nil, err
  44. }
  45. case e2e.PerturbationKill:
  46. logger.Info(fmt.Sprintf("Killing node %v...", node.Name))
  47. if err := execCompose(testnet.Dir, "kill", "-s", "SIGKILL", node.Name); err != nil {
  48. return nil, err
  49. }
  50. time.Sleep(10 * time.Second)
  51. if err := execCompose(testnet.Dir, "start", node.Name); err != nil {
  52. return nil, err
  53. }
  54. case e2e.PerturbationPause:
  55. logger.Info(fmt.Sprintf("Pausing node %v...", node.Name))
  56. if err := execCompose(testnet.Dir, "pause", node.Name); err != nil {
  57. return nil, err
  58. }
  59. time.Sleep(10 * time.Second)
  60. if err := execCompose(testnet.Dir, "unpause", node.Name); err != nil {
  61. return nil, err
  62. }
  63. case e2e.PerturbationRestart:
  64. logger.Info(fmt.Sprintf("Restarting node %v...", node.Name))
  65. if err := execCompose(testnet.Dir, "kill", "-s", "SIGTERM", node.Name); err != nil {
  66. return nil, err
  67. }
  68. time.Sleep(10 * time.Second)
  69. if err := execCompose(testnet.Dir, "start", node.Name); err != nil {
  70. return nil, err
  71. }
  72. default:
  73. return nil, fmt.Errorf("unexpected perturbation %q", perturbation)
  74. }
  75. // Seed nodes do not have an RPC endpoint exposed so we cannot assert that
  76. // the node recovered. All we can do is hope.
  77. if node.Mode == e2e.ModeSeed {
  78. return nil, nil
  79. }
  80. ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
  81. defer cancel()
  82. status, err := waitForNode(ctx, logger, node, 0)
  83. if err != nil {
  84. return nil, err
  85. }
  86. logger.Info(fmt.Sprintf("Node %v recovered at height %v", node.Name, status.SyncInfo.LatestBlockHeight))
  87. return status, nil
  88. }