From 926b21a776acc557314a4446d5137d7af98b9d47 Mon Sep 17 00:00:00 2001 From: Arindam Nayak Date: Thu, 31 Oct 2019 18:15:20 +0530 Subject: [PATCH 1/3] coverted vtctld_test.py to go Signed-off-by: Arindam Nayak --- go/test/endtoend/cluster/cluster_process.go | 4 +- go/test/endtoend/clustertest/vtcltd_test.go | 123 +++++++++++++++++++- go/vt/vtctl/grpcvtctlclient/client.go | 5 + 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/go/test/endtoend/cluster/cluster_process.go b/go/test/endtoend/cluster/cluster_process.go index e9e9a61733d..da542fdac77 100644 --- a/go/test/endtoend/cluster/cluster_process.go +++ b/go/test/endtoend/cluster/cluster_process.go @@ -80,6 +80,7 @@ type Vttablet struct { HTTPPort int GrpcPort int MySQLPort int + Alias string // background executable processes mysqlctlProcess MysqlctlProcess @@ -187,6 +188,7 @@ func (cluster *LocalProcessCluster) StartKeyspace(keyspace Keyspace, shardNames cluster.Hostname, cluster.TmpDirectory, cluster.VtTabletExtraArgs) + tablet.Alias = tablet.vttabletProcess.TabletPath log.Info(fmt.Sprintf("Starting vttablet for tablet uid %d, grpc port %d", tablet.TabletUID, tablet.GrpcPort)) if err = tablet.vttabletProcess.Setup(); err != nil { @@ -238,7 +240,7 @@ func (cluster *LocalProcessCluster) StartVtgate() (err error) { cluster.VtgateMySQLPort, cluster.Cell, cluster.Cell, - cluster.Hostname, + cluster.Hostname, "MASTER,REPLICA", cluster.topoProcess.Port, cluster.TmpDirectory, diff --git a/go/test/endtoend/clustertest/vtcltd_test.go b/go/test/endtoend/clustertest/vtcltd_test.go index 4704fd7f99a..3a504c1fb0c 100644 --- a/go/test/endtoend/clustertest/vtcltd_test.go +++ b/go/test/endtoend/clustertest/vtcltd_test.go @@ -18,11 +18,132 @@ limitations under the License. package clustertest import ( + "context" + "encoding/json" "fmt" + "io/ioutil" + "net/http" + "reflect" + "strings" "testing" + "time" + + "github.com/smartystreets/assertions" + vtctl "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient" +) + +var ( + oneTableOutput = ` ++---+ +| a | ++---+ +| 1 | ++---+ +` ) func TestVtctldProcess(t *testing.T) { - url := fmt.Sprintf("http://localhost:%d/api/keyspaces/", clusterInstance.VtctldHTTPPort) + url := fmt.Sprintf("http://%s:%d/api/keyspaces/", clusterInstance.Hostname, clusterInstance.VtctldHTTPPort) testURL(t, url, "keyspace url") + + healthCheckURL := fmt.Sprintf("http://%s:%d/debug/health/", clusterInstance.Hostname, clusterInstance.VtctldHTTPPort) + testURL(t, healthCheckURL, "vtctld health check url") + + url = fmt.Sprintf("http://%s:%d/api/topodata/", clusterInstance.Hostname, clusterInstance.VtctldHTTPPort) + + testTopoDataAPI(url) + testListAllTablets() + testTabletStatus() + testExecuteAsDba() + testExecuteAsApp() + testListAllTabletsUsingVtctlGrpc() +} + +func testTopoDataAPI(url string) { + resp, err := http.Get(url) + assertions.ShouldBeNil(err) + assertions.ShouldEqual(resp.StatusCode, 200) + + resultMap := make(map[string]interface{}) + respByte, _ := ioutil.ReadAll(resp.Body) + err = json.Unmarshal(respByte, &resultMap) + assertions.ShouldBeNil(err) + + errorValue := reflect.ValueOf(resultMap["Error"]) + assertions.ShouldBeEmpty(errorValue.String()) + + assertions.ShouldContainKey(resultMap, "Children") + children := reflect.ValueOf(resultMap["Children"]) + assertions.ShouldContain(children, "global") + assertions.ShouldContain(children, clusterInstance.Cell) +} + +func testListAllTablets() { + result, err := clusterInstance.VtctlclientProcess.ExecuteCommandWithOutput("ListAllTablets", clusterInstance.Cell) + assertions.ShouldBeNil(err) + + tablets := getAllTablets() + + tabletsFromCMD := strings.Split(result, "\n") + fmt.Println("len from cmd: " + fmt.Sprintf("%d", len(tabletsFromCMD))) + fmt.Println("len from tab: " + fmt.Sprintf("%d", len(tabletsFromCMD))) + + assertions.ShouldEqual(len(tabletsFromCMD), len(tablets)) + + for _, line := range tabletsFromCMD { + assertions.ShouldContain(tablets, strings.Split(line, " ")[0]) + } +} + +func testTabletStatus() { + resp, err := http.Get(fmt.Sprintf("http://%s:%d", clusterInstance.Hostname, clusterInstance.Keyspaces[0].Shards[0].Vttablets[0].HTTPPort)) + assertions.ShouldBeNil(err) + respByte, err := ioutil.ReadAll(resp.Body) + assertions.ShouldBeNil(err) + result := string(respByte) + assertions.ShouldBeTrue(strings.Contains(result, `Polling health information from. MySQLReplicationLag`)) + assertions.ShouldBeTrue(strings.Contains(result, `Alias: Date: Fri, 1 Nov 2019 17:00:05 +0530 Subject: [PATCH 2/3] changed assertions library to use the existing one Signed-off-by: Arindam Nayak --- go/test/endtoend/clustertest/vtcltd_test.go | 91 +++++++++++---------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/go/test/endtoend/clustertest/vtcltd_test.go b/go/test/endtoend/clustertest/vtcltd_test.go index 3a504c1fb0c..a9cceb1c784 100644 --- a/go/test/endtoend/clustertest/vtcltd_test.go +++ b/go/test/endtoend/clustertest/vtcltd_test.go @@ -24,17 +24,18 @@ import ( "io/ioutil" "net/http" "reflect" + "regexp" "strings" "testing" "time" - "github.com/smartystreets/assertions" + "github.com/stretchr/testify/assert" + vtctl "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient" ) var ( - oneTableOutput = ` -+---+ + oneTableOutput = `+---+ | a | +---+ | 1 | @@ -51,87 +52,93 @@ func TestVtctldProcess(t *testing.T) { url = fmt.Sprintf("http://%s:%d/api/topodata/", clusterInstance.Hostname, clusterInstance.VtctldHTTPPort) - testTopoDataAPI(url) - testListAllTablets() - testTabletStatus() - testExecuteAsDba() - testExecuteAsApp() - testListAllTabletsUsingVtctlGrpc() + testTopoDataAPI(t, url) + testListAllTablets(t) + testTabletStatus(t) + testExecuteAsDba(t) + testExecuteAsApp(t) + testListAllTabletsUsingVtctlGrpc(t) } -func testTopoDataAPI(url string) { +func testTopoDataAPI(t *testing.T, url string) { resp, err := http.Get(url) - assertions.ShouldBeNil(err) - assertions.ShouldEqual(resp.StatusCode, 200) + assert.Nil(t, err) + assert.Equal(t, resp.StatusCode, 200) resultMap := make(map[string]interface{}) respByte, _ := ioutil.ReadAll(resp.Body) err = json.Unmarshal(respByte, &resultMap) - assertions.ShouldBeNil(err) + assert.Nil(t, err) errorValue := reflect.ValueOf(resultMap["Error"]) - assertions.ShouldBeEmpty(errorValue.String()) + assert.Empty(t, errorValue.String()) - assertions.ShouldContainKey(resultMap, "Children") + assert.Contains(t, resultMap, "Children") children := reflect.ValueOf(resultMap["Children"]) - assertions.ShouldContain(children, "global") - assertions.ShouldContain(children, clusterInstance.Cell) + childrenGot := fmt.Sprintf("%s", children) + assert.Contains(t, childrenGot, "global") + assert.Contains(t, childrenGot, clusterInstance.Cell) } -func testListAllTablets() { +func testListAllTablets(t *testing.T) { result, err := clusterInstance.VtctlclientProcess.ExecuteCommandWithOutput("ListAllTablets", clusterInstance.Cell) - assertions.ShouldBeNil(err) + assert.Nil(t, err) tablets := getAllTablets() tabletsFromCMD := strings.Split(result, "\n") - fmt.Println("len from cmd: " + fmt.Sprintf("%d", len(tabletsFromCMD))) - fmt.Println("len from tab: " + fmt.Sprintf("%d", len(tabletsFromCMD))) - - assertions.ShouldEqual(len(tabletsFromCMD), len(tablets)) + tabletCountFromCMD := 0 for _, line := range tabletsFromCMD { - assertions.ShouldContain(tablets, strings.Split(line, " ")[0]) + if len(line) > 0 { + tabletCountFromCMD = tabletCountFromCMD + 1 + assert.Contains(t, tablets, strings.Split(line, " ")[0]) + } } + assert.Equal(t, tabletCountFromCMD, len(tablets)) } -func testTabletStatus() { +func testTabletStatus(t *testing.T) { resp, err := http.Get(fmt.Sprintf("http://%s:%d", clusterInstance.Hostname, clusterInstance.Keyspaces[0].Shards[0].Vttablets[0].HTTPPort)) - assertions.ShouldBeNil(err) + assert.Nil(t, err) respByte, err := ioutil.ReadAll(resp.Body) - assertions.ShouldBeNil(err) + assert.Nil(t, err) result := string(respByte) - assertions.ShouldBeTrue(strings.Contains(result, `Polling health information from. MySQLReplicationLag`)) - assertions.ShouldBeTrue(strings.Contains(result, `Alias: Date: Mon, 4 Nov 2019 11:29:09 +0530 Subject: [PATCH 3/3] reverted grpc testing for vtctl Signed-off-by: Arindam Nayak --- go/test/endtoend/clustertest/vtcltd_test.go | 23 --------------------- go/vt/vtctl/grpcvtctlclient/client.go | 5 ----- 2 files changed, 28 deletions(-) diff --git a/go/test/endtoend/clustertest/vtcltd_test.go b/go/test/endtoend/clustertest/vtcltd_test.go index a9cceb1c784..97a157f3458 100644 --- a/go/test/endtoend/clustertest/vtcltd_test.go +++ b/go/test/endtoend/clustertest/vtcltd_test.go @@ -18,7 +18,6 @@ limitations under the License. package clustertest import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -27,11 +26,8 @@ import ( "regexp" "strings" "testing" - "time" "github.com/stretchr/testify/assert" - - vtctl "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient" ) var ( @@ -57,7 +53,6 @@ func TestVtctldProcess(t *testing.T) { testTabletStatus(t) testExecuteAsDba(t) testExecuteAsApp(t) - testListAllTabletsUsingVtctlGrpc(t) } func testTopoDataAPI(t *testing.T, url string) { @@ -125,24 +120,6 @@ func testExecuteAsApp(t *testing.T) { assert.Equal(t, result, oneTableOutput) } -func testListAllTabletsUsingVtctlGrpc(t *testing.T) { - client, err := vtctl.NewClient(clusterInstance.VtctlclientProcess.Server) - assert.Nil(t, err) - - ctx := context.Background() - stream, err := client.ExecuteVtctlCommand(ctx, []string{"ListAllTablets", clusterInstance.Cell}, 1*time.Second) - assert.Nil(t, err) - - tablets := getAllTablets() - for range tablets { - result, err := stream.Recv() - assert.Nil(t, err) - assert.NotNil(t, result) - assert.Contains(t, tablets, strings.Split(result.Value, " ")[0]) - } - client.Close() -} - func getAllTablets() []string { tablets := make([]string, 0) for _, ks := range clusterInstance.Keyspaces { diff --git a/go/vt/vtctl/grpcvtctlclient/client.go b/go/vt/vtctl/grpcvtctlclient/client.go index c001935492d..62f5c5d00eb 100644 --- a/go/vt/vtctl/grpcvtctlclient/client.go +++ b/go/vt/vtctl/grpcvtctlclient/client.go @@ -96,8 +96,3 @@ func (client *gRPCVtctlClient) Close() { func init() { vtctlclient.RegisterFactory("grpc", gRPCVtctlClientFactory) } - -// NewClient instantiates a vtctl client instance from address -func NewClient(address string) (vtctlclient.VtctlClient, error) { - return gRPCVtctlClientFactory(address) -}