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.

216 lines
5.2 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "github.com/tendermint/tendermint/libs/log"
  8. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  9. )
  10. var (
  11. logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  12. )
  13. func main() {
  14. NewCLI().Run()
  15. }
  16. // CLI is the Cobra-based command-line interface.
  17. type CLI struct {
  18. root *cobra.Command
  19. testnet *e2e.Testnet
  20. preserve bool
  21. }
  22. // NewCLI sets up the CLI.
  23. func NewCLI() *CLI {
  24. cli := &CLI{}
  25. cli.root = &cobra.Command{
  26. Use: "runner",
  27. Short: "End-to-end test runner",
  28. SilenceUsage: true,
  29. SilenceErrors: true, // we'll output them ourselves in Run()
  30. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  31. file, err := cmd.Flags().GetString("file")
  32. if err != nil {
  33. return err
  34. }
  35. testnet, err := e2e.LoadTestnet(file)
  36. if err != nil {
  37. return err
  38. }
  39. cli.testnet = testnet
  40. return nil
  41. },
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. if err := Cleanup(cli.testnet); err != nil {
  44. return err
  45. }
  46. if err := Setup(cli.testnet); err != nil {
  47. return err
  48. }
  49. chLoadResult := make(chan error)
  50. ctx, loadCancel := context.WithCancel(context.Background())
  51. defer loadCancel()
  52. go func() {
  53. err := Load(ctx, cli.testnet)
  54. if err != nil {
  55. logger.Error(fmt.Sprintf("Transaction load failed: %v", err.Error()))
  56. }
  57. chLoadResult <- err
  58. }()
  59. if err := Start(cli.testnet); err != nil {
  60. return err
  61. }
  62. if lastMisbehavior := cli.testnet.LastMisbehaviorHeight(); lastMisbehavior > 0 {
  63. // wait for misbehaviors before starting perturbations. We do a separate
  64. // wait for another 5 blocks, since the last misbehavior height may be
  65. // in the past depending on network startup ordering.
  66. if err := WaitUntil(cli.testnet, lastMisbehavior); err != nil {
  67. return err
  68. }
  69. }
  70. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  71. return err
  72. }
  73. if cli.testnet.HasPerturbations() {
  74. if err := Perturb(cli.testnet); err != nil {
  75. return err
  76. }
  77. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  78. return err
  79. }
  80. }
  81. loadCancel()
  82. if err := <-chLoadResult; err != nil {
  83. return err
  84. }
  85. if err := Wait(cli.testnet, 5); err != nil { // wait for network to settle before tests
  86. return err
  87. }
  88. if err := Test(cli.testnet); err != nil {
  89. return err
  90. }
  91. if !cli.preserve {
  92. if err := Cleanup(cli.testnet); err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. },
  98. }
  99. cli.root.PersistentFlags().StringP("file", "f", "", "Testnet TOML manifest")
  100. _ = cli.root.MarkPersistentFlagRequired("file")
  101. cli.root.Flags().BoolVarP(&cli.preserve, "preserve", "p", false,
  102. "Preserves the running of the test net after tests are completed")
  103. cli.root.AddCommand(&cobra.Command{
  104. Use: "setup",
  105. Short: "Generates the testnet directory and configuration",
  106. RunE: func(cmd *cobra.Command, args []string) error {
  107. return Setup(cli.testnet)
  108. },
  109. })
  110. cli.root.AddCommand(&cobra.Command{
  111. Use: "start",
  112. Short: "Starts the Docker testnet, waiting for nodes to become available",
  113. RunE: func(cmd *cobra.Command, args []string) error {
  114. _, err := os.Stat(cli.testnet.Dir)
  115. if os.IsNotExist(err) {
  116. err = Setup(cli.testnet)
  117. }
  118. if err != nil {
  119. return err
  120. }
  121. return Start(cli.testnet)
  122. },
  123. })
  124. cli.root.AddCommand(&cobra.Command{
  125. Use: "perturb",
  126. Short: "Perturbs the Docker testnet, e.g. by restarting or disconnecting nodes",
  127. RunE: func(cmd *cobra.Command, args []string) error {
  128. return Perturb(cli.testnet)
  129. },
  130. })
  131. cli.root.AddCommand(&cobra.Command{
  132. Use: "wait",
  133. Short: "Waits for a few blocks to be produced and all nodes to catch up",
  134. RunE: func(cmd *cobra.Command, args []string) error {
  135. return Wait(cli.testnet, 5)
  136. },
  137. })
  138. cli.root.AddCommand(&cobra.Command{
  139. Use: "stop",
  140. Short: "Stops the Docker testnet",
  141. RunE: func(cmd *cobra.Command, args []string) error {
  142. logger.Info("Stopping testnet")
  143. return execCompose(cli.testnet.Dir, "down")
  144. },
  145. })
  146. cli.root.AddCommand(&cobra.Command{
  147. Use: "load",
  148. Short: "Generates transaction load until the command is cancelled",
  149. RunE: func(cmd *cobra.Command, args []string) error {
  150. return Load(context.Background(), cli.testnet)
  151. },
  152. })
  153. cli.root.AddCommand(&cobra.Command{
  154. Use: "test",
  155. Short: "Runs test cases against a running testnet",
  156. RunE: func(cmd *cobra.Command, args []string) error {
  157. return Test(cli.testnet)
  158. },
  159. })
  160. cli.root.AddCommand(&cobra.Command{
  161. Use: "cleanup",
  162. Short: "Removes the testnet directory",
  163. RunE: func(cmd *cobra.Command, args []string) error {
  164. return Cleanup(cli.testnet)
  165. },
  166. })
  167. cli.root.AddCommand(&cobra.Command{
  168. Use: "logs",
  169. Short: "Shows the testnet logs",
  170. RunE: func(cmd *cobra.Command, args []string) error {
  171. return execComposeVerbose(cli.testnet.Dir, "logs")
  172. },
  173. })
  174. cli.root.AddCommand(&cobra.Command{
  175. Use: "tail",
  176. Short: "Tails the testnet logs",
  177. RunE: func(cmd *cobra.Command, args []string) error {
  178. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow")
  179. },
  180. })
  181. return cli
  182. }
  183. // Run runs the CLI.
  184. func (cli *CLI) Run() {
  185. if err := cli.root.Execute(); err != nil {
  186. logger.Error(err.Error())
  187. os.Exit(1)
  188. }
  189. }