diff --git a/go/vt/vtctl/gorpcvtctlclient/client_test.go b/go/vt/vtctl/gorpcvtctlclient/client_test.go index e31395c6572..f5f52c22129 100644 --- a/go/vt/vtctl/gorpcvtctlclient/client_test.go +++ b/go/vt/vtctl/gorpcvtctlclient/client_test.go @@ -5,7 +5,6 @@ package gorpcvtctlclient import ( - "fmt" "net" "net/http" "testing" @@ -27,13 +26,12 @@ func TestVtctlServer(t *testing.T) { if err != nil { t.Fatalf("Cannot listen: %v", err) } - port := listener.Addr().(*net.TCPAddr).Port // Create a Go Rpc server and listen on the port server := rpcplus.NewServer() server.Register(gorpcvtctlserver.NewVtctlServer(ts)) - // create the HTTP server, serve the server from it + // Create the HTTP server, serve the server from it handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, server, false) httpServer := http.Server{ @@ -42,7 +40,7 @@ func TestVtctlServer(t *testing.T) { go httpServer.Serve(listener) // Create a VtctlClient Go Rpc client to talk to the fake server - client, err := goRPCVtctlClientFactory(fmt.Sprintf("localhost:%v", port), 30*time.Second) + client, err := goRPCVtctlClientFactory(listener.Addr().String(), 30*time.Second) if err != nil { t.Fatalf("Cannot create client: %v", err) } diff --git a/go/vt/wrangler/testlib/backup_test.go b/go/vt/wrangler/testlib/backup_test.go index 8ac84d35d25..ad54745bf57 100644 --- a/go/vt/wrangler/testlib/backup_test.go +++ b/go/vt/wrangler/testlib/backup_test.go @@ -29,6 +29,8 @@ func TestBackupRestore(t *testing.T) { ctx := context.Background() ts := zktopo.NewTestServer(t, []string{"cell1", "cell2"}) wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), time.Second) + vp := NewVtctlPipe(t, ts) + defer vp.Close() // Initialize our temp dirs root, err := ioutil.TempDir("", "backuptest") @@ -88,22 +90,10 @@ func TestBackupRestore(t *testing.T) { sourceTablet.StartActionLoop(t, wr) defer sourceTablet.StopActionLoop(t) - ti, err := ts.GetTablet(ctx, sourceTablet.Tablet.Alias) - if err != nil { - t.Fatalf("GetTablet failed: %v", err) - } - // run the backup - logStream, errFunc, err := wr.TabletManagerClient().Backup(ctx, ti, 4) - if err != nil { + if err := vp.Run([]string{"Backup", sourceTablet.Tablet.Alias.String()}); err != nil { t.Fatalf("Backup failed: %v", err) } - for e := range logStream { - t.Logf("%v", e) - } - if err := errFunc(); err != nil { - t.Fatalf("Backup errFunc failed: %v", err) - } // verify the full status if err := sourceTablet.FakeMysqlDaemon.CheckSuperQueryList(); err != nil { diff --git a/go/vt/wrangler/testlib/vtctl_pipe.go b/go/vt/wrangler/testlib/vtctl_pipe.go new file mode 100644 index 00000000000..6be11a39565 --- /dev/null +++ b/go/vt/wrangler/testlib/vtctl_pipe.go @@ -0,0 +1,84 @@ +// Copyright 2015, Google Inc. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package testlib + +import ( + "net" + "net/http" + "testing" + "time" + + "github.com/youtube/vitess/go/rpcplus" + "github.com/youtube/vitess/go/rpcwrap/bsonrpc" + "github.com/youtube/vitess/go/vt/topo" + "github.com/youtube/vitess/go/vt/vtctl/gorpcvtctlserver" + "github.com/youtube/vitess/go/vt/vtctl/vtctlclient" + "golang.org/x/net/context" + + // we need to import the gorpcvtctlclient library so the go rpc + // vtctl client is registered and can be used. + _ "github.com/youtube/vitess/go/vt/vtctl/gorpcvtctlclient" +) + +// VtctlPipe is a vtctl server based on a topo server, and a client that +// is connected to it via bson rpc. +type VtctlPipe struct { + listener net.Listener + client vtctlclient.VtctlClient + t *testing.T +} + +// NewVtctlPipe creates a new VtctlPipe based on the given topo server. +func NewVtctlPipe(t *testing.T, ts topo.Server) *VtctlPipe { + // Listen on a random port + listener, err := net.Listen("tcp", ":0") + if err != nil { + t.Fatalf("Cannot listen: %v", err) + } + + // Create a Go Rpc server and listen on the port + server := rpcplus.NewServer() + server.Register(gorpcvtctlserver.NewVtctlServer(ts)) + + // Create the HTTP server, serve the server from it + handler := http.NewServeMux() + bsonrpc.ServeCustomRPC(handler, server, false) + httpServer := http.Server{ + Handler: handler, + } + go httpServer.Serve(listener) + + // Create a VtctlClient Go Rpc client to talk to the fake server + client, err := vtctlclient.New(listener.Addr().String(), 30*time.Second) + if err != nil { + t.Fatalf("Cannot create client: %v", err) + } + + return &VtctlPipe{ + listener: listener, + client: client, + t: t, + } +} + +// Close will stop listening and free up all resources. +func (vp *VtctlPipe) Close() { + vp.client.Close() + vp.listener.Close() +} + +// Run executes the provided command remotely, logs the output in the +// test logs, and returns the command error. +func (vp *VtctlPipe) Run(args []string) error { + actionTimeout := 30 * time.Second + lockTimeout := 10 * time.Second + ctx := context.Background() + + c, errFunc := vp.client.ExecuteVtctlCommand(ctx, args, actionTimeout, lockTimeout) + for le := range c { + vp.t.Logf(le.String()) + } + return errFunc() +}