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.

317 lines
7.4 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.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo, false)
  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. chLoadResult <- err
  56. }()
  57. if err := Start(cli.testnet); err != nil {
  58. return err
  59. }
  60. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  61. return err
  62. }
  63. if cli.testnet.HasPerturbations() {
  64. if err := Perturb(cli.testnet); err != nil {
  65. return err
  66. }
  67. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  68. return err
  69. }
  70. }
  71. if cli.testnet.Evidence > 0 {
  72. if err := InjectEvidence(cli.testnet, cli.testnet.Evidence); err != nil {
  73. return err
  74. }
  75. if err := Wait(cli.testnet, 5); err != nil { // ensure chain progress
  76. return err
  77. }
  78. }
  79. loadCancel()
  80. if err := <-chLoadResult; err != nil {
  81. return fmt.Errorf("transaction load failed: %w", err)
  82. }
  83. if err := Wait(cli.testnet, 5); err != nil { // wait for network to settle before tests
  84. return err
  85. }
  86. if err := Test(cli.testnet); err != nil {
  87. return err
  88. }
  89. if !cli.preserve {
  90. if err := Cleanup(cli.testnet); err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. },
  96. }
  97. cli.root.PersistentFlags().StringP("file", "f", "", "Testnet TOML manifest")
  98. _ = cli.root.MarkPersistentFlagRequired("file")
  99. cli.root.Flags().BoolVarP(&cli.preserve, "preserve", "p", false,
  100. "Preserves the running of the test net after tests are completed")
  101. cli.root.SetHelpCommand(&cobra.Command{
  102. Use: "no-help",
  103. Hidden: true,
  104. })
  105. cli.root.AddCommand(&cobra.Command{
  106. Use: "setup",
  107. Short: "Generates the testnet directory and configuration",
  108. RunE: func(cmd *cobra.Command, args []string) error {
  109. return Setup(cli.testnet)
  110. },
  111. })
  112. cli.root.AddCommand(&cobra.Command{
  113. Use: "start",
  114. Short: "Starts the Docker testnet, waiting for nodes to become available",
  115. RunE: func(cmd *cobra.Command, args []string) error {
  116. _, err := os.Stat(cli.testnet.Dir)
  117. if os.IsNotExist(err) {
  118. err = Setup(cli.testnet)
  119. }
  120. if err != nil {
  121. return err
  122. }
  123. return Start(cli.testnet)
  124. },
  125. })
  126. cli.root.AddCommand(&cobra.Command{
  127. Use: "perturb",
  128. Short: "Perturbs the Docker testnet, e.g. by restarting or disconnecting nodes",
  129. RunE: func(cmd *cobra.Command, args []string) error {
  130. return Perturb(cli.testnet)
  131. },
  132. })
  133. cli.root.AddCommand(&cobra.Command{
  134. Use: "wait",
  135. Short: "Waits for a few blocks to be produced and all nodes to catch up",
  136. RunE: func(cmd *cobra.Command, args []string) error {
  137. return Wait(cli.testnet, 5)
  138. },
  139. })
  140. cli.root.AddCommand(&cobra.Command{
  141. Use: "stop",
  142. Short: "Stops the Docker testnet",
  143. RunE: func(cmd *cobra.Command, args []string) error {
  144. logger.Info("Stopping testnet")
  145. return execCompose(cli.testnet.Dir, "down")
  146. },
  147. })
  148. cli.root.AddCommand(&cobra.Command{
  149. Use: "resume",
  150. Short: "Resumes the Docker testnet",
  151. RunE: func(cmd *cobra.Command, args []string) error {
  152. logger.Info("Resuming testnet")
  153. return execCompose(cli.testnet.Dir, "up")
  154. },
  155. })
  156. cli.root.AddCommand(&cobra.Command{
  157. Use: "load [multiplier]",
  158. Args: cobra.MaximumNArgs(1),
  159. Short: "Generates transaction load until the command is canceled",
  160. RunE: func(cmd *cobra.Command, args []string) (err error) {
  161. m := 1
  162. if len(args) == 1 {
  163. m, err = strconv.Atoi(args[0])
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. return Load(context.Background(), cli.testnet, m)
  169. },
  170. })
  171. cli.root.AddCommand(&cobra.Command{
  172. Use: "evidence [amount]",
  173. Args: cobra.MaximumNArgs(1),
  174. Short: "Generates and broadcasts evidence to a random node",
  175. RunE: func(cmd *cobra.Command, args []string) (err error) {
  176. amount := 1
  177. if len(args) == 1 {
  178. amount, err = strconv.Atoi(args[0])
  179. if err != nil {
  180. return err
  181. }
  182. }
  183. return InjectEvidence(cli.testnet, amount)
  184. },
  185. })
  186. cli.root.AddCommand(&cobra.Command{
  187. Use: "test",
  188. Short: "Runs test cases against a running testnet",
  189. RunE: func(cmd *cobra.Command, args []string) error {
  190. return Test(cli.testnet)
  191. },
  192. })
  193. cli.root.AddCommand(&cobra.Command{
  194. Use: "cleanup",
  195. Short: "Removes the testnet directory",
  196. RunE: func(cmd *cobra.Command, args []string) error {
  197. return Cleanup(cli.testnet)
  198. },
  199. })
  200. cli.root.AddCommand(&cobra.Command{
  201. Use: "logs [node]",
  202. Short: "Shows the testnet or a specefic node's logs",
  203. Example: "runner logs validator03",
  204. Args: cobra.MaximumNArgs(1),
  205. RunE: func(cmd *cobra.Command, args []string) error {
  206. return execComposeVerbose(cli.testnet.Dir, append([]string{"logs", "--no-color"}, args...)...)
  207. },
  208. })
  209. cli.root.AddCommand(&cobra.Command{
  210. Use: "tail [node]",
  211. Short: "Tails the testnet or a specific node's logs",
  212. Args: cobra.MaximumNArgs(1),
  213. RunE: func(cmd *cobra.Command, args []string) error {
  214. if len(args) == 1 {
  215. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow", args[0])
  216. }
  217. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow")
  218. },
  219. })
  220. cli.root.AddCommand(&cobra.Command{
  221. Use: "benchmark",
  222. Short: "Benchmarks testnet",
  223. Long: `Benchmarks the following metrics:
  224. Mean Block Interval
  225. Standard Deviation
  226. Min Block Interval
  227. Max Block Interval
  228. over a 100 block sampling period.
  229. Does not run any perbutations.
  230. `,
  231. RunE: func(cmd *cobra.Command, args []string) error {
  232. if err := Cleanup(cli.testnet); err != nil {
  233. return err
  234. }
  235. if err := Setup(cli.testnet); err != nil {
  236. return err
  237. }
  238. chLoadResult := make(chan error)
  239. ctx, loadCancel := context.WithCancel(context.Background())
  240. defer loadCancel()
  241. go func() {
  242. err := Load(ctx, cli.testnet, 1)
  243. chLoadResult <- err
  244. }()
  245. if err := Start(cli.testnet); err != nil {
  246. return err
  247. }
  248. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  249. return err
  250. }
  251. // we benchmark performance over the next 100 blocks
  252. if err := Benchmark(cli.testnet, 100); err != nil {
  253. return err
  254. }
  255. loadCancel()
  256. if err := <-chLoadResult; err != nil {
  257. return err
  258. }
  259. if err := Cleanup(cli.testnet); err != nil {
  260. return err
  261. }
  262. return nil
  263. },
  264. })
  265. return cli
  266. }
  267. // Run runs the CLI.
  268. func (cli *CLI) Run() {
  269. if err := cli.root.Execute(); err != nil {
  270. logger.Error(err.Error())
  271. os.Exit(1)
  272. }
  273. }