From 8a9c4bc3bb269381ca971f31546d358cf131d920 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Wed, 18 Sep 2019 11:17:47 +0800 Subject: [PATCH 1/3] Fix pd-ctl help output (#1763) Signed-off-by: lhy1024 (cherry picked from commit 682b5013e6cb96550efc05930018fe27549ca457) --- tools/pd-ctl/main.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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) From c0217dc42fa8a0a30f28301cdc38cbde69d29c7a Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Tue, 24 Sep 2019 15:08:17 +0800 Subject: [PATCH 2/3] Fix pd-ctl echo output (#1765) Signed-off-by: lhy1024 (cherry picked from commit c973515613157209832d943d698fc3e2133624a5) Signed-off-by: lhy1024 --- tests/pdctl/cluster/cluster_test.go | 17 ++++------------- tests/pdctl/helper.go | 18 ++++++++++++++++++ tests/pdctl/operator/operator_test.go | 8 ++++++++ tests/pdctl/region/region_test.go | 16 +++------------- tests/pdctl/scheduler/scheduler_test.go | 9 +++++++++ tools/pd-ctl/pdctl/command/operator.go | 1 + tools/pd-ctl/pdctl/command/scheduler.go | 1 + 7 files changed, 44 insertions(+), 26 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..4ddc7064df2 100644 --- a/tests/pdctl/operator/operator_test.go +++ b/tests/pdctl/operator/operator_test.go @@ -194,4 +194,12 @@ 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", "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/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!") } From 205748367baef69e2652fe87b084dc2362315fd7 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Thu, 26 Sep 2019 11:46:07 +0800 Subject: [PATCH 3/3] fix test and misspell Signed-off-by: lhy1024 --- tests/pdctl/operator/operator_test.go | 4 +++- tools/pd-ctl/pdctl/command/global.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/pdctl/operator/operator_test.go b/tests/pdctl/operator/operator_test.go index 4ddc7064df2..25425d2e18c 100644 --- a/tests/pdctl/operator/operator_test.go +++ b/tests/pdctl/operator/operator_test.go @@ -196,7 +196,9 @@ func (s *operatorTestSuite) TestOperator(c *C) { c.Assert(strings.Contains(string(output), "scatter-region"), IsTrue) // test echo - echo := pdctl.GetEcho([]string{"-u", pdAddr, "operator", "add", "scatter-region", "1"}) + 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) 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!") } }