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.

298 lines
7.1 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.SetHelpCommand(&cobra.Command{
  105. Use: "no-help",
  106. Hidden: true,
  107. })
  108. cli.root.AddCommand(&cobra.Command{
  109. Use: "setup",
  110. Short: "Generates the testnet directory and configuration",
  111. RunE: func(cmd *cobra.Command, args []string) error {
  112. return Setup(cli.testnet)
  113. },
  114. })
  115. cli.root.AddCommand(&cobra.Command{
  116. Use: "start",
  117. Short: "Starts the Docker testnet, waiting for nodes to become available",
  118. RunE: func(cmd *cobra.Command, args []string) error {
  119. _, err := os.Stat(cli.testnet.Dir)
  120. if os.IsNotExist(err) {
  121. err = Setup(cli.testnet)
  122. }
  123. if err != nil {
  124. return err
  125. }
  126. return Start(cli.testnet)
  127. },
  128. })
  129. cli.root.AddCommand(&cobra.Command{
  130. Use: "perturb",
  131. Short: "Perturbs the Docker testnet, e.g. by restarting or disconnecting nodes",
  132. RunE: func(cmd *cobra.Command, args []string) error {
  133. return Perturb(cli.testnet)
  134. },
  135. })
  136. cli.root.AddCommand(&cobra.Command{
  137. Use: "wait",
  138. Short: "Waits for a few blocks to be produced and all nodes to catch up",
  139. RunE: func(cmd *cobra.Command, args []string) error {
  140. return Wait(cli.testnet, 5)
  141. },
  142. })
  143. cli.root.AddCommand(&cobra.Command{
  144. Use: "stop",
  145. Short: "Stops the Docker testnet",
  146. RunE: func(cmd *cobra.Command, args []string) error {
  147. logger.Info("Stopping testnet")
  148. return execCompose(cli.testnet.Dir, "down")
  149. },
  150. })
  151. cli.root.AddCommand(&cobra.Command{
  152. Use: "load [multiplier]",
  153. Args: cobra.MaximumNArgs(1),
  154. Short: "Generates transaction load until the command is canceled",
  155. RunE: func(cmd *cobra.Command, args []string) (err error) {
  156. m := 1
  157. if len(args) == 1 {
  158. m, err = strconv.Atoi(args[0])
  159. if err != nil {
  160. return err
  161. }
  162. }
  163. return Load(context.Background(), cli.testnet, m)
  164. },
  165. })
  166. cli.root.AddCommand(&cobra.Command{
  167. Use: "test",
  168. Short: "Runs test cases against a running testnet",
  169. RunE: func(cmd *cobra.Command, args []string) error {
  170. return Test(cli.testnet)
  171. },
  172. })
  173. cli.root.AddCommand(&cobra.Command{
  174. Use: "cleanup",
  175. Short: "Removes the testnet directory",
  176. RunE: func(cmd *cobra.Command, args []string) error {
  177. return Cleanup(cli.testnet)
  178. },
  179. })
  180. cli.root.AddCommand(&cobra.Command{
  181. Use: "logs [node]",
  182. Short: "Shows the testnet or a specefic node's logs",
  183. Example: "runner logs validator03",
  184. Args: cobra.MaximumNArgs(1),
  185. RunE: func(cmd *cobra.Command, args []string) error {
  186. if len(args) == 1 {
  187. return execComposeVerbose(cli.testnet.Dir, "logs", args[0])
  188. }
  189. return execComposeVerbose(cli.testnet.Dir, "logs")
  190. },
  191. })
  192. cli.root.AddCommand(&cobra.Command{
  193. Use: "tail [node]",
  194. Short: "Tails the testnet or a specific node's logs",
  195. Args: cobra.MaximumNArgs(1),
  196. RunE: func(cmd *cobra.Command, args []string) error {
  197. if len(args) == 1 {
  198. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow", args[0])
  199. }
  200. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow")
  201. },
  202. })
  203. cli.root.AddCommand(&cobra.Command{
  204. Use: "benchmark",
  205. Short: "Benchmarks testnet",
  206. Long: `Benchmarks the following metrics:
  207. Mean Block Interval
  208. Standard Deviation
  209. Min Block Interval
  210. Max Block Interval
  211. over a 100 block sampling period.
  212. Does not run any perbutations.
  213. `,
  214. RunE: func(cmd *cobra.Command, args []string) error {
  215. if err := Cleanup(cli.testnet); err != nil {
  216. return err
  217. }
  218. if err := Setup(cli.testnet); err != nil {
  219. return err
  220. }
  221. chLoadResult := make(chan error)
  222. ctx, loadCancel := context.WithCancel(context.Background())
  223. defer loadCancel()
  224. go func() {
  225. err := Load(ctx, cli.testnet, 1)
  226. if err != nil {
  227. logger.Error(fmt.Sprintf("Transaction load failed: %v", err.Error()))
  228. }
  229. chLoadResult <- err
  230. }()
  231. if err := Start(cli.testnet); err != nil {
  232. return err
  233. }
  234. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  235. return err
  236. }
  237. // we benchmark performance over the next 100 blocks
  238. if err := Benchmark(cli.testnet, 100); err != nil {
  239. return err
  240. }
  241. loadCancel()
  242. if err := <-chLoadResult; err != nil {
  243. return err
  244. }
  245. if err := Cleanup(cli.testnet); err != nil {
  246. return err
  247. }
  248. return nil
  249. },
  250. })
  251. return cli
  252. }
  253. // Run runs the CLI.
  254. func (cli *CLI) Run() {
  255. if err := cli.root.Execute(); err != nil {
  256. logger.Error(err.Error())
  257. os.Exit(1)
  258. }
  259. }