github.com/m3db/m3@v1.5.0/src/cmd/tools/dtest/tests/dtest.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package dtests 22 23 import ( 24 "fmt" 25 "os" 26 27 "github.com/m3db/m3/src/cmd/tools/dtest/config" 28 29 "github.com/spf13/cobra" 30 "go.uber.org/zap" 31 ) 32 33 var ( 34 // DTestCmd represents the base command when called without any subcommands 35 DTestCmd = &cobra.Command{ 36 Use: "dtest", 37 Short: "Command line tool to execute m3db dtests", 38 } 39 40 globalArgs = &config.Args{} 41 ) 42 43 // Run executes the dtest command. 44 func Run() { 45 if err := DTestCmd.Execute(); err != nil { 46 fmt.Println(err) 47 os.Exit(-1) 48 } 49 } 50 51 func init() { 52 DTestCmd.AddCommand( 53 seededBootstrapTestCmd, 54 simpleBootstrapTestCmd, 55 removeUpNodeTestCmd, 56 replaceUpNodeTestCmd, 57 replaceDownNodeTestCmd, 58 addDownNodeAndBringUpTestCmd, 59 removeDownNodeTestCmd, 60 addUpNodeRemoveTestCmd, 61 replaceUpNodeRemoveTestCmd, 62 replaceUpNodeRemoveUnseededTestCmd, 63 ) 64 65 globalArgs.RegisterFlags(DTestCmd) 66 } 67 68 func printUsage(cmd *cobra.Command) { 69 if err := cmd.Usage(); err != nil { 70 panic(err) 71 } 72 } 73 74 func panicIf(cond bool, msg string) { 75 if cond { 76 panic(msg) 77 } 78 } 79 80 func panicIfErr(err error, msg string) { 81 if err == nil { 82 return 83 } 84 errStr := err.Error() 85 panic(fmt.Errorf("%s: %s", msg, errStr)) 86 } 87 88 func newLogger(cmd *cobra.Command) *zap.Logger { 89 rawLogger, err := zap.NewDevelopment() 90 panicIfErr(err, "unable to create logger") 91 logger := rawLogger.Sugar() 92 logger.Infof("============== %v ==============", cmd.Name()) 93 desc := cmd.Long 94 if desc == "" { 95 desc = cmd.Short 96 } 97 logger.Infof("Test description: %v", desc) 98 logger.Infof("============================") 99 return rawLogger 100 }