forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
107 lines (92 loc) · 3.43 KB
/
client.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
// Copyright 2014, 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 vtctlclienttest provides testing library for vtctl
// implementations to use in their tests.
package vtctlclienttest
import (
"strings"
"testing"
"time"
"github.com/youtube/vitess/go/vt/tabletmanager/tmclient"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtctl/vtctlclient"
"github.com/youtube/vitess/go/vt/zktopo"
"golang.org/x/net/context"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
// import the gRPC client implementation for tablet manager
_ "github.com/youtube/vitess/go/vt/tabletmanager/grpctmclient"
)
func init() {
// enforce we will use the right protocol (gRPC) (note the
// client is unused, but it is initialized, so it needs to exist)
*tmclient.TabletManagerProtocol = "grpc"
}
// CreateTopoServer returns the test topo server properly configured
func CreateTopoServer(t *testing.T) topo.Server {
return zktopo.NewTestServer(t, []string{"cell1", "cell2"})
}
// TestSuite runs the test suite on the given topo server and client
func TestSuite(t *testing.T, ts topo.Server, client vtctlclient.VtctlClient) {
ctx := context.Background()
// Create a fake tablet
tablet := &pb.Tablet{
Alias: &pb.TabletAlias{Cell: "cell1", Uid: 1},
Hostname: "localhost",
Ip: "10.11.12.13",
PortMap: map[string]int32{
"vt": 3333,
"mysql": 3334,
},
Tags: map[string]string{"tag": "value"},
Keyspace: "test_keyspace",
Type: pb.TabletType_MASTER,
}
if err := ts.CreateTablet(ctx, tablet); err != nil {
t.Errorf("CreateTablet: %v", err)
}
// run a command that's gonna return something on the log channel
logs, errFunc, err := client.ExecuteVtctlCommand(ctx, []string{"ListAllTablets", "cell1"}, 30*time.Second, 10*time.Second)
if err != nil {
t.Fatalf("Remote error: %v", err)
}
count := 0
for e := range logs {
expected := "cell1-0000000001 test_keyspace <null> master localhost:3333 localhost:3334 [tag: \"value\"]\n"
if e.String() != expected {
t.Errorf("Got unexpected log line '%v' expected '%v'", e.String(), expected)
}
count++
}
if count != 1 {
t.Errorf("Didn't get expected log line only, got %v lines", count)
}
if err := errFunc(); err != nil {
t.Fatalf("Remote error: %v", err)
}
// run a command that's gonna fail
logs, errFunc, err = client.ExecuteVtctlCommand(ctx, []string{"ListAllTablets", "cell2"}, 30*time.Second, 10*time.Second)
if err != nil {
t.Fatalf("Remote error: %v", err)
}
if e, ok := <-logs; ok {
t.Errorf("Got unexpected line for logs: %v", e.String())
}
expected := "node doesn't exist"
if err := errFunc(); err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Unexpected remote error, got: '%v' was expecting to find '%v'", err, expected)
}
// run a command that's gonna panic
logs, errFunc, err = client.ExecuteVtctlCommand(ctx, []string{"Panic"}, 30*time.Second, 10*time.Second)
if err != nil {
t.Fatalf("Remote error: %v", err)
}
if e, ok := <-logs; ok {
t.Errorf("Got unexpected line for logs: %v", e.String())
}
expected1 := "this command panics on purpose"
expected2 := "uncaught vtctl panic"
if err := errFunc(); err == nil || !strings.Contains(err.Error(), expected1) || !strings.Contains(err.Error(), expected2) {
t.Fatalf("Unexpected remote error, got: '%v' was expecting to find '%v' and '%v'", err, expected1, expected2)
}
}