From c973515613157209832d943d698fc3e2133624a5 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Tue, 24 Sep 2019 15:08:17 +0800 Subject: [PATCH] Fix pd-ctl echo output (#1765) Signed-off-by: lhy1024 --- tests/pdctl/cluster/cluster_test.go | 16 ++-------------- tests/pdctl/helper.go | 18 ++++++++++++++++++ tests/pdctl/operator/operator_test.go | 8 ++++++++ tests/pdctl/region/region_test.go | 17 +++-------------- 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, 42 insertions(+), 28 deletions(-) diff --git a/tests/pdctl/cluster/cluster_test.go b/tests/pdctl/cluster/cluster_test.go index 5dbaadccc2d..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) { @@ -63,16 +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) - os.Remove(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 f2c3c471d95..39898c8d8eb 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 eb3af191674..6e7128db13c 100644 --- a/tests/pdctl/operator/operator_test.go +++ b/tests/pdctl/operator/operator_test.go @@ -195,4 +195,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 dacb279f95d..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,16 +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) - os.Remove(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!") }