forked from vitessio/vitess
/
vtctl_pipe.go
129 lines (109 loc) · 3.27 KB
/
vtctl_pipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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 (
"bytes"
"flag"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
"google.golang.org/grpc"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/servenv"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtctl/grpcvtctlserver"
"github.com/youtube/vitess/go/vt/vtctl/vtctlclient"
"golang.org/x/net/context"
// we need to import the grpcvtctlclient library so the gRPC
// vtctl client is registered and can be used.
_ "github.com/youtube/vitess/go/vt/vtctl/grpcvtctlclient"
)
var servenvInitialized sync.Once
// VtctlPipe is a vtctl server based on a topo server, and a client that
// is connected to it via gRPC.
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 {
// Register all vtctl commands
servenvInitialized.Do(func() {
// make sure we use the right protocol
flag.Set("vtctl_client_protocol", "grpc")
// Enable all query groups
flag.Set("enable_queries", "true")
servenv.FireRunHooks()
})
// Listen on a random port
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("Cannot listen: %v", err)
}
// Create a gRPC server and listen on the port
server := grpc.NewServer()
grpcvtctlserver.StartServer(server, ts)
go server.Serve(listener)
// Create a VtctlClient gRPC 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 {
return vp.run(args, func(line string) {
vp.t.Log(line)
})
}
// RunAndOutput is similar to Run, but returns the output as a multi-line string
// instead of logging it.
func (vp *VtctlPipe) RunAndOutput(args []string) (string, error) {
var output bytes.Buffer
err := vp.run(args, func(line string) {
output.WriteString(line)
})
return output.String(), err
}
func (vp *VtctlPipe) run(args []string, outputFunc func(string)) error {
actionTimeout := 30 * time.Second
ctx := context.Background()
stream, err := vp.client.ExecuteVtctlCommand(ctx, args, actionTimeout)
if err != nil {
return fmt.Errorf("VtctlPipe.Run() failed: %v", err)
}
for {
le, err := stream.Recv()
switch err {
case nil:
outputFunc(logutil.EventString(le))
case io.EOF:
return nil
default:
return err
}
}
}
// RunAndStreamOutput returns the output of the vtctl command as a channel.
// When the channcel is closed, the command did finish.
func (vp *VtctlPipe) RunAndStreamOutput(args []string) (logutil.EventStream, error) {
actionTimeout := 30 * time.Second
ctx := context.Background()
return vp.client.ExecuteVtctlCommand(ctx, args, actionTimeout)
}