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.

99 lines
2.8 KiB

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