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

Vtctld Test cases in Go using cluster #25

Merged
merged 4 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion go/test/endtoend/cluster/cluster_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Vttablet struct {
HTTPPort int
GrpcPort int
MySQLPort int
Alias string

// background executable processes
mysqlctlProcess MysqlctlProcess
Expand Down Expand Up @@ -187,6 +188,7 @@ func (cluster *LocalProcessCluster) StartKeyspace(keyspace Keyspace, shardNames
cluster.Hostname,
cluster.TmpDirectory,
cluster.VtTabletExtraArgs)
tablet.Alias = tablet.vttabletProcess.TabletPath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when does the TabletPath get set and what is it set to? Alias is usually of the form cell1-00000001 (cell + uid).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, tabletpath is set as cell1-000000001 here.

log.Info(fmt.Sprintf("Starting vttablet for tablet uid %d, grpc port %d", tablet.TabletUID, tablet.GrpcPort))

if err = tablet.vttabletProcess.Setup(); err != nil {
Expand Down Expand Up @@ -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,
Expand Down
107 changes: 106 additions & 1 deletion go/test/endtoend/clustertest/vtcltd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,116 @@ limitations under the License.
package clustertest

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

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(t, url)
testListAllTablets(t)
testTabletStatus(t)
testExecuteAsDba(t)
testExecuteAsApp(t)
}

func testTopoDataAPI(t *testing.T, url string) {
resp, err := http.Get(url)
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)
assert.Nil(t, err)

errorValue := reflect.ValueOf(resultMap["Error"])
assert.Empty(t, errorValue.String())

assert.Contains(t, resultMap, "Children")
children := reflect.ValueOf(resultMap["Children"])
childrenGot := fmt.Sprintf("%s", children)
assert.Contains(t, childrenGot, "global")
assert.Contains(t, childrenGot, clusterInstance.Cell)
}

func testListAllTablets(t *testing.T) {
result, err := clusterInstance.VtctlclientProcess.ExecuteCommandWithOutput("ListAllTablets", clusterInstance.Cell)
assert.Nil(t, err)

tablets := getAllTablets()

tabletsFromCMD := strings.Split(result, "\n")
tabletCountFromCMD := 0

for _, line := range tabletsFromCMD {
if len(line) > 0 {
tabletCountFromCMD = tabletCountFromCMD + 1
assert.Contains(t, tablets, strings.Split(line, " ")[0])
}
}
assert.Equal(t, tabletCountFromCMD, len(tablets))
}

func testTabletStatus(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s:%d", clusterInstance.Hostname, clusterInstance.Keyspaces[0].Shards[0].Vttablets[0].HTTPPort))
assert.Nil(t, err)
respByte, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
result := string(respByte)
println(result)
println(strings.Contains(result, "Polling health information from."))
matched, err := regexp.Match(`Polling health information from.+MySQLReplicationLag`, []byte(result))
assert.Nil(t, err)
assert.True(t, matched)
assert.True(t, strings.Contains(result, `Alias: <a href="http://localhost:`))
assert.True(t, strings.Contains(result, `</html>`))
}

func testExecuteAsDba(t *testing.T) {
result, err := clusterInstance.VtctlclientProcess.ExecuteCommandWithOutput("ExecuteFetchAsDba", clusterInstance.Keyspaces[0].Shards[0].Vttablets[0].Alias, `SELECT 1 AS a`)
assert.Nil(t, err)
assert.Equal(t, result, oneTableOutput)
}

func testExecuteAsApp(t *testing.T) {
result, err := clusterInstance.VtctlclientProcess.ExecuteCommandWithOutput("ExecuteFetchAsApp", clusterInstance.Keyspaces[0].Shards[0].Vttablets[0].Alias, `SELECT 1 AS a`)
assert.Nil(t, err)
assert.Equal(t, result, oneTableOutput)
}

func getAllTablets() []string {
tablets := make([]string, 0)
for _, ks := range clusterInstance.Keyspaces {
for _, shard := range ks.Shards {
for _, tablet := range shard.Vttablets {
tablets = append(tablets, tablet.Alias)
}
}
}
return tablets
}