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.

284 lines
6.6 KiB

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