Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pd-ctl: fix output (#1765) (#1763) (cherry pick to release-3.0) #1770

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions tests/pdctl/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ package cluster_test

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -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) {
Expand All @@ -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()

Expand All @@ -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"}
Expand Down
18 changes: 18 additions & 0 deletions tests/pdctl/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"

"github.com/pingcap/check"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions tests/pdctl/operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 3 additions & 13 deletions tests/pdctl/region/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ package region_test

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/pdctl/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package scheduler_test

import (
"encoding/json"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
20 changes: 13 additions & 7 deletions tools/pd-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
}

Expand Down
1 change: 1 addition & 0 deletions tools/pd-ctl/pdctl/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ func removeOperatorCommandFunc(cmd *cobra.Command, args []string) {
cmd.Println(err)
return
}
cmd.Println("Success!")
}

func parseUint64s(args []string) ([]uint64, error) {
Expand Down
1 change: 1 addition & 0 deletions tools/pd-ctl/pdctl/command/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,5 @@ func removeSchedulerCommandFunc(cmd *cobra.Command, args []string) {
cmd.Println(err)
return
}
cmd.Println("Success!")
}