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.3 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 err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  64. return err
  65. }
  66. if cli.testnet.HasPerturbations() {
  67. if err := Perturb(cli.testnet); err != nil {
  68. return err
  69. }
  70. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  71. return err
  72. }
  73. }
  74. if cli.testnet.Evidence > 0 {
  75. if err := InjectEvidence(cli.testnet, cli.testnet.Evidence); err != nil {
  76. return err
  77. }
  78. if err := Wait(cli.testnet, 1); err != nil { // ensure chain progress
  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: "evidence [amount]",
  168. Args: cobra.MaximumNArgs(1),
  169. Short: "Generates and broadcasts evidence to a random node",
  170. RunE: func(cmd *cobra.Command, args []string) (err error) {
  171. amount := 1
  172. if len(args) == 1 {
  173. amount, err = strconv.Atoi(args[0])
  174. if err != nil {
  175. return err
  176. }
  177. }
  178. return InjectEvidence(cli.testnet, amount)
  179. },
  180. })
  181. cli.root.AddCommand(&cobra.Command{
  182. Use: "test",
  183. Short: "Runs test cases against a running testnet",
  184. RunE: func(cmd *cobra.Command, args []string) error {
  185. return Test(cli.testnet)
  186. },
  187. })
  188. cli.root.AddCommand(&cobra.Command{
  189. Use: "cleanup",
  190. Short: "Removes the testnet directory",
  191. RunE: func(cmd *cobra.Command, args []string) error {
  192. return Cleanup(cli.testnet)
  193. },
  194. })
  195. cli.root.AddCommand(&cobra.Command{
  196. Use: "logs [node]",
  197. Short: "Shows the testnet or a specefic node's logs",
  198. Example: "runner logs validator03",
  199. Args: cobra.MaximumNArgs(1),
  200. RunE: func(cmd *cobra.Command, args []string) error {
  201. if len(args) == 1 {
  202. return execComposeVerbose(cli.testnet.Dir, "logs", args[0])
  203. }
  204. return execComposeVerbose(cli.testnet.Dir, "logs")
  205. },
  206. })
  207. cli.root.AddCommand(&cobra.Command{
  208. Use: "tail [node]",
  209. Short: "Tails the testnet or a specific node's logs",
  210. Args: cobra.MaximumNArgs(1),
  211. RunE: func(cmd *cobra.Command, args []string) error {
  212. if len(args) == 1 {
  213. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow", args[0])
  214. }
  215. return execComposeVerbose(cli.testnet.Dir, "logs", "--follow")
  216. },
  217. })
  218. cli.root.AddCommand(&cobra.Command{
  219. Use: "benchmark",
  220. Short: "Benchmarks testnet",
  221. Long: `Benchmarks the following metrics:
  222. Mean Block Interval
  223. Standard Deviation
  224. Min Block Interval
  225. Max Block Interval
  226. over a 100 block sampling period.
  227. Does not run any perbutations.
  228. `,
  229. RunE: func(cmd *cobra.Command, args []string) error {
  230. if err := Cleanup(cli.testnet); err != nil {
  231. return err
  232. }
  233. if err := Setup(cli.testnet); err != nil {
  234. return err
  235. }
  236. chLoadResult := make(chan error)
  237. ctx, loadCancel := context.WithCancel(context.Background())
  238. defer loadCancel()
  239. go func() {
  240. err := Load(ctx, cli.testnet, 1)
  241. if err != nil {
  242. logger.Error(fmt.Sprintf("Transaction load failed: %v", err.Error()))
  243. }
  244. chLoadResult <- err
  245. }()
  246. if err := Start(cli.testnet); err != nil {
  247. return err
  248. }
  249. if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through
  250. return err
  251. }
  252. // we benchmark performance over the next 100 blocks
  253. if err := Benchmark(cli.testnet, 100); err != nil {
  254. return err
  255. }
  256. loadCancel()
  257. if err := <-chLoadResult; err != nil {
  258. return err
  259. }
  260. if err := Cleanup(cli.testnet); err != nil {
  261. return err
  262. }
  263. return nil
  264. },
  265. })
  266. return cli
  267. }
  268. // Run runs the CLI.
  269. func (cli *CLI) Run() {
  270. if err := cli.root.Execute(); err != nil {
  271. logger.Error(err.Error())
  272. os.Exit(1)
  273. }
  274. }