Skip to content

Commit

Permalink
Vtctld Test cases in Go using cluster (#25)
Browse files Browse the repository at this point in the history
* converted vtctld_test.py to go

Signed-off-by: Arindam Nayak <[email protected]>
Signed-off-by: Arindam Nayak <[email protected]>
  • Loading branch information
arindamnayak authored and ajeetj committed Dec 6, 2019
1 parent 9c78e80 commit 53b1a49
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go/test/endtoend/cluster/cluster_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Vttablet struct {
HTTPPort int
GrpcPort int
MySQLPort int
Alias string

// background executable processes
mysqlctlProcess MysqlctlProcess
Expand Down Expand Up @@ -199,6 +200,7 @@ func (cluster *LocalProcessCluster) StartKeyspace(keyspace Keyspace, shardNames
cluster.TmpDirectory,
cluster.VtTabletExtraArgs,
cluster.EnableSemiSync)
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 {
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
}

0 comments on commit 53b1a49

Please sign in to comment.