From 8e90d1a996873e402714847a0864ec070d4f443d Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Wed, 2 Oct 2019 10:57:08 +0800 Subject: [PATCH] pd-ctl: fix output (#1765) (#1763) (cherry pick to release-3.1) (#1771) Signed-off-by: lhy1024 --- tests/pdctl/cluster/cluster_test.go | 17 ++++------------- tests/pdctl/helper.go | 18 ++++++++++++++++++ tests/pdctl/operator/operator_test.go | 10 ++++++++++ tests/pdctl/region/region_test.go | 16 +++------------- tests/pdctl/scheduler/scheduler_test.go | 9 +++++++++ tools/pd-ctl/main.go | 20 +++++++++++++------- tools/pd-ctl/pdctl/command/global.go | 2 +- tools/pd-ctl/pdctl/command/operator.go | 1 + tools/pd-ctl/pdctl/command/scheduler.go | 1 + 9 files changed, 60 insertions(+), 34 deletions(-) diff --git a/tests/pdctl/cluster/cluster_test.go b/tests/pdctl/cluster/cluster_test.go index d3ec229c656..69a2bed6c7e 100644 --- a/tests/pdctl/cluster/cluster_test.go +++ b/tests/pdctl/cluster/cluster_test.go @@ -15,9 +15,6 @@ package cluster_test import ( "encoding/json" - "io/ioutil" - "os" - "path/filepath" "strings" "testing" @@ -26,7 +23,6 @@ import ( "github.com/pingcap/pd/server" "github.com/pingcap/pd/tests" "github.com/pingcap/pd/tests/pdctl" - ctl "github.com/pingcap/pd/tools/pd-ctl/pdctl" ) func Test(t *testing.T) { @@ -50,6 +46,8 @@ func (s *clusterTestSuite) TestClusterAndPing(c *C) { c.Assert(err, IsNil) cluster.WaitLeader() pdAddr := cluster.GetConfig().GetClientURLs() + i := strings.Index(pdAddr, "//") + pdAddr = pdAddr[i+2:] cmd := pdctl.InitCommand() defer cluster.Destroy() @@ -61,15 +59,8 @@ func (s *clusterTestSuite) TestClusterAndPing(c *C) { c.Assert(json.Unmarshal(output, ci), IsNil) c.Assert(ci, DeepEquals, cluster.GetCluster()) - fname := filepath.Join(os.TempDir(), "stdout") - old := os.Stdout - temp, _ := os.Create(fname) - os.Stdout = temp - ctl.Start([]string{"-u", pdAddr, "--cacert=ca.pem", "cluster"}) - temp.Close() - os.Stdout = old - out, _ := ioutil.ReadFile(fname) - c.Assert(strings.Contains(string(out), "no such file or directory"), IsTrue) + echo := pdctl.GetEcho([]string{"-u", pdAddr, "--cacert=ca.pem", "cluster"}) + c.Assert(strings.Contains(echo, "no such file or directory"), IsTrue) // cluster status args = []string{"-u", pdAddr, "cluster", "status"} diff --git a/tests/pdctl/helper.go b/tests/pdctl/helper.go index 4ef65d8af74..f241cd2d660 100644 --- a/tests/pdctl/helper.go +++ b/tests/pdctl/helper.go @@ -17,6 +17,9 @@ import ( "bytes" "context" "fmt" + "io/ioutil" + "os" + "path/filepath" "sort" "github.com/pingcap/check" @@ -27,6 +30,7 @@ import ( "github.com/pingcap/pd/server/core" "github.com/pingcap/pd/tests" "github.com/pingcap/pd/tools/pd-ctl/pdctl" + ctl "github.com/pingcap/pd/tools/pd-ctl/pdctl" "github.com/pingcap/pd/tools/pd-ctl/pdctl/command" "github.com/spf13/cobra" ) @@ -132,3 +136,17 @@ func MustPutRegion(c *check.C, cluster *tests.TestCluster, regionID, storeID uin c.Assert(err, check.IsNil) return r } + +// GetEcho is used to get echo from stdout. +func GetEcho(args []string) string { + filename := filepath.Join(os.TempDir(), "stdout") + old := os.Stdout + temp, _ := os.Create(filename) + os.Stdout = temp + ctl.Start(args) + temp.Close() + os.Stdout = old + out, _ := ioutil.ReadFile(filename) + _ = os.Remove(filename) + return string(out) +} diff --git a/tests/pdctl/operator/operator_test.go b/tests/pdctl/operator/operator_test.go index 3693d817fc4..25425d2e18c 100644 --- a/tests/pdctl/operator/operator_test.go +++ b/tests/pdctl/operator/operator_test.go @@ -194,4 +194,14 @@ func (s *operatorTestSuite) TestOperator(c *C) { _, output, err = pdctl.ExecuteCommandC(cmd, args...) c.Assert(err, IsNil) c.Assert(strings.Contains(string(output), "scatter-region"), IsTrue) + + // test echo + echo := pdctl.GetEcho([]string{"-u", pdAddr, "operator", "remove", "1"}) + c.Assert(strings.Contains(echo, "Success!"), IsTrue) + echo = pdctl.GetEcho([]string{"-u", pdAddr, "operator", "add", "scatter-region", "1"}) + c.Assert(strings.Contains(echo, "Success!"), IsTrue) + echo = pdctl.GetEcho([]string{"-u", pdAddr, "operator", "remove", "1"}) + c.Assert(strings.Contains(echo, "Success!"), IsTrue) + echo = pdctl.GetEcho([]string{"-u", pdAddr, "operator", "remove", "1"}) + c.Assert(strings.Contains(echo, "Success!"), IsFalse) } diff --git a/tests/pdctl/region/region_test.go b/tests/pdctl/region/region_test.go index c916e2a6c2e..37569c8012b 100644 --- a/tests/pdctl/region/region_test.go +++ b/tests/pdctl/region/region_test.go @@ -15,9 +15,6 @@ package region_test import ( "encoding/json" - "io/ioutil" - "os" - "path/filepath" "strings" "testing" @@ -29,7 +26,6 @@ import ( "github.com/pingcap/pd/server/core" "github.com/pingcap/pd/tests" "github.com/pingcap/pd/tests/pdctl" - ctl "github.com/pingcap/pd/tools/pd-ctl/pdctl" ) func Test(t *testing.T) { @@ -60,15 +56,9 @@ func (s *regionTestSuite) TestRegionKeyFormat(c *C) { leaderServer := cluster.GetServer(cluster.GetLeader()) c.Assert(leaderServer.BootstrapCluster(), IsNil) pdctl.MustPutStore(c, leaderServer.GetServer(), store.Id, store.State, store.Labels) - fname := filepath.Join(os.TempDir(), "stdout") - old := os.Stdout - temp, _ := os.Create(fname) - os.Stdout = temp - ctl.Start([]string{"-u", url, "region", "key", "--format=raw", " "}) - temp.Close() - os.Stdout = old - out, _ := ioutil.ReadFile(fname) - c.Assert(strings.Contains(string(out), "unknown flag"), IsFalse) + + echo := pdctl.GetEcho([]string{"-u", url, "region", "key", "--format=raw", " "}) + c.Assert(strings.Contains(string(echo), "unknown flag"), IsFalse) } func (s *regionTestSuite) TestRegion(c *C) { diff --git a/tests/pdctl/scheduler/scheduler_test.go b/tests/pdctl/scheduler/scheduler_test.go index 333a81c634f..69a4036204c 100644 --- a/tests/pdctl/scheduler/scheduler_test.go +++ b/tests/pdctl/scheduler/scheduler_test.go @@ -15,6 +15,7 @@ package scheduler_test import ( "encoding/json" + "strings" "testing" "time" @@ -131,4 +132,12 @@ func (s *schedulerTestSuite) TestScheduler(c *C) { for _, scheduler := range schedulers { c.Assert(expected[scheduler], Equals, true) } + + // test echo + echo := pdctl.GetEcho([]string{"-u", pdAddr, "scheduler", "add", "balance-region-scheduler"}) + c.Assert(strings.Contains(echo, "Success!"), IsTrue) + echo = pdctl.GetEcho([]string{"-u", pdAddr, "scheduler", "remove", "balance-region-scheduler"}) + c.Assert(strings.Contains(echo, "Success!"), IsTrue) + echo = pdctl.GetEcho([]string{"-u", pdAddr, "scheduler", "remove", "balance-region-scheduler"}) + c.Assert(strings.Contains(echo, "Success!"), IsFalse) } diff --git a/tools/pd-ctl/main.go b/tools/pd-ctl/main.go index a544e656ce9..9625960ab15 100644 --- a/tools/pd-ctl/main.go +++ b/tools/pd-ctl/main.go @@ -34,19 +34,21 @@ var ( detach bool interact bool version bool + help bool caPath string certPath string keyPath string ) func init() { - flag.StringVarP(&url, "pd", "u", "http://127.0.0.1:2379", "The pd address") - flag.BoolVarP(&detach, "detach", "d", true, "Run pdctl without readline") - flag.BoolVarP(&interact, "interact", "i", false, "Run pdctl with readline") - flag.BoolVarP(&version, "version", "V", false, "print version information and exit") - flag.StringVar(&caPath, "cacert", "", "path of file that contains list of trusted SSL CAs.") - flag.StringVar(&certPath, "cert", "", "path of file that contains X509 certificate in PEM format.") - flag.StringVar(&keyPath, "key", "", "path of file that contains X509 key in PEM format.") + flag.StringVarP(&url, "pd", "u", "http://127.0.0.1:2379", "The pd address.") + flag.BoolVarP(&detach, "detach", "d", true, "Run pdctl without readline.") + flag.BoolVarP(&interact, "interact", "i", false, "Run pdctl with readline.") + flag.BoolVarP(&version, "version", "V", false, "Print version information and exit.") + flag.StringVar(&caPath, "cacert", "", "The path of file that contains list of trusted SSL CAs.") + flag.StringVar(&certPath, "cert", "", "The path of file that contains X509 certificate in PEM format.") + flag.StringVar(&keyPath, "key", "", "The path of file that contains X509 key in PEM format.") + flag.BoolVarP(&help, "help", "h", false, "Help message.") } func main() { @@ -57,6 +59,10 @@ func main() { flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true flag.Parse() + if help { + flag.Usage() + os.Exit(0) + } if version { server.PrintPDInfo() os.Exit(0) diff --git a/tools/pd-ctl/pdctl/command/global.go b/tools/pd-ctl/pdctl/command/global.go index 2082748fb9e..a564ba983ad 100644 --- a/tools/pd-ctl/pdctl/command/global.go +++ b/tools/pd-ctl/pdctl/command/global.go @@ -146,7 +146,7 @@ func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) { cmd.Println(err) } } else { - cmd.Println("success!") + cmd.Println("Success!") } } diff --git a/tools/pd-ctl/pdctl/command/operator.go b/tools/pd-ctl/pdctl/command/operator.go index c2a541090b0..34092929031 100644 --- a/tools/pd-ctl/pdctl/command/operator.go +++ b/tools/pd-ctl/pdctl/command/operator.go @@ -408,6 +408,7 @@ func removeOperatorCommandFunc(cmd *cobra.Command, args []string) { cmd.Println(err) return } + cmd.Println("Success!") } func parseUint64s(args []string) ([]uint64, error) { diff --git a/tools/pd-ctl/pdctl/command/scheduler.go b/tools/pd-ctl/pdctl/command/scheduler.go index 8d4d27e2552..b25bd3ef7ff 100644 --- a/tools/pd-ctl/pdctl/command/scheduler.go +++ b/tools/pd-ctl/pdctl/command/scheduler.go @@ -315,4 +315,5 @@ func removeSchedulerCommandFunc(cmd *cobra.Command, args []string) { cmd.Println(err) return } + cmd.Println("Success!") }